Sub topic 3
CALCULATE: changing filter context with filter arguments
This chapter assumes you can write basic measures with SUM and COUNTROWS. It also assumes you know how relationships in a model pass filters from dimension tables to fact tables, and that you have used slicers and visuals in Power BI. The chapter on filter context covered how a measure is evaluated. Here you learn how to change that context on purpose.
What CALCULATE does
CALCULATE evaluates an expression in a filter context that you modify. Its syntax is:
CALCULATE ( <expression>, <filter1>, <filter2>, ... )The first argument is the expression to evaluate, usually a measure. Every later argument is a filter that adds to the current filter context or replaces part of it. Without CALCULATE, a measure can only react to filters that the report applies. With CALCULATE, a measure can set its own filters.
Start with a base measure:
Total Sales = SUM ( Sales[Amount] )In a matrix with Product[Category] on the rows, this measure returns one value per category. Each row of the matrix applies a different filter, and the measure never mentions categories. This is filter context at work.
The evaluation steps
CALCULATE follows the same sequence every time. Knowing it explains most surprising results.
- It evaluates the filter arguments in the original filter context, the one that existed before
CALCULATEran. - It builds a new filter context. Each filter argument is applied to the original context. A filter on a column replaces any existing filter on that same column. Filters on other columns stay in place.
- It evaluates the expression in the new filter context and returns the result.
Step 2 matters most. CALCULATE does not usually clear the whole context. It overrides the columns you name and leaves the rest alone.
Boolean filter arguments
The simplest filter argument is a boolean expression that compares one column to a value:
Red Sales =
CALCULATE ( [Total Sales], 'Product'[Color] = "Red" )This measure returns the sales of red products, whatever color the report is filtered to. Suppose a slicer selects Color = Blue. Red Sales still returns red sales, because the argument replaces the filter on Product[Color].
DAX treats a boolean argument as shorthand for a table expression. The measure above behaves as if you had written this:
Red Sales Expanded =
CALCULATE (
[Total Sales],
FILTER ( ALL ( 'Product'[Color] ), 'Product'[Color] = "Red" )
)ALL ( 'Product'[Color] ) returns every distinct color, ignoring existing filters on that column. FILTER keeps only the red row. That one-row table becomes the new filter on Product[Color]. This expansion explains why a boolean argument replaces the filter on its column. The shorthand is also cheaper to evaluate, so prefer it when it is allowed.
Other columns keep their filters. If a slicer selects Category = Bikes, Red Sales returns red bike sales. The Category filter is untouched.
The table below shows the result with example numbers.
| Slicer selection | Total Sales | Red Sales |
|---|---|---|
| None | 1,000 | 250 |
| Color = Blue | 300 | 250 |
| Category = Bikes | 600 | 180 |
| Category = Bikes and Color = Blue | 120 | 180 |
In the last row, the Color slicer is overridden but the Category slicer is kept. The numbers are illustrative, but the pattern is how CALCULATE behaves.
Several values and several arguments
To match several values of one column, put them in a single boolean expression:
Red or Blue Sales =
CALCULATE ( [Total Sales], 'Product'[Color] IN { "Red", "Blue" } )Separate filter arguments combine with AND. The expression below returns red products that are also in the Bikes category:
Red Bike Sales =
CALCULATE (
[Total Sales],
'Product'[Color] = "Red",
'Product'[Category] = "Bikes"
)Each argument applies to a different column, so the result is the intersection of the two.
Rules for boolean arguments
A boolean filter argument must follow these rules:
- It can refer to only one column.
- It cannot call a measure or a function that evaluates a measure.
- It cannot contain
CALCULATEor another function that evaluates a filter context, such as a nested aggregation over a different table. - It must be a simple comparison of a column against constants or scalar expressions that do not depend on rows.
A boolean argument can still use a scalar value calculated elsewhere. For example, Sales[Net Price] > 100 is valid, and so is a comparison against a variable that holds a number.
High Price Sales =
CALCULATE ( [Total Sales], Sales[Net Price] > 100 )Table filter arguments
When a condition involves more than one column, or needs a measure, a boolean argument is not allowed. You supply a table expression instead. The usual choice is FILTER:
Large Line Sales =
CALCULATE (
[Total Sales],
FILTER ( Sales, Sales[Quantity] * Sales[Net Price] > 1000 )
)The condition multiplies two columns, so it cannot be a boolean argument. FILTER iterates the Sales table row by row. It tests the condition on each row, and it returns the rows that pass. CALCULATE then uses those rows as the new filter on Sales, and [Total Sales] sums the amounts of those rows.
A table filter argument works like this:
- The table expression is evaluated in the original filter context, before
CALCULATEchanges anything. - The rows it returns become a filter on the columns of that table.
- A table that carries many columns, such as a whole fact table, filters on all of them at once.
The second point has a consequence. FILTER ( Sales, ... ) starts from the Sales rows that the current filters already allow. If a slicer selects the year 2024, only 2024 rows are tested, so the result is large lines within 2024. To test rows regardless of existing filters, you wrap the table in ALL, for example FILTER ( ALL ( Sales ), ... ). The ALL function has its own chapter, so treat that form as a preview here.
Boolean or table argument
Both forms can express some filters, so you choose between them:
| Situation | Preferred argument |
|---|---|
| Compare one column to a constant | Boolean, such as `'Product'[Color] = "Red"` |
| Match a list of values in one column | Boolean with `IN` |
| Condition across several columns of one table | `FILTER` over that table |
| Condition that uses a measure | `FILTER` over a table or column that supplies the rows to test |
| Remove or keep filters | A filter modifier such as `REMOVEFILTERS`, covered next |
Filter arguments that replace or add
Whether an argument replaces an existing filter or narrows it depends on the argument.
- A boolean argument on a column overrides the existing filter on that column.
- A table argument overrides the existing filters on the columns of the table it returns. Because
FILTER ( Sales, ... )starts from the already-filteredSalesrows, it appears to narrow the context. The filter is replaced, but the replacement is a subset of the original. - A table built from
ALLorVALUEScan widen or restate the filter, depending on what the function returns.
If you need a new filter to intersect with the existing filter on the same column, wrap the argument in KEEPFILTERS. The last chapter of this tutorial introduces it.
CALCULATE inside a row context
CALCULATE has a second job, called context transition. If it runs inside a row context, it converts the current row into filters first. Consider a calculated column on the Customer table:
Customer Sales = CALCULATE ( SUM ( Sales[Amount] ) )For each customer row, CALCULATE turns the row's column values into filters on Customer. Those filters flow through the relationship to Sales, so each customer gets their own total. Without CALCULATE, every row would show the grand total, because a row context does not filter related tables by itself.
The same thing happens when a measure is referenced inside an iterator such as SUMX. A measure reference is wrapped in an implicit CALCULATE, so the current row becomes a filter. You do not need to master this now. Remember only that CALCULATE converts rows into filters, and that this conversion happens before the filter arguments are applied.
Common errors with filter arguments
These mistakes appear often when you start writing CALCULATE expressions.
Using a measure in a boolean argument. This fails with an error:
Bad Measure =
CALCULATE ( [Total Sales], [Total Sales] > 1000 )A boolean argument cannot reference a measure. Use FILTER over a table that provides the rows to test, such as a column of products or customers.
Referring to two columns in one boolean argument. This also fails:
Bad Columns =
CALCULATE ( [Total Sales], Sales[Quantity] * Sales[Net Price] > 1000 )The condition touches two columns. Use FILTER ( Sales, ... ) as in Large Line Sales.
Expecting the filter to affect other tables without a relationship. A filter on Product[Color] changes Sales only because a relationship connects them. If no active relationship exists, the filter has no effect on the other table.
Assuming a boolean argument clears sibling columns. A filter on Product[Color] leaves Product[Category] alone. If you want to clear filters, you must say so with a filter modifier.
Filtering a whole table where one column is enough. FILTER ( 'Product', 'Product'[Color] = "Red" ) works, but it iterates every product row and carries every product column into the filter. The boolean form 'Product'[Color] = "Red" states the same intent more cheaply.
Checking your understanding by tracing a result
To trace any CALCULATE, ask these questions in order:
- What is the filter context before
CALCULATEruns? - What columns does each filter argument touch?
- Which existing filters are on those columns? Those filters are replaced.
- Which filters remain after replacement? They stay in force.
- What does the expression return in the resulting context?
Apply this to Red Sales in a matrix with Product[Category] on the rows. The row for Bikes filters Category = Bikes. The argument touches Color, which has no filter in that cell. The new context is Category = Bikes and Color = Red. The measure returns red bike sales.
Summary of the key ideas
CALCULATEevaluates an expression in a modified filter context.- Filter arguments are evaluated in the original context, then applied together.
- A filter on a column replaces the existing filter on that column and keeps filters on other columns.
- Boolean arguments cover simple single-column conditions and expand to
FILTERoverALLof that column. - Table arguments such as
FILTERhandle multi-column conditions and conditions that use measures. - Inside a row context,
CALCULATEconverts the row into filters first.
The next chapter looks at FILTER itself: what it returns, how it iterates, and where it differs from a boolean argument.
Exercise
A matrix shows Product[Category] on the rows, and a slicer selects Product[Color] = "Blue". The measure Red Sales = CALCULATE ( [Total Sales], 'Product'[Color] = "Red" ) is added to the matrix. What does the Bikes row show, and why?
Show answer
It shows the sales of red bikes. The row filters Category = Bikes. The boolean argument replaces the slicer's filter on Color (Blue) with Color = Red, but it leaves the filter on Category unchanged. Evaluating [Total Sales] in Category = Bikes and Color = Red gives red bike sales.
Exercise
Why does CALCULATE ( [Total Sales], Sales[Quantity] * Sales[Net Price] > 1000 ) return an error, and how do you rewrite it?
Show answer
A boolean filter argument can refer to only one column, and this one refers to two. Rewrite it with a table argument: CALCULATE ( [Total Sales], FILTER ( Sales, Sales[Quantity] * Sales[Net Price] > 1000 ) ). FILTER tests the condition row by row on the Sales table, and CALCULATE uses the matching rows as the new filter.
Exercise
Write a measure that returns sales of products that are either Red or Blue and are in the Bikes category, using boolean arguments only.
Show answer
Red or Blue Bike Sales = CALCULATE ( [Total Sales], 'Product'[Color] IN { "Red", "Blue" }, 'Product'[Category] = "Bikes" ). The IN list matches several values of one column in a single argument. The two arguments touch different columns, so they combine with AND.