Sub topic 3
Ratios and margins: why aggregation order matters
This chapter assumes you can build a pivot table, add a calculated field, and read a Sheets formula. You should already know the difference between a row-level formula and a sum of a column.
Ratios and margins: why aggregation order matters
A ratio such as margin or revenue per unit must be calculated from totals. Averaging row-level ratios gives a different answer.
Use this sample data, with columns Date, Region, Product, Units, Revenue, and Cost, across five rows. Total revenue is 1,550 and total units is 36, so revenue per unit is 1,550 / 36 = 43.06. Adding the five per-row revenue-per-unit values gives 25 + 25 + 60 + 60 + 50 = 220, which is wrong.
To get the correct result in a pivot table, add a calculated field and set its summarize option to Custom:
=SUM(Revenue) / SUM(Units)The Custom option aggregates each column first, then divides.
Show values as a percentage of a total
The Show as option on a value converts results into shares of a row total, column total, or grand total. Use it to see each group's share of the whole. Check that the grand total row matches your expected overall figure before you trust the percentages.
Use groups and calculated fields together
Combine the two features by first grouping items, then adding a calculated field. For example, group the regions into two sets, then add a margin field. The calculated field works on each group's totals, so the group's margin is computed correctly.
::: Note: Recalculate the grand total after each change. A correct group total and a correct grand total together confirm that the formula aggregates as intended.
Limitations and troubleshooting
- Dates stored as text do not group by date. Convert them to real dates first.
- Blank cells in a Units or Revenue column can make denominators smaller than expected. Fill or exclude them before you summarize.
- A per-row sum of ratios almost never equals the ratio of totals. If the totals look wrong, switch the field to Custom.
Exercise
Exercise
Using the sample data, what is the overall margin, (Revenue - Cost) / Revenue, to one decimal place?
Show answer
Total revenue is 1,550 and total cost is 980, so profit is 570. The margin is 570 / 1,550 = 36.8%.