Sub topic 3
CALCULATE changes the dates a measure sees
This chapter assumes you can write basic DAX measures, you know a star schema (a fact table Sales related to a dimension table Date), and you have used a measure in a matrix or line chart. It also assumes you have the marked Date table from the earlier chapter. All examples use the Total Sales measure:
Total Sales = SUM ( Sales[Sales Amount] )The idea in one sentence
A measure is always evaluated inside a filter context, which is the set of filters that come from the visual (rows, columns, slicers). CALCULATE evaluates an expression in a modified copy of that filter context. Every time intelligence measure in this tutorial works by using CALCULATE to swap the visible dates for different dates.
CALCULATE ( <expression>, <filter1>, <filter2>, ... )The first argument is the expression to evaluate, usually a measure. The remaining arguments describe how to change the filters before the expression runs.
What happens when CALCULATE runs
Take a matrix with Date[Year] on the rows. For the row 2024, the filter context contains Date[Year] = 2024. Now consider this measure:
Sales 2023 Fixed =
CALCULATE ( [Total Sales], 'Date'[Year] = 2023 )The steps are:
- Power BI reads the current filter context (
Year = 2024). - It applies each filter argument. The argument
Date[Year] = 2023replaces the existing filter onDate[Year]. - It evaluates
[Total Sales]in the new context (Year = 2023) and returns the result.
Every row of the matrix therefore shows the 2023 total. The filter in the argument wins over the filter from the visual, for the same column.
Filter arguments come in two forms
You can pass CALCULATE a Boolean expression or a table expression.
| Form | Example | What it does |
|---|---|---|
| Boolean on one column | `'Date'[Year] = 2023` | Replaces the filter on that column with the listed values. |
| Table expression | `DATESYTD ( 'Date'[Date] )` | Replaces the filter on the column(s) the table contains with the rows in the table. |
| Filter removal | `ALL ( 'Date' )` | Removes all filters from the table, or from the columns you name. |
Time intelligence functions belong to the second form. DATESYTD, SAMEPERIODLASTYEAR, DATEADD, and DATESINPERIOD each return a table with one column of dates. CALCULATE uses that table as a filter on Date[Date].
Time intelligence functions are filter arguments
Compare these two measures from the plan. They return the same numbers:
Sales YTD = TOTALYTD ( [Total Sales], 'Date'[Date] )
Sales YTD (CALCULATE) =
CALCULATE ( [Total Sales], DATESYTD ( 'Date'[Date] ) )DATESYTD looks at the dates visible in the current context, finds the last one, and returns every date from January 1 of that year up to it. CALCULATE then replaces the visible dates with that list. TOTALYTD is shorthand for the CALCULATE form.
Prior-year measures follow the same shape:
Sales PY =
CALCULATE ( [Total Sales], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )For a row showing March 2024, SAMEPERIODLASTYEAR returns the dates of March 2023. CALCULATE evaluates [Total Sales] for those dates. The next chapter looks at what these functions return in more detail. Here the point is that they only produce a list of dates. CALCULATE is what applies the list.
A time intelligence function cannot change a context by itself. DATESYTD ( 'Date'[Date] ) on its own is just a table. If you put it inside SUM, nothing changes.
Replacing, adding, and removing filters
Whether a new filter replaces an old one depends on the columns involved.
- A filter argument on column X replaces any existing filter on column X.
- It does not touch filters on other columns. Those stay, and the filters combine with AND.
ALLandREMOVEFILTERSremove filters instead of adding them.
This leads to a problem you should know about. Suppose a matrix has Date[Year] on the rows and you write:
Sales Jan 2023 Only =
CALCULATE ( [Total Sales], 'Date'[Month Number] = 1, 'Date'[Year] = 2023 )Both columns are replaced, so each row shows January 2023. If you wrote only 'Date'[Month Number] = 1, the row's Year filter would stay, and the cell would show January of the row's year. Both results are correct DAX. They answer different questions.
The same rule is why you should filter the Date[Date] column in time intelligence and not Date[Year]. A visual that slices by year and month puts filters on Year and Year Month. If CALCULATE replaced only Date[Date], those other filters could survive, and the new dates would be cut down to nothing, because 2023 dates do not match Year = 2024.
Time intelligence handles this when the date table is marked. With a marked date table, a time intelligence filter on the date column clears filters on all other columns of the Date table at the same time. This is one reason the first chapter asked you to mark the table. If you write your own date logic with Boolean filters, you must do the clearing yourself:
Sales Same Month Last Year (manual) =
VAR CurrentYear = MAX ( 'Date'[Year] )
VAR CurrentMonth = MAX ( 'Date'[Month Number] )
RETURN
CALCULATE (
[Total Sales],
ALL ( 'Date' ),
'Date'[Year] = CurrentYear - 1,
'Date'[Month Number] = CurrentMonth
)The variables are read before CALCULATE runs, so they hold the values from the visual's context. ALL ( 'Date' ) removes every filter on the table, and the two Boolean filters then set the new dates. This measure assumes the visual shows one month. If a row covers several months, MAX picks only the last one.
Keeping a filter instead of replacing it
Sometimes you want the visual's filter to survive. KEEPFILTERS changes a filter argument so that it intersects with the existing filter instead of replacing it:
Sales 2023 If Visible =
CALCULATE ( [Total Sales], KEEPFILTERS ( 'Date'[Year] = 2023 ) )On the 2023 row this returns the 2023 total. On the 2024 row it returns blank, because Year = 2024 and Year = 2023 have no dates in common. Use KEEPFILTERS when a slicer should limit what your measure can reach, and not for ordinary time intelligence.
Order of evaluation
Two rules explain most surprises:
- Filter arguments are evaluated in the original filter context, before any of them is applied. A filter argument cannot depend on another filter argument in the same
CALCULATE. - After all arguments are evaluated, they are applied together, and the expression runs last.
For this reason DATESYTD ( 'Date'[Date] ) sees the dates the visual shows, not dates changed by another argument. It also means you can nest CALCULATE when you need a second step. The inner CALCULATE runs in the context set by the outer one.
Check that you see the shift
You can see the swap by putting a measure and its shifted version side by side in a matrix. The Date table below is the contiguous table from the first chapter.
Visible Dates =
COUNTROWS ( 'Date' )
Dates In PY Window =
CALCULATE ( COUNTROWS ( 'Date' ), SAMEPERIODLASTYEAR ( 'Date'[Date] ) )Place Date[Year Month] on the rows. For a normal month, both measures show the same count, such as 31 for a January. If they differ, the prior-year period has gaps, or the date table does not cover that year. This is a quick test to run whenever a prior-year number looks wrong, and it is covered again in the chapter on pitfalls.
Common mistakes
- Putting a time intelligence function outside
CALCULATE.SAMEPERIODLASTYEARalone returns a table, not a number. - Filtering a column from the fact table, such as
Sales[Order Date], instead ofDate[Date]. The date table's filters do not change, so the visual still shows its own period. - Using a Boolean filter on
Yearand expectingMonthfilters to disappear. They stay unless you remove them withALL. - Forgetting that filter arguments are evaluated first, in the original context, and then applied together.
Exercise
A matrix shows Date[Year] on the rows. The measure Sales X = CALCULATE ( [Total Sales], 'Date'[Year] = 2022 ) is placed in it. What does the 2024 row show, and what would change if you wrapped the filter in KEEPFILTERS?
Show answer
The 2024 row shows the 2022 sales total, because the filter argument replaces the visual's filter on Date[Year]. With KEEPFILTERS, the argument intersects with the existing filter instead. The 2022 row would show the 2022 total, and every other row would be blank, because no date has both Year = 2022 and another year.
Exercise
Why does Sales YTD (CALCULATE) = CALCULATE ( [Total Sales], DATESYTD ( 'Date'[Date] ) ) work when the matrix rows are months, even though each row has a filter on a single month?
Show answer
DATESYTD reads the dates visible in the current row, takes the last one, and returns all dates from January 1 of that year to it. CALCULATE then replaces the filter on Date[Date] with that list, so the total covers the whole year to date. Because the date table is marked, the month filters on other Date columns are cleared at the same time.