Sheet Clinic

Calculated field in a Google Sheets pivot table: add one for margin %

Add a calculated field to a Google Sheets pivot table: Values, Add, Calculated Field, =(Revenue-Cost)/Revenue, keep SUM. Why summed Margin % hits 405%

Sheet Clinic editorsPublished
J1=(Revenue-Cost)/Revenue

Before SUM of Margin %

GHIJ
1RegionSUM of RevenueSUM of CostSUM of Margin %
2East10000790095.00%
3North93007000115.00%
4South87006300100.00%
5West9900780095.00%
6Grand Total3790029000405.00%

What changed

  1. SUM of Margin % removed from Values
  2. Calculated field Profit margin added

After calculated field

GHIJ
1RegionSUM of RevenueSUM of CostProfit margin
2East10000790021.00%
3North9300700024.73%
4South8700630027.59%
5West9900780021.21%
6Grand Total379002900023.48%
Figure 1. The pivot table in G1:J6 before and after. Revenue and Cost are the same on both sides. Only column J changed, from added-up row percentages to profit divided by revenue for each region.

To add a calculated field to a pivot table in Google Sheets, open the Pivot table editor, click Add next to Values and pick Calculated Field. Type a formula that uses your column headers, such as =(Revenue-Cost)/Revenue, and leave Summarize by on SUM.

North shows a margin of 115.00% in the pivot table on the Broken tab. South is at 100.00%, and the Grand Total row says 405.00%. North brought in 9300 and spent 7000, so a margin above 100% would mean the costs were negative. A pivot table prints numbers like these when a percent column from the source data sits under Values, because it adds the row percentages together. Margin needs a calculated field instead, a value column the pivot table works out from the Revenue and Cost columns. It is added in the Pivot table editor: next to Values, click Add, then Calculated Field.

On the Fixed tab that field is named Profit margin and holds =(Revenue-Cost)/Revenue, with Summarize by left on SUM. It took the place of SUM of Margin % in column J, and North now reads 24.73%.

The source is a small sales list in A1:E13. Each of the twelve rows has a rep, a region, Revenue in column C and Cost in column D. Column E, Margin %, works out each sale on its own with =(C2-D2)/C2, and those row margins are correct.

The numbers go wrong once column E is dragged into the Values section of the pivot table. Sheets put it in as SUM of Margin %, so for North it added Aisha's 20.00%, Priya's 75.00% and Tom's 20.00% and showed 115.00%. The Grand Total cell adds all twelve row margins. Priya's 75.00% came from an 800 sale and Tom's 20.00% from a 6000 sale, and the sum treats them as if they were the same size.

The Broken tab with J3 selected. The formula bar shows 115.00%. Columns A to E hold Rep, Region, Revenue, Cost and Margin %. The pivot table in G1:J6 lists East 95.00%, North 115.00%, South 100.00%, West 95.00% and Grand Total 405.00% under SUM of Margin %.
Figure 2. The Broken tab. J3 is the North row of SUM of Margin %.

Adding a calculated field for margin % in the Pivot table editor

Google's help page on pivot tables covers this under adding calculated fields with SUM or a custom formula. These are the steps as they went on the Fixed tab.

  1. Click any cell in the pivot table. If the Pivot table editor panel does not open on the right, click the Edit button that appears under the table.
  2. Next to Values, click Add. The list opens with Calculated Field at the top, above the source columns Rep, Region, Revenue, Cost and Margin %.
  3. A dialog titled Add calculated field opens. Its name box is prefilled with Calculated Field 1. Replace that with Profit margin, which becomes the column header in J1.
  4. In the Formula box, type =(Revenue-Cost)/Revenue. The Columns tab on the right lists the source headers by name, which is how the formula refers to them, so with other data the two names are whichever revenue and cost headers that tab shows.
  5. Wait for the green check in the lower right corner of the formula box. Google's page says a red exclamation mark in its place means the syntax is not valid. Leave Summarize by on SUM and click Add.
  6. Back in the editor, find the SUM of Margin % card under Values and click the X on it to take the old column out.

The new column filled J2:J6 straight away, but as long decimals. Selecting J2:J6 and clicking Format as percent on the toolbar turned them into the two-decimal percentages in the figures here.

The Fixed tab with J3 selected. The formula bar shows 24.73%. The pivot table in G1:J6 has the header Profit margin in J1 and lists East 21.00%, North 24.73%, South 27.59%, West 21.21% and Grand Total 23.48%.
Figure 3. The Fixed tab. Revenue and Cost did not move, and J now holds Profit margin.

Why a calculated field on Summarize by SUM divides the totals instead of adding percentages

Column E and the calculated field use the same arithmetic, revenue minus cost, divided by revenue. Column E applies it to each sale, and the pivot then adds the answers. With Summarize by left on SUM, the calculated field gave a different result: in this copy, each region's cell came out as that region's summed Revenue minus summed Cost, divided by summed Revenue.

The check column on the Fixed tab confirms it row by row. L2 holds =ARRAYFORMULA((H2:H6-I2:I6)/H2:H6), which reads the totals already in H and I, and L2:L6 matches J2:J6 exactly: 21.00%, 24.73%, 27.59%, 21.21%, 23.48%. The Grand Total row follows the same rule, (37900 - 29000) / 37900.

Both columns divide and add, in opposite order. Adding first and dividing second gives margin on the money. Dividing first and adding second gives a sum of ratios, which is what the SUM of Margin % column on the Broken tab shows.

SUM or AVERAGE of a percent column against a calculated field, on the same rows

The take-home copy runs the same twelve rows through six setups. North and the Grand Total are enough to see where they split:

How the margin was addedColumnNorthGrand Total
SUM of Margin % (column E in Values)J of Broken115.00%405.00%
AVERAGE of Margin % (column E in Values)K of Other results38.33%33.75%
Calculated field =(Revenue-Cost)/Revenue, Summarize by SUMJ of Fixed24.73%23.48%
Calculated field =(SUM(Revenue)-SUM(Cost))/SUM(Revenue), Summarize by CustomL of Other results24.73%23.48%
Calculated field =(Revenue-Cost)/Revenue, Summarize by CustomM of Other results20.00%25.00%
Formula next to the pivot, =ARRAYFORMULA((H2:H6-I2:I6)/H2:H6)L of Fixed24.73%23.48%

Three setups land on 24.73% for North, the figure from the Fixed tab, and three don't.

AVERAGE looks closer, which makes it harder to catch. It gives each sale an equal vote, so Priya's small sale counts as much as Tom's large one, and North averages 38.33% even though most of its money came from Tom's low-margin sale. The Grand Total under AVERAGE is 33.75%, the plain average of the twelve values in column E, against a real 23.48%.

Column E is still useful for reading one sale at a time. It is only inside the pivot table, under SUM or AVERAGE, that it gives North 115.00% or 38.33% instead of 24.73%.

The Custom result without SUM is the strangest. Column M of Other results shows 25.00% for East, 20.00% for North, 30.00% for South and 15.00% for West. Those are the row margins of Dana, Aisha, Ken and Emily, the first source row of each region, and Grand Total shows Dana's 25.00% again. The formula box still said the formula was valid, and no error appeared anywhere.

The Other results tab with M2 selected. The pivot table in G1:M6 shows Profit margin in J (21.00%, 24.73%, 27.59%, 21.21%, 23.48%), AVERAGE of Margin % in K (31.67%, 38.33%, 33.33%, 31.67%, 33.75%), SUM() inside, Custom in L (21.00%, 24.73%, 27.59%, 21.21%, 23.48%) and No SUM, Custom in M (25.00%, 20.00%, 30.00%, 15.00%, 25.00%).
Figure 4. Other results. J and L agree. K averages the row margins, and M repeats the first row of each region.

Summarize by Custom needs SUM() around every column name

Google's example of a custom formula is =sum(Price)/counta(Product), with Summarize by set to Custom. Every column name in that example sits inside a function, SUM or COUNTA, and that turns out to matter.

Column L of Other results uses =(SUM(Revenue)-SUM(Cost))/SUM(Revenue) with Summarize by Custom. Every row matches the Fixed tab, North at 24.73% and Grand Total at 23.48%. If you need something SUM cannot express, such as average order size with COUNTA, Custom is the mode for it.

Column M has the short formula, =(Revenue-Cost)/Revenue, also on Custom. That is the setup behind the 20.00% for North in the comparison table: one row's margin per region, picked by position in the source. Nothing on screen marks it as wrong, so if you switch a field to Custom, check that every column name in the formula sits inside a function.

Google's page carries one more instruction for custom formulas, in its own terms: field values with spaces get quotations around them. Every header in this copy is a single word, so that case was not tested here.

A check formula beside the pivot table, and the Try it yourself tab

Try it yourself is the Broken tab again, with SUM of Margin % in column J and an empty column L under the header Check: (H-I)/H. Type =ARRAYFORMULA((H2:H6-I2:I6)/H2:H6) into L2. L2:L6 fills with 21.00%, 24.73%, 27.59%, 21.21% and 23.48%, next to the 95.00% and 115.00% in J. The one formula fills all five cells because it is wrapped in ARRAYFORMULA, a function the post on ARRAYFORMULA down a whole column goes through.

Then follow the calculated field steps above in that tab's pivot table: add Profit margin as a calculated field and remove SUM of Margin %. Column J should end up equal to column L and to J2:J6 on the Fixed tab.

The check formula is handy on its own, but it is tied to fixed cells. It reads H2:H6 and I2:I6, so a fifth region would push the Grand Total row below its range. The calculated field is a column of the pivot table itself, so there is no range to keep in step.

Input12 sales, Revenue in C2:C13, Cost in D2:D13; rows East, North, South, West, Grand Total
Formula=(Revenue-Cost)/Revenue (calculated field, Summarize by SUM)
Expected21.00%, 24.73%, 27.59%, 21.21%, 23.48%
Sheets returned21.00%, 24.73%, 27.59%, 21.21%, 23.48% Matches expected
Checked2026-11-11, free personal account

Take-home copy. Four tabs: Broken (as you see it), Fixed, Try it yourself, Other results. It opens as a copy in your own Drive.

Copy the sheet