Sub topic 12
Common pitfalls and how to debug them
This chapter assumes you know basic DAX measures and calculated columns, a star schema with one-to-many relationships, and how slicers, rows and columns filter a measure. It also assumes you have read the earlier chapters on filter context, row context, CALCULATE arguments, KEEPFILTERS, context transition and evaluation order. Here we collect the mistakes that appear most often and a repeatable way to find them.
Why `CALCULATE` mistakes are hard to see
A wrong CALCULATE measure usually does not raise an error. It returns a number that looks plausible. The cause is nearly always one of three things: a filter was replaced when you expected it to be kept, a context transition happened when you did not expect it, or a value was fixed before CALCULATE could change it.
The base measure used throughout is:
Total Sales = SUM ( Sales[Amount] )Its result depends entirely on the filter context where it is evaluated. Every pitfall below is a case where the filter context is not what you assumed.
Pitfall 1: Expecting a variable to respond to a later `CALCULATE`
A variable is evaluated once, at the point where it is defined, in the filter context that exists at that point. After that it holds a fixed value. CALCULATE changes the filter context, but it cannot reach back and recompute a variable.
Wrong Red Sales =
VAR t = [Total Sales]
RETURN
CALCULATE ( t, 'Product'[Color] = "Red" )The variable t is computed in the original filter context. CALCULATE then evaluates t, which is just a stored number, so the color filter has no effect. The measure returns the same result as [Total Sales].
The fix is to put the expression that must be re-evaluated inside CALCULATE, and use variables only for values you want fixed:
Red Sales =
CALCULATE (
[Total Sales],
'Product'[Color] = "Red"
)Variables are still useful when you want a value frozen on purpose. For example, you can capture the current selection before changing the context, then use it inside the new context.
Pitfall 2: Forgetting that a Boolean filter replaces the existing filter
A Boolean argument such as 'Product'[Color] = "Red" replaces any existing filter on 'Product'[Color]. It does not combine with it. If a slicer selects Blue, Red Sales still returns red sales, because the slicer's filter on color is removed and replaced with Red.
This is often what you want. It becomes a bug when the author believed the measure would respect the slicer.
To intersect instead of replace, wrap the filter in KEEPFILTERS:
Red Sales Within Selection =
CALCULATE (
[Total Sales],
KEEPFILTERS ( 'Product'[Color] = "Red" )
)Now the red filter is intersected with the current selection on color. If Red is not selected, the result is blank.
| Slicer selects | `Red Sales` | `Red Sales Within Selection` |
|---|---|---|
| Nothing | Red sales | Red sales |
| Red | Red sales | Red sales |
| Blue | Red sales | Blank |
| Red and Blue | Red sales | Red sales |
Pitfall 3: Removing too much or too little
REMOVEFILTERS, ALL and ALLEXCEPT clear filters, and the scope of what they clear is a frequent source of wrong percentages.
Sales Share of Category =
DIVIDE (
[Total Sales],
CALCULATE ( [Total Sales], REMOVEFILTERS ( 'Product'[Category] ) )
)The denominator clears only the filter on 'Product'[Category]. The numerator keeps the current selection. The result is a share of all categories.
Common mistakes with this pattern:
- Clearing a whole table, such as
REMOVEFILTERS ( 'Product' ), when only one column should be cleared. The share then ignores color, brand and every other product filter as well. - Clearing a column that the visual does not use. Nothing changes, and the measure looks correct until the visual changes.
- Using
ALLas a filter argument and as a table function interchangeably. InsideCALCULATEit removes filters. Inside an iterator orFILTERit returns a table of values. PreferREMOVEFILTERSwhen your intent is only to remove filters, because the name states the intent. - Forgetting that removing a filter on a dimension column also affects what the relationship passes to the fact table. The fact table sees the result.
Pitfall 4: Putting a measure inside an iterator without noticing context transition
When a measure is referenced inside a row context, DAX wraps it in an implicit CALCULATE. That converts the current row into a filter. This is context transition.
Average Sales per Customer =
AVERAGEX ( Customer, [Total Sales] )For each row of Customer, [Total Sales] is evaluated with a filter on that customer. That is the intended behavior here. The problems appear in two other situations.
First, a measure you did not expect to be transitioned. A column reference in a row context does not transition. A measure reference does. If you replace [Total Sales] with SUM ( Sales[Amount] ) inside the iterator, the transition does not happen, and every row returns the same overall total.
Second, a transition on a table with duplicate rows. Context transition turns all columns of the current row into filters. If the iterated table has rows that are identical across every column, each of those rows filters to all of the identical rows, and the measure counts them together. Iterating over a table that has a unique key avoids this.
Pitfall 5: Confusing a row context with a filter
A row context identifies a current row. It does not filter other tables. In a calculated column on Sales, Sales[Amount] refers to the amount in the current row, but SUM ( Sales[Amount] ) still sums the whole column in the current filter context, which in a calculated column is usually everything.
Relationships also do not propagate a row context the way they propagate filters. From a row context on Sales, RELATED reads a value from the one side. From a row context on a dimension, RELATEDTABLE returns the matching rows of the many side. Without those functions, or without a context transition, the other table is not affected.
If a calculated column returns the same value in every row, a missing context transition is the usual cause. Wrapping the aggregation in CALCULATE turns the current row into a filter:
Customer Total Sales =
CALCULATE ( SUM ( Sales[Amount] ) )Placed as a calculated column on Customer, this returns each customer's own total, because the row context on Customer becomes a filter that flows to Sales through the relationship.
Pitfall 6: Table filters that are heavier than needed
A table argument built with FILTER keeps the current filters on the table you pass and adds a row condition:
High Quantity Sales =
CALCULATE (
[Total Sales],
FILTER ( Sales, Sales[Quantity] > 2 )
)This works, but it iterates the whole Sales table, and the filter acts on all columns of the rows that pass. A Boolean argument on one column is usually cheaper and clearer:
High Quantity Sales Simple =
CALCULATE (
[Total Sales],
Sales[Quantity] > 2
)Use FILTER when the condition needs a measure, compares two columns, or involves another row-level calculation. Otherwise a Boolean argument is simpler. Note the difference in behavior: the Boolean version replaces existing filters on Sales[Quantity], while the FILTER ( Sales, ... ) version keeps them and intersects. They are not always equivalent when a filter on quantity already exists.
Pitfall 7: Inactive relationships that never activate
If a model has an inactive relationship, such as one between Sales[ShipDate] and 'Date'[Date], no measure uses it by default. A measure that should report by ship date will silently report by order date.
Sales by Ship Date =
CALCULATE (
[Total Sales],
USERELATIONSHIP ( Sales[ShipDate], 'Date'[Date] )
)USERELATIONSHIP activates that relationship only for the duration of this CALCULATE. Two things to remember. First, it applies to the expression evaluated inside this CALCULATE, so a measure called from within also sees it. Second, it does not remove the active relationship from other measures. Each measure chooses its own relationship.
A debugging routine
Use the same steps each time a number looks wrong.
- Write down the filter context you expect at the cell you are checking. List the slicer selections, row and column values, and any page or visual filters.
- Evaluate the base measure alone in that cell. If
[Total Sales]is already wrong, the problem is in the model or the relationships, not inCALCULATE. - Add one
CALCULATEargument at a time. After each, check whether the result changes the way you predict. - For each argument, ask whether it replaces, removes, keeps or adds a filter, and on which column.
- Look for hidden context transitions: measure references inside iterators and
CALCULATEinside calculated columns. - Look for variables defined before the
CALCULATEthat hold values you wanted to change.
Helper measures make the context visible. Measures such as these show what the cell actually sees:
Debug Selected Colors =
CONCATENATEX (
VALUES ( 'Product'[Color] ),
'Product'[Color],
", "
)
Debug Color Filtered =
IF ( ISFILTERED ( 'Product'[Color] ), "Filtered", "Not filtered" )
Debug Row Count =
COUNTROWS ( Sales )Place them in the same visual as the problem measure. VALUES returns the colors visible in the current context, ISFILTERED tells you whether a direct filter exists on the column, and COUNTROWS shows how many fact rows survive. Remove them when you are finished.
The diagram summarizes the checks in order.
Summary of the checks
| Symptom | Likely cause | Fix |
|---|---|---|
| Measure ignores a slicer on the filtered column | Boolean argument replaced the filter | Use `KEEPFILTERS` |
| Measure with a variable ignores `CALCULATE` filters | Variable evaluated before `CALCULATE` | Move the expression inside `CALCULATE` |
| Share is wrong in the denominator | Wrong scope of `REMOVEFILTERS` | Clear only the needed column |
| Iterator returns the same value on every row | No context transition | Reference a measure, or wrap in `CALCULATE` |
| Calculated column repeats one value | Missing context transition | Wrap the aggregation in `CALCULATE` |
| Date measure uses the wrong date | Inactive relationship | Use `USERELATIONSHIP` |
Exercise
A measure is defined as VAR t = [Total Sales] RETURN CALCULATE ( t, 'Product'[Color] = "Red" ). A report page has a slicer that selects Blue. What does the measure return, and how do you change it to return red sales?
Show answer
It returns the total sales for Blue, the same as [Total Sales]. The variable t is computed once in the original filter context, so CALCULATE cannot change it. Rewrite it as CALCULATE ( [Total Sales], 'Product'[Color] = "Red" ), which re-evaluates the measure after the color filter replaces the slicer's filter.
Exercise
You write Red Sales Within Selection using KEEPFILTERS ( 'Product'[Color] = "Red" ). The slicer selects only Blue. What is returned, and why?
Show answer
Blank. KEEPFILTERS intersects the red filter with the existing Blue selection on color. No color is both Red and Blue, so no rows remain.
Exercise
AVERAGEX ( Sales, SUM ( Sales[Amount] ) ) returns the same value for every row of the iterated table. Why does this happen, and what is one way to change it?
Show answer
SUM ( Sales[Amount] ) is not a measure reference, so no context transition happens. Each row evaluates the sum in the unchanged filter context. Wrap the sum in CALCULATE, or reference a measure such as [Total Sales], so the current row becomes a filter. For an average of row amounts, AVERAGEX ( Sales, Sales[Amount] ) is simpler.