Sub topic 6
REMOVEFILTERS: clearing filters as a CALCULATE modifier
This chapter assumes you can write basic DAX measures with SUM and COUNTROWS, and that you know what filter context is and how CALCULATE changes it. You should also be comfortable with slicers and with how a visual's rows and columns filter a measure.
What REMOVEFILTERS does
REMOVEFILTERS clears filters. You pass it a table, one or more columns, or nothing at all, and it removes the filters on those objects from the filter context. It does not return a table. It only works as a filter argument of CALCULATE (or CALCULATETABLE), where it acts as a modifier.
Here is the base measure used in this chapter:
Total Sales = SUM ( Sales[Amount] )The next measure clears every filter on the Product table:
All Product Sales =
CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product' ) )In a matrix with product names on the rows, each row would normally show that product's sales. This measure shows the same grand total on every row. Slicers on any Product column are ignored too, because the whole table is cleared.
What you can pass to it
The argument decides how much is cleared.
| Call | Effect |
|---|---|
| `REMOVEFILTERS ( 'Product' )` | Clears all filters on every column of the Product table |
| `REMOVEFILTERS ( 'Product'[Color] )` | Clears only the filter on Color. Filters on other Product columns stay |
| `REMOVEFILTERS ( 'Product'[Color], 'Product'[Size] )` | Clears the filters on both listed columns |
| `REMOVEFILTERS ()` | Clears all filters on all tables in the model |
Clearing a single column is often what you want. If a slicer filters Category and the visual groups by Color, clearing Color keeps the Category slicer working:
Sales All Colors =
CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product'[Color] ) )Clearing a whole table when one column is enough also removes filters you meant to keep. Name the column when you can.
Using it for a percent of total
The most common use is the denominator of a ratio. The numerator keeps the current filters. The denominator removes the Product filters:
Sales % of All Products =
DIVIDE (
[Total Sales],
CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product' ) )
)Each product row now shows its share of the total. DIVIDE returns a blank instead of an error if the denominator is zero. Filters on other tables, such as a date slicer, still apply to both parts. The percentages therefore stay correct for the selected period.
REMOVEFILTERS and ALL
Inside CALCULATE, ALL ( 'Product' ) has the same effect as REMOVEFILTERS ( 'Product' ):
All Sales Using ALL =
CALCULATE ( [Total Sales], ALL ( 'Product' ) )Both give the same result. REMOVEFILTERS is the better choice when you only want to clear filters, because its name states that intent. A reader of the code does not have to work out which role ALL is playing.
The two differ in one way. ALL returns a table, so you can use it where a table is expected. REMOVEFILTERS returns no table, so you cannot.
Category Count = COUNTROWS ( ALL ( 'Product'[Category] ) )This counts every category in the model, ignoring filters. Replacing ALL with REMOVEFILTERS here would be an error, because COUNTROWS needs a table.
Common mistakes
- Using it outside CALCULATE. It is a modifier, so it has no effect or meaning on its own in a measure.
- Clearing a whole table when one column would do. This removes slicers you wanted to keep.
- Expecting it to add a filter. It only removes filters. To set a new filter, add a boolean or table filter argument alongside it.
- Forgetting the relationship direction. Clearing filters on one table does not clear filters on other tables, even when they are related. If a filter on Sales or Date still reaches your result, clear that table as well.
Exercise
A matrix shows Product[Category] on the rows, and a slicer filters Product[Color]. You want each category's sales as a percentage of the total sales for the colors selected in the slicer. Which expression should the denominator use: CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product' ) ) or CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product'[Category] ) )?
Show answer
Use CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product'[Category] ) ). It clears only the Category filter coming from the rows, so the Color slicer still applies and the percentages add up to 100% of the selected colors. Clearing the whole Product table would also remove the Color slicer, so the denominator would include all colors.
Exercise
Why does Category Count = COUNTROWS ( REMOVEFILTERS ( 'Product'[Category] ) ) fail, and how do you fix it?
Show answer
REMOVEFILTERS does not return a table, and COUNTROWS needs one. Replace it with ALL: COUNTROWS ( ALL ( 'Product'[Category] ) ).