Sub topic 8
Removing and preserving filters with ALL, REMOVEFILTERS, and KEEPFILTERS
This chapter assumes you know what filter context is, how CALCULATE adds filter arguments, and how a measure like [Sales Amount] is defined. Here you learn the three tools that control what happens to filters that already exist: removing them, or keeping them while you add more.
What CALCULATE does by default
When you write CALCULATE ( [Sales Amount], Product[Color] = "Red" ), DAX first builds a new filter on Product[Color]. It then replaces any existing filter on that same column. Filters on other columns stay in place.
That replacement is usually what you want. Sometimes you need something else:
- Ignore a filter completely, for example to compute a percentage of a grand total.
- Add a filter and respect the one already there.
Removing filters with ALL and REMOVEFILTERS
ALL and REMOVEFILTERS both clear filters when used as a CALCULATE filter argument. You can pass a table, one or more columns, or no argument at all, which clears everything.
All Sales =
CALCULATE ( [Sales Amount], REMOVEFILTERS ( Product ) )
All Sales Any Color =
CALCULATE ( [Sales Amount], REMOVEFILTERS ( Product[Color] ) )The first measure clears every filter on the Product table. The second clears only the filter on Product[Color]. A category slicer would still apply to it.
ALL does the same job in this position, so CALCULATE ( [Sales Amount], ALL ( Product ) ) returns the same result as the first measure. Prefer REMOVEFILTERS when your goal is to remove filters, because the name states the intent. ALL is also a table function that returns every row of a table or column. You need that behavior when you use it inside an iterator or as the input of another function. REMOVEFILTERS can only be used as a CALCULATE modifier.
A common use is a share-of-total measure:
Share of All Products =
DIVIDE (
[Sales Amount],
CALCULATE ( [Sales Amount], REMOVEFILTERS ( Product ) )
)The numerator uses the current filter context. The denominator removes the Product filters, so it is the total across all products.
Preserving filters with KEEPFILTERS
Normally a filter argument replaces an existing filter on the same column. KEEPFILTERS changes this: the new filter is intersected with the existing one.
Red Sales =
CALCULATE ( [Sales Amount], Product[Color] = "Red" )
Red Sales Within Selection =
CALCULATE (
[Sales Amount],
KEEPFILTERS ( Product[Color] = "Red" )
)Suppose a slicer selects Blue and Green. Red Sales ignores the slicer on color and returns red sales. Red Sales Within Selection keeps the slicer, so only colors that are both Red and in the selection survive. Red is not in the selection, so the result is blank.
Choosing between them
| Goal | Tool | Effect on the existing filter |
|---|---|---|
| Override a filter with a new value | Plain filter argument | Replaced on that column |
| Ignore a filter entirely | `REMOVEFILTERS` or `ALL` | Removed |
| Narrow the current selection | `KEEPFILTERS` | Intersected |
Common errors
- Removing too much.
REMOVEFILTERS ( Product )clears filters on every column of Product, including the category slicer. Name specific columns when you want to clear only some. - Expecting a slicer to still apply. A plain
Product[Color] = "Red"argument replaces the slicer on color. UseKEEPFILTERSif the slicer must still limit the result. - Forgetting that other tables are unaffected.
REMOVEFILTERS ( Product )does not clear filters on Customer or Date.
Exercise
A report has a slicer on Product[Category] set to Bikes. Write a measure that returns the sales for all categories, ignoring that slicer, and say why a plain Product[Category] = "Bikes" argument would not be the right tool.
Show answer
Use CALCULATE ( [Sales Amount], REMOVEFILTERS ( Product[Category] ) ). A plain filter argument can only set a specific value on the column, replacing the slicer with that value. It cannot clear the filter to show all categories. REMOVEFILTERS removes the filter on the column, so every category is included.
Exercise
A slicer selects the colors Red and Blue. What does CALCULATE ( [Sales Amount], KEEPFILTERS ( Product[Color] = "Red" ) ) return, compared with the same formula without KEEPFILTERS?
Show answer
With KEEPFILTERS, the new filter is intersected with the slicer, so the result is the sales of red products only, because Red is in both. Without KEEPFILTERS, the new filter replaces the slicer, so the result is also red sales here. The results differ when the value is not in the selection. If the slicer selected only Blue, KEEPFILTERS would return blank and the plain version would still return red sales.