06.2Iterators

With an aggregator, you give only a column. To run a calculation on each row before adding up, DAX has another family of functions, the iterators.

06.2.1A trap: multiplying two sums

To find the revenue without the Amount column, you could multiply the sum of the quantities by the sum of the prices:

SUM( Sales[Qty] ) * SUM( Sales[Price] )
Power BI accepts this DAX foryes, Measureyes, Calculated columnno, Calculated tableno, Virtual table

Power BI accepts it, but the result is wrong. Let us look at two sales only, V001 and V002:

  • The right calculation, row by row, gives 10 × 25,000 + 3 × 50,000, that is 400,000.
  • The two sums give 13 for the quantities and 75,000 for the prices, that is 13 × 75,000 = 975,000.

Over the 12 sales, the gap becomes huge. The quantities total 75, the prices added together 355,000. The formula returns 26,625,000, instead of 1,700,000.

Watch out!

The right calculation multiplies the quantity by the price on each row, then adds up the amounts obtained, 250,000 and 150,000, that is 400,000. An aggregator cannot multiply row by row.

06.2.2SUMX, row by row, then the total

In Excel, SUMPRODUCT multiplies two columns row by row, then adds up the results. In DAX, SUMX does this job.

SUMX takes two required arguments, a table and an expression:

SUMX( <table>, <expression> )

Here is a measure that calculates the revenue row by row, without going through the Amount column:

Revenue = SUMX( Sales, Sales[Qty] * Sales[Price] )
Power BI accepts this DAX foryes, Measureyes, Calculated columnno, Calculated tableno, Virtual table
In plain words

“I want the revenue, going sale by sale, calculating the quantity times the price, then adding up the results.”

SUMX places itself on the first row of Sales, V001, and calculates 10 × 25,000. It moves on to row V002, and so on up to V012. Then it adds up the 12 results, and returns 1,700,000. Follow it:

SUMX goes through the Sales table
Sale IDDateCustomer IDProduct CodeQtyPriceQty × Price
V0011/15/2024C001A1011025,000
V0023/8/2024C002A202350,000
V0035/20/2024C004A201425,000
V0049/12/2024C005A1021010,000
V00511/25/2024C006A201625,000
V0061/10/2025C001A101825,000
V0072/18/2025C003A202350,000
V0083/27/2025C002A1021510,000
V0095/6/2025C004A201625,000
V0107/14/2025C005A202350,000
V0119/30/2025C007A202250,000
V01212/2/2025C006A102510,000

Sales[Qty] and Sales[Price] thus give the quantity and the price of each row. This is the case announced in the lesson on the calculated column. Inside an iterator, the formula has a current row.

A function that goes through a table row by row, calculates an expression on each row, then draws a single result from all these calculations, is called an . SUMX adds them up. Without this last step, 12 amounts would remain, and not a revenue figure.

Practice

Each exercise is solved with the function you just discovered. Two exercises per function, a direct use and a combined use, from easiest to hardest, on data other than the course data. Pick the right formula out of five.

  1. Direct useMeasureDifficulty

    A bakery records its orders in the “Orders” table, with the quantity and the price of each item, in dollars. No column gives the amount. Write the “Takings” measure, the total of the amounts of all the rows.

    Orders
    ItemQuantityPrice
    Bread42
    Croissant101
    Tart215

    Which formula should you write for “Takings”?

  2. Combined useMeasureDifficulty

    A hotel records in the “Stays” table the price of each room and the discount granted, as a share of the price (0.1 for 10%). Write the “Collected” measure, the total of the prices after discount of all the stays.

    Stays
    RoomPriceDiscount
    1011000.1
    1022000
    1031500.2

06.2.3The other X functions

Other iterators work like SUMX. Their names also end in X. Each one goes through the table in the same way, then runs its own calculation on the results:

  • AVERAGEX( Sales, Sales[Qty] * Sales[Price] ) takes the average of the 12 results, that is 141,666.67.
  • MAXX( Sales, Sales[Qty] * Sales[Price] ) keeps the largest, that is 250,000.
  • MINX( Sales, Sales[Qty] * Sales[Price] ) keeps the smallest, that is 50,000.

FILTER, which you know, is also an iterator, even though its name does not end in X. It examines each row of the table, and keeps those that meet the condition.

Practice

Each exercise is solved with the function you just discovered. Two exercises per function, a direct use and a combined use, from easiest to hardest, on data other than the course data. Pick the right formula out of five.

  1. Direct useMeasureDifficulty

    A bike rental shop records in the “Rentals” table the duration of each rental, in hours, and its hourly rate, in dollars. Write the “Average Rental” measure, the average price of a rental.

    Rentals
    BikeHoursHourly Rate
    V128
    V256
    V3114

    Which formula should you write for “Average Rental”?

  2. Combined useMeasureDifficulty

    The rental shop wants the average price of the rentals of 2 hours or more, in the “Rentals” table. Write the “Average Long Rental” measure.

    Rentals
    BikeHoursHourly Rate
    V128
    V256
    V3114
  3. Direct useMeasureDifficulty

    A carrier loads its trucks with crates. The “Trucks” table gives the number of crates in each truck and the weight of one crate, in kilos. Write the “Heaviest Load” measure, the weight of the most heavily loaded truck.

    Trucks
    TruckCratesKilos per Crate
    T14025
    T23040
    T35020
  4. Combined useMeasureDifficulty

    The carrier wants the gap between the heaviest and the lightest load of the “Trucks” table. Write the “Load Gap” measure.

    Trucks
    TruckCratesKilos per Crate
    T14025
    T23040
    T35020
  5. Direct useMeasureDifficulty

    In a league, a win is worth 3 points and a draw 1 point. The “Teams” table gives the wins and the draws of each team. Write the “Last Place Points” measure, the smallest points total of a team.

    Teams
    TeamWinsDraws
    Eagles52
    Lions44
    Wolves60
  6. Combined useMeasureDifficulty

    The league wants the smallest points total among the teams of the “Teams” table that won at least 5 matches. Write the “Last Qualified Points” measure.

    Teams
    TeamWinsDraws
    Eagles52
    Lions44
    Wolves60

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

The “Purchases” table has two rows. The first sells 2 items at 10, the second 3 items at 20. What does SUM( Purchases[Qty] ) * SUM( Purchases[Price] ) return?

Question 2

Each car is sold for 5,000 less than its price. Which measure returns the new revenue?

Question 3

Without any filter, what does MAXX( FILTER( Sales, Sales[Price] = 50000 ), Sales[Qty] * Sales[Price] ) return?

Key takeaways
  • Multiplying two sums is not the same as multiplying row by row.
  • An iterator goes through a table row by row, calculates an expression on each row, then reduces the results.
  • SUMX adds up, AVERAGEX takes the average, MAXX and MINX keep the largest and the smallest. FILTER is also an iterator.