Sub topic 3
CALCULATE changes the dates a measure sees
This chapter assumes you can write basic DAX measures, 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, marked date table from the earlier chapter.
Same measure, different dates
A measure such as [Total Sales] does not know which dates it covers. A visual supplies the dates through the filter context, which is the set of filters active for one cell. In a matrix with months on the rows, the March 2024 cell filters the date table to March 2024 before the measure runs.
CALCULATE evaluates an expression in a modified copy of that filter context. Its first argument is the expression. Every later argument is a filter that adjusts the context.
Total Sales = SUM ( Sales[Sales Amount] )
Sales 2024 =
CALCULATE ( [Total Sales], 'Date'[Year] = 2024 )Sales 2024 returns the 2024 total in every row of the matrix, including a 2023 row. The filter on 'Date'[Year] replaces the year filter the row supplied. Filters from other tables, such as a product category, stay in place.
Replacing the date filter
Time intelligence functions work the same way. Each one returns a table of dates, and CALCULATE uses that table to replace the date filter.
Sales YTD (CALCULATE) =
CALCULATE ( [Total Sales], DATESYTD ( 'Date'[Date] ) )For the March 2024 cell, the evaluation runs in these steps:
- The visual filters
'Date'to 2024-03-01 through 2024-03-31. DATESYTDreads the last visible date, 2024-03-31, and returns every date from 2024-01-01 to 2024-03-31.CALCULATEreplaces the March filter with that range.[Total Sales]is evaluated for January through March.
Why the filter replaces instead of stacking
If the new dates were added to the existing filter, you would get the intersection of March and January through March, which is just March. Time intelligence would never widen a period. Filters in CALCULATE that target a column therefore override the existing filters on that column. With a marked date table, filters on the other date columns, such as Year and Month Number, are cleared too. This is why the measure still works when a matrix shows years and months on its rows.
Where the filter order matters
CALCULATE evaluates its filter arguments against the original context, then applies them together. You can combine a date function with a regular filter:
Sales YTD Bikes =
CALCULATE (
[Total Sales],
DATESYTD ( 'Date'[Date] ),
Product[Category] = "Bikes"
)Both filters are applied, one on dates and one on products. Neither affects how the other is calculated.
Common errors
| Symptom | Likely cause |
|---|---|
| Measure returns the same value in every row | The filter argument targets a column the visual does not use, or `ALL` removes the date filter |
| Time intelligence returns blank | The date table is not related to the fact table, or you passed a fact table column instead of `'Date'[Date]` |
| Values are wrong only in some months | The date table has gaps |
Exercise
A matrix shows months on its rows. The measure CALCULATE ( [Total Sales], 'Date'[Year] = 2023 ) returns the same number on every row. Why, and what does it show?
Show answer
The filter on 'Date'[Year] replaces the year and month filters supplied by each row, so every row is evaluated for all of 2023. Each cell shows the 2023 total for whatever product or region filters remain.