Sub topic 5
Boolean filters replace filters on the same column
This chapter assumes you know basic DAX measures, a star-schema model with one-to-many relationships, and how slicers and report rows and columns filter a measure. It also assumes you have met filter context and the basic shape of CALCULATE.
The rule
A Boolean filter argument in CALCULATE looks like a simple condition on one column, such as 'Product'[Color] = "Red". DAX does not apply it as an extra condition on top of the existing filters. It replaces any existing filter on that same column, and filters on other columns stay as they are.
Start with the base measure:
Total Sales = SUM ( Sales[Amount] )Now add a measure that forces the color to red:
Red Sales =
CALCULATE (
[Total Sales],
'Product'[Color] = "Red"
)Suppose a slicer selects Blue. Without CALCULATE, [Total Sales] returns blue sales. Red Sales still returns red sales, because the Boolean argument overwrites the slicer's filter on 'Product'[Color]. The result is not blank, even though no row is both blue and red.
What DAX does internally
A Boolean filter is shorthand. DAX rewrites the argument as a table filter that returns the distinct values of the column that satisfy the condition. The Red Sales filter becomes roughly this:
FILTER ( ALL ( 'Product'[Color] ), 'Product'[Color] = "Red" )ALL on the column drops the existing filter on that column first. FILTER then keeps the red value. That is why the old selection disappears.
The rewrite only touches the one column named in the condition. A slicer on 'Product'[Category] or a row for a given year keeps filtering as before.
Mermaid diagrams must use the diagram fence, so here is the flow in that form.
Different columns combine
If the Boolean filter names a column that nothing else filters, it adds a condition. If it names a column that is already filtered, it replaces that filter. Two Boolean arguments on different columns are combined with AND:
Red Bike Sales =
CALCULATE (
[Total Sales],
'Product'[Color] = "Red",
'Product'[Category] = "Bikes"
)Each argument replaces the filter on its own column. The final context requires color Red and category Bikes, whatever the slicers say about those two columns.
| Existing filter | Boolean argument | Result in the new context |
|---|---|---|
| Color = Blue | Color = "Red" | Color = Red (replaced) |
| Category = Bikes | Color = "Red" | Category = Bikes and Color = Red |
| No filter on Color | Color = "Red" | Color = Red (added) |
Rules for Boolean arguments
A Boolean filter must refer to a single column from a single table. It can use constants or expressions that do not depend on the current row. It cannot compare two columns, and it cannot call a measure.
This works:
Large Orders =
CALCULATE (
[Total Sales],
Sales[Quantity] > 2
)This does not work as a Boolean argument:
-- Invalid: compares two columns, and refers to a measure
CALCULATE (
[Total Sales],
Sales[Quantity] > Sales[MinQuantity]
)For conditions like that, use a table filter with FILTER, which a later chapter covers.
Keeping the existing selection
Sometimes replacement is not what you want. You may want red sales only when red is part of the user's selection. Wrapping the filter in KEEPFILTERS changes replacement into intersection. A later chapter explains it. For now, remember that the default behavior of a Boolean argument is to overwrite.
Checking your prediction
To predict a CALCULATE result, follow these steps:
- List the filters in the current context, column by column.
- For each Boolean argument, find the column it names.
- Remove the existing filter on that column and apply the new one.
- Leave every other column's filter in place.
- Evaluate the expression in the resulting context.
Exercise
A matrix shows Category on rows, and a slicer selects Color = Green. The measure is CALCULATE ( [Total Sales], 'Product'[Color] = "Red" ). For the row Category = Accessories, which filters apply to Sales when the measure is evaluated, and what happens to the Green selection?
Show answer
Two filters apply: Category = Accessories (from the row, kept because the Boolean argument names a different column) and Color = Red (from the argument). The Green selection from the slicer is replaced, because the Boolean filter overwrites any existing filter on 'Product'[Color]. The cell shows red accessories sales.
Exercise
Why is CALCULATE ( [Total Sales], Sales[Quantity] > Sales[MinQuantity] ) not allowed, and what would you use instead?
Show answer
A Boolean filter argument must refer to one column and cannot compare two columns of the row. Use a table filter instead, for example CALCULATE ( [Total Sales], FILTER ( Sales, Sales[Quantity] > Sales[MinQuantity] ) ).