Sub topic 12
Adding calculated fields
This chapter assumes you can already build a pivot table with rows, columns and values, and that you know how Summarize by works. It covers how to add a calculated field, how its formula is evaluated, and where it gives misleading results.
What a calculated field is
A calculated field is a custom value in the pivot table editor. You define it with a formula that refers to the column names of the source data. Google Sheets treats it like another column in the source, then summarizes it in the pivot table.
Use one when the number you need does not exist as a column. Common examples are profit (revenue minus cost), revenue per unit, or a commission amount.
The alternative is to add a helper column to the source data. A helper column is easier to inspect and can be used by other formulas. A calculated field keeps the source data untouched and is defined in one place. The cost is that its behavior is harder to see, as the sections below show.
Sample data
The examples use a Sales sheet with this layout. The header is in row 1.
| Date | Region | Product | Units | Revenue | Cost |
|---|---|---|---|---|---|
| 2026-01-05 | West | Widget | 10 | 500 | 300 |
| 2026-01-12 | West | Gadget | 4 | 400 | 220 |
| 2026-02-03 | East | Widget | 8 | 400 | 240 |
| 2026-02-14 | East | Gadget | 5 | 500 | 275 |
| 2026-03-09 | West | Widget | 6 | 300 | 180 |
Build a pivot table with Region in Rows and Revenue in Values (summarized by SUM). The calculated field will sit next to it.
Adding a calculated field
- Click any cell inside the pivot table to open the pivot table editor.
- Next to Values, click Add.
- Choose Calculated field at the bottom of the list.
- A new entry appears, named "Calculated Field 1" by default. Click the name to rename it, for example to
Profit. - In the formula box, enter the formula. The default text is
=and you type the rest. - Press Enter or click outside the box to apply.
For profit, the formula is:
=Revenue - CostThe pivot table now shows a Profit column for each region. For West, revenue is 1200 and cost is 700, so Profit is 500. For East, revenue is 900 and cost is 515, so Profit is 385.
How the formula is written
Inside the formula box you refer to source columns by their header text. A few rules apply.
- Use the exact header name. Capitalization is not significant, but spelling is.
- If a header contains spaces or special characters, wrap it in single quotes:
='Unit Price' * Units. - You can use arithmetic operators (
+ - * /), parentheses, numeric constants, and comparison operators. - You can use some functions, such as
IF,SUM,ROUNDandSUMIF-style expressions that work on a column. Support is narrower than in ordinary cells, so test each function you rely on. - You cannot refer to cell addresses such as
B2, to other sheets, or to other calculated fields.
The ratio case is a typical one:
=Revenue / UnitsYou will meet the trap in this formula shortly.
Summarize by for calculated fields
A calculated field also has a Summarize by setting. The choices are SUM, CUSTOM and a few others. The default for a new field is SUM, and the right choice depends on the formula.
- With SUM, the formula is first computed for each source row. The results are then added up within each pivot group.
- With CUSTOM, the formula is computed once per group, using the summarized values of the columns it references.
This difference decides whether a ratio is correct.
The ratio problem
Take =Revenue / Units with SUM. For the West group, the rows give:
- Row 1: 500 / 10 = 50
- Row 2: 400 / 4 = 100
- Row 5: 300 / 6 = 50
SUM adds these: 200. That number has no meaning as revenue per unit. The correct figure is total revenue divided by total units: 1200 / 20 = 60.
To get the correct figure, you need the formula evaluated on group totals. Two options:
- Set Summarize by to CUSTOM, if your version of the editor offers it for the field. The formula then uses the group totals of
RevenueandUnits. - Write the formula as an explicit ratio of sums:
=SUM(Revenue) / SUM(Units)Check the result against a known group before you trust it. West should return 60. East has 900 revenue and 13 units, so it should return about 69.23.
Worked example: profit margin
Margin is profit divided by revenue. You can build it in one field:
=(SUM(Revenue) - SUM(Cost)) / SUM(Revenue)For West, that is (1200 - 700) / 1200, or 0.4167. For East, it is (900 - 515) / 900, or 0.4278.
The value appears as a decimal. Select the Margin column in the pivot table and apply a percent format from Format, then Number, then Percent. The format applies to the displayed cells and survives refreshes of the pivot table in most cases. If the formatting disappears after the layout changes, reapply it.
Do not confuse this with Show as. Show as, from the previous chapter on percentages, expresses a value relative to a total, such as percent of grand total. A margin is a ratio between two different measures, so it needs a calculated field.
Grand totals and subtotals
Totals are where calculated fields most often surprise people. With SUM, the grand total is the sum of the row-level results. With a ratio written as SUM(a) / SUM(b), the grand total is computed over all rows, which is what you want.
Compare the two approaches for margin across both regions:
| Approach | West | East | Grand total |
|---|---|---|---|
| Average of region margins | 0.4167 | 0.4278 | 0.4222 |
| Total profit / total revenue | 0.4167 | 0.4278 | 0.4190 |
The grand total of 0.4190 is (2100 - 1215) / 2100... check the sums first. Total revenue is 2100 and total cost is 1215, so profit is 885 and margin is 885 / 2100, which is 0.4214. The point of the table holds, though. Averaging group ratios gives a different answer from computing the ratio on totals, and the second is the correct one when groups differ in size.
Always compute the ratio from sums, and then the grand total and the group figures agree in method.
Calculated fields and filters
A calculated field respects the pivot table's filters and its Rows and Columns layout. If you filter out the East region, the grand total of the calculated field covers West only. This is the expected behavior, but it matters when you compare the pivot table with a formula that does not apply the same filter.
The cross-check below confirms West profit. It uses the source columns directly:
=SUMIFS(Sales!E:E, Sales!B:B, "West") - SUMIFS(Sales!F:F, Sales!B:B, "West")The result is 500, the same as the pivot table. Chapter 15 covers these checks in detail.
Referencing a calculated field with GETPIVOTDATA
You can read a calculated field with GETPIVOTDATA by using its name as the value field:
=GETPIVOTDATA("Profit", Pivot!A1, "Region", "West")If the name does not match the field name exactly, the formula returns #REF!. Renaming the field in the editor breaks existing formulas that point to the old name.
Limitations to know
- A calculated field cannot be used in Rows, Columns or Filters. It only works in Values.
- It cannot reference another calculated field. Repeat the logic in each formula.
- It cannot look up data from other sheets. Bring the data into the source range first.
- Text columns are treated as zero or cause errors in arithmetic, so a column that mixes text and numbers gives wrong totals.
- Division by zero returns
#DIV/0!in groups where the divisor sums to zero. Guard it:
=IF(SUM(Units) = 0, 0, SUM(Revenue) / SUM(Units))- There is no step-through view. If a result looks wrong, test the formula with a helper column in a copy of the source data and compare.
Deciding between a calculated field and a helper column
| Situation | Better choice |
|---|---|
| Simple difference or sum of two columns, such as profit | Calculated field |
| Ratio, average or percentage | Calculated field with sums, or a helper column plus Summarize by |
| Need to filter or group by the new value | Helper column |
| Other formulas or charts outside the pivot table need the value | Helper column |
| Logic depends on another calculated value | Helper column |
| Source data is shared and you cannot add columns | Calculated field |
Troubleshooting
- The field shows a different total than a manual check. Check whether Summarize by is SUM on a ratio formula.
- The formula is rejected. Check header spelling, quote headers that contain spaces, and remove cell addresses.
- The field is blank for some groups. The source cells may contain text, or the divisor may be zero.
- The field disappears after you change the source range. Confirm the new range still includes every column the formula uses, with the same header text.
Summary
A calculated field adds a derived measure inside the pivot table without changing the source data. It is reliable for sums and differences. For ratios, build them from sums so that group values and grand totals agree, and verify one group by hand. When you need to filter, group or reuse the value, add a helper column instead.
Exercise
Using the sample data, you add a calculated field =Revenue / Units with Summarize by set to SUM. The West row shows 200. What is the correct revenue per unit for West, and how do you fix the field?
Show answer
The correct value is 1200 / 20 = 60. SUM adds the row-level ratios (50 + 100 + 50 = 200). Fix it by writing the formula as =SUM(Revenue) / SUM(Units), or by using a CUSTOM summary so the formula runs on the group totals.
Exercise
Write a calculated field formula for profit margin that returns 0 instead of an error when a group has no revenue.
Show answer
=IF(SUM(Revenue) = 0, 0, (SUM(Revenue) - SUM(Cost)) / SUM(Revenue))