Foundations › Power BI › DAX: Calculate Smartly
DAX: Calculate SmartlyIterators and Variables: SUMX, AVERAGEX and VAR
SUM adds up a ready-made column. But if the calculation must happen row by row before adding (quantity × unit price, say) you need an iterator. The odd name is simple: a function that loops through a table row by row.
- A table (FactSales)
- For each row: evaluate the expression
- Add or average the results
SUMX: row by row
Total Sales = SUM(FactSales[Sales])
Total Cost = SUM(FactSales[Cost])
Profit = [Total Sales] - [Total Cost]
Gross Profit = SUMX(FactSales, FactSales[Quantity] * (FactSales[UnitPrice] - FactSales[UnitCost]))Both approaches give the same number! Because Sales and Cost already exist as columns. The example shows an iterator is not always needed, and how it works when those columns are missing.
RELATED: fetch a column from the other table
Inside an iterator, each FactSales row has exactly one related DimProduct row. RELATED fetches a value from it. We compare the actual price with the list price.
Price Uplift = SUMX(FactSales, FactSales[Quantity] * (FactSales[UnitPrice] - RELATED(DimProduct[ListPrice])))In 2024 the price equals the list price so the result is zero; in 2025 selling prices are 7% above list so an uplift appears. This will help us later in the «why did profit fall» investigation.
AVERAGEX: average of ratios is not the ratio of sums
Total Sales = SUM(FactSales[Sales])
Total Cost = SUM(FactSales[Cost])
Margin % = DIVIDE([Total Sales] - [Total Cost], [Total Sales])
Avg Order Margin = AVERAGEX(FactSales, DIVIDE(FactSales[Sales] - FactSales[Cost], FactSales[Sales]))Margin % is weighted by sales volume; Avg Order Margin treats every order equally. Both are correct but they answer different questions.
VAR and RETURN: a formula you can read
Margin Check =
VAR s = SUM(FactSales[Sales])
VAR c = SUM(FactSales[Cost])
RETURN DIVIDE(s - c, s)
Margin Flag =
VAR s = SUM(FactSales[Sales])
VAR m = DIVIDE(s - SUM(FactSales[Cost]), s)
RETURN IF(m < 0.25, "Low", "OK")VAR names an intermediate result and calculates it once, so the formula reads as steps. Good naming (s for sales, c for cost) matters.
🧩 Classify each case: is plain SUM enough, or do you need an iterator?
Advanced Advanced: context transition
When you call a measure inside an iterator (like SUMX(VALUES(DimProduct[Product]), [Total Sales])), Power BI turns the current row into a filter automatically. That is what makes the measure calculate per product.
Exercise 1 — Write a measure named Answer for the cost of sales at the reference cost: the sum of quantity × ListCost from the product table.
Exercise 2 — Write a measure named Answer for profit per unit using VAR: (sales − cost) ÷ total quantity.
Exercise 3 — Write a measure named Answer for the average margin of a single order (profit ÷ sales per order), averaged over all orders.
Try it in real Power BI Desktop
- SUMX and AVERAGEX walk through rows and evaluate an expression per row.
- RELATED fetches a value from the dimension table inside an iterator.
- An average of ratios differs from the ratio of sums.
- VAR makes formulas clearer and faster; do not use an iterator when SUM is enough.
Need a real Power BI dashboard for your business?
I design a data model and an executive dashboard fitted to your business and your real data.