Sub topic 8
Table filters and FILTER
What this chapter assumes
This chapter assumes you have read the earlier chapters on filter context, row context, Boolean filter arguments and context transition. You should also be comfortable with a star schema where Sales is the fact table on the many side of relationships to 'Product' and 'Date'.
The examples use this base measure:
Total Sales = SUM ( Sales[Amount] )Boolean arguments are shorthand for table filters
Every filter argument of CALCULATE ends up as a table of values that is applied to the filter context. A Boolean argument is a shorthand for a table built over a single column:
Red Sales =
CALCULATE (
[Total Sales],
'Product'[Color] = "Red"
)
Red Sales Expanded =
CALCULATE (
[Total Sales],
FILTER ( ALL ( 'Product'[Color] ), 'Product'[Color] = "Red" )
)The two measures return the same result. The ALL inside the expanded form is the reason a Boolean argument replaces an existing filter on the same column. The table starts from every color, so the old selection no longer matters.
You write the table form yourself when a Boolean expression cannot say what you need. A Boolean expression cannot call a measure. It also cannot decide which rows to test by looking at the current filters. Table arguments cover those cases.
How CALCULATE applies a table argument
CALCULATE handles a table argument in three steps:
- It evaluates the table expression in the original filter context, before any of its own modifications.
- The table's columns carry lineage, which means each column remembers which model column it came from. CALCULATE reads the distinct values of those columns in the returned rows.
- It replaces the existing filter on each of those model columns with those values, then evaluates the first argument.
Here is the spine example:
High Quantity Sales =
CALCULATE (
[Total Sales],
FILTER ( Sales, Sales[Quantity] > 2 )
)FILTER iterates the Sales table. It runs in the filter context that exists outside CALCULATE, so it only sees the Sales rows that are currently visible. Those rows are tested against the condition, and the survivors become the new filter on the Sales columns.
The existing filters on Sales therefore survive in effect. A row that an existing filter already hid never enters the table. The new filter replaces the old one on the Sales columns, but it can only contain rows that passed the old one.
Why this differs from a Boolean argument
Suppose a slicer selects Sales[Quantity] values 1, 2 and 3. The three ways to write "quantity above 2" give different answers:
| Argument | Effect on the slicer | Quantities included |
|---|---|---|
| `Sales[Quantity] > 2` | Replaces it | Every quantity above 2, including 4 and 5 |
| `FILTER ( Sales, Sales[Quantity] > 2 )` | Intersects with it | Only 3 |
| `KEEPFILTERS ( Sales[Quantity] > 2 )` | Intersects with it | Only 3 |
The second row is the behavior the spine describes. FILTER over the fact table keeps the current filters on Sales and adds the row condition.
Conditions on measures need a table argument
A Boolean argument cannot reference a measure. To keep only categories whose sales exceed a threshold, iterate the categories and test the measure on each one:
Sales in Large Categories =
CALCULATE (
[Total Sales],
FILTER ( ALL ( 'Product'[Category] ), [Total Sales] > 100000 )
)Inside FILTER, each category row sets up a row context. The measure reference is wrapped in an implicit CALCULATE, so context transition turns that row into a filter on 'Product'[Category]. Other filters, such as the selected year, still apply. The test therefore asks whether this category's sales in the current year exceed 100,000.
Because the table is built from ALL ( 'Product'[Category] ), the result replaces any selection on category. In a matrix with categories on rows, every row shows the same number, which is the total of all large categories. That is rarely what you want.
To respect the selection, iterate the visible values:
Sales in Selected Large Categories =
CALCULATE (
[Total Sales],
FILTER ( VALUES ( 'Product'[Category] ), [Total Sales] > 100000 )
)Now a row for "Bikes" shows Bikes sales if Bikes is large and blank if it is not. VALUES returns only the categories visible in the current context, so the new filter is a subset of the old one.
Other table expressions you can pass
FILTER is the most common table argument, but any expression that returns a table with lineage works:
| Expression | What it provides as a filter |
|---|---|
| `ALL ( 'Product'[Color] )` | Every color, which clears the filter on that column |
| `VALUES ( 'Product'[Color] )` | The currently visible colors, which leaves the selection unchanged |
| `FILTER ( table, condition )` | The rows of the table that pass the condition |
| `TREATAS ( { "Red", "Blue" }, 'Product'[Color] )` | A fixed list of values, applied to the named column |
| `CALCULATETABLE ( table, filters )` | A table computed in a modified context |
A column created inside the expression, for example with ADDCOLUMNS, has no lineage to a model column and cannot filter anything. Only columns that trace back to the model take part.
Filters on the fact table do not travel upstream
Relationships propagate filters from the one side to the many side. A filter on Sales columns, such as the one from High Quantity Sales, restricts Sales rows. It does not hide products from the 'Product' table, because a single-direction relationship does not carry filters from Sales back to 'Product'.
The opposite direction works as you would expect. A filter on 'Product' columns propagates to Sales:
Expensive Product Sales =
CALCULATE (
[Total Sales],
FILTER ( 'Product', 'Product'[ListPrice] > 1000 )
)FILTER iterates the visible products and returns those above the price. Their values filter 'Product', and the relationship passes that filter down to Sales. As with Sales, the iterated table already respects the outer context, so a product slicer still applies.
Cost and when to avoid FILTER
A table argument that iterates a whole fact table can be costly. FILTER ( Sales, ... ) tests every visible row of a table that may have millions of them. The filter it produces can also involve many columns.
- Use a Boolean argument when the condition is a simple comparison on one column and replacing the existing filter is what you want.
- Use
KEEPFILTERSwith a Boolean argument when you want to intersect with the existing selection. It is usually cheaper thanFILTER ( Sales, ... ). - Use
FILTERover a single-column table, such asALL ( Sales[Quantity] ), when the condition needs a measure or logic that a Boolean argument cannot express. - Avoid
FILTER ( ALL ( Sales ), ... )just to test one column. It clears the filters on everySalescolumn, not only the one you care about.
Debugging table arguments
When a measure with a table argument returns something unexpected, work through these checks:
- Name the table expression's starting point. Does it begin from
ALL,VALUESor a base table in the outer context? - List which model columns the table carries. Those are the columns whose filters get replaced.
- Check whether the condition uses a measure. If so, find where context transition happens and which filters it leaves in place.
- Test the table on its own. Wrap the same expression in
COUNTROWSin a separate measure, or evaluate it in DAX query view, to see what CALCULATE receives.
Visible Large Categories =
COUNTROWS (
FILTER ( VALUES ( 'Product'[Category] ), [Total Sales] > 100000 )
)If this count differs from your expectation, the problem is in the table, not in CALCULATE.
Exercise
A slicer selects Category = "Bikes". Measure A is CALCULATE ( [Total Sales], FILTER ( ALL ( 'Product'[Category] ), [Total Sales] > 100000 ) ). Measure B uses VALUES ( 'Product'[Category] ) in place of ALL ( 'Product'[Category] ). Bikes has sales of 250,000 and Clothing has sales of 150,000 in the current context. What do A and B return?
Show answer
A returns 400,000. The ALL version builds its table from every category, both Bikes and Clothing pass the threshold, and the new filter replaces the Bikes selection. B returns 250,000. VALUES starts from the selected category (Bikes), which passes the threshold, so the filter stays on Bikes.
Exercise
A slicer selects Sales[Quantity] values 1 and 2. What does CALCULATE ( [Total Sales], FILTER ( Sales, Sales[Quantity] > 2 ) ) return, and why does it differ from CALCULATE ( [Total Sales], Sales[Quantity] > 2 )?
Show answer
The FILTER version returns blank. FILTER iterates only the visible Sales rows, which have Quantity 1 or 2, and none pass the condition, so the new filter is an empty table. The Boolean version replaces the slicer filter on Sales[Quantity] with all values above 2, so it returns the sales for those quantities.
Exercise
Why is FILTER ( ALL ( Sales ), Sales[Quantity] > 2 ) a poor choice for a simple quantity condition?
Show answer
It clears the filters on every column of Sales, not only Quantity, and it iterates the whole table. A Boolean argument, or KEEPFILTERS with a Boolean argument if you want to keep the slicer, expresses the same condition more cheaply and without ignoring other Sales column filters.