Sub topic 14
When Iterators Are Slow and What to Do About It
This chapter assumes you can write SUMX, AVERAGEX, and RANKX measures and that you understand row context and context transition from the earlier chapters. You will learn what makes an iterator expensive and how to reduce its cost.
Where the cost comes from
An iterator visits every row of its table argument. The cost depends on two things: how many rows it visits, and how much work each visit does.
A simple expression over a fact table is cheap. In SUMX ( Sales, Sales[Quantity] * Sales[Net Price] ), the engine can usually compute the product inside the fast storage engine in one pass.
The expensive cases are these:
- A measure reference inside the iterator. Each row triggers a context transition, which is a separate filtered evaluation of the measure.
- A large table argument. Iterating over a million customers means a million evaluations.
- Nested iterators. The inner iterator runs once per outer row, so the work multiplies.
- FILTER over a whole fact table. Every row is tested one at a time.
Replace the iterator when an aggregation is equivalent
Consider the per-customer average from earlier in this chapter's series:
Avg Sales per Customer =
AVERAGEX (
VALUES ( Customer[CustomerKey] ),
[Sales Amount]
)This runs a context transition for each customer. When the model is large, you can often get the same number from two column aggregations:
Avg Sales per Customer Fast =
DIVIDE (
[Sales Amount],
DISTINCTCOUNT ( Sales[CustomerKey] )
)Both versions skip customers with no sales. AVERAGEX ignores blank results, and DISTINCTCOUNT on the fact table only counts customers that appear there. Check that the two versions agree on your data before you switch.
Filter columns, not tables
Passing FILTER ( Sales, ... ) to an iterator forces a row-by-row test. A column predicate in CALCULATE lets the engine filter the column directly.
Sales Multi Quantity =
CALCULATE (
[Sales Amount],
Sales[Quantity] > 1
)Reduce the rows and the repeated work
- Iterate over the lowest-cardinality table that still gives the right answer. For a daily average,
VALUES ( 'Date'[Date] )has far fewer rows than the Sales table. - Store a value that never changes per row, such as a line total, in a column during data load. This costs memory but removes the per-row calculation at query time.
- Use variables so that a value is computed once instead of in every branch of an expression.
Watch RANKX in particular
RANKX evaluates its expression once for every item in the ranking table, and it does this for every cell in the visual. With ALL ( Product[Product Name] ) and 20,000 products, each visible row triggers 20,000 measure evaluations. The cost grows with the product of visible rows and ranked items.
To limit the cost, rank over the smallest set that answers the question, for example ALLSELECTED with a Top N filter on the visual. Use HASONEVALUE so the total row does not run a useless full ranking.
Measure before you rewrite
Use DAX Studio or Performance Analyzer in Power BI Desktop to compare the options. In DAX Studio, Server Timings show how much time went to the storage engine and how much to the formula engine. A high formula engine share, or many storage engine queries, points to per-row measure calls.
Exercise
A measure uses SUMX ( Sales, Sales[Quantity] * Sales[Net Price] ) and a second measure uses AVERAGEX ( VALUES ( Customer[CustomerKey] ), [Sales Amount] ) over 2 million customers. Which one is more likely to be slow, and why?
Show answer
The AVERAGEX measure. It calls the [Sales Amount] measure for each of 2 million customers, and each call needs a context transition and its own evaluation. The SUMX measure uses a plain column expression that the storage engine can compute in one pass.