Skip to main content
Albero Academy

Iterators & Row Context

Calculate row by row inside a measure — the X-functions (SUMX, AVERAGEX, RANKX) and the context transition that links them to CALCULATE.

Chapter 10Power BI15 min readBeginner friendly
  • #Power BI
  • #DAX
  • #Iterators

Sometimes a calculation must happen row by row before it's aggregated — total sales when you don't have a line-total column, a weighted average, a rank. Iterator functions (the "X" functions) do exactly that, and they introduce the second kind of context: row context.

Why iterators?

SUM(Sales[Amount]) needs an Amount column to exist. But if you only have Quantity and Price, you can't sum a product that isn't stored. An iterator computes it per row and aggregates — no calculated column needed.

SUMX & the X-functions

An iterator takes a table and an expression, evaluates the expression for each row, then aggregates the results:

SUMX(table, expression-per-row)
Total Sales = SUMX(Sales, Sales[Quantity] * Sales[Price])
-- For each row: qty × price. Then SUM them all. No stored column needed.
Weighted Avg = DIVIDE(
SUMX(Sales, Sales[Score] * Sales[Weight]),
SUM(Sales[Weight])
)

The family: SUMX, AVERAGEX, MINX, MAXX, COUNTX, RANKX — same pattern, different aggregation.

Row context

Inside an iterator (and a calculated column), DAX has row context — it knows "the current row", so Sales[Price] refers to this row's price. This is different from filter context (a set of filters). The subtle, powerful bit is context transition: wrapping a measure in CALCULATE inside an iterator turns the current row into a filter.

RANKX

ranking
Product Rank =
RANKX(
ALL(Product[Name]), -- rank across all products
[Total Sales], -- by this measure
, DESC -- highest = rank 1
)

Practice

Write Total Sales as a SUMX(Sales, Quantity * Price) without a line-total column. Build a weighted average with two SUMX-style aggregations. Then rank products by total sales with RANKX and show the top ones.

Key takeaways

  1. 01Iterators (SUMX, AVERAGEX, MINX, MAXX, RANKX) compute an expression per row, then aggregate.
  2. 02Use them when the value must be computed per row (qty × price, weighted average) rather than already stored.
  3. 03Inside an iterator/column, row context means "the current row" — distinct from filter context.
  4. 04Context transition: CALCULATE inside an iterator turns the current row into a filter.
  5. 05Prefer SUM when the column exists; SUMX when you must calculate per row first.