Sub topic 9
Choosing between FILTER, ALL, ALLEXCEPT, and REMOVEFILTERS
This chapter assumes you can write basic measures with SUM and COUNTROWS, and that you know how slicers and visual rows filter a model through relationships. It also assumes you have met filter context, row context, and CALCULATE in the earlier chapters. Here the focus is on choosing: given a requirement, which function do you reach for, and why?
Start from what the function does to filters
The four functions look like they do similar work, but they answer different questions.
| Function | Returns a table? | Effect on filters | Typical home |
|---|---|---|---|
| `FILTER` | Yes, the rows that pass a test | Adds a condition. Keeps existing filters unless you wrap `ALL` inside it | A filter argument of `CALCULATE`, or an iterator input |
| `ALL` | Yes, all rows of a table or all distinct values of columns | Ignores filters on what it names | A `CALCULATE` modifier or a table expression |
| `ALLEXCEPT` | Yes, the table without filters on all but the named columns | Clears every filter on the table except the listed columns | A `CALCULATE` modifier |
| `REMOVEFILTERS` | No | Clears filters on a table, columns, or the whole model | Only as a `CALCULATE` modifier |
Two sentences summarize the split. FILTER is about selecting rows. The other three are about discarding filters. Among those three, REMOVEFILTERS is the only one that cannot be used as a table.
Decision flow
Ask these questions in order.
Adding a condition: boolean argument or FILTER
Use a boolean filter argument when the test involves one column, a constant, and no measure.
Red Sales =
CALCULATE ( [Total Sales], 'Product'[Color] = "Red" )DAX rewrites this internally as a filter over all values of 'Product'[Color]. Because it ignores the existing filter on that column, it replaces it. A slicer set to Blue does not combine with this measure to give an empty result. The measure returns red sales anyway.
Use FILTER when the test cannot be written that way. The usual reasons are:
- The condition compares several columns, such as
Sales[Quantity] * Sales[Net Price] > 1000. - The condition uses a measure, such as "categories with more than 1 million in sales".
- The condition needs row-by-row logic.
Large Line Sales =
CALCULATE (
[Total Sales],
FILTER ( Sales, Sales[Quantity] * Sales[Net Price] > 1000 )
)FILTER iterates Sales row by row, which is the row context from the earlier chapter. It does not ignore existing filters. It scans only the rows still visible in the current filter context.
Pick the smallest table for FILTER
The first argument of FILTER controls both cost and meaning. Compare these two measures.
Sales of Big Categories =
CALCULATE (
[Total Sales],
FILTER ( ALL ( 'Product'[Category] ), [Total Sales] > 1000000 )
)Here FILTER iterates the distinct categories, which is a few rows. ALL ( 'Product'[Category] ) also removes the current Category filter. Without it, the test would run only on categories already in the filter context. For each category, [Total Sales] runs with a context transition, so the test compares that category's sales with the threshold.
If you wrote FILTER ( Sales, ... ) for a condition on category sales, the engine would iterate the whole fact table. The result might be the same, but the cost is much higher.
Clearing filters: which of the three?
REMOVEFILTERS: the default for clearing
When you only want to clear filters, REMOVEFILTERS states that intent directly.
All Product Sales =
CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product' ) )It accepts a table, one or more columns, or no argument. With no argument it clears every filter in the model, so use that form deliberately.
ALL: clearing filters or returning a table
Inside CALCULATE, ALL and REMOVEFILTERS have the same effect.
All Sales Using ALL =
CALCULATE ( [Total Sales], ALL ( 'Product' ) )ALL is the right choice when you need a table value, because REMOVEFILTERS returns none.
Category Count =
COUNTROWS ( ALL ( 'Product'[Category] ) )It is also the right choice as the first argument of FILTER, as in the earlier big-categories example. Many older measures use ALL everywhere, and those still work. For new code, prefer REMOVEFILTERS when you only clear filters, because a reader sees the intent from the name.
ALLEXCEPT: clearing everything except some columns
Use ALLEXCEPT when you need to remove all filters on a table but keep a few.
Sales % of Category =
DIVIDE (
[Total Sales],
CALCULATE ( [Total Sales], ALLEXCEPT ( 'Product', 'Product'[Category] ) )
)In a visual showing category and subcategory, the numerator is the subcategory's sales. The denominator keeps the category filter and clears all other filters on Product, including subcategory. The ratio is therefore each subcategory's share of its category.
Listing the columns to remove is the alternative, and it is more explicit:
Sales % of Category (explicit) =
DIVIDE (
[Total Sales],
CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product'[Subcategory], 'Product'[Product Name] ) )
)Choose by which list is shorter and more stable. If the table has many columns and you want to keep one, ALLEXCEPT is shorter. It also covers columns added to the table later. If you want to clear only one or two known columns, a column-level REMOVEFILTERS is clearer and never clears something by accident.
Table-level or column-level?
Whether you name a table or a column changes the result.
Sales Ignoring Color =
CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product'[Color] ) )
Sales Ignoring All Product Filters =
CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product' ) )The first measure removes the filter on Color only. Category and Brand filters from slicers still apply. The second removes every filter on every column of Product. Pick the smaller scope that meets the requirement. Clearing a whole table when a column is enough discards filters that the report user chose, and the number then ignores their selections without warning.
Percent of total, done with the right tool
The standard pattern uses the same function to clear filters, but the scope depends on the question.
Sales % of All Products =
DIVIDE (
[Total Sales],
CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product' ) )
)This divides by sales of all products. Slicers on other tables, such as Date or Customer, still apply, because only Product filters are cleared. That is usually what report users expect. If you also removed filters on Date, the percentage would compare a selected year with all years, which is a different question.
The table below maps requirements to the function you would use.
| Requirement | Function |
|---|---|
| Sales for red products only | Boolean argument: `'Product'[Color] = "Red"` |
| Sales for lines above a value, using two columns | `FILTER ( Sales, ... )` |
| Sales for categories above a threshold | `FILTER ( ALL ( 'Product'[Category] ), [Total Sales] > x )` |
| Share of all products | `REMOVEFILTERS ( 'Product' )` in the denominator |
| Share within the current category | `ALLEXCEPT ( 'Product', 'Product'[Category] )` in the denominator |
| Count of all categories regardless of filters | `COUNTROWS ( ALL ( 'Product'[Category] ) )` |
Common mistakes in choosing
REMOVEFILTERS used as a table
This fails because REMOVEFILTERS returns no table:
Broken Category Count =
COUNTROWS ( REMOVEFILTERS ( 'Product'[Category] ) )Replace it with ALL ( 'Product'[Category] ).
FILTER on a whole table when a column would do
Compare these two:
Slow Red Sales =
CALCULATE ( [Total Sales], FILTER ( 'Product', 'Product'[Color] = "Red" ) )
Better Red Sales =
CALCULATE ( [Total Sales], 'Product'[Color] = "Red" )The first measure iterates every visible row of Product. The second works on a single column. The two also differ in meaning. FILTER ( 'Product', ... ) respects existing filters on Product, so a slicer on Blue yields blank. The boolean version replaces the Color filter and returns red sales.
Expecting FILTER to clear filters
FILTER ( Sales, ... ) never removes filters. If your measure still reacts to a slicer you meant to ignore, the clearing function is missing, not FILTER.
ALLEXCEPT with a column from another table
The columns listed in ALLEXCEPT must belong to the table named in its first argument. If you want to keep a filter that comes from another table, handle that table separately or clear filters only on the columns you want to remove.
Performance in the decision
Performance rarely changes which function is correct, but it can break ties.
- Boolean arguments and column-level
ALLorREMOVEFILTERSwork on column values, which are small. FILTERover a fact table iterates many rows, and every row-level calculation adds cost.- If
FILTERmust test a measure, iterate a small column such asALL ( 'Product'[Category] ), not the fact table. - Do not compute the same
CALCULATE ( ..., REMOVEFILTERS ( ... ) )several times in one measure. Store it in a variable.
Sales % of All Products (variable) =
VAR AllProductSales =
CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product' ) )
RETURN
DIVIDE ( [Total Sales], AllProductSales )Exercise
A matrix shows Category in rows and Subcategory beneath it. You want each subcategory's share of its own category's sales, and a slicer on Date must still apply. Which function goes in the denominator's CALCULATE, and why not REMOVEFILTERS ( 'Product' )?
Show answer
Use ALLEXCEPT ( 'Product', 'Product'[Category] ). It clears the Subcategory filter and any other Product filters but keeps Category, so the denominator is the sales of the current category. The Date slicer is untouched because it is on another table. REMOVEFILTERS ( 'Product' ) would also clear the Category filter, giving each subcategory's share of all product sales.
Exercise
You want total sales for categories whose sales exceed 1,000,000, even if a slicer selects one category. Write the measure and explain why FILTER takes ALL ( 'Product'[Category] ) and not 'Product'.
Show answer
Sales of Big Categories = CALCULATE ( [Total Sales], FILTER ( ALL ( 'Product'[Category] ), [Total Sales] > 1000000 ) ). ALL returns the distinct categories and ignores the slicer's Category filter, so every category is tested. It also iterates a short column list, not the whole Product table. Each category's [Total Sales] is evaluated through context transition, and the result becomes the new Category filter.
Exercise
Why does COUNTROWS ( REMOVEFILTERS ( 'Product'[Category] ) ) fail, and what is the fix?
Show answer
REMOVEFILTERS only works as a CALCULATE modifier and returns no table, so COUNTROWS has nothing to count. Use COUNTROWS ( ALL ( 'Product'[Category] ) ), because ALL returns a table of distinct categories.