How to Insert a Formula in a Pivot Table
In this article
You cannot type a formula directly into a pivot table's value area, but you can add a calculated field (a new column computed from your existing fields) or a calculated item (a formula applied to one row or column label). In Excel, click any cell in the pivot table, then go to PivotTable Analyze > Fields, Items & Sets > Calculated Field, give it a name, write the formula using field names, and click OK. In Google Sheets, open the pivot table editor, scroll to Values, and use a helper column in the source data if you need math between fields. Both methods recalculate automatically when the source data changes or you refresh.
Labels can differ slightly by Excel version, platform and region, and the wording in Google Sheets depends on your locale. The steps below are written for Excel on Windows and Mac desktop, with a short note on Google Sheets where it behaves differently.
What You'll Need
- A pivot table built from a proper source range or Excel Table, with a header row and no blank header cells.
- At least one field already in the Values area, so the calculated field has something to sit beside.
- Excel desktop (Windows or Mac). Excel for the web has limited pivot table editing, so calculated fields are best done in the desktop app.
- If you use Google Sheets: a source range with numeric columns you can reference in a helper column.
Step-by-Step
- Click inside the pivot table. Any cell in the body works. This makes the PivotTable Analyze and Design tabs appear on the ribbon.
- Open the calculated field dialog. Go to PivotTable Analyze > Fields, Items & Sets > Calculated Field. On Mac the tab may be called PivotTable Analyze or simply Analyze depending on version.
- Name the field. Type something descriptive in the Name box, such as "Profit" or "Avg Order Value". Avoid a name that matches an existing source column, or Excel will warn you and may refuse to create it.
- Write the formula. In the Formula box, start with an equals sign and reference fields by their exact header names. For example
=Sales-Amount - Costor=Revenue / Units. Field names with spaces need to be selected from the Fields list, which inserts them with the correct syntax automatically. - Insert field names by clicking, not typing. Double-click a field in the Fields list to drop it into the formula. This avoids syntax errors from misspelled or spaced names.
- Check the sample. Click OK. Excel shows a confirmation and adds the new field to the Values area, usually as "Sum of Profit". Verify one row by hand against the source data before trusting the whole table.
- Adjust the number format if needed. Right-click the new value in the pivot table, choose Value Field Settings, then Number Format, and set currency, percentage or decimals to match the rest of the table.
- For a calculated item instead, select a row or column label (not the whole table), then use PivotTable Analyze > Fields, Items & Sets > Calculated Item. This is for formulas between categories in one field, such as "East minus West". It does not work when the same field also sits in the Filters or Columns area, and Excel will say so.
- Refresh after source changes. Right-click the pivot table and choose Refresh, or press Alt+F5 on Windows. Calculated fields recalculate on refresh, not instantly.
- In Google Sheets, click the pivot table, open the Pivot table editor on the right, and look under Values. There is no calculated field option there. Instead, add a formula column to the source data (for example
=D2-E2dragged down), then add that new column as a Values field and set Summarize by to Sum or Average.
Common Mistakes
- Typing field names with spaces by hand. Excel needs the exact bracketed syntax; clicking the field in the list is safer.
- Using a calculated field for row-level logic. Calculated fields work on sums, not on individual rows. A formula like "if this row's value is over 100" belongs in the source data as a helper column.
- Dividing by a blank or zero. Wrap risky divisions in
IFERROR, for example=IFERROR(Revenue/Units,0). - Expecting the formula to survive a source restructure. If you rename a source column, the calculated field breaks until you edit it.
- Adding a calculated item to a field that is also a filter or column label. Excel blocks this; move the field or use a calculated field instead.
- Forgetting to refresh. New source rows will not appear, and the formula will look wrong when it is simply stale.
Tips
- Convert your source range to an Excel Table (Ctrl+T) first. The pivot table then expands automatically when you add rows, and calculated fields keep working.
- Name calculated fields clearly, such as "Profit (calc)", so nobody confuses them with raw source columns.
- Use
IFERRORin any division formula to keep #DIV/0! errors out of your summary. - Calculated fields appear in the field list with a small function icon; you can drag them between Rows, Columns and Values like any other field.
- If you need different math per row, add the formula to the source sheet instead. Pivot tables summarize; they do not compute row by row.
- Keep a copy of the workbook before heavy edits to calculated fields, since deleting one removes it from the table entirely.
Troubleshooting
The Calculated Field option is greyed out. This usually means the active cell is outside the pivot table, the source is an external data connection that does not allow it, or the pivot table is in an older compatibility mode. Click inside the table first, then check the source type.
Excel says the formula contains an error. Re-select each field from the Fields list rather than typing it. Check that every field name exists in the source and that operators and parentheses are balanced.
The result is wrong or shows the same number everywhere. Remember calculated fields sum the underlying data first, then apply the formula. If you expected row-level math, move the formula into the source data.
#DIV/0! appears. A denominator is zero or blank. Edit the calculated field and wrap the division in IFERROR.
The new field vanished after refresh. It may have been deleted, or the source range shrank. Re-add it and confirm the source range covers all rows.
Google Sheets has no calculated field. That is correct; it is not offered. Use a helper column in the source data, then summarize it in the pivot table.
If none of that works, copy the pivot table to a new sheet, rebuild it from the same source, and re-add the calculated field. If the workbook misbehaves, save a copy before any repair or reset step, because those can discard custom settings.
Frequently Asked Questions
Can I type a formula straight into a pivot table cell?
No. Typed formulas are overwritten on refresh. Use a calculated field, a calculated item, or a helper column in the source data.
What is the difference between a calculated field and a calculated item?
A calculated field adds a new value column computed from other fields. A calculated item creates a new row or column label computed from other items in the same field.
Does a calculated field update automatically?
It recalculates when you refresh the pivot table, not the moment source data changes. Press Alt+F5 on Windows or right-click and choose Refresh.
Why can't I add a calculated item?
Excel blocks calculated items when the same field is used in the Filters or Columns area, or when the pivot table is based on certain external sources. Move the field or use a calculated field instead.
Can I use calculated fields in Google Sheets?
Google Sheets pivot tables do not offer calculated fields. Add the formula as a column in the source range, then include that column in the pivot table's Values area.
Will calculated fields work in Excel for the web?
Web editing of pivot tables is limited. For reliable calculated field editing, use the Excel desktop app and reopen the file in the browser afterwards.