Sub topic 7
Intersecting with Existing Filters Using KEEPFILTERS
This chapter assumes you can write basic DAX measures, know that a Boolean filter argument in CALCULATE replaces any existing filter on the same column, and have a star-schema model with a Sales fact table and a 'Product' dimension. You will learn what KEEPFILTERS changes about that replacement behavior, how to predict its results, and where it causes surprises.
The problem: replacement discards the user's selection
Start with the base measure from earlier chapters:
Total Sales = SUM ( Sales[Amount] )A Boolean filter argument in CALCULATE is shorthand for a table filter on one column. When the argument filters 'Product'[Color], CALCULATE first removes any existing filter on 'Product'[Color], then applies the new one. The measure below therefore ignores a color slicer entirely:
Red Sales =
CALCULATE (
[Total Sales],
'Product'[Color] = "Red"
)If a user selects Blue in a slicer, Red Sales still returns red sales. Often this is what you want. Sometimes it is not. You may want a measure that means "red sales, but only if red is part of what the user selected." Replacement cannot express that. KEEPFILTERS can.
What KEEPFILTERS does
KEEPFILTERS is a filter modifier. You wrap a filter argument in it, and CALCULATE changes how it applies that argument. Instead of replacing the existing filter on the column, it intersects the new filter with the existing one. A row of 'Product' survives only if it satisfies both filters.
Red Sales Within Selection =
CALCULATE (
[Total Sales],
KEEPFILTERS ( 'Product'[Color] = "Red" )
)The result depends on the selection already present on 'Product'[Color]:
| Existing filter on Color | Red Sales | Red Sales Within Selection |
|---|---|---|
| None (all colors visible) | Red sales | Red sales |
| Red | Red sales | Red sales |
| Blue | Red sales | Blank |
| Red and Blue | Red sales | Red sales |
The intersection of "all colors" with "Red" is Red. The intersection of "Blue" with "Red" is empty, so no Sales rows match and the measure returns blank.
How it works internally
A Boolean expression such as 'Product'[Color] = "Red" is expanded by the engine into a table expression, roughly FILTER ( ALL ( 'Product'[Color] ), 'Product'[Color] = "Red" ). The ALL part is what makes the filter independent of the current selection, and CALCULATE then overwrites the existing Color filter with this table of values.
With KEEPFILTERS, CALCULATE does not overwrite. It adds the new filter on top of the existing one, and the engine evaluates the combined filters as an AND. The existing filter stays in the filter context, and the new filter joins it.
This also explains why KEEPFILTERS has an effect only when there is an existing filter on the same column. If the column is not filtered, there is nothing to replace, and the two forms give the same result.
KEEPFILTERS with a table filter argument
KEEPFILTERS also accepts a table expression. A common use is a multi-value filter built with a table constructor or VALUES:
Core Colors Sales =
CALCULATE (
[Total Sales],
KEEPFILTERS ( 'Product'[Color] IN { "Red", "Blue", "Green" } )
)If the user selects Red and Yellow, the filter context after CALCULATE contains only Red, because Red is the single value in both sets. Without KEEPFILTERS, the result would include Red, Blue and Green regardless of the selection.
You can also use KEEPFILTERS around a whole FILTER call. For a FILTER over a single column that you want to treat as a column filter, the effect is the same as the Boolean case: the result is intersected with the existing column filter.
KEEPFILTERS versus a FILTER over the table
A different way to get "red, within the current selection" is to put a filter over the table that evaluates in the existing context:
High Quantity Sales =
CALCULATE (
[Total Sales],
FILTER ( Sales, Sales[Quantity] > 2 )
)This works because FILTER ( Sales, ... ) iterates the rows of Sales as they are visible in the current filter context. The existing filters on Sales and on related dimensions have already reduced those rows. The resulting table then filters Sales further. The condition is therefore added to what is there already, and nothing on a different column is replaced.
This behavior comes from the table being evaluated in the original context, before CALCULATE changes it. It is an intersection too, but it works differently from KEEPFILTERS:
| Row | KEEPFILTERS on a column filter | FILTER over a fact table |
|---|---|---|
| What is filtered | One column, for example `'Product'[Color]` | Rows of the table you iterate |
| How intersection arises | CALCULATE keeps the existing filter on the column | The iterated rows are already restricted by the existing context |
| Cost | Cheap, one column | Can be costly on large tables, because it iterates rows |
| Typical use | Respect a slicer on the same column | Conditions that involve several columns or measures |
Use a Boolean argument with KEEPFILTERS when the condition is on a single column. Use FILTER when the condition needs a row-level test that a simple Boolean argument cannot express.
KEEPFILTERS inside iterators and filter arguments of other functions
KEEPFILTERS appears in places other than the direct arguments of CALCULATE. Two matter in practice.
First, it can wrap the table argument of SUMMARIZE-like functions and the filter table argument of TREATAS, which control how a virtual table filters the model. A frequent pattern is:
Sales for Selected Pairs =
CALCULATE (
[Total Sales],
KEEPFILTERS (
TREATAS (
{ ( "Red", "Large" ), ( "Blue", "Small" ) },
'Product'[Color],
'Product'[Size]
)
)
)Without KEEPFILTERS, TREATAS would replace the filters on Color and Size. With it, the pairs are intersected with whatever the user selected on those columns.
Second, KEEPFILTERS can be used in a CALCULATETABLE in the same way. The rules are identical, because CALCULATETABLE applies filter arguments the same way CALCULATE does.
Interaction with other filter modifiers
KEEPFILTERS changes only how its own argument is applied. It does not change other arguments in the same CALCULATE. Consider two arguments together:
Mixed Filters =
CALCULATE (
[Total Sales],
KEEPFILTERS ( 'Product'[Color] = "Red" ),
'Product'[Size] = "Large"
)Here the Color filter is intersected with the existing Color selection, while the Size filter replaces any existing Size selection. Each argument is handled independently.
Combining KEEPFILTERS with REMOVEFILTERS on the same column needs care. The order of evaluation matters: CALCULATE applies filter-removal modifiers before it applies new filters. So if you write both REMOVEFILTERS ( 'Product'[Color] ) and KEEPFILTERS ( 'Product'[Color] = "Red" ), the removal happens first, no existing filter remains, and the KEEPFILTERS filter intersects with nothing. The measure behaves like the plain replacement version. Combining the two is therefore redundant, and it usually signals a misunderstanding.
Redundant Combination =
CALCULATE (
[Total Sales],
REMOVEFILTERS ( 'Product'[Color] ),
KEEPFILTERS ( 'Product'[Color] = "Red" )
)KEEPFILTERS and row context from iterators
When an iterator creates a row context and then context transition happens, the row values become filters. Those filters replace existing filters on the same columns by default. This is the reason AVERAGEX ( Customer, [Total Sales] ) produces one customer's sales per row: the transition filters on the customer's columns replace any existing filter on those columns.
The engine also uses KEEPFILTERS-like behavior internally in one case: when you iterate over a column with an iterator such as SUMX ( VALUES ( 'Product'[Color] ), ... ), the context transition on 'Product'[Color] keeps the current value set intact rather than replacing it. The VALUES table is derived from the current filter context, so the result is already consistent with the selection. You do not need to add KEEPFILTERS in that case.
Common mistakes
- Using KEEPFILTERS with no existing filter. It has no effect when the column is unfiltered. If a test with no slicer shows identical results, that is expected. Test with a slicer that excludes the value.
- Expecting a number instead of blank. When the intersection is empty, CALCULATE returns blank, not zero. Add
+ 0orCOALESCEonly if a visible zero is a business requirement. - Wrapping a filter that is not on the column you meant. KEEPFILTERS affects the columns that appear in its argument. A filter on
'Product'[Color]does not preserve a selection on'Product'[Category]. - Applying KEEPFILTERS to REMOVEFILTERS or ALL. These are removal modifiers, not filter arguments, so the combination does not preserve anything.
- Expecting KEEPFILTERS to work on a variable. A variable that holds a computed value does not respond to later filters at all, whether or not KEEPFILTERS is used.
Debugging a KEEPFILTERS measure
When a result is not what you expect, check these in order:
- Find the filter on the column your argument targets. Add a card with
CONCATENATEX ( VALUES ( 'Product'[Color] ), 'Product'[Color], ", " )to see the current selection. - Predict the intersection by hand. If the selection and your filter share no values, blank is correct.
- Compare against the same measure without KEEPFILTERS. If both match, the column was not filtered.
- Check whether another argument in the same CALCULATE removes the filter on that column first.
Exercise
A report has a slicer on 'Product'[Color] with Red and Blue selected. The measure CALCULATE ( [Total Sales], KEEPFILTERS ( 'Product'[Color] IN { "Red", "Green" } ) ) is shown in a card. Which colors does the filter context contain when [Total Sales] is evaluated, and what would the card show if KEEPFILTERS were removed?
Show answer
With KEEPFILTERS, the context contains only Red, because Red is the only value in both the selection {Red, Blue} and the filter {Red, Green}. The card shows red sales. Without KEEPFILTERS, the filter replaces the selection, so the context contains Red and Green, and the card shows the sum of red and green sales.
Exercise
You write CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product'[Color] ), KEEPFILTERS ( 'Product'[Color] = "Red" ) ). A user selects Blue in a color slicer. What does the measure return, and why?
Show answer
It returns red sales. CALCULATE applies REMOVEFILTERS before the new filters, so the Blue selection is already gone when the KEEPFILTERS argument is applied. The Red filter then intersects with an unfiltered column, which leaves Red.