Sub topic 10
Using the table in time intelligence measures
This chapter assumes you have already built a date table, related it to a fact table on a date column, and marked it as the date table. You should also know basic DAX syntax for measures. Here you will write the measures that use that table.
How time intelligence functions work
Time intelligence functions such as TOTALYTD and SAMEPERIODLASTYEAR do not look at the fact table. They take the date column of the date table and return a set of dates. CALCULATE then uses that set to filter the fact table through the relationship.
This is why the earlier steps matter. The date column must be unique, contiguous, and of type Date. The relationship must connect the date table to the fact table. If any of these is missing, the functions return wrong values or errors.
Start with a base measure
Write the base measure first. Every time intelligence measure builds on it, so the aggregation logic lives in one place.
Total Sales = SUM ( Sales[Amount] )Year-to-date and same period last year
These two measures use the date column of the date table, not the date column in the fact table.
Sales YTD = TOTALYTD ( [Total Sales], 'Date'[Date] )
Sales PY =
CALCULATE ( [Total Sales], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )TOTALYTD is shorthand for a CALCULATE that filters to January 1 through the last date in the current filter context. SAMEPERIODLASTYEAR shifts every date in the current filter context back one year.
Place 'Date'[Month] (sorted by 'Date'[Month Number]) on a matrix rows area and add the three measures. In the March row, Sales YTD should show January through March combined, and Sales PY should show March of the previous year.
Build a comparison on top of the measures
Once Sales PY exists, you can reuse it. A growth measure divides the difference by the prior value and uses DIVIDE to avoid a division-by-zero error in the first year.
Sales YoY % =
DIVIDE ( [Total Sales] - [Sales PY], [Sales PY] )Common problems
| Symptom | Likely cause | Fix |
|---|---|---|
| `Sales YTD` equals `Total Sales` in every row | The visual uses a column from the fact table, not from the date table | Use columns from the date table on rows and axes |
| `Sales PY` is blank for every period | The date table has no dates a year earlier, or the relationship is missing | Extend the date table to cover whole years and check the relationship in Model view |
| `Sales PY` is blank for some periods only | The date column contains gaps | Rebuild the table with one row per day |
| A "date column must contain unique values" error appears | Duplicate dates in the date table | Remove duplicates before marking the table |
Check your results
Compare one value by hand. Filter a table visual to a single month, then use a card or a second visual to confirm that Sales PY matches the Total Sales of the same month one year earlier. One matching month usually confirms the relationship and date range are correct.
Exercise
A matrix shows Sales YTD for each month, but the column from the Sales table, Sales[OrderDate], is used on the rows. Every row shows the same value as Total Sales. Why does this happen, and how do you fix it?
Show answer
The rows filter the Sales table directly, so the date table's filter never changes. TOTALYTD replaces the date filter on 'Date'[Date], but the visual's filter comes from Sales[OrderDate], which still limits each row to a single period. Replace Sales[OrderDate] on the rows with a column from the date table, such as 'Date'[Month].
Exercise
Write a measure named Sales Last Month that returns the total sales for the previous month, using the date table.
Show answer
Sales Last Month = CALCULATE ( [Total Sales], PREVIOUSMONTH ( 'Date'[Date] ) )