Sub topic 8
Write calculated field formulas
This chapter covers the formula language inside a calculated field. It explains how to name columns, which operators you can use, and when Google Sheets evaluates the formula (per row, or after aggregation). The earlier chapter showed where the calculated field menu is. Here you write the formulas.
Prerequisites
This chapter assumes you can do the following:
- Build a basic pivot table with rows, columns, values and filters.
- Write ordinary cell formulas such as
=B2-C2and=SUM(B2:B6). - Read the "Summarize by" setting on a value (SUM, COUNT, AVERAGE).
The running example is the sales table from earlier in the tutorial. It has the columns Date, Region, Product, Units, Revenue and Cost.
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 420What a calculated field formula is
A calculated field is a value you add to the pivot table that does not exist in the source range. You create it in the pivot editor: next to Values, choose Add, then Calculated field. You get a formula box that starts with =.
A calculated field formula differs from a cell formula in three ways:
- It refers to source columns by their header text, not by cell address.
=Revenue - Costis valid, and=D2 - E2is not. - It sees one source row at a time (by default), not a range.
- It lives inside the pivot table definition. Nothing is written to your source data, so you can't see the formula in a cell on the sheet.
The first example is a profit field:
=Revenue - CostWhen you add it to Values, each pivot cell shows the profit for that row or column group. In our data, the West region shows 275 (that is 100 from the Widget row plus 175 from the Gadget row).
Refer to columns by name
The name in the formula must match the header text in the source range. Three rules cover most cases.
- Single-word headers are written as they are:
Revenue,Cost,Units. - Headers with spaces go inside single quotes. If your header is
Unit Price, write the following:
='Unit Price' * Quantity- Renaming a header breaks the formula. If you change
CosttoUnit Costin the source, a formula that saysRevenue - Costno longer finds its column. Edit the field and update the name.
The same quoting rule applies to other awkward headers. If the header has a space, a hyphen or other punctuation, put it in single quotes. Short, one-word headers avoid the problem entirely. That is one more reason to prepare your source headers carefully.
A calculated field has its own display name, which is separate from the formula. Name it something clear, such as Profit, because that name becomes the column title in the pivot table.
Operators and functions
You can use the usual arithmetic operators in a calculated field:
| Operator | Meaning | Example |
|---|---|---|
| `+` | Add | `=Revenue + Tax` |
| `-` | Subtract | `=Revenue - Cost` |
| `*` | Multiply | `=Units * 2` |
| `/` | Divide | `=Revenue / Units` |
| `^` | Power | `=Units ^ 2` |
| `( )` | Control order | `=(Revenue - Cost) / Revenue` |
You can also put numbers in the formula, so =Revenue * 0.2 works for a fixed 20 percent.
Some single-row functions work too. IF is the most useful. A division by zero otherwise produces an error that spreads into your totals, so guard against it:
=IF(Units = 0, 0, Revenue / Units)Function support in the calculated field box is narrower than in a normal cell. You can't use cell references, ranges such as A2:A10, or functions that look up other parts of the sheet. If the editor rejects a function, that function is not available there. Move the logic into a helper column in the source data instead.
Evaluate per row, then sum
This is the most important rule in the chapter. By default, a calculated field is evaluated once for every source row. Google Sheets then sums those results for each pivot group. The default behavior is the field's "Summarize by" setting, which is SUM.
The sequence looks like this:
Because that line must be a proper diagram, here it is in the required form.
For addition and subtraction the two orders give the same answer. For profit:
- Per row:
(250-150) + (420-245) = 100 + 175 = 275for West. - Totals first:
(250+420) - (150+245) = 670 - 395 = 275.
For ratios the two orders give different answers. That is the next section.
Why ratios break
Suppose you want a profit margin, which is profit divided by revenue. You add a calculated field named Margin:
=(Revenue - Cost) / RevenueWith the default SUM setting, Sheets computes a margin for each source row and adds the margins up. Here are the per-row margins:
| Row | Region | Revenue | Cost | Row margin |
|---|---|---|---|---|
| 1 | West | 250 | 150 | 0.400 |
| 2 | East | 100 | 60 | 0.400 |
| 3 | West | 420 | 245 | 0.417 |
| 4 | South | 180 | 105 | 0.417 |
| 5 | East | 600 | 420 | 0.300 |
Compare the pivot result with the true margin for each region:
| Region | Default SUM result | Correct margin |
|---|---|---|
| West | 0.817 (81.7%) | 275 / 670 = 41.0% |
| East | 0.700 (70.0%) | 220 / 700 = 31.4% |
| South | 0.417 (41.7%) | 75 / 180 = 41.7% |
| Grand total | 1.933 (193.3%) | 570 / 1550 = 36.8% |
South looks right only because it has a single source row. Every region with two or more rows is wrong. A margin over 100 percent is the clearest warning sign.
Fix ratios with Custom aggregation
In the calculated field's settings, change Summarize by from SUM to Custom. A custom formula can use aggregate functions such as SUM, so you can add each column up for the group first and divide afterwards.
For revenue per unit:
=SUM(Revenue) / SUM(Units)For margin:
=(SUM(Revenue) - SUM(Cost)) / SUM(Revenue)Each group now divides its own total revenue by its own total units. The results for the sample data are:
| Region | Revenue | Units | Revenue per unit (per-row sum) | Revenue per unit (custom) |
|---|---|---|---|---|
| West | 670 | 17 | 25 + 60 = 85.0 | 39.4 |
| East | 700 | 16 | 25 + 50 = 75.0 | 43.8 |
| South | 180 | 3 | 60.0 | 60.0 |
The grand total row is also correct, because Sheets applies the formula to the grand totals instead of adding up the rows above. The grand total revenue per unit is 1550 / 36 = 43.1.
Format the field as a percentage in the cell format menu (Format, Number, Percent) if it is a margin. The formula returns a decimal, so 0.41 displays as 41%.
Choosing between the two modes
| Question | Use default SUM | Use Custom |
|---|---|---|
| Is the result a ratio, average or rate? | No | Yes |
| Is the result an amount like profit or tax? | Yes | Optional |
| Do you need `IF` on individual rows? | Yes | Awkward, because the formula sees totals |
| Must the grand total be correct? | Only for additive amounts | Yes |
A related point is that IF in a Custom formula tests the group totals, not individual rows. If you need a row-level condition, such as "only count rows where Units is above 5", make a helper column in the source data and aggregate that column.
A worked example from start to finish
You want one pivot with Region as rows, plus Profit and Margin.
- Create the pivot from the source range and add Region to Rows.
- Add
RevenueandCostto Values as plain SUM fields. You will use them to check the results. - Add a calculated field named
Profitwith=Revenue - Cost. Leave it on SUM. - Add a second calculated field named
Margin. Enter=(SUM(Revenue) - SUM(Cost)) / SUM(Revenue), then set Summarize by to Custom if it is not already. - Format
Marginas a percentage. - Check the totals: Profit must equal Revenue minus Cost in every row (West 275, East 220, South 75, total 570), and Margin must match profit divided by revenue.
Step 6 is the verification habit to keep. The plain Revenue and Cost columns let you recompute any ratio by hand.
Common errors and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
| Formula error mentioning an unknown name | Header text differs from the name in the formula, or quotes are missing around a header with spaces | Copy the header text exactly and add single quotes if it has spaces |
| Margin over 100% or a rate that grows with each row | The field is on SUM and divides columns | Set Summarize by to Custom and aggregate each column first |
| `#DIV/0!` in some groups | A row or group has a zero denominator | Wrap the row-level formula in `IF`, or check the data for zeros |
| Field returns 0 or blank for text numbers | The source column holds numbers stored as text | Convert the column to real numbers in the source |
| Field disappears or fails after editing the source | A header was renamed or the column was removed | Update the formula to the new header name |
Summary
- Calculated field formulas use source header names, not cell addresses, and headers with spaces need single quotes.
- By default the formula runs on each source row and the results are summed.
- That order is fine for amounts (profit) and wrong for ratios (margin, price per unit).
- Set Summarize by to Custom and write
SUM(...)around each column to get ratios right, including in grand totals. - Always verify against plain summed columns before you trust a new field.
Exercise
Using the sample data, the East region has two rows: Revenue 100, Cost 60, Units 4, and Revenue 600, Cost 420, Units 12. (a) What does a default SUM calculated field =Cost / Units show for East? (b) Write the Custom formula that gives the correct cost per unit for East and state its value.
Show answer
(a) Per row, the costs per unit are 60/4 = 15 and 420/12 = 35. The default SUM adds them to give 50, which is not a real cost per unit. (b) Use =SUM(Cost) / SUM(Units) with Summarize by set to Custom. East has total cost 480 and total units 16, so the result is 480 / 16 = 30.
Exercise
Your source header is Unit Price. Write a calculated field formula for line total (unit price times units), and explain whether it needs Custom aggregation.
Show answer
='Unit Price' * Units. The single quotes are needed because the header contains a space. It does not need Custom aggregation. A line total is an amount, so summing the per-row results gives the correct group total.