06.3Aggregator or iterator?
You may have noticed that an aggregator and an iterator can return the same result. Let us see why, then which one to choose.
06.3.1SUM, a shortcut for SUMX
Placed in a visual, these two measures return the same result. On the “Total” row, both return 1,700,000.
SUMX goes through the 12 rows and adds up the amount of each one. SUM adds up the Amount column directly. The path changes, the result stays the same.
SUM is in fact a shortcut for SUMX, when the expression is a plain column. Most aggregators have their iterator in this way. AVERAGE has AVERAGEX, MIN has MINX, MAX has MAXX. DISTINCTCOUNT, however, has none.
06.3.2Which one to choose?
The question to ask yourself is simple. Does the calculation already exist in a column?
- Yes. The Amount column exists, so take the aggregator, SUM( Sales[Amount] ). It is shorter, and easier to read.
- No. You first have to calculate something on each row, like the quantity times the price. Take the iterator, SUMX( Sales, Sales[Qty] * Sales[Price] ).
For example, taking off 5,000 per car sold is calculated on each sale, then added up:
“I want the revenue after taking off 5,000 per car, going sale by sale, calculating the quantity times the price minus 5,000, then adding up the results.”
On row V001, SUMX calculates 10 × 20,000, that is 200,000. Over the 12 sales, it returns 1,325,000.
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
You want the total quantity sold. The Sales[Qty] column exists. Which formula should you choose?
Question 2
In the same visual, you place SUM( Sales[Amount] ) and SUMX( Sales, Sales[Amount] ). What do they show on the “Total” row?
Question 3
To take 10% off each sale, you could write SUMX( Sales, Sales[Amount] * 0.9 ), which returns 1,530,000. What would SUM( Sales[Amount] ) * 0.9 return?
- SUM is a shortcut for SUMX, when the expression is a plain column. Most aggregators have their iterator. DISTINCTCOUNT, for example, has none.
- If the calculation already exists in a column, take the aggregator. Otherwise, take the iterator.