Sub topic 11
Shifting periods with DATEADD
This chapter assumes you have a contiguous 'Date' table marked as the model's date table, related to the fact table on the date column. It also assumes you can write a basic measure such as Total Sales and use it in a matrix. The measures below use the Total Sales measure from earlier chapters:
Total Sales = SUM ( Sales[Sales Amount] )What DATEADD does
DATEADD takes the dates that are visible in the current filter context and moves each one by a fixed number of intervals. It returns a table with one column, the shifted dates. On its own that table does nothing. You pass it to CALCULATE as a filter, and CALCULATE replaces the original date filter with the shifted one.
The syntax is:
DATEADD ( <dates column>, <number of intervals>, <interval> )- The first argument is a date column, normally
'Date'[Date]. - The second argument is a whole number. A negative number shifts back in time and a positive number shifts forward.
- The third argument is one of
DAY,MONTH,QUARTER, orYEAR.
The prior-year measure from the previous chapter was written this way:
Sales PY (DATEADD) =
CALCULATE ( [Total Sales], DATEADD ( 'Date'[Date], -1, YEAR ) )If a matrix cell is filtered to March 2025, DATEADD returns March 2024. CALCULATE evaluates [Total Sales] for those dates, and the matrix cell shows the March 2024 total in the March 2025 row.
Shifting by other intervals
SAMEPERIODLASTYEAR can only move one year back. DATEADD handles any distance and any of the four intervals, so one pattern covers most comparisons:
Sales Prior Month =
CALCULATE ( [Total Sales], DATEADD ( 'Date'[Date], -1, MONTH ) )
Sales Prior Quarter =
CALCULATE ( [Total Sales], DATEADD ( 'Date'[Date], -1, QUARTER ) )
Sales Two Years Ago =
CALCULATE ( [Total Sales], DATEADD ( 'Date'[Date], -2, YEAR ) )
Sales Next Month =
CALCULATE ( [Total Sales], DATEADD ( 'Date'[Date], 1, MONTH ) )A positive shift is useful for forecasts or for checking what the next period looks like. It only returns values if the date table extends far enough forward and the fact table has data there.
A month-over-month growth measure combines DATEADD with the variable and DIVIDE pattern from the year-over-year chapter:
Sales MoM % =
VAR CurrentSales = [Total Sales]
VAR PriorSales = [Sales Prior Month]
RETURN
DIVIDE ( CurrentSales - PriorSales, PriorSales )Shifting by days
Day shifts matter when you compare weekdays. Shifting back one year by YEAR lands on the same calendar date, which is a different weekday most years. Shifting back 364 days, which is 52 weeks, keeps the weekday:
Sales Same Weekday Last Year =
CALCULATE ( [Total Sales], DATEADD ( 'Date'[Date], -364, DAY ) )Retail and restaurant reports often use this measure because Saturdays compare poorly with Tuesdays. The cost is drift. Over several years the 364-day shift moves away from the calendar date by one or two days per year.
Reusing another time intelligence measure
Because DATEADD only changes the date filter, you can wrap any existing measure in it. This gives the prior-year year-to-date total without a new formula:
Sales YTD PY =
CALCULATE ( [Sales YTD], DATEADD ( 'Date'[Date], -1, YEAR ) )The shifted dates become the filter context for [Sales YTD]. The TOTALYTD inside it then works from the last shifted date, so on 15 May 2025 the measure returns the total from 1 January 2024 to 15 May 2024. This is the standard way to build a comparison column next to a year-to-date column.
How DATEADD treats the dates it shifts
DATEADD shifts the dates that are visible, not the whole period they belong to. This has several effects.
Partial periods stay partial
If the filter context holds 1 to 15 March 2025, the prior-month result holds 1 to 15 February 2025. The comparison covers only the first half of the month. That is usually what you want for a month-to-date comparison. It surprises people who expect a full prior month.
If you want a full prior period regardless of the selected days, use PARALLELPERIOD, which returns whole periods:
| Function | Shifts by | Result for a selection of 1 to 15 March 2025, shifted back one month |
|---|---|---|
| `DATEADD` | Any number of days, months, quarters, or years | 1 to 15 February 2025 |
| `PARALLELPERIOD` | Whole months, quarters, or years | All of February 2025 |
| `SAMEPERIODLASTYEAR` | One year back only | 1 to 15 March 2024 (when the shift is a year) |
Short months
A day that does not exist in the target month moves to the end of that month. Shifting 31 March back one month lands on 28 February, or 29 February in a leap year. Shifting 29 February 2024 back one year lands on 28 February 2023. Several source days can map to the same target day, so the shifted period can be shorter than the original. Check month-end rows in a matrix before you trust a monthly comparison.
Missing data in the shifted period
If the shifted dates exist in the date table but have no sales, [Total Sales] returns blank. A blank is correct there. It is not an error. If the shifted dates fall outside the date table, the result is also blank, so the first year in your model shows blank for prior year.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| An error that the dates are not contiguous, or a prior-period column that is blank for some rows | The date table has gaps, or the date column used in `DATEADD` comes from the fact table instead of the date table | Build the date table with `CALENDAR`, with one row per day, and pass `'Date'[Date]` |
| The prior-year column is blank for every row | No relationship between `'Date'` and the fact table, or the visual uses a date column from the fact table | Relate the tables and put `'Date'` columns in the visual |
| The prior-year column repeats the current value | The visual does not filter the date table, so there is nothing to shift | Use a `'Date'` column on the rows of the visual |
| The shift looks one day off | Dates stored with a time portion in the fact table, so the relationship misses some rows | Remove the time portion in the source or in a column used for the relationship |
| The comparison for a partial month looks too low | `DATEADD` keeps the partial selection, but the data for the prior month is complete, or the reverse | Decide which behavior you want, and use `PARALLELPERIOD` for whole periods |
Choosing between DATEADD and SAMEPERIODLASTYEAR
For a one-year shift the two return the same dates. SAMEPERIODLASTYEAR is shorter and states its purpose. DATEADD is better when you need the same pattern with different intervals, because you change one number instead of the function. A common compromise is to use SAMEPERIODLASTYEAR for the headline prior-year measure and DATEADD for everything else.
Both functions need the date table to cover the shifted period. If your fact table starts in 2020, the date table should start at 1 January 2020 and extend at least one year beyond the latest date you expect to shift forward to.
Exercise
Write a measure named Sales Same Quarter Last Year that returns the sales total for the same quarter one year earlier, using DATEADD. Then say what the measure returns for 31 March 2025 if you shift 'Date'[Date] back one month instead.
Show answer
Sales Same Quarter Last Year = CALCULATE ( [Total Sales], DATEADD ( 'Date'[Date], -4, QUARTER ) ). A one-year shift is also correct as DATEADD ( 'Date'[Date], -1, YEAR ) when the selected dates are a quarter. Shifting 31 March 2025 back one month gives 28 February 2025, because February has no 31st, so the day moves to the end of the shorter month.
Exercise
A matrix shows months on the rows. Sales Prior Month is blank for January 2020, the first month in the date table, and correct elsewhere. Is this a bug, and what would you change if you wanted a value there?
Show answer
It is not a bug. The shifted dates for January 2020 fall in December 2019, which is outside the date table, so there is nothing to evaluate. To get a value you would have to start the date table earlier, at 1 December 2019 or before, and the fact table would need sales in that month.