Sub topic 11
Pitfalls: duplicate rows, missing keys, and slow iterations
This chapter assumes you know row context, filter context, CALCULATE, and how a measure reference acts as an implicit CALCULATE. It covers three situations where context transition returns wrong numbers or runs slowly.
Why transition is risky
Context transition turns the current row into filters on every column of that row's table. It does not filter by a key. Two consequences follow:
- If two rows have identical values in every column, the filter selects both.
- If a row has no matching row on the other side of a relationship, the filter may reach nothing, or reach a row you did not intend.
Transition also runs once per iterated row. On a large table, that cost adds up.
Duplicate rows
Suppose Sales has no unique column, and two rows are identical (same product, date, quantity, and price). Add this calculated column:
-- Calculated column in the Sales table
Line Total Wrong = [Sales Amount]Each of the two identical rows turns into a filter that matches both rows. Both rows therefore show 20 instead of 10. A visual that sums Line Total Wrong reports 10 more than the measure [Sales Amount].
The measure is correct. The column is wrong because row identity was lost.
There are two fixes. The first is to avoid transition and read the row directly:
-- Calculated column in the Sales table
Line Total = Sales[Quantity] * Sales[Unit Price]The second is to add a unique key, such as an order line ID, to the table. Transition filters on all columns, including the key, so only one row survives.
-- Calculated column in the Sales table
Line Total Keyed = [Sales Amount]This works only if the key column really is unique. Check it with COUNTROWS ( Sales ) against DISTINCTCOUNT ( Sales[Line ID] ).
Key Is Unique =
COUNTROWS ( Sales ) = DISTINCTCOUNT ( Sales[Line ID] )Missing keys
Suppose a Sales row has ProductKey = 99, and Product has no such key. The relationship still works, but the row attaches to a virtual blank row in Product. Two effects follow:
RELATED ( Product[Category] )returns BLANK for that sales row.- A visual grouped by
Product[Category]shows an unnamed blank group holding those sales.
Iterating Product directly does not visit the virtual blank row:
Sales Via Products =
SUMX ( Product, [Sales Amount] )This measure skips sales whose key has no product. At the grand total it can be lower than [Sales Amount], with no error. If two totals disagree, check for unmatched keys first.
This measure counts the sales rows that have no matching product:
Unmatched Sales Rows =
COUNTROWS (
FILTER ( Sales, ISBLANK ( RELATED ( Product[Product Key] ) ) )
)Fix the data, or add the missing keys to Product, rather than working around it in DAX.
Slow iterations
Each measure reference inside an iterator costs a transition per row. Iterating a table with millions of rows can be expensive. Here [Unit Cost] is a measure:
Cost Slow =
SUMX ( Sales, Sales[Quantity] * [Unit Cost] )If the cost lives in a column, you can read it through the relationship without any transition:
Cost Faster =
SUMX ( Sales, Sales[Quantity] * RELATED ( Product[Unit Cost] ) )Nested iterators multiply the cost. An iterator over Customer that contains an iterator over Sales with a measure reference does a transition for every inner row. Where possible, do the following:
- Iterate the smallest table that gives the right grain.
- Use column references or
RELATEDin place of measure references. - Apply simple filters in
CALCULATErather than filtering a whole table withFILTER.
Red Sales Fast =
CALCULATE ( [Sales Amount], Product[Color] = "Red" )This is usually better than CALCULATE ( [Sales Amount], FILTER ( Product, Product[Color] = "Red" ) ), because a column filter does not need to iterate the table.
Quick reference
| Symptom | Likely cause | Fix |
|---|---|---|
| Calculated column sums to more than the measure | Duplicate rows, transition matches all copies | Read columns directly or add a unique key |
| Iterator total is lower than the measure total | Unmatched keys sit on the virtual blank row | Repair keys in the data |
| Blank category in a visual | Fact rows with no dimension match | Add or correct dimension rows |
| Slow measure with `SUMX` over a fact table | Measure reference transitions per row | Use `RELATED` or iterate a smaller table |
Exercise
A calculated column in Sales is defined as Line Total = [Sales Amount]. Sales has two identical rows with a quantity of 1 and a price of 10. What value does each of those rows show, and why?
Show answer
Each shows 20. Context transition filters Sales by every column of the current row. Both identical rows match that filter, so [Sales Amount] adds both rows (10 + 10) for each of them.
Exercise
SUMX ( Product, [Sales Amount] ) returns less than [Sales Amount] in the grand total. Give one likely cause and one way to check it.
Show answer
Some Sales rows have a product key that does not exist in Product. They attach to the virtual blank row, which the iterator over Product never visits. Check by counting Sales rows where RELATED ( Product[Product Key] ) is blank.