Sub topic 8
Table filters and FILTER
This chapter assumes you know that CALCULATE evaluates an expression in a modified filter context, and that a Boolean argument such as 'Product'[Color] = "Red" replaces any existing filter on that column. Here you will see the second kind of filter argument, a table, and learn when you need it.
What a table filter argument is
CALCULATE accepts any expression that returns a table as a filter argument. The rows of that table become the filter. For each column in the returned table, CALCULATE keeps only the values that appear in those rows.
FILTER is the usual way to build such a table. It takes a table and a condition, then returns the rows for which the condition is true.
High Quantity Sales =
CALCULATE (
[Total Sales],
FILTER ( Sales, Sales[Quantity] > 2 )
)FILTER is an iterator. It creates a row context on each row of Sales and tests Sales[Quantity] > 2. The rows that pass form the table that CALCULATE applies as a filter.
Where the table argument is evaluated
CALCULATE evaluates its filter arguments in the original filter context, before it changes anything. So FILTER ( Sales, ... ) iterates only the Sales rows that are visible in the current cell of the report.
This explains the behavior in the example above. If a slicer limits Sales to one year, FILTER sees only that year's rows. The result is the current selection plus the quantity condition, which is an intersection.
Boolean filter versus FILTER over a table
The two forms look similar but treat existing filters differently.
| Form | Table CALCULATE receives | Existing filter on `Sales[Quantity]` |
|---|---|---|
| `Sales[Quantity] > 2` | `FILTER ( ALL ( Sales[Quantity] ), Sales[Quantity] > 2 )` | Replaced |
| `FILTER ( Sales, Sales[Quantity] > 2 )` | Visible `Sales` rows that pass the test | Kept, then narrowed |
A Boolean argument is shorthand for FILTER over ALL of one column. It ignores the current filter on that column. FILTER over the whole Sales table starts from the rows that survive the current filters.
Suppose a slicer selects Quantity values 1 and 2. The Boolean version returns the sales for all quantities above 2, because it replaced the slicer's filter. The FILTER over Sales version returns blank, because no visible row has a quantity above 2.
When you need FILTER
A Boolean argument can only compare one column with a value. Use FILTER when the condition:
- compares two columns, such as
Sales[Quantity] * Sales[Price] > 100, - depends on a measure,
- must respect the current filters on the table being tested.
Here is a condition that depends on a measure. It sums sales only for categories whose own total exceeds one million.
Sales in Large Categories =
CALCULATE (
[Total Sales],
FILTER (
VALUES ( 'Product'[Category] ),
[Total Sales] > 1000000
)
)VALUES returns the categories visible in the current context. FILTER creates a row context on each category. Referencing [Total Sales] inside it triggers a context transition, so the measure is evaluated for that one category. The next chapters cover that mechanism in detail.
Keep the iterated table small
FILTER over a whole fact table visits every visible row. On a large Sales table this is slower than filtering a single column. Iterate the smallest table that contains the columns you need. For a condition on one column, FILTER ( VALUES ( 'Product'[Category] ), ... ) iterates only the distinct categories, not every sale.
Common mistakes
- Expecting
FILTER ( Sales, ... )to ignore a slicer onSalescolumns. It does not, because it starts from the visible rows. - Using FILTER over
ALL ( Sales )without needing to. This removes all filters onSalescolumns and iterates the entire table. - Writing FILTER where a Boolean argument would give the same result with less work.
Exercise
A slicer selects Product[Color] = "Blue". The measure below is placed in a card. What does it return, and how would the result change if the argument were the Boolean 'Product'[Color] = "Red"? Red Sales via FILTER = CALCULATE ( [Total Sales], FILTER ( 'Product', 'Product'[Color] = "Red" ) )
Show answer
It returns blank. FILTER iterates the Product rows visible in the original context, which are only the blue products. No row passes the red test, so the filter table is empty. With the Boolean argument 'Product'[Color] = "Red", CALCULATE replaces the slicer's filter on Color and returns the sales for red products.