Sub topic 8
AVERAGEX: averaging per-row or per-member results
This chapter assumes you know row context and filter context, and the iterator pattern from the previous chapters: a table, an expression, and an aggregation. It also assumes you can write simple measures with SUM and CALCULATE, and that your model is a star schema with a Sales fact table and Customer, Product, and Date dimensions. The measure [Sales Amount] is the SUMX measure from the SUMX chapter.
Why AVERAGE is not enough
AVERAGE takes one column and averages its values. That works for "average net price per sales line". It fails for "average sales per customer", because no column holds sales per customer. You first have to compute a value for each customer, then average those values.
AVERAGEX does this. It has the same shape as SUMX:
AVERAGEX ( <table>, <expression> )It visits each row of the table, evaluates the expression in that row's context, and returns the arithmetic mean of the results.
Averaging per member
The following measure averages sales across customers:
Avg Sales per Customer =
AVERAGEX (
VALUES ( Customer[CustomerKey] ),
[Sales Amount]
)VALUES ( Customer[CustomerKey] ) returns the customers that survive the current filter context. For each one, AVERAGEX creates a row context. The reference to [Sales Amount] is a measure, so it is wrapped in an implicit CALCULATE. That turns the row context into a filter on that customer (context transition). The measure therefore returns the sales of one customer at a time, and AVERAGEX averages those amounts.
Averaging per day
The same pattern works over dates:
Avg Daily Sales =
AVERAGEX (
VALUES ( 'Date'[Date] ),
[Sales Amount]
)This gives sales per day. The table argument controls what counts as "a day".
How blanks are treated
AVERAGEX ignores rows where the expression returns blank. It does not treat them as zero. Suppose a store is closed on Sundays and [Sales Amount] is blank on those days. Those Sundays are left out of both the sum and the count, so the average reflects only days with sales.
This matters when you choose the table argument:
VALUES ( 'Date'[Date] )returns the dates in the current filter context. If theDatetable has dates with no sales, those dates return blank and are skipped.- A real zero is not blank. If an expression returns 0, that row is included and pulls the average down.
If you want days without sales to count as zero, convert the blank explicitly:
Avg Daily Sales Incl Zero =
AVERAGEX (
VALUES ( 'Date'[Date] ),
[Sales Amount] + 0
)Adding 0 turns blank into 0, so every visible date is counted. Decide deliberately which definition your report needs, and name the measure so readers can tell them apart.
Common errors
- Using AVERAGE on a measure.
AVERAGEaccepts only a column. To average a measure, you need AVERAGEX. - Iterating the fact table.
AVERAGEX ( Sales, [Sales Amount] )averages the sales amount of each sales row's context, which is a different question from sales per customer. Choose the table that defines the unit you are averaging. - Forgetting that blanks are skipped. The average can look higher than expected because empty members are not counted.
- Averaging averages by accident. If the inner measure is itself a ratio, the result is the mean of ratios, not the overall ratio.
Exercise
A store sells nothing on 10 of the 30 days in a month. Avg Daily Sales uses AVERAGEX over VALUES ( 'Date'[Date] ) and the month's total sales are 6,000, spread over the 20 days with sales. What does the measure return for that month, and what would Avg Daily Sales Incl Zero return?
Show answer
Avg Daily Sales returns 300 (6,000 / 20), because the 10 blank days are skipped. Avg Daily Sales Incl Zero returns 200 (6,000 / 30), because adding 0 turns each blank into a counted zero.