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:
“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:
| Sale ID | Date | Customer ID | Product Code | Customer city | Qty | Price | Qty × Price |
|---|---|---|---|---|---|---|---|
| V001 | 1/15/2024 | C001 | A101 | New York | 10 | 25,000 | |
| V002 | 3/8/2024 | C002 | A202 | New York | 3 | 50,000 | |
| V003current | 5/20/2024 | C004 | A201 | Chicago | 4 | 25,000 | 4 × 25,000 = |
| V004current | 9/12/2024 | C005 | A102 | Chicago | 10 | 10,000 | 10 × 10,000 = |
| V005current | 11/25/2024 | C006 | A201 | Toronto | 6 | 25,000 | 6 × 25,000 = |
| V006 | 1/10/2025 | C001 | A101 | New York | 8 | 25,000 | |
| V007 | 2/18/2025 | C003 | A202 | New York | 3 | 50,000 | |
| V008 | 3/27/2025 | C002 | A102 | New York | 15 | 10,000 | |
| V009current | 5/6/2025 | C004 | A201 | Chicago | 6 | 25,000 | 6 × 25,000 = |
| V010current | 7/14/2025 | C005 | A202 | Chicago | 3 | 50,000 | 3 × 50,000 = |
| V011current | 9/30/2025 | C007 | A202 | Toronto | 2 | 50,000 | 2 × 50,000 = |
| V012current | 12/2/2025 | C006 | A102 | Toronto | 5 | 10,000 | 5 × 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?
- 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.