SUMX and AVERAGEX
Aggregate the expression at its true grain
An iterator evaluates an expression with a current row of an input table, then aggregates those results. The table controls the grain. A sale-line discount must be applied before summing; a unit-price average needs a denominator chosen for the business question. Use the isolated Import table DiscountSales from labs/dax-iterators/discount-sales.csv; no relationships or RLS are required. Text: LineID and Region. Whole numbers: Quantity, UnitPriceUSD, DiscountPercent and GrossUSD. DiscountPercent is in percentage points (10 means 10%, not 0.10). GrossUSD is the pre-discount line amount. Five ordinary sale lines and one synthetic zero-quantity test line are present. Fixed USD, no taxes, returns or rounding policy beyond this integer-valued fixture. All results below are expected from independent fixture arithmetic; native DAX execution is NOT RUN. Create each named measure separately. DAX Query View queries run separately and do not inherit report slicers.
Example 1 — Apply row-level discounts
Gross From Column = SUM(DiscountSales[GrossUSD])Gross From Rows = SUMX(DiscountSales, DiscountSales[Quantity] * DiscountSales[UnitPriceUSD])Net Revenue = SUMX(DiscountSales, DiscountSales[Quantity] * DiscountSales[UnitPriceUSD] * (1 - DiscountSales[DiscountPercent] / 100))The two gross measures both return 200 USD because the stored values agree with quantity × price. The net line values are 24, 27, 27, 50, 30, 0 USD (L01–L05, Z01). Net revenue is 158 USD; North 101, South 57, Zero 0. Multiplying total quantity by total price or applying an unweighted average discount to gross is a different calculation. A row context identifies values for the current sale line; it does not by itself filter an unrelated aggregation.
Example 2 — Choose weighted versus unweighted average
Mean Listed Price = AVERAGEX( FILTER(DiscountSales, DiscountSales[Quantity] > 0), DiscountSales[UnitPriceUSD])Units = SUM(DiscountSales[Quantity])Weighted Gross Price = DIVIDE([Gross From Rows], [Units])Weighted Net Price = DIVIDE([Net Revenue], [Units])The five positive-quantity rows give an equal-line mean of 134/5 = 26.80 USD. Nine units were sold; the gross weighted price is 200/9 ≈ 22.22 and net weighted price 158/9 ≈ 17.56. The test line has no units and is excluded from the equal-line mean by policy. With Region = Zero, Units = 0, Net Revenue = 0 and both weighted prices are BLANK; the equal-line mean is also BLANK because its filtered input is empty. An empty selection differs from a real zero-valued row. Keep ratios numeric and format only their display.
Count and range row expressions
EVALUATEROW( "Positive quantity count", COUNTX(DiscountSales, IF(DiscountSales[Quantity] > 0, 1, BLANK())), "Boolean result count", COUNTAX(DiscountSales, DiscountSales[Quantity] > 0), "Minimum net line", MINX(DiscountSales, DiscountSales[GrossUSD] * (1 - DiscountSales[DiscountPercent] / 100)), "Maximum net line", MAXX(DiscountSales, DiscountSales[GrossUSD] * (1 - DiscountSales[DiscountPercent] / 100)))Expected: 5, 6, 0, 50. COUNTX counts eligible nonblank numeric/date/text results, not logical values. COUNTAX can count logical results, including FALSE; it is not a count of true conditions. To count qualifying rows use COUNTROWS(FILTER(...)) or emit 1/BLANK explicitly. MINX/MAXX inspect the numeric result per row; zero is a value, not a missing value. Mixed-type ordering is outside this numeric lesson. SUMX and AVERAGEX have their own blank/type rules; avoid assuming every iterator shares the same rules.
Practice — calculate South
Select South. Compute net amounts, units, gross weighted price and net weighted price, and compare with the equal-line mean. Explain why averaging regional weighted prices does not generally reproduce the total. Then select Zero and distinguish zero from BLANK. Keep the row-level business rule fixed.
Performance and official reading
If a persisted column already expresses the required metric, SUM is simpler. A row expression in SUMX avoids storing another column, but runtime cost depends on the engine, expression and cardinality. Nested iterators may multiply work; first check equivalent results, then profile in an actual model. The examples are Import measures or queries; iterator functions have documented restrictions in DirectQuery calculated columns and RLS. No native timings or DirectQuery tests. SUMX · AVERAGEX · COUNTX · COUNTAX · MINX · MAXX · aggregation functions