SUMX and iterators

SUM adds up a column. If something has to be calculated per row first, such as quantity times price, you need an iterator: SUMX, AVERAGEX, MAXX and their relatives walk through a table row by row.

In short: SUMX(table, expression) evaluates the expression for each row of the table and adds up the results. AVERAGEX, MINX, MAXX and COUNTX do the same with a different aggregation. FILTER keeps the rows for which a condition is true, including a condition on a measure.

Before this: Percent of total and Column or measure?

Try it now

Task Create the measure "Avg List Price": the average of Sales[Amount] divided by Sales[Quantity], calculated per row of Sales. Every order counts the same, however many units it has.

Sales 120 rows, 7 columns
OrderIDOrderDateShipDateProductIDRegionIDQuantityAmount
13 Oct 20266 Oct 20261221798
29 Nov 202519 Nov 20252351145
320 Sep 202627 Sep 2026544596
416 Jun 20252 Jul 20252151145
526 Mar 202631 Mar 20261343596
64 Dec 202510 Dec 2025545745
78 Jul 202526 Jul 2025614196
820 Feb 20251 Mar 202532359.7
99 Dec 202524 Dec 202562298
1012 Aug 202625 Aug 2026212458
117 Sep 202612 Sep 2026542298
122 Mar 202614 Mar 2026623147

Showing the first 12 of 120 rows.

Measures: [Total Sales]

Product 6 rows, 4 columns
ProductIDProductCategoryPrice
1LaptopHardware899
2MonitorHardware229
3MouseAccessories19.9
4KeyboardAccessories39.9
5Office suiteSoftware149
6AntivirusSoftware49
Region 4 rows, 2 columns
RegionIDRegion
1North
2South
3West
4East
Date 730 rows, 4 columns
DateYearMonthMonthName
1 Jan 202520251Jan
2 Jan 202520251Jan
3 Jan 202520251Jan
4 Jan 202520251Jan
5 Jan 202520251Jan
6 Jan 202520251Jan
7 Jan 202520251Jan
8 Jan 202520251Jan
9 Jan 202520251Jan
10 Jan 202520251Jan
11 Jan 202520251Jan
12 Jan 202520251Jan

Showing the first 12 of 730 rows.

How an iterator works

An iterator has two arguments: a table and an expression. For each row of the table there is a current row, as in a calculated column, and the expression is evaluated there. Then the iterator aggregates: SUMX adds, AVERAGEX averages, MAXX takes the largest.

SUMX(Sales, Sales[Quantity] * RELATED(Product[Price])) is therefore the list-price revenue, without creating a column for it. The table is read in the cell's filter context: in the Hardware row, SUMX only goes through the hardware orders.

Context transition: a measure per row

If the expression contains a measure, Power BI turns the current row into a filter. MAXX(VALUES('Date'[Month]), [Total Sales]) therefore calculates the sales of each month separately and takes the largest. FILTER uses the same mechanism to put conditions on measures, which CALCULATE alone cannot do.

Task In how many months were a region's sales above 3000? Create the measure "Strong Months": count the months of 'Date'[Month] in which [Total Sales] is greater than 3000. The matrix is filtered to 2026.

Sales 120 rows, 7 columns
OrderIDOrderDateShipDateProductIDRegionIDQuantityAmount
13 Oct 20266 Oct 20261221798
29 Nov 202519 Nov 20252351145
320 Sep 202627 Sep 2026544596
416 Jun 20252 Jul 20252151145
526 Mar 202631 Mar 20261343596
64 Dec 202510 Dec 2025545745
78 Jul 202526 Jul 2025614196
820 Feb 20251 Mar 202532359.7
99 Dec 202524 Dec 202562298
1012 Aug 202625 Aug 2026212458
117 Sep 202612 Sep 2026542298
122 Mar 202614 Mar 2026623147

Showing the first 12 of 120 rows.

Measures: [Total Sales]

Product 6 rows, 4 columns
ProductIDProductCategoryPrice
1LaptopHardware899
2MonitorHardware229
3MouseAccessories19.9
4KeyboardAccessories39.9
5Office suiteSoftware149
6AntivirusSoftware49
Region 4 rows, 2 columns
RegionIDRegion
1North
2South
3West
4East
Date 730 rows, 4 columns
DateYearMonthMonthName
1 Jan 202520251Jan
2 Jan 202520251Jan
3 Jan 202520251Jan
4 Jan 202520251Jan
5 Jan 202520251Jan
6 Jan 202520251Jan
7 Jan 202520251Jan
8 Jan 202520251Jan
9 Jan 202520251Jan
10 Jan 202520251Jan
11 Jan 202520251Jan
12 Jan 202520251Jan

Showing the first 12 of 730 rows.

This exercise is part of Premium.

Premium unlocks every exercise in every lesson, unlimited practice with fresh tasks and the final test with certificate.

See Premium Already Premium? Log in

The first exercise in this lesson is free.

In real Power BI

  1. Create a new measure, for example List Revenue = SUMX(Sales, Sales[Quantity] * RELATED(Product[Price])).
  2. Look at the rows and the total row in a matrix: with SUMX the total row is the sum over all rows of the table, not a product of sums.

Common mistakes

SUM over a product

SUM(Sales[Quantity]) * SUM(Product[Price]) multiplies two sums. In a row with only one product this happens to be right, in the total row it is not. Calculating per row means SUMX.

Column without its table in the iterator

The expression uses columns of the table the iterator walks through. Columns of another table come in through RELATED.

FAQ

Is SUMX slower than SUM?

For SUMX(Sales, Sales[Amount]), Power BI works exactly as for SUM internally. Iterators become slow over large tables with expensive expressions, such as a measure per row.

A quantity-times-price column or SUMX?

Both work. The column needs memory in the model, SUMX is calculated with each query. For a KPI that only appears as totals, SUMX is usually the better choice.

Up next: RANKX and TOPN

Rankings and the top three.

Next lesson

Write to us

Choose language

Select your preferred language