Sub topic 4
What time intelligence functions actually return
This chapter assumes you can write basic DAX measures with SUM and DIVIDE, you know a star schema with a fact table and a date dimension, and you have placed a measure in a matrix or line chart. It also assumes you have a contiguous date table related to your fact table.
Functions that return tables of dates
Most time intelligence functions do not return a number. They return a table with one column, and that column holds dates. DATESYTD, SAMEPERIODLASTYEAR, DATEADD, and DATESINPERIOD all work this way.
A measure cannot use a table of dates as its result. The table has to be handed to something that turns it into a filter. That something is usually CALCULATE, which takes the dates and applies them as a filter on the date column.
Sales YTD (CALCULATE) =
CALCULATE ( [Total Sales], DATESYTD ( 'Date'[Date] ) )The steps are:
- The visual supplies a filter context, for example the month March 2025.
DATESYTDreads the last visible date in that context and returns every date from January 1 to that day.CALCULATEreplaces the existing filter on'Date'[Date]with that table of dates.[Total Sales]is evaluated against the new filter.
Shorthand functions
TOTALYTD wraps the same logic in one call. It takes the expression, the date column, and an optional year-end date.
Total Sales = SUM ( Sales[Sales Amount] )
Sales YTD = TOTALYTD ( [Total Sales], 'Date'[Date] )Sales YTD and the CALCULATE version above return the same value. Use TOTALYTD for brevity. Use CALCULATE with DATESYTD when you need to add other filters in the same call.
Seeing the returned table
Because these functions return tables, you can inspect them in a query. This is a good way to check what a measure will actually filter on. Run it in DAX query view or DAX Studio.
EVALUATE
CALCULATETABLE (
DATESYTD ( 'Date'[Date] ),
'Date'[Date] >= DATE ( 2025, 3, 1 ),
'Date'[Date] <= DATE ( 2025, 3, 31 )
)The result lists every date from January 1 to March 31, 2025. If the list has gaps or starts on the wrong day, the measure will be wrong too.
What the functions need from the date table
These functions look at the whole date column, not just the rows visible in the visual. To move from March to the same dates a year earlier, they must find those dates in the table. That is why the date table must have one row for every day, with no gaps, and must be marked as the date table.
Two common errors follow from this:
- Using a date column from the fact table, such as
Sales[Order Date], as the argument. The fact column has gaps on days with no sales, so shifts and ranges can fail or return the wrong period. - Putting a column from the date table on the visual axis that is not related to the fact table. The measure then returns the same total in every row.
Context transition and the last date
DATESYTD and SAMEPERIODLASTYEAR both work from the dates visible in the current filter context. When a visual shows a single month, the last visible date is the month end. When a visual shows a whole year, it is December 31. The same measure therefore gives different answers at different levels of a matrix, and that is the intended behavior.
Sales PY =
CALCULATE ( [Total Sales], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )Placed in a matrix by month, each row shows that month's total from the year before. Placed by year, each row shows the previous full year.
Exercise
What does DATESYTD ( 'Date'[Date] ) return, and what must you do with it to get a sales total?
Show answer
It returns a one-column table of dates from January 1 of the current year to the last date in the current filter context. You pass it as a filter argument to CALCULATE, for example CALCULATE ( [Total Sales], DATESYTD ( 'Date'[Date] ) ), so the sales total is evaluated over those dates.
Exercise
A year-to-date measure returns the same value in every row of a monthly matrix. Name two things to check.
Show answer
Check that the date table is related to the fact table, and that the matrix axis uses a column from the date table, not from the fact table. Also confirm the measure uses the date table's date column as its argument.