Sub topic 7
Intersecting with existing filters using KEEPFILTERS
This chapter assumes you know that a Boolean filter argument in CALCULATE replaces any existing filter on the same column. It also assumes you are comfortable with basic measures and slicers on a star-schema model.
The problem: replacement is not always what you want
Start with the base measure:
Total Sales = SUM ( Sales[Amount] )Here is the measure that restricts the calculation to red products:
Red Sales =
CALCULATE (
[Total Sales],
'Product'[Color] = "Red"
)A Boolean argument like 'Product'[Color] = "Red" is shorthand for a table filter on the Color column. CALCULATE applies it by replacing whatever filter already exists on 'Product'[Color]. If a slicer selects Blue, Red Sales still returns red sales. The slicer selection is overwritten.
Sometimes that is the behavior you want. Sometimes you want the measure to respect the slicer and add its own condition on top. That is the job of KEEPFILTERS.
What KEEPFILTERS does
KEEPFILTERS is a filter modifier. It wraps a filter argument and changes how CALCULATE applies it. The new filter is intersected with the existing filter on the same column instead of replacing it.
Red Sales Within Selection =
CALCULATE (
[Total Sales],
KEEPFILTERS ( 'Product'[Color] = "Red" )
)The result depends on what is already selected for Color:
| Existing filter on Color | `Red Sales` | `Red Sales Within Selection` |
|---|---|---|
| No filter | Red sales | Red sales |
| Red | Red sales | Red sales |
| Blue | Red sales | Blank |
| Red and Blue | Red sales | Red sales |
Only the values that appear in both filters survive. Red and Blue intersected with Red leaves Red. Blue intersected with Red leaves nothing, so the measure returns blank.
Where KEEPFILTERS is useful
KEEPFILTERS is useful when the CALCULATE filter is a narrowing condition rather than a override. Typical cases:
- A measure that should show a subset of whatever the user selected, such as "red items among the chosen colors".
- A row or column in a matrix that already filters Color. Without KEEPFILTERS, the measure ignores the matrix and repeats the same value on every row.
- Iterators and measures that build a filter from a list of values and must not widen the selection the report has already applied.
Compare the matrix behavior. With Color on the rows, Red Sales shows the red total on every row, including the Blue row. Red Sales Within Selection shows red sales on the Red row and blanks on the other rows.
KEEPFILTERS with table filters
KEEPFILTERS also works on table arguments. A filter built from a table expression normally replaces the filters on the columns it covers. Wrapping it in KEEPFILTERS makes it intersect with them instead.
Red or Blue Sales Within Selection =
CALCULATE (
[Total Sales],
KEEPFILTERS (
FILTER ( ALL ( 'Product'[Color] ), 'Product'[Color] IN { "Red", "Blue" } )
)
)If the slicer selects Blue and Green, the result covers Blue only. If it selects Green alone, the result is blank.
Common mistakes
- Expecting KEEPFILTERS to add to the selection. It never widens a filter. It can only narrow it or leave it unchanged.
- Using it when you want an override. A measure such as "Sales of red products regardless of the slicer" needs the plain Boolean filter. KEEPFILTERS would return blank whenever the slicer excludes red.
- Forgetting that a blank can be correct. An empty intersection is the intended outcome, not a bug. Check the selection before assuming the measure is broken.
Summary
- A plain Boolean filter in CALCULATE replaces the existing filter on that column.
- KEEPFILTERS makes the new filter intersect with the existing one.
- The intersection can only keep values present in both filters, so it can return blank.
- Choose replacement for overrides and KEEPFILTERS for restrictions within the user's selection.
Exercise
A matrix shows Color on the rows (Red, Blue, Green). Measure A is CALCULATE ( [Total Sales], 'Product'[Color] = "Red" ). Measure B is CALCULATE ( [Total Sales], KEEPFILTERS ( 'Product'[Color] = "Red" ) ). What does each measure show on the Blue row?
Show answer
Measure A shows the total red sales, because the Boolean filter replaces the Blue filter that the row supplies. Measure B shows blank, because the intersection of Blue and Red is empty.
Exercise
A slicer selects Red and Green. What does Red Sales Within Selection return, and what does it return if the slicer is cleared?
Show answer
With Red and Green selected, the intersection with Red leaves Red, so it returns red sales. With the slicer cleared there is no existing filter on Color, so the result is still red sales.