06.2Iterators
With an aggregator, you give only a column. To run a calculation on each row before adding up, DAX has another family of functions, the iterators.
06.2.1A trap: multiplying two sums
To find the revenue without the Amount column, you could multiply the sum of the quantities by the sum of the prices:
Power BI accepts it, but the result is wrong. Let us look at two sales only, V001 and V002:
- The right calculation, row by row, gives 10 × 25,000 + 3 × 50,000, that is 400,000.
- The two sums give 13 for the quantities and 75,000 for the prices, that is 13 × 75,000 = 975,000.
Over the 12 sales, the gap becomes huge. The quantities total 75, the prices added together 355,000. The formula returns 26,625,000, instead of 1,700,000.
The right calculation multiplies the quantity by the price on each row, then adds up the amounts obtained, 250,000 and 150,000, that is 400,000. An aggregator cannot multiply row by row.
06.2.2SUMX, row by row, then the total
In Excel, SUMPRODUCT multiplies two columns row by row, then adds up the results. In DAX, SUMX does this job.
SUMX takes two required arguments, a table and an expression:
Here is a measure that calculates the revenue row by row, without going through the Amount column:
“I want the revenue, going sale by sale, calculating the quantity times the price, then adding up the results.”
SUMX places itself on the first row of Sales, V001, and calculates 10 × 25,000. It moves on to row V002, and so on up to V012. Then it adds up the 12 results, and returns 1,700,000. Follow it:
| Sale ID | Date | Customer ID | Product Code | Qty | Price | Qty × Price |
|---|---|---|---|---|---|---|
| V001 | 1/15/2024 | C001 | A101 | 10 | 25,000 | |
| V002 | 3/8/2024 | C002 | A202 | 3 | 50,000 | |
| V003 | 5/20/2024 | C004 | A201 | 4 | 25,000 | |
| V004 | 9/12/2024 | C005 | A102 | 10 | 10,000 | |
| V005 | 11/25/2024 | C006 | A201 | 6 | 25,000 | |
| V006 | 1/10/2025 | C001 | A101 | 8 | 25,000 | |
| V007 | 2/18/2025 | C003 | A202 | 3 | 50,000 | |
| V008 | 3/27/2025 | C002 | A102 | 15 | 10,000 | |
| V009 | 5/6/2025 | C004 | A201 | 6 | 25,000 | |
| V010 | 7/14/2025 | C005 | A202 | 3 | 50,000 | |
| V011 | 9/30/2025 | C007 | A202 | 2 | 50,000 | |
| V012 | 12/2/2025 | C006 | A102 | 5 | 10,000 |
Sales[Qty] and Sales[Price] thus give the quantity and the price of each row. This is the case announced in the lesson on the calculated column. Inside an iterator, the formula has a current row.
A function that goes through a table row by row, calculates an expression on each row, then draws a single result from all these calculations, is called an . SUMX adds them up. Without this last step, 12 amounts would remain, and not a revenue figure.
Practice
Each exercise is solved with the function you just discovered. Two exercises per function, a direct use and a combined use, from easiest to hardest, on data other than the course data. Pick the right formula out of five.
Direct useMeasureDifficulty
A bakery records its orders in the “Orders” table, with the quantity and the price of each item, in dollars. No column gives the amount. Write the “Takings” measure, the total of the amounts of all the rows.
Orders Item Quantity Price Bread 4 2 Croissant 10 1 Tart 2 15 Which formula should you write for “Takings”?
Combined useMeasureDifficulty
A hotel records in the “Stays” table the price of each room and the discount granted, as a share of the price (0.1 for 10%). Write the “Collected” measure, the total of the prices after discount of all the stays.
Stays Room Price Discount 101 100 0.1 102 200 0 103 150 0.2
06.2.3The other X functions
Other iterators work like SUMX. Their names also end in X. Each one goes through the table in the same way, then runs its own calculation on the results:
- AVERAGEX( Sales, Sales[Qty] * Sales[Price] ) takes the average of the 12 results, that is 141,666.67.
- MAXX( Sales, Sales[Qty] * Sales[Price] ) keeps the largest, that is 250,000.
- MINX( Sales, Sales[Qty] * Sales[Price] ) keeps the smallest, that is 50,000.
FILTER, which you know, is also an iterator, even though its name does not end in X. It examines each row of the table, and keeps those that meet the condition.
Practice
Each exercise is solved with the function you just discovered. Two exercises per function, a direct use and a combined use, from easiest to hardest, on data other than the course data. Pick the right formula out of five.
Direct useMeasureDifficulty
A bike rental shop records in the “Rentals” table the duration of each rental, in hours, and its hourly rate, in dollars. Write the “Average Rental” measure, the average price of a rental.
Rentals Bike Hours Hourly Rate V1 2 8 V2 5 6 V3 1 14 Which formula should you write for “Average Rental”?
Combined useMeasureDifficulty
The rental shop wants the average price of the rentals of 2 hours or more, in the “Rentals” table. Write the “Average Long Rental” measure.
Rentals Bike Hours Hourly Rate V1 2 8 V2 5 6 V3 1 14 Direct useMeasureDifficulty
A carrier loads its trucks with crates. The “Trucks” table gives the number of crates in each truck and the weight of one crate, in kilos. Write the “Heaviest Load” measure, the weight of the most heavily loaded truck.
Trucks Truck Crates Kilos per Crate T1 40 25 T2 30 40 T3 50 20 Combined useMeasureDifficulty
The carrier wants the gap between the heaviest and the lightest load of the “Trucks” table. Write the “Load Gap” measure.
Trucks Truck Crates Kilos per Crate T1 40 25 T2 30 40 T3 50 20 Direct useMeasureDifficulty
In a league, a win is worth 3 points and a draw 1 point. The “Teams” table gives the wins and the draws of each team. Write the “Last Place Points” measure, the smallest points total of a team.
Teams Team Wins Draws Eagles 5 2 Lions 4 4 Wolves 6 0 Combined useMeasureDifficulty
The league wants the smallest points total among the teams of the “Teams” table that won at least 5 matches. Write the “Last Qualified Points” measure.
Teams Team Wins Draws Eagles 5 2 Lions 4 4 Wolves 6 0
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
The “Purchases” table has two rows. The first sells 2 items at 10, the second 3 items at 20. What does SUM( Purchases[Qty] ) * SUM( Purchases[Price] ) return?
Question 2
Each car is sold for 5,000 less than its price. Which measure returns the new revenue?
Question 3
Without any filter, what does MAXX( FILTER( Sales, Sales[Price] = 50000 ), Sales[Qty] * Sales[Price] ) return?
- Multiplying two sums is not the same as multiplying row by row.
- An iterator goes through a table row by row, calculates an expression on each row, then reduces the results.
- SUMX adds up, AVERAGEX takes the average, MAXX and MINX keep the largest and the smallest. FILTER is also an iterator.