Sub topic 7
CALCULATE: changing the filter context
This chapter assumes you know the difference between a measure and a calculated column, can write simple aggregations such as SUM and COUNTROWS, and understand one-to-many relationships. It also assumes you have met filter context (the set of filters that decides which rows a formula can see) and row context (a pointer to a single row during iteration).
What CALCULATE does
CALCULATE evaluates an expression in a filter context that you modify. Its shape is:
CALCULATE ( <expression>, <filter1>, <filter2>, ... )The first argument is the expression to evaluate, usually a measure or an aggregation. The remaining arguments are filter arguments. CALCULATE builds a new filter context from the existing one plus the filter arguments, then evaluates the expression in that new context.
Two points matter most:
- CALCULATE does not change the report. It only changes the context in which its own expression runs.
- The filter arguments are evaluated in the original (outer) filter context, and only then applied to produce the new one.
Here is the base measure used in this chapter:
Sales Amount =
SUMX ( Sales, Sales[Quantity] * Sales[Unit Price] )SUMX creates a row context over Sales and adds up the per-row product. The surrounding filter context decides which Sales rows are iterated.
A first CALCULATE
Red Sales =
CALCULATE ( [Sales Amount], Product[Color] = "Red" )Product[Color] = "Red" is a filter argument. CALCULATE adds a filter on Product[Color] and replaces any existing filter on that column. It then evaluates [Sales Amount] in the new filter context. Because Product filters Sales through the relationship, only sales of red products are iterated.
Suppose a visual shows one row per color. In the Blue row, the outer filter context says Color = "Blue". Red Sales still shows red sales in that row, because the new filter on Product[Color] overwrites the Blue filter. That is why Red Sales repeats the same number on every color row.
Filter arguments are tables of values
A filter argument is not a true/false test applied to rows one at a time. Internally, CALCULATE turns each filter argument into a table, and that table becomes a filter on the model.
The boolean shorthand Product[Color] = "Red" is syntactic sugar. DAX rewrites it as a filter over a single column:
Red Sales Expanded =
CALCULATE (
[Sales Amount],
FILTER ( ALL ( Product[Color] ), Product[Color] = "Red" )
)The result is a one-column table containing the single value "Red". CALCULATE applies that table as a filter on Product[Color]. Knowing this explains several rules:
- A boolean filter argument can reference only one column.
- A boolean filter argument cannot contain a measure or a call to CALCULATE.
- You can also pass a table expression directly, such as
FILTER ( ... ),VALUES ( ... ), orALL ( ... ).
Replace by default
When a filter argument targets a column that is already filtered, the default behavior is replacement. The new filter overwrites the old one on that same column. Filters on other columns stay.
| Existing filter context | CALCULATE argument | Resulting filter on Color | Other filters |
|---|---|---|---|
| Color = Blue | Color = "Red" | Red | Kept |
| Color in {Blue, Green} | Color = "Red" | Red | Kept |
| No filter on Color | Color = "Red" | Red | Kept |
| Category = Bikes | Color = "Red" | Red | Category = Bikes kept |
Replacement works on the column named in the argument, not on the whole table. A filter on Product[Category] survives a CALCULATE that only sets Product[Color].
Preserving the existing filter with KEEPFILTERS
Sometimes replacement is not what you want. You may want the new filter to narrow the existing selection instead of overriding it. KEEPFILTERS does this:
Red Sales Within Selection =
CALCULATE (
[Sales Amount],
KEEPFILTERS ( Product[Color] = "Red" )
)KEEPFILTERS intersects the new filter with the existing filter on Product[Color] instead of replacing it. In the Blue row, the existing filter is Blue and the new one is Red. Their intersection is empty, so the result is blank. In the Red row, the result is red sales. With no filter on color, the result is red sales.
Removing filters
To ignore a filter that the report applied, remove it inside CALCULATE. The two common tools are ALL and REMOVEFILTERS. They are covered fully in the next chapter, but a short example shows how they fit the CALCULATE model:
Sales All Colors =
CALCULATE ( [Sales Amount], REMOVEFILTERS ( Product[Color] ) )This clears any filter on Product[Color] before evaluating [Sales Amount]. Other filters, such as one on Product[Category], stay in place.
Order of evaluation
CALCULATE follows a fixed sequence. Memorizing it prevents most surprises.
- CALCULATE evaluates all filter arguments in the original filter context. Each one yields a table of filter values.
- If the original context contains a row context, CALCULATE converts it into filter context (context transition). The next chapters treat this in detail.
- CALCULATE applies the filter arguments to the filter context. Arguments that target a column already filtered replace that filter, unless wrapped in KEEPFILTERS.
- CALCULATE evaluates the first argument in the resulting filter context.
Step 1 has a practical consequence. A filter argument that reads a measure or a table sees the outer context, not the context CALCULATE is building. For example, a filter that compares against [Sales Amount] uses the value for the current outer selection.
Multiple filter arguments
Several filter arguments combine with AND logic. Each narrows the context further, as long as they target different columns.
Red Bike Sales =
CALCULATE (
[Sales Amount],
Product[Color] = "Red",
Product[Category] = "Bikes"
)Only sales of products that are both red and in the Bikes category are included. If two arguments target the same column, the later one does not refine the first. Both are applied independently to the same column, and the resulting filters are intersected. For example, Product[Color] = "Red" together with Product[Color] = "Blue" leaves no valid color, so the result is blank.
If you need OR logic across values of the same column, use a single argument:
Red or Blue Sales =
CALCULATE (
[Sales Amount],
Product[Color] IN { "Red", "Blue" }
)OR logic across different columns needs a table filter, for example a FILTER over a table that tests both columns in one expression. That form is more expensive, because it works on combinations of columns.
Filters travel through relationships
A filter argument on Product reaches Sales because the relationship propagates the filter from the one side to the many side. CALCULATE does not need to know about the relationship. It only changes the filter context, and normal propagation takes over. This is why Red Sales works even though the colors live in Product and the amounts live in Sales.
The reverse direction does not happen by default. A filter on Sales does not filter Product unless the relationship is configured for bidirectional filtering, or you use a function such as CROSSFILTER inside CALCULATE.
Common errors
- Expecting a filter to add instead of replace. A color filter in a visual does not combine with
Product[Color] = "Red"in CALCULATE. It is overwritten. Use KEEPFILTERS when you want the combination. - Filtering a table instead of a column.
CALCULATE ( [Sales Amount], Product )with a whole table can remove or alter more filters than intended, and it is expensive. Prefer filtering the columns you need. - Using a measure in a boolean filter argument.
CALCULATE ( [Sales Amount], [Sales Amount] > 1000 )is invalid. Wrap the logic inFILTERover a table and decide carefully which table to iterate. - Assuming filter arguments see the new context. They are evaluated in the outer context first.
- Clearing too much. Removing filters from a whole table also removes filters from its columns that you may have wanted to keep. Name the exact columns.
A worked example: share of a total
A common use of CALCULATE is a ratio against a modified context. The numerator uses the current context. The denominator removes the color filter.
Color Share =
DIVIDE (
[Sales Amount],
CALCULATE ( [Sales Amount], REMOVEFILTERS ( Product[Color] ) )
)In a visual with one row per color, the numerator is sales for that color. The denominator is sales for all colors under the remaining filters. If a slicer limits the report to the Bikes category, both parts respect it, so the share is within Bikes. Only the color filter is removed.
Exercise
A matrix shows one row per Product[Color]. The measure Red Sales = CALCULATE ( [Sales Amount], Product[Color] = "Red" ) is placed in it. What does the Blue row show, and what does it show if you change the filter to KEEPFILTERS ( Product[Color] = "Red" )?
Show answer
With the plain filter, the Blue row shows the same red sales figure as every other row, because the filter on Product[Color] replaces the Blue filter from the row. With KEEPFILTERS, the new filter is intersected with the existing Blue filter. Blue and Red share no value, so the Blue row is blank. The Red row shows red sales in both versions.
Exercise
In what order does CALCULATE evaluate its filter arguments and its first argument, and why does that matter for a filter that references a measure?
Show answer
CALCULATE first evaluates the filter arguments in the original (outer) filter context, then converts any row context into filter context, then applies the filters, and finally evaluates the first argument in the new context. A filter argument that depends on a measure therefore sees the outer context, not the modified one.