Short answer: CALCULATE ( expression, filter1, filter2, ... ) evaluates the expression in a changed filter context. Each filter argument is a Boolean condition on one table's columns ('Product'[Color] = "Red"), a table (FILTER ( ... ), DATESYTD ( ... )), or a modifier (REMOVEFILTERS, ALL, KEEPFILTERS, USERELATIONSHIP, CROSSFILTER). By default a filter on a column replaces any existing filter on that column; wrap it in KEEPFILTERS to add to it. In a row context, CALCULATE also turns the current row into a filter (context transition).
Almost every useful measure beyond a plain sum uses CALCULATE: last year, share of total, red products only, sales by ship date. Understanding what it does to the filters is most of understanding DAX.
What happens step by step?
- 1Startthe filters of the cell: slicers, rows, columns, page filters
- 2Context transitionin a row context, the current row becomes a filter
- 3ModifiersREMOVEFILTERS, ALL, USERELATIONSHIP, CROSSFILTER are applied
- 4Filterseach Boolean or table filter replaces the filter on its columns, or adds with KEEPFILTERS
- 5Evaluatethe expression runs in the result
The filter arguments are evaluated in the original filter context, before any of them is applied.
What kinds of filters are there?
-- Boolean filter: columns of one table, no measures, no nested CALCULATE
Red Revenue = CALCULATE ( [Revenue], 'Product'[Color] = "Red" )
-- several conditions: AND by separate arguments, OR inside one
Red or Blue Revenue =
CALCULATE ( [Revenue], 'Product'[Color] = "Red" || 'Product'[Color] = "Blue" )
-- table filter
YTD Revenue = CALCULATE ( [Revenue], DATESYTD ( 'Date'[Date] ) )
-- modifier
All Products Revenue = CALCULATE ( [Revenue], REMOVEFILTERS ( 'Product' ) )A Boolean filter is shorthand for a FILTER ( ALL ( column ), condition ), which is why it replaces the existing filter on that column. Microsoft's rules for Boolean filters: columns from a single table, no measure references, no nested CALCULATE, and no table-scanning functions except as arguments of aggregations ('Sales'[OrderDateKey] = MAX ( 'Sales'[OrderDateKey] ) is fine).
Replace or add?
| Product color on the row | CALCULATE ( [Revenue], Color = "Red" ) | CALCULATE ( [Revenue], KEEPFILTERS ( Color = "Red" ) ) |
|---|---|---|
| Blue | Red revenue (the row filter is replaced) | blank (Blue and Red at once) |
| Red | Red revenue | Red revenue |
Use the plain form for a fixed comparison ("Red, whatever the row"), and KEEPFILTERS for "only the red part of what is already filtered". See KEEPFILTERS.
What is context transition?
In a calculated column or an iterator, there is a current row but no filter. CALCULATE turns the row into an equivalent filter:
-- calculated column on Customer
Customer Revenue = CALCULATE ( [Revenue] )Every measure reference is wrapped in an implicit CALCULATE, so [Revenue] inside SUMX over customers is evaluated per customer. Details in filter context and context transition.
Which CALCULATE patterns come up every day?
-- 1 share of total
Share of All = DIVIDE ( [Revenue], CALCULATE ( [Revenue], REMOVEFILTERS ( 'Product' ) ) )
-- 2 share of visible total
Share of Visible = DIVIDE ( [Revenue], CALCULATE ( [Revenue], ALLSELECTED ( 'Product' ) ) )
-- 3 last year
Revenue LY = CALCULATE ( [Revenue], SAMEPERIODLASTYEAR ( 'Date'[Date] ) )
-- 4 year to date
Revenue YTD = CALCULATE ( [Revenue], DATESYTD ( 'Date'[Date] ) )
-- 5 one segment
Online Revenue = CALCULATE ( [Revenue], Sales[Channel] = "Online" )
-- 6 inactive relationship
Revenue by Ship Date = CALCULATE ( [Revenue], USERELATIONSHIP ( Sales[Ship Date], 'Date'[Date] ) )
-- 7 condition on a measure: needs FILTER over a column
Revenue from Big Customers =
CALCULATE ( [Revenue], FILTER ( VALUES ( Customer[Customer] ), [Revenue] > 10000 ) )
-- 8 count with a condition
Late Orders = CALCULATE ( COUNTROWS ( Orders ), Orders[Days Late] > 0 )
-- 9 ignore one filter, keep the others
Revenue All Regions = CALCULATE ( [Revenue], REMOVEFILTERS ( Region[Region] ) )
-- 10 last date's value (stock)
Closing Stock = CALCULATE ( SUM ( Stock[Units] ), LASTDATE ( 'Date'[Date] ) )What goes wrong?
- A measure in a Boolean filter.
CALCULATE ( [Revenue], [Margin] > 0 )is not allowed; useFILTERover the column you want to test (pattern 7). - Expecting AND across the same column.
Color = "Red", Color = "Blue"gives blank: both must be true at once. - Filtering a whole table with FILTER.
FILTER ( Sales, Sales[Amount] > 100 )scans and filters every column of Sales; filter the column:Sales[Amount] > 100. - Forgetting replacement. A row filter on the same column is overwritten; the measure shows the same value on every row.
Where can you check this?
- Microsoft Learn: CALCULATE function (DAX)
- Microsoft Learn: Avoid using FILTER as a filter argument
- Microsoft Learn: KEEPFILTERS function (DAX)
Where do you start?
Write patterns 1, 3 and 5 against your own model and put them next to [Revenue] in a matrix by product and year. Then read FILTER in DAX for conditions a Boolean filter cannot express.