09.5An iterator and the two contexts

In the lesson on iterators, SUMX went through the Sales table row by row. With what you now know about the two contexts, let’s look at what really happens.

09.5.1SUMX creates a row context

Let’s go back to this measure:

Revenue = SUMX( Sales, Sales[Qty] * Sales[Price] )
Power BI accepts this DAX foryes, Measureyes, Calculated columnno, Calculated tableno, Virtual table
In plain words

“I want the revenue, going sale by sale, calculating the quantity times the price, then adding up the results.”

A measure has no current row. Yet here, Sales[Qty] and Sales[Price] do return the quantity and the price of each row. SUMX is what places itself on each row, one after another.

Each of these rows is a row context. An iterator creates a row context, even in a measure.

09.5.2SUMX sees only the visible rows

Let’s place this measure in the visual by city. On the New York row, the filter context leaves only the 5 New York sales visible. SUMX goes through only these 5 rows, and returns 900,000. Choose a city, then follow SUMX:

SUMX on the row of a city in the visual
Sale IDDateCustomer IDProduct CodeCustomer cityQtyPriceQty × Price
V0011/15/2024C001A101New York1025,000
V0023/8/2024C002A202New York350,000
V003current5/20/2024C004A201Chicago425,0004 × 25,000 =
V004current9/12/2024C005A102Chicago1010,00010 × 10,000 =
V005current11/25/2024C006A201Toronto625,0006 × 25,000 =
V0061/10/2025C001A101New York825,000
V0072/18/2025C003A202New York350,000
V0083/27/2025C002A102New York1510,000
V009current5/6/2025C004A201Chicago625,0006 × 25,000 =
V010current7/14/2025C005A202Chicago350,0003 × 50,000 =
V011current9/30/2025C007A202Toronto250,0002 × 50,000 =
V012current12/2/2025C006A102Toronto510,0005 × 10,000 =

On the New York row of the visual, only the 5 New York sales remain visible. SUMX places itself on the first one, calculates Qty × Price, moves to the next one, then adds up the 5 results. SUMX never sees the other 7 sales.

Quiz

The questions often rely on the course tables. Keep them in view in their floating window, which you can move and enlarge.

Question 1

In the visual by city, what does SUMX( Sales, Sales[Qty] * Sales[Price] ) return on the Toronto row?

Question 2

You want to recalculate the amount in a new column of Sales, with SUMX. You add the calculated column Amount SUMX = SUMX( Sales, Sales[Qty] * Sales[Price] ). What is its value on the V001 row?

Question 3

In the visual by city, what does AVERAGEX( Sales, Sales[Qty] * Sales[Price] ) return on the Chicago row?

Key takeaways
  • An iterator creates a row context, even in a measure.
  • On a table of the model, like Sales, an iterator goes through only the visible rows. The filter context chooses the rows, the row context goes through them.