Power Pivot & DAX course · Lesson 7 of 16
CALCULATE: changing the filter context in DAX
Learn DAX CALCULATE with worked PivotTable results: adding and replacing filters, KEEPFILTERS, AND/OR conditions, and FILTER for conditions on measures.
CALCULATE is the most important function in DAX. It evaluates an expression in a modified filter context: you keep the filters coming from the PivotTable and add, replace or remove some of them. Shares of total, comparisons with a fixed category, year-to-date, last year: all of them are CALCULATE underneath. This lesson builds it up one step at a time, using the Revenue measure from Your first DAX measures: SUM, AVERAGE, COUNTROWS, DISTINCTCOUNT and the contexts from Filter context and row context in DAX.
The syntax
CALCULATE ( <expression>, <filter1>, <filter2>, ... )- expression: what to calculate, typically a measure such as
[Revenue]or an aggregation such asSUM ( Sales[Amount] ). - filters: zero or more filter arguments. Simple conditions on one column, such as
Products[Category] = "Computers", are called Boolean filters. You can also pass a table, for example the result ofFILTERorALL. - All filter arguments are combined with AND.
Adding a filter
Revenue Computers := CALCULATE ( [Revenue], Products[Category] = "Computers" )Put this next to Revenue in a PivotTable with Region on rows, and each row shows the computer revenue of that region: the region filter from the PivotTable stays, and the category filter is added. North shows 2,320 (Anna Weber's laptops and tablet).
Replacing a filter
Now put Category on rows instead. Something interesting happens:
Computers Only := CALCULATE ( [Revenue], KEEPFILTERS ( Products[Category] = "Computers" ) )| A | B | C | D | |
|---|---|---|---|---|
| 1 | Category | Revenue | Revenue Computers | Computers Only |
| 2 | Accessories | 745 | 4950 | |
| 3 | Computers | 4950 | 4950 | 4950 |
| 4 | Displays | 945 | 4950 | |
| 5 | Grand Total | 6640 | 4950 | 4950 |
Products[Category] on rows. Revenue Computers replaces the row filter; Computers Only intersects with it.
Revenue Computers shows 4,950 in every row, even Accessories. A Boolean filter on a column replaces any existing filter on that same column. In the Accessories row the PivotTable says Category = Accessories, CALCULATE overwrites it with Category = Computers, and the result is the computer revenue. Filters on other columns (Region, Segment, dates) are left alone.
KEEPFILTERS changes replace into intersect: the new condition is combined with the existing one, so the Accessories row has Accessories AND Computers, which is empty, and shows blank. Use the plain form when you want a fixed comparison figure in every row (for example to compute each category's ratio to Computers); use KEEPFILTERS when the condition should only narrow down what is already there.
Several conditions
North Computers := CALCULATE ( [Revenue], Customers[Region] = "North", Products[Category] = "Computers" )
North or West := CALCULATE ( [Revenue], Customers[Region] = "North" || Customers[Region] = "West" )Separate arguments mean AND: North Computers returns 2,320. To get OR on one column, put both conditions in the same argument with || (the DAX OR operator; && is AND). At the grand total, North or West gives 2,495 + 2,058 = 4,553. Newer versions of DAX also accept Customers[Region] IN { "North", "West" }, which is easier to read when the list grows.
Filtering on a numeric column
Boolean filters can use any comparison on a single column. Suppose orders of five units or more count as bulk:
Bulk Revenue := CALCULATE ( [Revenue], Sales[Qty] >= 5 )
Bulk % := DIVIDE ( [Bulk Revenue], [Revenue] )| A | B | C | D | |
|---|---|---|---|---|
| 1 | Region | Revenue | Bulk Revenue | Bulk % |
| 2 | East | 807 | 240 | 29.7% |
| 3 | North | 2495 | 175 | 7.0% |
| 4 | South | 1280 | 330 | 25.8% |
| 5 | West | 2058 | ||
| 6 | Grand Total | 6640 | 745 | 11.2% |
Customers[Region] on rows. Bulk % formatted as a percentage with 1 decimal.
Only four order lines have five or more units (1002, 1004, 1007 and 1010), all of them mice and keyboards, so bulk revenue is 745. West's only orders were 4 tablets and 2 monitors, so its Bulk Revenue is blank.
When a Boolean filter is not enough: FILTER
A Boolean filter cannot contain a measure. CALCULATE ( [Revenue], [Revenue] > 1000 ) is rejected. When the condition depends on a measure, pass a table built with FILTER, which iterates the rows of a table and keeps those where the condition is true:
Revenue Big Customers := CALCULATE ( [Revenue], FILTER ( VALUES ( Customers[CustomerID] ), [Revenue] > 1000 ) )VALUES ( Customers[CustomerID] ) lists the customers visible in the current cell. FILTER evaluates [Revenue] for each of them (context transition again) and keeps those above 1,000. Here that is 3 customers out of 4 (Carter & Co, with 807, falls out), so at the grand total the measure returns 5,833.
FILTER ( Sales, ... ) on a whole fact table when a Boolean filter on a column will do. CALCULATE ( [Revenue], FILTER ( Sales, Sales[Qty] >= 5 ) ) returns the same number as Bulk Revenue but forces the engine to scan every row with all columns, which is far slower on large tables. It also behaves differently: a whole-table filter keeps the existing filters on Sales instead of replacing the Qty filter.CALCULATE without filters
CALCULATE ( [Revenue] ) with no filter arguments looks pointless in a measure, but in a row context it performs context transition, as you saw with the Customer Revenue column. Remember this when you meet CALCULATE inside iterators.
Order of evaluation, simplified
- Take the filter context of the current cell (rows, columns, slicers).
- If there is a row context, convert it into filters (context transition).
- Evaluate each filter argument. Boolean filters replace filters on their column;
KEEPFILTERSintersects;ALLand its relatives remove (see Removing filters: ALL, ALLEXCEPT, ALLSELECTED and REMOVEFILTERS). - Evaluate the expression in the new filter context.
Revenue), a CALCULATE variation (Revenue Computers), then a ratio (DIVIDE ( [Revenue], [Revenue Computers] )). Each layer can be checked in the PivotTable before the next one is added.Check yourself
1. What does CALCULATE ( [Revenue], Products[Category] = "Computers" ) show in the Accessories row?
The Boolean filter replaces the existing Category filter.
2. Which function makes a Boolean filter intersect with the existing filter instead of replacing it?
KEEPFILTERS keeps the current filter and adds the new condition.
3. How do you express Region = North OR Region = West inside CALCULATE?
Separate arguments are AND; || within one argument gives OR.
4. Why is CALCULATE ( [Revenue], [Revenue] > 1000 ) rejected?
Conditions on measures need FILTER (or another table expression).
5. Which filter arguments are combined with AND?
Each extra argument narrows the context further.