Sub topic 11
Shifting periods with DATEADD
This chapter assumes you can write basic DAX measures, have a contiguous date table related to your fact table, and know that CALCULATE changes filter context. Here you will use DATEADD to shift the dates a measure sees by any number of days, months, quarters, or years.
What DATEADD does
DATEADD takes a column of dates, a number of intervals, and an interval type. It returns a table of dates moved by that amount.
DATEADD ( 'Date'[Date], -1, YEAR )The interval can be DAY, MONTH, QUARTER, or YEAR. A negative number moves back in time and a positive number moves forward.
DATEADD looks at the dates that are visible in the current filter context. It shifts each of them, then returns the shifted dates. CALCULATE then uses that table as the new date filter.
Prior month and prior quarter
Define the base measure once, then shift it.
Total Sales = SUM ( Sales[Sales Amount] )
Sales PM =
CALCULATE ( [Total Sales], DATEADD ( 'Date'[Date], -1, MONTH ) )
Sales PQ =
CALCULATE ( [Total Sales], DATEADD ( 'Date'[Date], -1, QUARTER ) )In a matrix with months on rows, the March row shows the February total in Sales PM. The shift is relative to whatever is visible, so the same measure works at month, quarter, or year level.
Comparing with SAMEPERIODLASTYEAR
DATEADD ( 'Date'[Date], -1, YEAR ) returns the same dates as SAMEPERIODLASTYEAR. Use SAMEPERIODLASTYEAR when you only need the prior year, because the name states the intent. Use DATEADD when the shift is anything else. Examples are the prior month, two years ago, or the next quarter.
Sales 2 Years Ago =
CALCULATE ( [Total Sales], DATEADD ( 'Date'[Date], -2, YEAR ) )
Sales Next Quarter =
CALCULATE ( [Total Sales], DATEADD ( 'Date'[Date], 1, QUARTER ) )Month ends and uneven lengths
When you shift by months, the days do not always line up. DATEADD moves each date by the interval. If the target day does not exist, DATEADD uses the last day of that month. For example, March 31 shifted back one month becomes February 28 (or 29 in a leap year).
When a whole month is visible, DATEADD returns the whole shifted month. March has 31 days and February has 28, so Sales PM for March covers all of February. That is usually what you want. If you compare a partial month to a full one, though, the totals are not like for like.
Requirements and common errors
DATEADD needs a date column from a proper date table. Check these points when results look wrong:
- The date table has one row for every day, with no gaps.
- The relationship uses the date column of the date table, not a text or key column.
- The column passed to DATEADD is
'Date'[Date], not a column from the fact table. - The visual has date-table fields on its axis. If a field from the fact table is on the axis, the shift has no effect on it.
- The shifted period must exist in the date table. Shifting back from January 2020 in a table that starts on 2020-01-01 returns blank.
DATEADD also needs a contiguous set of dates. It fails if the date column has gaps because of an applied filter. It is safest to use the full date column and let the visual supply the filter.
Using the shifted measure
Combine the shifted measure with variables and DIVIDE for a month-over-month change.
Sales MoM % =
VAR CurrentSales = [Total Sales]
VAR PriorSales = [Sales PM]
RETURN
DIVIDE ( CurrentSales - PriorSales, PriorSales )DIVIDE returns blank when the prior month has no sales, so you do not see an error in the first month of data.
Exercise
Write a measure named Sales Last Quarter that returns the sales total for the previous quarter. Then say what it returns for the first quarter in a date table that starts on 2020-01-01.
Show answer
Sales Last Quarter = CALCULATE ( [Total Sales], DATEADD ( 'Date'[Date], -1, QUARTER ) ) For Q1 2020 the shifted dates fall before 2020-01-01. They are not in the date table, so no sales match and the measure returns blank.