Statistical Iterators: AVERAGEX, MINX & MAXX
Overview & Learning Objectives
In real-world data, revenue is rarely pre-calculated as a single column; it must be calculated on-the-fly by multiplying Quantity by Unit Price row-by-row before summing. Basic `SUM()` cannot do this because it only accepts a single column. The iterator function `SUMX()` solves this: it establishes an in-memory Row Context, steps through a table row-by-row, computes the expression, and sums the results.
- Understand the architecture of DAX iterator functions ending in "X".
- Master the syntax: `SUMX(Table, Expression)`.
- Filter tables dynamically inside `SUMX(FILTER(Table, ...), ...)`.
Industry Standard
Standard: Analytics standards at Amazon and Flipkart require using `SUMX` over filtered virtual tables rather than adding permanent calculated columns to multi-million row fact tables.
Real-World Example: Scoop & Swirl Ice Cream Parlour
Scoop & Swirl invoices record Unit Price, Quantity Sold, and Discount %. Revenue must be computed row-by-row.
| Item Name | Unit Price (₹) | Quantity | Discount | Row Expression Evaluated by SUMX |
|---|---|---|---|---|
| Belgian Dark Chocolate | 120 | 3 | 0.10 | 120 * 3 * (1 - 0.10) = ₹ 324 |
| Alphonso Mango | 150 | 2 | 0.00 | 150 * 2 * (1 - 0.00) = ₹ 300 |
| Waffle Cone | 30 | 5 | 0.00 | 30 * 5 * (1 - 0.00) = ₹ 150 |
Syntax & Core Workflow
Total Net Sales = SUMX(Fact_Sales, Fact_Sales[Quantity] * Fact_Sales[UnitPrice] * (1 - Fact_Sales[Discount]))
Parameters Breakdown
Table: The table to iterate through row-by-row (e.g. `Fact_Sales` or `FILTER(...)`).Expression: The mathematical expression evaluated for every individual row under row context.Aggregation: After stepping through all rows, SUMX sums every computed row value into a final scalar total.
Sample Walkthrough (Q&A)
Question: Why not just create a Calculated Column for `Net Revenue` and then use standard `SUM()`?
Formula / Action: A calculated column stores millions of numbers permanently in RAM.
Result: Using `SUMX` computes the result virtually without wasting storage memory.
Explanation: Calculated columns consume permanent space in the VertiPaq model. `SUMX` computes the math on demand in memory, keeping your `.pbix` file size compact.
Common Mistakes & Limitations
- Using `SUMX` when a simple `SUM` would suffice (e.g. `SUMX(Sales, Sales[Amount])` is slower than `SUM(Sales[Amount])`).
- Iterating over an entire Fact table when iterating over a filtered subset or Dimension table is 10x faster.
Keyboard Shortcuts
| Action | Windows | Mac |
|---|---|---|
| Create Measure | Alt -> Modeling -> New Measure | New Measure |
| Format Currency | Measure Tools -> Currency symbol (₹) | Currency |
Pro Tips & Modern Alternatives
- If multiplying simple columns without discounts, calculate the column in your SQL source view before ingestion so VertiPaq can use native `SUM()`.
- Combine `SUMX` with `RELATED()` to pull unit prices from dimension tables during iteration.