Sub topic 12
Common pitfalls and how to debug them
This chapter assumes you know how filter context, row context, and CALCULATE filter arguments work, as covered in the earlier chapters. It collects the mistakes that cause most wrong CALCULATE results. It also gives a repeatable way to find them.
Pitfall 1: expecting a variable to react to CALCULATE
A variable is evaluated once, where it is declared, in the filter context that exists at that point. A later CALCULATE cannot change its value.
Wrong Red Sales =
VAR t = [Total Sales]
RETURN
CALCULATE ( t, 'Product'[Color] = "Red" )
Red Sales =
CALCULATE ( [Total Sales], 'Product'[Color] = "Red" )Wrong Red Sales returns the same number as [Total Sales]. The red filter is applied, but t is already a constant. Put the expression you want to modify inside CALCULATE, not in a variable computed before it.
Variables are still useful for values that must stay fixed. For example, store a grand total in a variable before changing filters, then use it later.
Pitfall 2: a Boolean filter overrides the slicer
A Boolean argument such as 'Product'[Color] = "Red" replaces any existing filter on that column. If a user selects Blue in a slicer, Red Sales still shows red sales. Often that is not the intent.
If you want the intersection of the slicer and red, wrap the filter in KEEPFILTERS. With Blue selected, the result is then blank.
Pitfall 3: removing a filter in the wrong place
REMOVEFILTERS only affects the CALCULATE that contains it. In a ratio, put it only in the part that should ignore the filter, usually the denominator. If you put it around the whole expression, the numerator also ignores the selection and every row shows 100%.
A related mistake is using ALL on a whole table when you meant one column. ALL ( 'Product' ) clears filters on every Product column, including a Color slicer you wanted to keep. Use REMOVEFILTERS ( 'Product'[Category] ) or ALLEXCEPT when you want to limit the scope.
Pitfall 4: unintended context transition
Referencing a measure inside an iterator wraps it in an implicit CALCULATE. Each row then becomes a filter. This is what makes AVERAGEX ( Customer, [Total Sales] ) work, but it causes trouble in two cases:
- Duplicate rows. Context transition filters on every column of the row. If a table has two identical rows, each row's filter matches both, so values are counted twice.
- Cost. The transition runs once per row. Iterating a large fact table with a measure inside is slow. Iterate a small table, such as a dimension, when you can.
Pitfall 5: FILTER on the wrong table
FILTER ( Sales, Sales[Quantity] > 2 ) iterates only the Sales rows that survive the current filters. It adds a condition and cannot widen the selection. To ignore a slicer and filter at the same time, use FILTER ( ALL ( ... ), ... ) or a Boolean argument on the column. Also prefer a Boolean filter when it is enough, because it is simpler and usually faster.
A debugging routine
When a measure returns a surprising value, work through these steps.
To inspect the context at a cell, write a temporary measure:
Debug Color Filter =
"Filtered: " & ISFILTERED ( 'Product'[Color] )
& " | Values: " & COUNTROWS ( VALUES ( 'Product'[Color] ) )Put it in a table visual next to the broken measure. It shows whether a filter reaches the column and how many values pass it.
To compare measures across a grouping, run a query in DAX query view. DEFINE MEASURE lets you test a change without editing the model.
DEFINE
MEASURE Sales[Red Sales] =
CALCULATE ( [Total Sales], 'Product'[Color] = "Red" )
EVALUATE
SUMMARIZECOLUMNS (
'Product'[Color],
"Total", [Total Sales],
"Red", [Red Sales]
)Each row of the result shows how the measure behaves under a single color filter.
Quick reference
| Symptom | Likely cause | Fix |
|---|---|---|
| CALCULATE has no effect on a variable | Variable evaluated before CALCULATE | Move the expression inside CALCULATE |
| Slicer is ignored | Boolean filter replaced it | Use KEEPFILTERS |
| Every share is 100% | Filter removed from the numerator too | Remove it in the denominator only |
| Totals double count | Duplicate rows in context transition | Iterate a table with a unique key |
Exercise
A slicer selects Blue on 'Product'[Color]. The measure below is shown in a card. What does it return, and how would you change it so it returns the red sales only when Red is also selected? Wrong Red Sales = VAR t = [Total Sales] RETURN CALCULATE ( t, 'Product'[Color] = "Red" )
Show answer
It returns the sales for Blue, which is the same as [Total Sales]. The variable t is computed once in the slicer's filter context, so CALCULATE cannot change it. To get the intersection, write CALCULATE ( [Total Sales], KEEPFILTERS ( 'Product'[Color] = "Red" ) ). With Blue selected this returns blank.