Sub topic 11
Why a summed ratio column gives the wrong answer
This chapter assumes you can write basic DAX, know the difference between row context and filter context, and have a Sales table related to a Product table in a star schema. You should also know that a calculated column is stored at refresh time and a measure is computed at query time.
The problem in one example
A common first attempt at a margin report is to add a calculated column to the Sales table:
Margin % (column) = DIVIDE(Sales[Profit], Sales[Line Amount])The column looks right. Each row shows a sensible percentage. The trouble starts when you drop it into a visual and let Power BI summarize it. Power BI will sum, average, or otherwise combine the stored row values, and none of those operations gives the true overall margin.
Consider three sales rows for one product:
| Row | Line Amount | Profit | Margin % (column) |
|---|---|---|---|
| 1 | 1,000 | 100 | 10% |
| 2 | 100 | 50 | 50% |
| 3 | 100 | 50 | 50% |
The true margin is total profit divided by total sales: 200 / 1,200, or about 16.7%. Now see what the column produces:
| Summarization | Result | Correct? |
|---|---|---|
| Sum | 110% | No. A margin above 100% is impossible here. |
| Average | 36.7% | No. It treats a 1,000 sale and a 100 sale as equal. |
| Minimum or maximum | 10% or 50% | No. It describes one row, not the total. |
The correct answer is 16.7%. No built-in summarization of the stored column produces it.
Why this happens
A ratio is a non-additive value. You cannot get the ratio of a group by combining the ratios of its members. You must first combine the numerator and the denominator separately, then divide.
A calculated column has already done its division. It did so once per row, during refresh, with row context. After that, the original numerator and denominator are not part of the column's value. A visual that aggregates the column only sees the finished percentages. It has no way to recover the weights, meaning how large each row was.
The average treats each row equally. Row 1 represents 1,000 of sales and rows 2 and 3 represent 100 each, but the average counts all three the same. Rows with small amounts distort the result as much as rows with large amounts.
The fix: divide the totals in a measure
A measure runs in filter context, so it can aggregate first and divide afterwards:
Margin % = DIVIDE(SUM(Sales[Profit]), SUM(Sales[Line Amount]))For the three rows above, SUM(Sales[Profit]) returns 200 and SUM(Sales[Line Amount]) returns 1,200. DIVIDE returns 0.1667. The same measure is also correct when the filter context is narrower or wider:
- On a single row's worth of data, it returns that row's ratio.
- For one product, it returns the product's weighted margin.
- For a category, a year, or the whole report, it recalculates from the totals visible at that level.
The measure does not store anything per row. It is evaluated for each cell of the visual, so the subtotal and grand total rows are each calculated from their own filter context rather than from the rows displayed above them.
Why DIVIDE and not the / operator
DIVIDE(numerator, denominator) returns a blank when the denominator is zero or blank. The / operator raises an error value (infinity or NaN) instead. In a visual with many filter combinations, some cells will have no sales, so DIVIDE is the safer choice. You can pass a third argument to return a different value, for example DIVIDE(a, b, 0).
Subtotals and grand totals
The most visible symptom appears in tables and matrices. Suppose a matrix shows margin by product category, with a total row at the bottom.
With the column set to Average, each category value is the unweighted mean of its rows. The total row is the unweighted mean of all rows, which generally does not equal the mean of the category values and does not equal the true overall margin. With the column set to Sum, every value is inflated and the total is a number with no business meaning.
With the measure, every row of the matrix, including the total, is the ratio of that row's own totals. The values are consistent at every level.
Total Profit = SUM(Sales[Profit])
Total Sales = SUM(Sales[Line Amount])
Margin % = DIVIDE([Total Profit], [Total Sales])This version is equivalent to the earlier measure, and it is the form most teams prefer. Each building block is a reusable measure, and the ratio references them by name.
Which values are non-additive
Ratios are the best-known case, but the same issue applies to any value where combining the parts requires more than adding:
- Percentages and rates, such as margin, discount rate, conversion rate and churn rate.
- Averages, such as average price or average order value.
- Distinct counts. The distinct customers across two regions are not the sum of each region's distinct customers, because one customer can appear in both.
- Per-unit values, such as price per kilogram, summed across rows with different quantities.
- Snapshot values such as balances or stock levels, which should not be summed across time.
For each of these, the safe pattern is the same: store or reference the additive components, and let a measure combine them in the current filter context.
Average Price = DIVIDE([Total Sales], SUM(Sales[Quantity]))
Customers = DISTINCTCOUNT(Sales[CustomerKey])A column holding a pre-computed unit price still has a legitimate role. It can be used as a slicer field or an axis, as covered in the previous chapter. Problems start only when it is aggregated as though it were additive.
Common errors
- Setting the column to Sum in the Fields pane. A numeric column gets a default summarization. A ratio column should be set to "Don't summarize", which stops Power BI from adding it automatically.
- Using Average as a fix. Average hides the problem because the number looks plausible. It is correct only when every row has the same weight.
- Writing the measure as an average of the column.
AVERAGE(Sales[Margin % (column)])is the same mistake in a measure. The aggregation is still over stored ratios. - Averaging in an iterator without weights.
AVERAGEX(Sales, DIVIDE(Sales[Profit], Sales[Line Amount]))computes the ratio per row and then averages them, so it is again unweighted. Use it only when an unweighted mean is what the business actually wants. - Dividing in the wrong place.
SUM(Sales[Profit] / Sales[Line Amount])is not valid, andSUMX(Sales, Sales[Profit] / Sales[Line Amount])is valid but again sums ratios.
When an unweighted average is the right answer
Sometimes the business question really is "what is the typical row margin?" For example, you may want the average margin across products, treating each product as one vote regardless of its sales volume. That is a legitimate question, but it should be written explicitly so the intent is clear:
Average Product Margin =
AVERAGEX(
VALUES(Product[ProductKey]),
[Margin %]
)Here VALUES produces one row per product in the current filter context. AVERAGEX evaluates [Margin %] for each product, which triggers context transition so each product's own weighted margin is used, then takes the plain mean across products. The result is an average of ratios, but by design and at the product grain.
Summary of the rule
- If the value can be added across rows and still mean something, a calculated column or a measure over it is fine.
- If the value is a ratio, rate, average, or distinct count, keep only the additive parts at row level, and compute the combined value in a measure.
- If you need the row-level ratio for slicing, bucketing, or sorting, keep it as a column, set it to "Don't summarize", and never report it as an aggregate.
Exercise
A Sales table has three rows with Line Amount of 400, 400 and 200, and Profit of 40, 80 and 100. You add a calculated column Profit % = DIVIDE(Sales[Profit], Sales[Line Amount]). (a) What does Average of that column return? (b) What does the measure DIVIDE(SUM(Sales[Profit]), SUM(Sales[Line Amount])) return? (c) Why do they differ?
Show answer
(a) The row ratios are 10%, 20% and 50%. Their average is 80% / 3, about 26.7%. (b) Total profit is 220 and total sales is 1,000, so the measure returns 22%. (c) The average gives each row the same weight, but the rows represent different amounts of sales. The measure sums the numerator and denominator first, so larger rows count for more, which gives the true overall margin.
Exercise
Your team wants a report showing the number of unique customers per region, with a correct grand total. Someone suggests adding a column to Sales that holds 1 for each customer's first purchase, and summing it. Why is a measure with DISTINCTCOUNT preferable, and what does the measure look like?
Show answer
A stored flag is fixed at refresh time and reflects one definition of "first purchase" across the whole model. It ignores the current filter context, so for a date range or a different grouping it counts customers by their first purchase overall, not within the filtered rows. A distinct count is non-additive, and the measure recomputes it for each filter context. Use: Customers = DISTINCTCOUNT(Sales[CustomerKey]).