Often you may want to create tables and other visuals which display multiple fields that are all ‘filtered’ to different date periods, as well as period-over-period fields. However we can’t use normal filters for this since filters get applied to all fields in the visual. Conceptually in this solution we are building the filters directly into the calculated field itself (using the ifelse logic), so that each field can have a different ‘filtered’ time period. You can then do whatever math you want on top of these fields, like Year Over Year, % Differences, etc. Aaaand one of the best parts is everything will update dynamically so you never need to touch them again!

**Desired output:**

Below you will find a list of common date comparison calculations. All references to the field ‘sales’ can be replaced with the metric you wish to display. You can replace all references to the parameter ‘${AsOfDate}’ with the function ‘now()’ if you want it to be based on today’s date. Or, if you want to base it of the latest date in your data set, then first create a calculated field like this and reference that date field as your end date instead:

Max Date:

`maxOver({your date field}, [ ], PRE_AGG)`

Between dateDiff(), addDateTime(), truncDate(), and extract() you can define whatever periods you want (and there are often multiple ways to do each one, these are just some proven examples).

**Today**:

`ifelse(datediff({order_date},${AsOfDate}) = 0,sales,0)`

**Yesterday:**

`ifelse(datediff({order_date},${AsOfDate}) = 1,sales,0)`

**Last 90 Days:**

`ifelse(datediff({order_date},${AsOfDate}) <=90, sales,0)`

**Last 2 Weeks:**

`ifelse(datediff({order_date},${AsOfDate},"WK") <=2, sales,0)`

**This year (aka Year to Date, YTD):**

`ifelse(datediff({order_date},${AsOfDate},"YYYY") = 0, sales, 0)`

**Last year:**

`ifelse(datediff({order_date},${AsOfDate},"YYYY") = 1, sales, 0)`

**Last year to date (aka LYTD):**

`ifelse({order_date} >= adddatetime(-1,"YYYY",truncdate("YYYY",${AsOfDate})) AND {order_date} <= adddatetime(-1,"YYYY",${AsOfDate}),sales,0)`

**This month (aka Month to Date, MTD):**

`ifelse(datediff({order_date},${AsOfDate},"MM") = 0, sales, 0)`

**Last month:**

`ifelse(datediff({order_date},${AsOfDate},"MM") = 1, sales, 0)`

**Same month last year:**

`ifelse(datediff({order_date},${AsOfDate},"MM") = 12, sales, 0)`

**This Quarter:**

`ifelse(datediff({order_date},${AsOfDate},"Q") = 0, sales, 0)`

**Last Quarter:**

`ifelse(datediff({order_date},${AsOfDate},"Q") = 1, sales, 0)`

**Same Quarter Last Year:**

`ifelse(datediff({order_date},${AsOfDate},"Q") = 4, sales, 0)`

**MoM % Diff:**

`(sum({This Month}) - sum({Last Month})) / sum({Last Month})`

**YoY % Diff:**

`(sum({This Year}) - sum({Last Year})) / sum({Last Year})`

**Monthly YoY % Diff:**

`(sum({This Month}) - sum({This Month Last Year})) / sum({This Month Last Year})`

**QoQ% Diff:**

`(sum({ This Quarter}) - sum({Last Quarter}))/sum({Last Quarter}))`