Sub topic 10
Using the table in time intelligence measures
You have built a date table, sorted its text columns, related it to the fact table, and marked it as the date table. This chapter uses that table in measures: year-to-date totals, prior-period comparisons, and rolling windows. It also covers why these measures fail when something upstream is wrong.
This chapter assumes you can write basic DAX measures, know what filter context is at a high level, and have a Sales fact table related to a marked 'Date' table on Sales[OrderDate] and 'Date'[Date].
How a time intelligence function works
A time intelligence function does not read a calendar. It takes the dates in the current filter context, computes a different set of dates, and hands that set to CALCULATE as a new filter on the date column. The relationship then carries that filter to the fact table.
Take a matrix row for March 2025 with a measure that calls SAMEPERIODLASTYEAR:
- The row filters
'Date'[Date]to the 31 days of March 2025. SAMEPERIODLASTYEARreturns the same 31 days shifted back one year.CALCULATEreplaces the date filter with that shifted set.- The relationship filters
Salesto March 2024 rows, and the measure sums them.
Each shift depends on the date column containing every day in the range. A missing day in the column means the shifted set also has a gap. This is why the earlier chapters stressed a contiguous, unique, date-typed column.
The base measure
Time intelligence measures wrap a base measure. Define it once and reuse it:
Total Sales = SUM ( Sales[Amount] )Every measure below calls [Total Sales]. If the definition of sales changes, you edit one place.
Year-to-date, quarter-to-date, and month-to-date
The TOTAL* functions combine a shift and an aggregation. Each takes an expression and the date column from the date table.
Sales YTD = TOTALYTD ( [Total Sales], 'Date'[Date] )
Sales QTD = TOTALQTD ( [Total Sales], 'Date'[Date] )
Sales MTD = TOTALMTD ( [Total Sales], 'Date'[Date] )TOTALYTD is shorthand for a CALCULATE with DATESYTD. Both forms give the same result:
Sales YTD (long form) =
CALCULATE ( [Total Sales], DATESYTD ( 'Date'[Date] ) )The long form is useful when you need to add other filters in the same CALCULATE. TOTALYTD also accepts an optional year-end argument for fiscal calendars. The fiscal calendar chapter covers that.
| Function | Dates it returns | Typical use |
|---|---|---|
| `DATESYTD` / `TOTALYTD` | January 1 to the last date in context | Running total within the year |
| `DATESQTD` / `TOTALQTD` | Start of the quarter to the last date in context | Running total within the quarter |
| `DATESMTD` / `TOTALMTD` | Start of the month to the last date in context | Running total within the month |
| `SAMEPERIODLASTYEAR` | The current dates, one year earlier | Year-over-year comparison |
| `DATEADD` | The current dates, shifted by any number of days, months, quarters, or years | Prior month, prior quarter |
| `DATESINPERIOD` | A window of a fixed length ending at a given date | Rolling 12 months |
Same period last year and year-over-year change
Sales PY =
CALCULATE ( [Total Sales], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )The year-over-year change builds on it. Use DIVIDE so a blank or zero prior year returns blank instead of an error:
Sales YoY % =
DIVIDE ( [Total Sales] - [Sales PY], [Sales PY] )Format this measure as a percentage in the Measure tools ribbon, not inside the DAX.
SAMEPERIODLASTYEAR is a special case of DATEADD. DATEADD handles other shifts:
Sales Prior Month =
CALCULATE ( [Total Sales], DATEADD ( 'Date'[Date], -1, MONTH ) )
Sales Prior Quarter =
CALCULATE ( [Total Sales], DATEADD ( 'Date'[Date], -1, QUARTER ) )Rolling windows
A rolling 12-month total ends at the last date in context and reaches back twelve months:
Sales Rolling 12M =
CALCULATE (
[Total Sales],
DATESINPERIOD ( 'Date'[Date], MAX ( 'Date'[Date] ), -12, MONTH )
)For a month-level row, MAX('Date'[Date]) is the last day of that month, so the window is that month and the eleven before it. At day level, the window ends on that day.
Stopping measures at the last date with data
A date table built through the end of the year contains future dates. Sales YTD gives the same running total for every future month, which draws a flat line on a chart. Compare the first date in context with the last sale date, and return blank for periods that start after it:
Sales YTD Capped =
VAR LastSaleDate =
CALCULATE ( MAX ( Sales[OrderDate] ), REMOVEFILTERS ( 'Date' ) )
RETURN
IF ( MIN ( 'Date'[Date] ) <= LastSaleDate, [Sales YTD] )REMOVEFILTERS('Date') clears the date filters so LastSaleDate is the last sale in the whole table, not in the current month. Use MIN for the comparison so that the current, partly complete month still shows a value.
What marking the table changes
Marking the table tells the engine that 'Date'[Date] is the authoritative date column. Time intelligence functions then work correctly even when the report applies filters to other columns of the date table. For example, a visual may filter on 'Date'[Year] or 'Date'[Month]. Without the mark, a measure can return results that depend on how the visual is filtered. With it, the functions behave the same way on any visual that uses the table.
Marking also validates the column. Power BI checks that it is of type Date, has no blanks, and has unique values. If the check fails, fix the table before debugging measures.
Using the right columns in visuals
A measure can only use the date table if the visual does too. Put these columns on the axis, rows, or slicers:
- Use
'Date'[Year],'Date'[Month], and'Date'[Date]. - Do not use
Sales[OrderDate]or its automatic date hierarchy.
If a matrix shows months from Sales[OrderDate], the visual filters the fact table directly. The date table never receives a filter, so SAMEPERIODLASTYEAR has no dates to shift. The result is blank or the same as the current period. When Sales PY returns the wrong value, check which table the axis column belongs to before you change the DAX.
A worked check
Create a matrix with 'Date'[Year] and 'Date'[Month] on rows and the measures Total Sales, Sales PY, Sales YTD, and Sales YoY % in values. Use these checks:
- For the earliest year in your data,
Sales PYis blank for every month. This is correct, since no earlier data exists. - For January of any later year,
Sales YTDequalsTotal Sales. - For December,
Sales YTDequals the full-year total shown on the year row. - For any month,
Sales PYmatches theTotal Salesfor the same month a year earlier.
If any check fails, the cause is usually one of the problems in the next table.
Common problems
| Symptom | Likely cause | Fix |
|---|---|---|
| Every measure ignores the date slicer | Slicer uses a fact table column, or the relationship is inactive | Use a column from `'Date'` and check that the relationship is active |
| `Sales PY` equals `Total Sales` | Visual axis comes from the fact table | Replace the axis field with `'Date'[Month]` or similar |
| `Sales PY` is blank for all rows | Relationship goes the wrong way, or the date table has no prior-year rows | Confirm one-to-many from `'Date'` to `Sales`, and confirm the date range |
| Error that a date column cannot be determined | Table is not marked, or the column has gaps or duplicates | Fix the column, then mark the table again |
| YTD line stays flat after the last sale | Date table covers future dates | Use the capped pattern above |
| Months appear alphabetically | Sort by column not set | Sort `Month` by `Month Number` |
| Results look doubled or wrong in totals | Measure aggregates a column already summed in the source | Check the base measure with a plain table first |
When you debug, strip the problem down. Put 'Date'[Date] and [Total Sales] in a table visual. If that is correct, add the time intelligence measure beside it. A shifted measure that is wrong on a plain date table points to the measure's definition or the relationship, not to formatting.
Exercise
A matrix shows months taken from Sales[OrderDate], and Sales PY returns the same value as Total Sales in every row. The date table is marked and related to Sales on OrderDate. What is the cause, and what is the fix?
Show answer
The visual filters the Sales table directly through Sales[OrderDate], so the date table never receives a filter and SAMEPERIODLASTYEAR has no dates to shift. The measure's shifted filter on 'Date'[Date] is combined with the fact-column filter, which leaves the current period. Replace the matrix field with 'Date'[Month] (or Year and Month) from the date table, and disable Auto date/time so the hidden hierarchy is not used.
Exercise
Write a measure named Sales Rolling 3M that sums Total Sales over the current month and the two months before it, based on the last date in context.
Show answer
Sales Rolling 3M = CALCULATE ( [Total Sales], DATESINPERIOD ( 'Date'[Date], MAX ( 'Date'[Date] ), -3, MONTH ) )