Sub topic 6
Removing filters with REMOVEFILTERS, ALL and ALLEXCEPT
This chapter assumes you can write basic measures, know that a slicer or a report row adds filters to a measure, and have read the previous chapters on filter context and on Boolean filters replacing filters on the same column.
Why remove filters
A Boolean filter such as 'Product'[Color] = "Red" replaces the filter on one column with a new value. Sometimes you want no filter on that column at all. The usual case is a denominator: sales for the current category divided by sales for all categories.
DAX has three tools for this. They all appear as arguments of CALCULATE, and they all delete filters from the filter context before the expression is evaluated.
REMOVEFILTERS
REMOVEFILTERS clears filters. It accepts a table, one or more columns, or nothing:
REMOVEFILTERS ( 'Product' )clears every filter on columns of the Product table.REMOVEFILTERS ( 'Product'[Category] )clears only the filter on Category.REMOVEFILTERS ()clears every filter in the model.
It is valid only as a CALCULATE filter argument. It returns no table.
Sales Share of Category =
DIVIDE (
[Total Sales],
CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product'[Category] ) )
)Suppose a matrix shows Category on rows. The numerator is evaluated in the row's own filter context. The denominator clears the Category filter, so it returns the total over all categories.
| Category | Total Sales | Denominator | Sales Share of Category |
|---|---|---|---|
| Bikes | 700 | 1000 | 70% |
| Accessories | 300 | 1000 | 30% |
Only the Category filter is removed. If the report also has a slicer on 'Product'[Color], that filter still applies to both parts. The result is each category's share of all sales for the selected colors.
ALL
ALL does the same job inside CALCULATE, with the same argument forms:
Sales All Categories =
CALCULATE ( [Total Sales], ALL ( 'Product'[Category] ) )This returns the same value as the REMOVEFILTERS version. ALL is also an ordinary table function. Outside the CALCULATE filter-argument position, it returns the distinct values of a column or all rows of a table, ignoring filters. That is why REMOVEFILTERS was added. It states the intent of removing filters and cannot be mistaken for a table you iterate over. In new code, prefer REMOVEFILTERS when you only want to clear filters.
ALLEXCEPT
ALLEXCEPT removes every filter on a table except the columns you list:
Sales Ignoring Product Attributes =
CALCULATE (
[Total Sales],
ALLEXCEPT ( 'Product', 'Product'[Category] )
)If the context filters Category, Subcategory and Color, this measure keeps the Category filter and drops the other two. It is shorthand for removing the whole table and then restoring the listed columns.
Gotchas
- Removing filters on one table does not remove filters that reach it through a relationship.
ALL ( Sales )clears filters on Sales columns, but a filter on'Date'[Year]still propagates to Sales. To ignore the year, remove it from the Date table. - A filter on a different column of the same table survives.
REMOVEFILTERS ( 'Product'[Category] )leaves Subcategory and Color filters in place. ALLEXCEPTtakes columns of the table in its first argument. Listing a column from another table gives an error.- Removal is not permanent. It affects only the expression inside that CALCULATE.
Exercise
A matrix has Subcategory on rows and a slicer on Color. You write the denominator CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product'[Subcategory] ) ). Which filters apply to the denominator, and how would you write a measure that ignores everything on the Product table except Color?
Show answer
The slicer filter on Color still applies, because only the Subcategory filter was removed. To ignore every Product filter except Color, use CALCULATE ( [Total Sales], ALLEXCEPT ( 'Product', 'Product'[Color] ) ).