Sub topic 9
Ratios and margins: why aggregation order matters
A ratio such as revenue per unit or profit margin depends on when you divide. If you divide row by row and then add the results, the pivot table shows a wrong number. If you add the columns first and then divide, it shows the right one. This chapter shows how to choose the right order in a Google Sheets calculated field and how to check the result.
What this chapter assumes
- You can create a pivot table and place fields in Rows, Columns and Values.
- You know how to add a calculated field with a formula such as
=Revenue - Cost. - You know that a Values field has a "Summarize by" setting (SUM, COUNT, AVERAGE and others).
- You have the five-row sample table from the earlier chapters in a sheet, with Date in column A through Cost in column F.
Date Region Product Units Revenue Cost
2026-01-05 West Widget 10 250 150
2026-01-19 East Widget 4 100 60
2026-02-02 West Gadget 7 420 245
2026-02-23 South Gadget 3 180 105
2026-03-09 East Gizmo 12 600 420The problem: a ratio that looks fine and is wrong
Suppose you add a calculated field named RevPerUnit with this formula and leave "Summarize by" at SUM.
=Revenue / UnitsPut Region in Rows and RevPerUnit in Values. The West row shows 85. West sold 17 units for 670 in revenue, so its revenue per unit is about 39.41. The 85 is not a price, and it is larger than any single price in the data.
The cause is the order of operations:
- For each source row, the pivot evaluates
Revenue / Units. West has two rows: 250 / 10 = 25 and 420 / 7 = 60. - The pivot then adds those per-row results for the group: 25 + 60 = 85.
Adding ratios has no business meaning. The grand total row has the same flaw: it adds all five row ratios (25 + 25 + 60 + 60 + 50) and shows 220.
Two orders of operation
The top path is what a calculated field does with the default SUM summary. The bottom path is what you want for a ratio. You get it by switching the field to a custom summary.
How the default SUM behaves
With "Summarize by" set to SUM, a calculated field evaluates its formula once for each source row and then adds the results for each pivot cell. This is correct for formulas that are additive:
=Revenue - Costgives Profit per row. Summing profit per row equals total revenue minus total cost.=Units * 2is a scaled copy of a column, and its sum scales the same way.
The test is whether the formula distributes over addition. For a sum of terms, f(a + b) = f(a) + f(b). Subtraction and multiplication by a constant pass the test. Division by another column does not. Revenue per unit, margin percentage, conversion rate and average order value all fail.
Aggregate first with a custom summary
For a ratio, add the calculated field and then set "Summarize by" to Custom. In Custom mode, the formula uses aggregate functions over the source columns.
=SUM(Revenue) / SUM(Units)The pivot now calculates the totals for each group's rows first and divides afterwards. The West row shows 670 / 17 = 39.41. The grand total row applies the same formula to all rows: 1550 / 36 = 43.06.
The steps in the editor are:
- In the pivot editor, next to Values, choose Add, then Calculated field.
- Enter the formula, using aggregate functions such as
SUM(Revenue) / SUM(Units). - Open the field's "Summarize by" list and choose Custom.
- Check the grand total against a value you can compute by hand.
If you leave the summary at SUM and the formula contains SUM(...) calls, the result is not reliable. Treat the summary setting and the formula as a pair, and change them together.
Results for the sample data
The table compares the two methods by region and for the grand total. "Average of ratios" is a third option, explained in the next section.
| Group | Units | Revenue | Sum of row ratios | Average of row ratios | SUM(Revenue)/SUM(Units) |
|---|---|---|---|---|---|
| West | 17 | 670 | 85.00 | 42.50 | 39.41 |
| East | 16 | 700 | 75.00 | 37.50 | 43.75 |
| South | 3 | 180 | 60.00 | 60.00 | 60.00 |
| Grand total | 36 | 1550 | 220.00 | 44.00 | 43.06 |
Two details stand out. South has one source row, so all three methods agree, which is why a test on a tiny group can hide the problem. West and East swap order: the sum of ratios ranks West higher, while the correct price per unit ranks East higher. A wrong aggregation order can reverse a business conclusion.
Margins work the same way
Profit margin is profit divided by revenue. Avoid the per-row version.
=(Revenue - Cost) / RevenueWith the default SUM, the grand total adds the five row margins (0.40 + 0.40 + 0.4167 + 0.4167 + 0.30) and shows 1.9333, or 193.3%. A margin over 100% should make you stop and look.
Use an aggregate-first formula with Custom summarizing:
=(SUM(Revenue) - SUM(Cost)) / SUM(Revenue)The total is (1550 - 980) / 1550 = 570 / 1550 = 0.3677. Format the field as a percentage in the sheet cells, or use the number format options, to read it as 36.8%.
Write the formula from source columns instead of referring to another calculated field such as Profit. A formula that depends on another calculated field is a common source of errors. Rebuilding the logic from the source columns is slightly longer but easier to audit. If you need a reusable profit column, add it to the source table as a normal column and refer to that.
An average of ratios is a different measure
The AVERAGE summary of a per-row ratio gives the unweighted mean of the row ratios. For West that is (25 + 60) / 2 = 42.5, compared with the correct 39.41. Each source row counts equally, whether it sold 3 units or 3,000.
Use this measure only when you want it: for example, "the typical ratio per transaction." For a business price or margin you almost always want the weighted version, where larger rows count more. The weighted version is the aggregate-first formula.
| Question | Formula | Summarize by |
|---|---|---|
| Total profit | `=Revenue - Cost` | SUM |
| Revenue per unit across all sales | `=SUM(Revenue) / SUM(Units)` | Custom |
| Margin of a group | `=(SUM(Revenue) - SUM(Cost)) / SUM(Revenue)` | Custom |
| Typical per-row ratio | `=Revenue / Units` | AVERAGE |
Verify a result outside the pivot
Do not trust a ratio until one value matches an independent calculation. Using the sample data in A1:F6 (header in row 1), put this in a spare cell to check the West region:
=SUMIFS(E2:E6, B2:B6, "West") / SUMIFS(D2:D6, B2:B6, "West")It returns 39.41, which matches the custom pivot value. To check the grand total, use =SUM(E2:E6)/SUM(D2:D6), which returns 43.06.
Run this check after any change to groups, because the pivot recomputes the ratio for each new group.
Ratios and groups
When you combine a custom ratio with a manual, date or numeric group, the custom formula runs once for each group, using all source rows in that group. For example, group Regions into "Coastal" (West, South) and "Inland" (East). Coastal then shows 850 / 20 = 42.50, which is not the average of its member ratios 39.41 and 60.00 (49.71). The aggregate-first formula handles the merged group correctly, and the default SUM does not. If you group dates by month, each month's ratio uses only that month's rows in the same way.
Common errors
- A ratio far above any real value. Almost always the default SUM added the per-row ratios. Switch to Custom and use aggregate functions.
- A margin above 100%. Same cause as above. Compare the grand total with total profit divided by total revenue.
- A
#DIV/0!or blank in a group. A group whose denominator is zero, such as no units sold. A formula like=IFERROR(SUM(Revenue)/SUM(Units), 0)can mask this, but it also hides real problems, so use it only when zero is the right answer to show. - Text instead of numbers. Revenue values stored as text, such as
"250"with a leading apostrophe, are ignored bySUM. Clean the source column first. - A header with spaces. Wrap the name in single quotes:
='Unit Price' * Quantity. In a custom formula:=SUM('Unit Price') / SUM(Quantity). - Rows added below the range. If the pivot's data range is fixed, new rows are missing from both the totals and the ratio. Extend the range or use whole-column references.
Decision rule
Ask whether the quantity you want could be written as a total. A total (profit, revenue, units) is additive, so use a per-row formula with SUM. A rate (price, margin, share, conversion) is a total divided by a total, so write the numerator and denominator as aggregates and use Custom. Use AVERAGE of a per-row ratio only when equal weight per row is the stated goal.
Exercise
Using the sample data, the East region has two rows: 4 units for 100 revenue and 12 units for 600 revenue. (a) What does a SUM-summarized calculated field =Revenue / Units show for East? (b) What does the custom formula =SUM(Revenue) / SUM(Units) show? (c) Which one is the revenue per unit that East actually earned?
Show answer
(a) 100/4 = 25 and 600/12 = 50, so the SUM summary shows 25 + 50 = 75. (b) Total revenue is 700 and total units are 16, so the custom formula shows 700 / 16 = 43.75. (c) 43.75 is the real revenue per unit, because it weights each row by its units. The value 75 is a sum of ratios and has no meaning.
Exercise
Write the custom calculated field formula for the profit margin of any pivot group, and state what it returns for the Gadget product (rows: revenue 420, cost 245; revenue 180, cost 105). Also say which "Summarize by" setting the field needs.
Show answer
The formula is =(SUM(Revenue) - SUM(Cost)) / SUM(Revenue), with "Summarize by" set to Custom. For Gadget, revenue is 600 and cost is 350, so the margin is (600 - 350) / 600 = 250 / 600 = 0.4167, or about 41.7%.
Exercise
A colleague's pivot shows a grand total margin of 193%. Give the most likely cause, the fix, and one check that confirms the fix.
Show answer
The calculated field =(Revenue - Cost)/Revenue is summarized by SUM, so it adds the five per-row margins. Change the formula to =(SUM(Revenue) - SUM(Cost)) / SUM(Revenue) and set "Summarize by" to Custom. Confirm by computing (SUM(E2:E6) - SUM(F2:F6)) / SUM(E2:E6) in a spare cell, which gives 0.3677 and should match the pivot's grand total.