Sub topic 8
Write calculated field formulas
Assume you already know basic Google Sheets formulas and can open the pivot table editor. You should also have a source table with one header row and one record per row. A calculated field is a new pivot value made from one or more source columns.
Add a calculated field
Use the running source table below:
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 420To calculate profit in a pivot table:
- Select the source range and choose Insert > Pivot table.
- Add a field such as
Regionunder Rows. - Add
Revenueunder Values. - In the pivot editor, under Values, choose Add > Calculated field.
- Enter
Profitas the field name. - Enter this formula:
=Revenue - CostThe pivot table evaluates the formula for the source records represented by each group. If Region is a row field, the result shows profit for each region.
A calculated field does not change the original source table. It creates a measure inside the pivot table.
Refer to source column names
Use source headers in calculated field formulas. Do not refer to the pivot table's cell addresses, because the pivot layout can change when you add or remove fields.
| Goal | Formula |
|---|---|
| Profit | `=Revenue - Cost` |
| Total value | `='Unit Price' * Quantity` |
| Profit margin | `=(Revenue - Cost) / Revenue` |
When a header contains spaces, wrap the header in single quotes:
='Unit Price' * QuantityUse the header exactly as it appears in the source data. A misspelled header, an extra space, or a renamed source column can make the calculated field fail or produce an unexpected result.
Choose the right formula structure
A calculated field can represent a per-record calculation, such as profit:
=Revenue - CostThe pivot then summarizes the calculated result for each group. For example, if the pivot summarizes by Region, it adds the profit values for all records in that region.
A ratio needs more care. Suppose you want revenue per unit. This formula calculates a ratio for each source record:
=Revenue / UnitsIf the pivot adds those row-level ratios, the result may not equal total revenue divided by total units. Groups with small and large records receive the same weight in the sum, which can make the result misleading.
For a group-level ratio, use a custom aggregation that combines the columns first:
=SUM(Revenue) / SUM(Units)This calculates total revenue and total units for the group, then divides the totals. The order matters.
Understand how fields are summarized
The Summarize by setting controls how a value is reduced within each pivot group. Common choices include SUM, COUNT, and AVERAGE.
For a calculated field, distinguish between these two operations:
- A per-record formula calculates a value for each source row, then the pivot summarizes those results.
- A custom formula aggregates source columns first, then performs the arithmetic.
The first approach is useful for additive measures such as profit. The second is usually better for rates, percentages, and unit measures.
Check formula results
After adding a calculated field, test one group manually. For profit, verify:
SUM(Revenue) - SUM(Cost)
For revenue per unit, verify:
SUM(Revenue) / SUM(Units)
Also check for zero or blank denominators. A formula that divides by Units cannot produce a meaningful rate when the group has no units. Clean source headers and numeric source values before debugging the calculated field.
Exercise
A pivot table shows total revenue of 770 and total units of 36 for a group. What custom calculated-field formula gives revenue per unit?
Show answer
Use =SUM(Revenue) / SUM(Units). The result is 770 divided by 36, rather than a sum of individual row-level revenue-per-unit values.