Sub topic 8
Removing and preserving filters with ALL, REMOVEFILTERS, and KEEPFILTERS
This chapter assumes you know the following:
- How filter context and row context differ.
- How a filter on one table reaches another through a one-to-many relationship.
- How
CALCULATEevaluates an expression in a modified filter context.
The model has Sales on the many side, with Product and Customer on the one side. The [Sales Amount] measure is the one defined earlier in the tutorial.
Why `CALCULATE` needs more than filter arguments
A filter argument in CALCULATE can only narrow or replace a filter on a column. It cannot say "ignore the slicer" or "respect the slicer and add my condition on top". Three tools cover those cases:
ALLandREMOVEFILTERSremove filters.KEEPFILTERSchanges how a new filter combines with the existing one.
To use them well, you need to know what a plain filter argument does by default.
The default: a filter argument replaces filters on the same column
Consider this measure:
Red Sales =
CALCULATE ( [Sales Amount], Product[Color] = "Red" )The Boolean expression Product[Color] = "Red" is shorthand for a table filter on one column:
Red Sales Expanded =
CALCULATE (
[Sales Amount],
FILTER ( ALL ( Product[Color] ), Product[Color] = "Red" )
)The ALL ( Product[Color] ) inside the expansion explains the replacement behavior. The filter table is built from all colors, ignoring any current filter on Product[Color]. CALCULATE then overwrites the existing filter on that column with the result.
Three details follow from this:
- A slicer that selects Blue is overwritten.
Red Salesstill returns red sales. - Filters on other columns survive. If a visual groups by
Product[Category], each row shows red sales for that category. - Filters on other tables survive. A
Customer[Country]slicer still reachesSalesthrough its relationship.
Removing filters with `ALL` and `REMOVEFILTERS`
A typical need is a percentage of a grand total. The denominator must ignore the filters that the visual applies to products.
Sales Share of All Products =
DIVIDE (
[Sales Amount],
CALCULATE ( [Sales Amount], REMOVEFILTERS ( Product ) )
)REMOVEFILTERS ( Product ) clears every filter on every column of Product. That covers slicers on Product[Color], rows of a matrix grouped by Product[Category], and so on. The numerator keeps the current context, so each row shows its share of the all-products total.
What each form removes
| Form | Removes |
|---|---|
| `REMOVEFILTERS ( Product[Color] )` | Filters on that one column |
| `REMOVEFILTERS ( Product[Color], Product[Category] )` | Filters on the listed columns |
| `REMOVEFILTERS ( Product )` | Filters on all columns of the table |
| `REMOVEFILTERS ()` | Filters on all columns of all tables in the model |
| `ALLEXCEPT ( Product, Product[Category] )` | Filters on all `Product` columns except the listed ones |
ALL accepts the same arguments as REMOVEFILTERS. As a CALCULATE argument, the two behave identically. Prefer REMOVEFILTERS there, because the name says what happens.
`ALL` is also a table function
ALL can return a table, and REMOVEFILTERS cannot. Use ALL when you need the rows themselves:
Product Count All =
COUNTROWS ( ALL ( Product ) )The count ignores the filter context and returns every product. The same applies to SUMX ( ALL ( Product ), ... ) and any other iterator that needs an unfiltered table.
Gotchas when removing filters
Removing the wrong table
REMOVEFILTERS ( Sales ) clears filters on the columns of Sales itself. It does not clear a filter on Product. That filter is a condition on Product columns, and it reaches Sales through the relationship. Clearing Sales columns leaves it in place. To ignore a product slicer, remove the filter from Product.
Removing too much
REMOVEFILTERS ( Product ) also removes the filter that identifies the current product in a matrix row. That is what you want for a grand total. It is a mistake when you only meant to ignore the color slicer. In that case name the column:
Sales Ignoring Color =
CALCULATE ( [Sales Amount], REMOVEFILTERS ( Product[Color] ) )Partial removal with `ALLEXCEPT`
To compute a share within the current category, keep the category filter and clear the rest of Product:
Sales Share of Category =
DIVIDE (
[Sales Amount],
CALCULATE ( [Sales Amount], ALLEXCEPT ( Product, Product[Category] ) )
)Removal and context transition
Modifiers and context transition interact. Inside a row context, CALCULATE first converts the current row into filters. The modifiers are then applied, so they can remove those transition filters. This measure shows the effect when it is used inside an iterator over Product:
Total Sales Seen From Each Product =
SUMX (
Product,
CALCULATE ( [Sales Amount], REMOVEFILTERS ( Product ) )
)For each product row, the transition filter on that product is removed. Every iteration therefore returns the all-products total, and the sum repeats that total once per product. The measure is useful only as a demonstration. It is also a common source of accidental bugs, where a REMOVEFILTERS placed inside an iterator cancels the per-row effect you wanted.
Preserving filters with `KEEPFILTERS`
Replacement is the default, but sometimes you want your condition added to the user's selection. KEEPFILTERS changes the behavior from replace to intersect.
Red Sales Within Selection =
CALCULATE (
[Sales Amount],
KEEPFILTERS ( Product[Color] = "Red" )
)The result depends on what the user has selected for color:
| Color selection | `Red Sales` | `Red Sales Within Selection` |
|---|---|---|
| No filter | Red sales | Red sales |
| Red | Red sales | Red sales |
| Red and Blue | Red sales | Red sales |
| Blue | Red sales | Blank |
With only Blue selected, the intersection of Blue and Red is empty, so no Product rows survive and no Sales rows are visible. The measure returns blank.
When `KEEPFILTERS` is the right choice
- The measure must never show data the user has filtered out. A report that shows "Red Sales" next to a Blue slicer may mislead readers.
- You build a measure that counts rows meeting an extra condition within the current selection.
- You want the filter to behave like a slicer selection added on top of the existing ones.
Using `KEEPFILTERS` with a table filter
KEEPFILTERS also wraps table expressions:
Top Category Sales =
CALCULATE (
[Sales Amount],
KEEPFILTERS ( VALUES ( Product[Category] ) )
)Here the table argument reads the categories currently visible and puts the same filter back. This pattern is rare. It shows that KEEPFILTERS modifies how a filter argument is applied, whatever the filter's shape.
Choosing between the three behaviors
Ask what the user's filter should mean for the measure:
- The measure should ignore the filter. Use
REMOVEFILTERSon the column or table. - The measure should override the filter with its own condition. Use a plain filter argument.
- The measure should respect the filter and add its own condition. Use
KEEPFILTERS.
Common errors
- Expecting a plain filter argument to respect a slicer.
Product[Color] = "Red"replaces the slicer. AddKEEPFILTERSif you want intersection. - Using
ALL ( Product )when only a column should be cleared. The measure then ignores category, brand, and every other product filter too. - Clearing the wrong table. Removing filters on
Salesdoes not remove a filter coming fromProductorCustomer. - Putting
REMOVEFILTERSinside an iterator without thinking about transition. It removes the filters that context transition created, which cancels the per-row result. - Using
REMOVEFILTERSwhere a table is required. It is aCALCULATEmodifier only. UseALLorALLEXCEPTas the table argument of an iterator orCOUNTROWS.
Putting it together
This measure shows a percentage of the filtered total. It keeps the color selection and ignores the category selection:
Share Across Categories =
VAR Numerator = [Sales Amount]
VAR Denominator =
CALCULATE ( [Sales Amount], REMOVEFILTERS ( Product[Category] ) )
RETURN
DIVIDE ( Numerator, Denominator )Both variables are evaluated in the current filter context, and the variable Denominator clears only the category filter. A color slicer therefore still narrows both values. The DIVIDE function returns blank when the denominator is zero or blank, so empty combinations do not raise an error.
Exercise
A matrix shows Product[Category] on rows, and a slicer selects Color = Blue. Measure A is CALCULATE ( [Sales Amount], Product[Color] = "Red" ). Measure B is CALCULATE ( [Sales Amount], KEEPFILTERS ( Product[Color] = "Red" ) ). What does each measure show on each category row, and why?
Show answer
Measure A shows red sales for each category. The plain filter argument replaces the slicer's filter on Product[Color], while the category filter from the row stays. Measure B shows blank on every row. KEEPFILTERS intersects Red with the slicer's Blue, so no Product rows remain and no Sales rows are visible.
Exercise
You want a measure that returns each category's sales as a share of total sales for all products, but a Product[Color] slicer should still narrow the numerator and the denominator alike. Which function do you use in the denominator, and on what?
Show answer
Use REMOVEFILTERS ( Product[Category] ) inside CALCULATE for the denominator. It clears only the category filter and leaves the color slicer in place. Using REMOVEFILTERS ( Product ) would also clear the color slicer, so the denominator would no longer respect the slicer.
Exercise
A measure inside SUMX ( Product, CALCULATE ( [Sales Amount], REMOVEFILTERS ( Product ) ) ) returns a very large number. Why?
Show answer
Inside the iterator, CALCULATE turns each product row into a filter, then REMOVEFILTERS ( Product ) removes that filter. Each iteration returns the all-products total, and SUMX adds that total once for every product row.