Sub topic 11
Why a summed ratio column gives the wrong answer
This chapter assumes you know Power BI Desktop's Report and Model views, can read basic DAX such as SUM and IF, and understand that a calculated column is evaluated row by row while a measure is evaluated in the current filter context. By the end, you can explain why a ratio stored in a column gives wrong totals and replace it with a measure.
The problem: a ratio is not additive
A ratio is not additive. Adding two ratios, or averaging them, does not give the ratio of the underlying totals. A calculated column stores one value per row, so a ratio column holds many small ratios. When a visual combines them, it must sum, average, or pick one of them. None of these gives the true overall ratio.
Here is a row-level margin stored as a calculated column:
Margin % (column) = DIVIDE(Sales[Profit], Sales[Line Amount])Take three sales rows.
| Row | Line Amount | Profit | Margin % (column) |
|---|---|---|---|
| 1 | 1000 | 100 | 10% |
| 2 | 100 | 50 | 50% |
| 3 | 100 | 50 | 50% |
The true overall margin is total profit divided by total sales: 200 / 1200, which is 16.7%.
- Summing the column gives 110%, which is impossible for a margin.
- Averaging the column gives 36.7%. Each row counts equally, so the two small rows outweigh the large one.
Both results are wrong because the column was computed before any visual existed. It knows nothing about which rows a visual will group together.
The fix: divide the totals in a measure
A measure divides the totals for whatever rows the current filter context leaves visible:
Margin % = DIVIDE(SUM(Sales[Profit]), SUM(Sales[Line Amount]))The numerator and denominator are each aggregated first, then divided. This happens separately for every cell in a visual. A single product, a category and the grand total each get the correct ratio for their own rows.
Why the measure works
The measure is evaluated at query time, once per visual cell, using that cell's filter context. The column is evaluated at refresh time using row context, so its result is fixed before any filtering happens. The measure can adapt to the grouping a user chooses. The column cannot.
A general rule
The same problem affects any value that depends on a combination of totals:
- Percentages and margins.
- Averages of averages, such as average price per unit.
- Distinct counts and other values that do not add up across groups.
For these, store the additive parts (profit, sales, quantity) as columns or source data, and compute the combined value in a measure. A row-level ratio column is only useful when you need the ratio itself as a field to slice or sort by. Even then, do not aggregate it.
Exercise
A Sales table has two rows. Row A has Line Amount 400 and Profit 40. Row B has Line Amount 100 and Profit 40. What is the correct overall margin, and what would you get by averaging a row-level Margin % column? Write the measure that returns the correct value.
Show answer
Total profit is 80 and total sales is 500, so the correct margin is 16%. The row margins are 10% and 40%, and their average is 25%, which is wrong. The correct measure is Margin % = DIVIDE(SUM(Sales[Profit]), SUM(Sales[Line Amount])).