Sub topic 9
Choosing between FILTER, ALL, ALLEXCEPT, and REMOVEFILTERS
This chapter assumes you can write basic measures with SUM and COUNTROWS. It also assumes you know how CALCULATE changes filter context and how a slicer filters a visual. The four functions below all appear inside CALCULATE, but they solve different problems. Picking the right one comes down to a few questions about what you need.
Start from the requirement
Ask what the measure must do to the current filters.
- Do you need to add or replace a filter with a simple condition on one column? Use a boolean filter argument.
- Do you need a condition that compares several columns, or tests a measure? Use
FILTER. - Do you need to clear filters and nothing else? Use
REMOVEFILTERS. - Do you need to clear filters on a table but keep one or more columns? Use
ALLEXCEPT. - Do you need a table of all rows or all distinct values to iterate or count? Use
ALL.
What each function returns
The functions differ in what they return, and that decides where you can use them.
| Function | Returns a table? | Typical role |
|---|---|---|
| `FILTER` | Yes, the rows that pass the condition | Add a complex condition as a filter |
| `ALL` | Yes, all rows or all distinct values | Table to count or iterate, or clear filters |
| `ALLEXCEPT` | Yes, but used as a `CALCULATE` modifier | Clear all filters except chosen columns |
| `REMOVEFILTERS` | No | Clear filters as a `CALCULATE` modifier |
REMOVEFILTERS can appear only as a CALCULATE filter argument. Because it returns no table, it cannot go inside COUNTROWS. ALL can do both jobs.
Simple condition or FILTER
A boolean filter on one column is short and fast. It replaces any existing filter on that column.
Red Sales =
CALCULATE ( [Total Sales], 'Product'[Color] = "Red" )When the condition needs several columns at once, a boolean argument cannot express it. FILTER iterates the table row by row, so each row can be tested with a combined expression.
Large Line Sales =
CALCULATE (
[Total Sales],
FILTER ( Sales, Sales[Quantity] * Sales[Net Price] > 1000 )
)Clearing filters: REMOVEFILTERS or ALL
Inside CALCULATE, these two give the same result.
All Sales Using ALL =
CALCULATE ( [Total Sales], ALL ( 'Product' ) )All Product Sales =
CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product' ) )Prefer REMOVEFILTERS when you only want to clear filters. The name states the intent, so the next reader does not have to wonder whether the table is also used as a value. Use ALL when you need the table itself.
Category Count = COUNTROWS ( ALL ( 'Product'[Category] ) )Keeping some filters: ALLEXCEPT
ALLEXCEPT clears every filter on a table except the columns you name. This suits a "percent of group" measure.
Sales % of Category =
DIVIDE (
[Total Sales],
CALCULATE ( [Total Sales], ALLEXCEPT ( 'Product', 'Product'[Category] ) )
)The denominator keeps the Category filter and drops filters on other Product columns, such as Color or Brand. For a percent of grand total, use REMOVEFILTERS ( 'Product' ) in the denominator instead.
Common choice mistakes
- Using
REMOVEFILTERSwhere a table is expected, such as insideCOUNTROWS. UseALLthere. - Filtering a whole table with
FILTER ( Sales, ... )when a boolean condition on one column would do. The table version iterates more rows and is slower. - Using
ALLon a table when you meant to clear one column. Name the column, as inALL ( 'Product'[Color] ), so other filters stay. - Using
ALLEXCEPTwhen you list almost every column. Clearing the one or two columns directly is clearer.
Exercise
A matrix shows Category in rows and Brand in columns. You want each cell to show its share of the sales for its Category, across all brands. Which function do you use in the denominator, and why not REMOVEFILTERS ( 'Product' )?
Show answer
Use CALCULATE ( [Total Sales], ALLEXCEPT ( 'Product', 'Product'[Category] ) ). It clears the Brand filter but keeps the Category filter, so the denominator is the Category total. REMOVEFILTERS ( 'Product' ) would also clear Category and return the grand total, giving a percent of all products instead.
Exercise
You need a measure that counts the distinct categories in the model, ignoring slicers. Can you write COUNTROWS ( REMOVEFILTERS ( 'Product'[Category] ) )?
Show answer
No. REMOVEFILTERS does not return a table, so COUNTROWS cannot use it. Write COUNTROWS ( ALL ( 'Product'[Category] ) ) instead.