Power Pivot & DAX course · Lesson 9 of 16
Iterators in DAX: SUMX, AVERAGEX, MAXX and RANKX
Row-by-row DAX: SUMX for cost and margin, AVERAGEX per customer, MAXX, CONCATENATEX and ranking products with RANKX and HASONEVALUE.
Plain aggregation functions such as SUM and AVERAGE accept a single column. Real business questions are rarely that tidy: cost is quantity times unit cost, the average should be per customer rather than per order line, and a ranking needs to compare each product with all others. For these you use iterators, the DAX functions ending in X (SUMX, AVERAGEX, MAXX, MINX, COUNTX) and their relatives RANKX and FILTER. They walk through a table row by row, evaluate an expression for each row, and then aggregate the results.
How an iterator works
SUMX ( <table>, <expression> )- The table argument is evaluated in the current filter context. In the Accessories row of a PivotTable,
Salescontains only accessory sales. - For each row of that table, the iterator creates a row context and evaluates the expression, so column references mean this row's value.
- The per-row results are aggregated: summed by
SUMX, averaged byAVERAGEX, and so on.
This is the combination of filter and row context from Filter context and row context in DAX in action.
SUMX: cost and margin without extra columns
Sales stores Qty and Amount, but cost per unit lives in Products. Instead of a calculated column, write one measure:
Revenue := SUM ( Sales[Amount] )
Cost := SUMX ( Sales, Sales[Qty] * RELATED ( Products[Cost] ) )
Margin := [Revenue] - [Cost]
Margin % := DIVIDE ( [Margin], [Revenue] )| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | ProductID | Product | Category | Price | Cost |
| 2 | P1 | Laptop | Computers | 950 | 700 |
| 3 | P2 | Mouse | Accessories | 12 | 5 |
| 4 | P3 | Monitor | Displays | 189 | 120 |
| 5 | P4 | Keyboard | Accessories | 35 | 18 |
| 6 | P5 | Tablet | Computers | 420 | 300 |
Products: unit Price and Cost.
| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Category | Revenue | Cost | Margin | Margin % |
| 2 | Accessories | 745 | 348 | 397 | 53.3% |
| 3 | Computers | 4950 | 3600 | 1350 | 27.3% |
| 4 | Displays | 945 | 600 | 345 | 36.5% |
| 5 | Grand Total | 6640 | 4548 | 2092 | 31.5% |
Products[Category] on rows. Margin % formatted as a percentage.
For Computers, SUMX visits four rows: 2 laptops × 700, 4 tablets × 300, 1 laptop × 700 and 1 tablet × 300, giving 3,600. Revenue is 4,950, so the margin is 1,350, or 27.3%. The result is identical to the calculated-column approach of Calculated columns vs measures in Power Pivot, but nothing extra is stored in the model.
SUMX ( Sales, Sales[Qty] * Sales[Price] ) are no problem. What becomes slow is calling complex measures for every row of a huge table, or nesting one iterator inside another over two large tables.AVERAGEX: average over the right thing
AVERAGE ( Sales[Amount] ) averages order lines. Managers often want the average per customer instead. Iterate over the list of customers, compute revenue for each, then average:
Avg per Order := AVERAGE ( Sales[Amount] )
Avg per Customer := AVERAGEX ( VALUES ( Sales[CustomerID] ), [Revenue] )| A | B | C | D | E | |
|---|---|---|---|---|---|
| 1 | Category | Revenue | Customers | Avg per Customer | Avg per Order |
| 2 | Accessories | 745 | 3 | 248.33 | 186.25 |
| 3 | Computers | 4950 | 3 | 1,650.00 | 1,237.50 |
| 4 | Displays | 945 | 2 | 472.50 | 472.50 |
| 5 | Grand Total | 6640 | 4 | 1,660.00 | 664.00 |
Products[Category] on rows; averages with 2 decimals.
In the Accessories row, three customers bought: Anna Weber 175, Brook Dental 330 (two orders) and Carter & Co 240. Their average is 248.33. Because [Revenue] is a measure, it is evaluated for each customer through context transition, so each customer's revenue is filtered correctly. Writing SUM ( Sales[Amount] ) instead of [Revenue] inside AVERAGEX would give the wrong answer, because a bare SUM does not trigger context transition; you would need CALCULATE ( SUM ( Sales[Amount] ) ).
Other iterators
| Function | Returns | Example |
|---|---|---|
| SUMX | Sum of the expression | SUMX ( Sales, Sales[Qty] * RELATED ( Products[Cost] ) ) |
| AVERAGEX | Average of the expression | AVERAGEX ( VALUES ( Sales[CustomerID] ), [Revenue] ) |
| MAXX / MINX | Largest / smallest value | MAXX ( VALUES ( Customers[Customer] ), [Revenue] ) |
| COUNTX | Count of non-blank results | COUNTX ( Products, [Revenue] ) |
| CONCATENATEX | Text joined with a separator | CONCATENATEX ( VALUES ( Products[Product] ), Products[Product], ", " ) |
| FILTER | The rows where the condition is true | FILTER ( Products, [Revenue] > 1000 ) |
MAXX ( VALUES ( Customers[Customer] ), [Revenue] ) returns the revenue of the best customer in the current cell, 2,495 at the grand total. CONCATENATEX is a lovely way to list the products bought by a customer in a single PivotTable cell.
RANKX: ranking products
RANKX ( <table>, <expression>, [value], [order], [ties] )RANKX evaluates the expression for every row of the table, then finds where the current cell's value falls in that list. The table must contain all products, otherwise each product would be ranked against only itself. That is where ALL from Removing filters: ALL, ALLEXCEPT, ALLSELECTED and REMOVEFILTERS comes in:
Rank (no fix) := RANKX ( ALL ( Products[Product] ), [Revenue] )
Product Rank :=
IF (
HASONEVALUE ( Products[Product] ),
RANKX ( ALL ( Products[Product] ), [Revenue] )
)| A | B | C | D | |
|---|---|---|---|---|
| 1 | Product | Revenue | Rank (no fix) | Product Rank |
| 2 | Keyboard | 385 | 4 | 4 |
| 3 | Laptop | 2850 | 1 | 1 |
| 4 | Monitor | 945 | 3 | 3 |
| 5 | Mouse | 360 | 5 | 5 |
| 6 | Tablet | 2100 | 2 | 2 |
| 7 | Grand Total | 6640 | 1 |
Products[Product] on rows, sorted alphabetically.
Laptop has the highest revenue (2,850) and ranks 1; Mouse, with 360, ranks 5. The Grand Total row of the first measure shows a meaningless 1: the total revenue of 6,640 is compared with the individual products and is larger than all of them. HASONEVALUE returns TRUE only when the filter context contains exactly one product, so Product Rank stays blank on totals. Without a third argument for IF, the result is blank.
Ties and order
- With the default SKIP, two products tied for 2nd are both 2 and the next one is 4, like a sports table.
- With DENSE, the next one is 3:
RANKX ( ALL ( Products[Product] ), [Revenue], , DESC, DENSE ). Note the empty third argument. - Use ASC to rank the smallest value first, for example the cheapest supplier or the fastest delivery time.
ALL ( Products[Product] ) still ranks each product against all products, not within its category. To rank within the category, use ALLSELECTED ( Products[Product] ) with the category filter kept, or rank over CALCULATETABLE ( VALUES ( Products[Product] ), ALL ( Products[Product] ) ). Check the numbers on a small example before trusting them.Practice
- Write
Units Value at Cost := SUMX ( Sales, Sales[Qty] * RELATED ( Products[Cost] ) )and check the grand total. - Create
Best Customer Revenue := MAXX ( VALUES ( Customers[Customer] ), [Revenue] )and put Region on rows. - Create a
Customer Rankmeasure withRANKXoverALL ( Customers[Customer] )and protect it withHASONEVALUE.
Check yourself
1. Why use SUMX ( Sales, Sales[Qty] * RELATED ( Products[Cost] ) ) instead of SUM?
Row-by-row arithmetic needs an iterator.
2. What table should AVERAGEX iterate to get the average revenue per customer?
Iterate over the customers, evaluating [Revenue] for each one.
3. Why does RANKX ( ALL ( Products[Product] ), [Revenue] ) show 1 at the Grand Total?
The total is compared with the individual products.
4. Which function hides the rank on total rows?
HASONEVALUE is TRUE only when one product is in context.
5. With ties = DENSE and two products tied for 2nd, what is the next rank?
DENSE does not skip numbers.