Sub topic 8
AVERAGEX: averaging per-row or per-member results
This chapter assumes you can write measures with SUM and CALCULATE, and that you know the row context and filter context ideas from the earlier chapters. It also assumes you have read the SUMX chapters, because AVERAGEX follows the same pattern: a table, an expression, and an aggregation.
What AVERAGEX does
AVERAGEX is the iterator version of AVERAGE. It takes two arguments:
AVERAGEX ( <table>, <expression> )It works in three steps:
- Evaluate the table argument in the current filter context.
- Evaluate the expression once for each row of that table, in a row context.
- Take the arithmetic mean of the non-blank results.
AVERAGE is a shorthand for a special case. AVERAGE ( Sales[Net Price] ) means the same as AVERAGEX ( Sales, Sales[Net Price] ). AVERAGEX becomes useful when the thing you want to average does not exist as a column.
Averaging a per-row result
Suppose you want the average value of a sales line. The Sales table stores quantity and net price, but not the line amount, so AVERAGE has no column to use.
Avg Line Amount =
AVERAGEX (
Sales,
Sales[Quantity] * Sales[Net Price]
)Each Sales row in the current filter context produces one number, and AVERAGEX returns the mean of those numbers. If a report slices by Year, each year shows the average line amount for that year's rows.
This is a simple average. It gives every line the same weight, whatever its quantity. If you want the average price per unit sold, which weights each price by its quantity, divide two sums instead:
Weighted Avg Price =
DIVIDE (
SUMX ( Sales, Sales[Quantity] * Sales[Net Price] ),
SUM ( Sales[Quantity] )
)AVERAGE ( Sales[Net Price] ) and Weighted Avg Price return different numbers whenever quantities vary. Decide which one the business question needs before you write the measure.
Averaging per member: customers, days, months
Business averages are often per member of a dimension, such as sales per customer or sales per day. No column holds that value. You build it by iterating over the members and evaluating a measure for each one:
Avg Sales per Customer =
AVERAGEX (
VALUES ( Customer[CustomerKey] ),
[Sales Amount]
)VALUES ( Customer[CustomerKey] ) returns the customer keys visible in the current filter context. AVERAGEX loops over those keys. For each key, the row context turns into a filter context through context transition, so [Sales Amount] is computed for that one customer. The result is the mean of those per-customer totals.
The same pattern gives a daily average:
Avg Daily Sales =
AVERAGEX (
VALUES ( 'Date'[Date] ),
[Sales Amount]
)The table argument sets the grain of the average. Iterate over dates and you get a per-day average. Iterate over a 'Date'[Year Month] column, assuming your Date table has one, and you get a per-month average. Iterate over product names and you get sales per product.
The average is not the average of the visible numbers
In a matrix with Year on rows, each cell computes AVERAGEX in its own filter context. The grand total row computes AVERAGEX over all customers in the total context. It does not average the yearly averages. Those two numbers are usually different, and the total is the one that answers "what is the average sales per customer overall".
How AVERAGEX treats blanks
AVERAGEX ignores rows where the expression returns blank. A skipped row is removed from both the sum and the count. Zero is a real value and is counted.
A small example shows the effect. Three customers are visible in the filter context:
| Customer | Sales Amount |
|---|---|
| Anna | 100 |
| Ben | 300 |
| Carla | blank (no sales in the period) |
Avg Sales per Customer returns (100 + 300) / 2 = 200. Carla is not counted, so the measure answers "average sales per customer who bought something".
If you want to count customers who did not buy, force the blank to zero:
Avg Sales per Customer incl. Zero =
AVERAGEX (
VALUES ( Customer[CustomerKey] ),
[Sales Amount] + 0
)Now Carla contributes 0 and the result is 400 / 3 ≈ 133.33. Neither version is wrong. They answer different questions, so choose on purpose and name the measure to match.
Why customers without sales appear at all
Filters flow from dimension tables to the fact table, not the other way. A filter on Date reduces the Sales rows, but it does not remove customers from the Customer table. VALUES ( Customer[CustomerKey] ) therefore still lists customers who bought nothing in the selected year. Their [Sales Amount] is blank, so the default version skips them. With + 0, they would all be counted.
The + 0 trick needs care on a Date table. A Date table often covers future dates. AVERAGEX ( VALUES ( 'Date'[Date] ), [Sales Amount] + 0 ) would include every future day as a zero and pull the average down. Without the + 0, the default Avg Daily Sales averages only days that had sales. That is often what you want, but it is not the average over calendar days. State which one a measure returns.
Choosing the table argument
The table argument decides which members are averaged. The common choices behave like this:
| Table argument | Members averaged | Typical use |
|---|---|---|
| `Sales` | Every fact row in the filter context | Average line value |
| `VALUES ( Customer[CustomerKey] )` | Customers visible in the filter context | Average per customer, responding to filters |
| `ALL ( Customer[CustomerKey] )` | Every customer, ignoring filters on that column | Fixed denominator across the report |
| `ALLSELECTED ( Customer[CustomerKey] )` | Customers allowed by slicers, ignoring the visual's own grouping | Average relative to what the user selected |
| `FILTER ( VALUES ( Customer[CustomerKey] ), [Sales Amount] > 1000 )` | Only the members that pass a test | Average among large customers |
Iterate over a column with VALUES rather than over the whole dimension table when you only need one key. Iterating over the key column gives one row per member. Iterating over a whole dimension table with duplicated attributes can be slower and harder to reason about.
A common mistake: iterating over the fact table with a measure
This looks similar to the customer average, but it is wrong:
Avg Sales per Row (misleading) =
AVERAGEX (
Sales,
[Sales Amount]
)Here each Sales row becomes a filter context through context transition. The measure is evaluated once per fact row, which is expensive. It also returns the average sales line, not the average customer. If you want a per-member average, iterate over the members and not over the fact table.
Reading the result
When an AVERAGEX result surprises you, check these in order:
- Which table is being iterated, and at what grain?
- Is the filter context what you expect in that cell, including the total row?
- Does the expression return blank for some members? If so, those members are skipped.
- Does the expression return zero for some members? If so, they are counted.
Performance in brief
AVERAGEX runs the expression once per row. Over a few thousand members, that is cheap. Over millions of fact rows with a measure inside, the work grows quickly because each context transition is a separate evaluation. Prefer a column or key with fewer distinct values as the table argument. Use AVERAGE when a single column is enough.
Exercise
A report shows Year on rows. Customers Dana (200 in sales) and Eli (600 in sales) bought in 2025. Fay is in the Customer table but made no purchase in 2025. Avg Sales per Customer iterates over VALUES ( Customer[CustomerKey] ) and evaluates [Sales Amount]. What does the 2025 row show? What would it show if the expression were [Sales Amount] + 0? Which version would you use for "average sales per active customer"?
Show answer
The default measure shows (200 + 600) / 2 = 400, because Fay's blank result is skipped. With + 0, Fay counts as 0 and the result is 800 / 3 ≈ 266.67. For "average sales per active customer" use the default version without + 0.
Exercise
Write an AVERAGEX measure for average sales per month, assuming the Date table has a 'Date'[Year Month] column. Then explain why the grand total shows a different number from the average of the yearly values.
Show answer
Avg Monthly Sales = AVERAGEX ( VALUES ( 'Date'[Year Month] ), [Sales Amount] ). The grand total evaluates the measure once in the total context, averaging over all months at once. A yearly value averages only that year's months, so averaging the yearly values weights each year equally even if the years have different numbers of months with sales.