Power Pivot & DAX course · Lesson 12 of 16
Time intelligence in DAX: TOTALYTD, SAMEPERIODLASTYEAR, DATEADD, DATESINPERIOD
Year-to-date, last year, month-on-month and rolling three-month measures in Power Pivot, with a marked date table and checked results.
Comparing periods is what most managers ask for first: How are we doing year to date? Is this month better than the same month last year? What is the trend over the last three months? DAX has a set of time-intelligence functions that answer these questions in one line each. They all work by shifting or extending the dates in the filter context, and they all rely on a proper calendar table, built in Building a calendar (date) table for Power Pivot.
The data
This lesson uses a slightly different Sales table that covers two years, so there is something to compare with. It is related to the same Products table and to a Calendar table covering 2025 and 2026 by Sales[OrderDate] → 'Calendar'[Date].
| A | B | C | D | |
|---|---|---|---|---|
| 1 | OrderDate | ProductID | Qty | Amount |
| 2 | 2025-01-14 | P1 | 1 | 950 |
| 3 | 2025-02-10 | P3 | 2 | 378 |
| 4 | 2025-02-25 | P2 | 15 | 180 |
| 5 | 2025-03-18 | P1 | 2 | 1900 |
| 6 | 2025-04-07 | P4 | 4 | 140 |
| 7 | 2026-01-09 | P1 | 2 | 1900 |
| 8 | 2026-01-22 | P5 | 1 | 420 |
| 9 | 2026-02-16 | P3 | 3 | 567 |
| 10 | 2026-03-05 | P2 | 25 | 300 |
| 11 | 2026-03-27 | P5 | 2 | 840 |
| 12 | 2026-04-14 | P1 | 1 | 950 |
Sales for January–April of 2025 and 2026. Amount = Qty × Price.
The measures
Revenue := SUM ( Sales[Amount] )
Revenue YTD := TOTALYTD ( [Revenue], 'Calendar'[Date] )
Revenue LY := CALCULATE ( [Revenue], SAMEPERIODLASTYEAR ( 'Calendar'[Date] ) )
YoY % := DIVIDE ( [Revenue] - [Revenue LY], [Revenue LY] )
Rolling 3M :=
CALCULATE (
[Revenue],
DATESINPERIOD ( 'Calendar'[Date], MAX ( 'Calendar'[Date] ), -3, MONTH )
)| A | B | C | D | E | F | |
|---|---|---|---|---|---|---|
| 1 | Month | Revenue | Revenue YTD | Revenue LY | YoY % | Rolling 3M |
| 2 | Jan | 2320 | 2320 | 950 | 144.2% | 2320 |
| 3 | Feb | 567 | 2887 | 558 | 1.6% | 2887 |
| 4 | Mar | 1140 | 4027 | 1900 | -40.0% | 4027 |
| 5 | Apr | 950 | 4977 | 140 | 578.6% | 2657 |
| 6 | Grand Total | 4977 | 4977 | 3548 | 40.3% | 2657 |
'Calendar'[Month] on rows, Year slicer = 2026, months Jan–Apr shown. YoY % with 1 decimal.
Year to date: TOTALYTD
TOTALYTD ( expression, dates ) evaluates the expression from 1 January to the last date in the current filter context. In the March row, the dates in context are 1–31 March 2026, so the measure uses 1 January–31 March: 2,320 + 567 + 1,140 = 4,027. It is a shorthand for:
Revenue YTD := CALCULATE ( [Revenue], DATESYTD ( 'Calendar'[Date] ) )For a fiscal year, add the year-end date as text: TOTALYTD ( [Revenue], 'Calendar'[Date], "6/30" ) runs the year from 1 July to 30 June. TOTALQTD and TOTALMTD (with DATESQTD and DATESMTD) work the same way for quarters and months.
Same period last year: SAMEPERIODLASTYEAR and DATEADD
SAMEPERIODLASTYEAR ( dates ) takes the dates in the current filter context and moves each one back one year. In the February 2026 row it returns 1–28 February 2025, so Revenue LY shows February 2025's 558 (a monitor order and a mouse order). Growth is then 567 − 558 divided by 558 = 1.6%.
DATEADD is the more general version: it shifts the dates by any number of days, months, quarters or years, forwards or backwards.
Revenue LY (DATEADD) := CALCULATE ( [Revenue], DATEADD ( 'Calendar'[Date], -1, YEAR ) )
Revenue Prev Month := CALCULATE ( [Revenue], DATEADD ( 'Calendar'[Date], -1, MONTH ) )
MoM % := DIVIDE ( [Revenue] - [Revenue Prev Month], [Revenue Prev Month] )Revenue LY (DATEADD) returns exactly the same values as Revenue LY. Revenue Prev Month in the March row returns February: 567, so MoM % is 101.1%. In the January row it looks at December 2025, which had no sales, so it is blank and DIVIDE returns blank instead of an error.
PREVIOUSMONTH, PREVIOUSQUARTER and PREVIOUSYEAR return the whole previous period regardless of how many days are selected; PARALLELPERIOD ( dates, -1, YEAR ) also returns the whole parallel year. SAMEPERIODLASTYEAR and DATEADD keep the exact shape of the selection, which is usually what a like-for-like comparison needs.Moving windows: DATESINPERIOD
DATESINPERIOD ( dates, start_date, number_of_intervals, interval ) returns a block of dates counted from a start date. With MAX ( 'Calendar'[Date] ) as the start and -3, MONTH, it returns the three months ending on the last visible date. In the April row that is February to April: 567 + 1,140 + 950 = 2,657. In the January row the window covers November 2025 to January 2026, and since there were no sales in November and December the result equals January's 2,320.
Divide the rolling sum by 3 for a three-month moving average, or use -12, MONTH for a rolling annual total, which smooths out seasonality and is a favourite on management dashboards.
Reading the Grand Total row
The totals follow the same rules with a larger filter context: 1 January–30 April 2026. Revenue YTD uses the last date (30 April), so it equals the four-month revenue 4,977. Revenue LY shifts the whole block back a year (3,548), giving 40.3% growth. Rolling 3M looks at the three months ending 30 April, not at all four months: 2,657. Totals of moving windows rarely mean much; you may prefer to hide them with IF ( HASONEVALUE ( 'Calendar'[Month] ), ... ).
Common problems
- Blank or wrong results: the calendar is not marked, has gaps, or does not cover complete years. Check with
COUNTROWS ( 'Calendar' ): 730 for 2025–2026. - Using the fact table's date:
SAMEPERIODLASTYEAR ( Sales[OrderDate] )only contains dates with sales, so periods with no sales cannot be shifted onto. Always use'Calendar'[Date]. - Future months showing YTD values: in May–December 2026 there is no revenue, but
Revenue YTDrepeats the April figure, so those months appear in the PivotTable. Hide them withIF ( NOT ISBLANK ( [Revenue] ), TOTALYTD ( [Revenue], 'Calendar'[Date] ) )or filter the months. - Comparing a partial year with a full year: if this year ends in April, a Year-level comparison with the whole of last year is unfair. Limit last year to the same dates, as the Grand Total above does automatically when the months are filtered.
'Calendar'[Year] in a slicer and 'Calendar'[Month] on rows, sorted by MonthNo. This layout makes every time-intelligence measure easy to read and check. The next lesson, Variables, DIVIDE and error handling in DAX, shows how to make growth measures robust when last year is blank.Check yourself
1. Which table must time-intelligence functions use for their dates argument?
A continuous, marked date table is required.
2. What does TOTALYTD ( [Revenue], 'Calendar'[Date] ) return in the March row?
Year to date runs from the start of the year to the last visible date.
3. Which expression returns the same values as SAMEPERIODLASTYEAR for a month?
Shifting by -1 year with DATEADD is equivalent.
4. What does DATESINPERIOD ( 'Calendar'[Date], MAX ( 'Calendar'[Date] ), -3, MONTH ) return in the April row?
Three months ending on the last visible date.
5. How do you run a fiscal year from 1 July?
The optional year_end_date argument sets the fiscal year end.