Sub topic 4
What time intelligence functions actually return
This chapter assumes you can write basic DAX measures such as SUM and DIVIDE. It also assumes you have a star schema with a marked, contiguous date table, and that you know CALCULATE replaces filters. The earlier chapters on the date table, filter context, and CALCULATE cover these. Here the focus is one question: what value does a function like DATESYTD hand back, and what happens to it afterward?
Most of them return a table, not a number
Beginners often expect DATESYTD to return a year-to-date total. It doesn't. DATESYTD, SAMEPERIODLASTYEAR, DATEADD, and DATESINPERIOD each return a table with one column, the date column you passed in. Each row is one date.
Because the result is a table of dates, you normally use it as a filter argument of CALCULATE. CALCULATE applies that table of dates as the new filter on the date column, and the measure then evaluates over those dates.
You can see the raw output by running a DAX query. In DAX query view in Power BI Desktop, or in DAX Studio, run this against a model that has a 'Date' table covering 2024:
EVALUATE
CALCULATETABLE (
DATESYTD ( 'Date'[Date] ),
'Date'[Date] = DATE ( 2024, 3, 15 )
)
ORDER BY 'Date'[Date]CALCULATETABLE first sets the visible dates to a single day, March 15, 2024. DATESYTD then returns every date from January 1 to March 15, 2024. That is 75 rows, because 2024 is a leap year (31 + 29 + 15). The query returns a list of dates, and no sales figure appears anywhere.
A second query shows a window function. Here the anchor date is passed in directly:
EVALUATE
DATESINPERIOD ( 'Date'[Date], DATE ( 2024, 3, 15 ), -3, DAY )
ORDER BY 'Date'[Date]This returns three rows: March 13, 14, and 15, 2024. The window ends at the anchor date and extends three days backward.
The two kinds of function
The time intelligence family has two groups. Knowing which one you are calling tells you what you get back.
| Group | Examples | Returns | Typical use |
|---|---|---|---|
| Table functions | `DATESYTD`, `DATESMTD`, `DATESQTD`, `SAMEPERIODLASTYEAR`, `DATEADD`, `DATESINPERIOD`, `DATESBETWEEN`, `PARALLELPERIOD` | A one-column table of dates | A filter argument in `CALCULATE` |
| Shorthand value functions | `TOTALYTD`, `TOTALMTD`, `TOTALQTD` | A single value | Used directly as a measure |
The shorthand functions wrap CALCULATE and a table function in one call. These two measures are equivalent:
Total Sales = SUM ( Sales[Sales Amount] )
Sales YTD = TOTALYTD ( [Total Sales], 'Date'[Date] )
Sales YTD (CALCULATE) =
CALCULATE ( [Total Sales], DATESYTD ( 'Date'[Date] ) )Prefer the CALCULATE form when you need to combine the date filter with other filters, or when no shorthand exists, as with prior year. Once you understand it, the shorthand is only a convenience.
What the function looks at
A table function does not read your whole date table. It reads the dates that are visible in the current filter context. Then it builds its result from them.
Each function uses the visible dates differently:
DATESYTDfinds the last visible date. It returns every date from the start of that date's year through that last date.SAMEPERIODLASTYEARtakes all visible dates and shifts each one back one year. Its result has as many rows as the visible set does.DATEADDshifts the whole visible set by the number of intervals you give it. The interval can beDAY,MONTH,QUARTER, orYEAR, and a negative number moves backward.DATESINPERIODignores the visible set apart from the start date you give it. You supply an anchor date, a count, and an interval, and it builds a window from those.
This is why the same measure gives a different answer in each cell of a matrix. In the cell for March 2024, the visible dates are March 1 through March 31, 2024. DATESYTD sees March 31 as the last date and returns January 1 through March 31. In the cell for April 2024, it returns January 1 through April 30.
Sales PY =
CALCULATE ( [Total Sales], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )
Sales PY (DATEADD) =
CALCULATE ( [Total Sales], DATEADD ( 'Date'[Date], -1, YEAR ) )In the March 2024 cell, both measures shift March 1 to 31, 2024 back to March 1 to 31, 2023. The result keeps the shape of the visible period. A single month stays a single month, and a full year stays a full year.
What CALCULATE does with the returned table
The returned dates become a filter on 'Date'[Date]. For that column, the new filter replaces the filter that came from the visual. It does not intersect with it. That replacement is what lets a March cell see January through March.
Filters from the visual that sit on other tables, such as Product[Category], are untouched. So the YTD total for a row labeled "Bikes" is still the bikes-only total.
Filters on other columns of the date table are a separate case. A matrix might put 'Date'[Year Month] on rows. When the date table is marked as a date table, the engine clears those other date-table filters when a time intelligence filter is applied. If you skip the marking step, a filter on Year Month can intersect with your YTD dates and cut them back to one month. This is one practical reason to mark the date table, as covered earlier in the tutorial.
Why the date column matters
Always pass the date column of your date table, such as 'Date'[Date]. Do not pass a date column from the fact table, such as Sales[Order Date]. The functions need a column with exactly one row per day, and a fact table column repeats dates and has gaps.
Passing the dimension column also lets the filter flow through the relationship to the fact table. The date table filters Sales through the one-to-many relationship, and Total Sales sums only the rows whose order date is in the returned set.
Table functions can be nested
Because the functions return tables, you can use them in places other than a plain CALCULATE filter. The 30-day average from the spine does this. It stores the window in a variable and passes it to an iterator:
Sales 30 Day Avg =
VAR LastDate = MAX ( 'Date'[Date] )
VAR Window = DATESINPERIOD ( 'Date'[Date], LastDate, -30, DAY )
RETURN
AVERAGEX ( Window, [Total Sales] )AVERAGEX iterates over the 30 rows of Window. For each date it evaluates [Total Sales], and it then averages the results. Days where [Total Sales] is blank are skipped, not counted as zero. Whether that is what you want depends on the business meaning, and a later chapter returns to this choice.
The three-month average builds the window the same way but passes it to CALCULATE:
Sales 3 Month Avg =
VAR Window = DATESINPERIOD ( 'Date'[Date], MAX ( 'Date'[Date] ), -3, MONTH )
RETURN
DIVIDE (
CALCULATE ( [Total Sales], Window ),
CALCULATE ( DISTINCTCOUNT ( 'Date'[Year Month] ), Window )
)Here Window is used twice as a filter. One CALCULATE totals the sales, and the other counts the distinct months in the same window.
The fiscal year argument
TOTALYTD and DATESYTD accept an optional year-end date. It is passed as text in month-day form:
Sales YTD Fiscal (June) =
TOTALYTD ( [Total Sales], 'Date'[Date], "6-30" )The returned set of dates now starts the day after June 30 instead of on January 1. It still ends at the last visible date.
Common mistakes with return values
- Using a date function as a measure on its own.
Bad = DATESYTD ( 'Date'[Date] )fails, because a measure must return a single value and this returns a table. Wrap it inCALCULATE. - Adding a filter on the same column afterward. Another filter on
'Date'[Date]inside the sameCALCULATEcan replace or conflict with the one you built. - Expecting full periods. The result follows the visible dates. At the day level,
DATESYTDreturns a range ending on that day. At the month level, it ends at the month's last day. - Forgetting the anchor in
DATESINPERIOD.MAX ( 'Date'[Date] )gives the last visible date. UsingMINmakes the window end at the start of the period instead.
Exercise
A colleague writes Sales YTD = DATESYTD ( 'Date'[Date] ) and gets an error. What does DATESYTD return, and how do you change the measure so it returns year-to-date sales?
Show answer
DATESYTD returns a one-column table of dates, from the start of the year through the last visible date. A measure must return a single value. Use the table as a filter inside CALCULATE: Sales YTD = CALCULATE ( [Total Sales], DATESYTD ( 'Date'[Date] ) ). Alternatively, use TOTALYTD ( [Total Sales], 'Date'[Date] ).
Exercise
A matrix shows March 2024 in one row. What dates does DATESYTD return for that row, and what dates does SAMEPERIODLASTYEAR return?
Show answer
The visible dates are March 1 to March 31, 2024. DATESYTD returns January 1 to March 31, 2024. SAMEPERIODLASTYEAR returns March 1 to March 31, 2023, because it shifts each visible date back one year.
Exercise
Write a query that lists the dates DATESINPERIOD returns for a 7-day window ending on 2024-03-15. How many rows does it return, and what are the first and last dates?
Show answer
EVALUATE DATESINPERIOD ( 'Date'[Date], DATE ( 2024, 3, 15 ), -7, DAY ) ORDER BY 'Date'[Date]. It returns 7 rows. The first date is 2024-03-09 and the last is 2024-03-15.