Sub topic 3
CALCULATE: changing filter context with filter arguments
This chapter assumes you can write basic measures with SUM and COUNTROWS. It also assumes you know how tables, columns, and relationships fit together in a data model, and how slicers filter a report visual.
What CALCULATE does
CALCULATE evaluates an expression in a filter context that you modify. The syntax is:
CALCULATE ( <expression>, <filter1>, <filter2>, ... )The expression is usually a measure. Each filter argument adds, replaces, or removes filters before the expression runs. The filters from the visual (rows, columns, slicers) are the starting point. CALCULATE then adjusts them.
Start with a base measure:
Total Sales = SUM ( Sales[Amount] )Placed in a matrix with product categories on the rows, this measure returns a different value for each row. Each row's category filters the Product table, and that filter flows to Sales through the relationship.
Boolean filter arguments
The simplest filter argument is a boolean expression that tests one column:
Red Sales =
CALCULATE ( [Total Sales], 'Product'[Color] = "Red" )This measure returns sales for red products. If a slicer selects Blue, Red Sales still shows red sales. The reason is that a boolean filter argument replaces any existing filter on that column, Product[Color]. Filters on other columns stay in place. A Category row filter still applies, so each row shows red sales within that category.
Behind the scenes, DAX rewrites the boolean as a table of the allowed values. The rewrite is roughly this:
Red Sales Expanded =
CALCULATE (
[Total Sales],
FILTER ( ALL ( 'Product'[Color] ), 'Product'[Color] = "Red" )
)You do not need to write it this way. It explains why the new filter replaces the old one: the table passed in starts from ALL of the column.
Rules for boolean filter arguments
A boolean filter argument has limits:
- It can refer to only one column.
- It cannot call a function that scans a table.
- It cannot use a measure.
The condition Sales[Quantity] * Sales[Net Price] > 1000 refers to two columns, so it fails as a boolean argument. Chapter 4 covers FILTER, which handles this case.
Table filter arguments
A filter argument can also be an expression that returns a table. CALCULATE uses the rows of that table as filters. Functions such as FILTER, ALL, and VALUES return tables. Later chapters show each of them in detail.
Here is how CALCULATE handles multiple filter arguments:
- It evaluates every filter argument in the original filter context, before any of them changes it.
- It combines the resulting filters with AND logic. Two arguments on different columns both apply. Two arguments on the same column are intersected.
- It evaluates the expression in the new filter context.
Red Bikes Sales =
CALCULATE (
[Total Sales],
'Product'[Color] = "Red",
'Product'[Category] = "Bikes"
)This measure returns sales for products that are both red and in the Bikes category.
The diagram shows the order of steps.
Common errors
- Expecting a boolean filter to add to an existing filter on the same column. It replaces that filter. With a Color slicer set to Blue,
Red Salesstill returns red sales. To combine the two, useKEEPFILTERS, which a later chapter introduces. - Putting a measure inside a boolean filter. A condition like
[Total Sales] > 1000is not allowed as a boolean argument. UseFILTERinstead. - Forgetting that other columns keep their filters. Replacing the filter on
Colordoes not clearCategoryorYear.
Exercise
A matrix shows product categories on rows, and a slicer selects the color Blue. The measure Red Sales = CALCULATE ( [Total Sales], 'Product'[Color] = "Red" ) is added to the matrix. What does each row show, and why?
Show answer
Each row shows the sales of red products in that row's category. The boolean argument replaces the existing filter on Product[Color], so the Blue slicer is overridden. The category filter from the row is on a different column, so it stays.
Exercise
Why can CALCULATE ( [Total Sales], Sales[Quantity] * Sales[Net Price] > 1000 ) not be used as written?
Show answer
A boolean filter argument can refer to only one column, and this condition uses two. A table filter built with FILTER over the Sales table is needed instead.