Sub topic 16
Adding a calculated field
A calculated field is a new field you define inside a PivotTable by writing a formula over other fields. This chapter assumes you can already build a PivotTable, place fields in the Rows, Columns and Values areas, and choose a summary function. It uses the sales dataset from earlier chapters.
Why a calculated field exists
A PivotTable summarises each source column on its own. If you want an average price, you could add Sales and Units as two value fields and divide them by hand in neighbouring cells. That breaks as soon as the layout changes, because the cells you wrote no longer line up with the PivotTable.
A calculated field solves this. It lives inside the PivotTable, appears in the field list, and can be dragged into the Values area like any other field. It is recalculated for every cell, subtotal and grand total.
The source data for this chapter:
Date,Region,Product,Units,Sales
2025-01-05,East,Widget,10,250
2025-01-12,West,Gadget,4,180
2025-02-03,East,Gadget,7,315
2025-02-14,West,Widget,12,300
2025-03-09,East,Widget,5,125Arrange the PivotTable like this:
Rows: Region
Columns: Product
Values: Sum of SalesCreating the field
- Click any cell inside the PivotTable.
- On the PivotTable Analyze tab, choose Fields, Items & Sets, then Calculated Field.
- In Name, type
AvgPrice. - In Formula, replace the default
=0with the formula below. Double-click a field in the Fields list to insert it, or type its name. - Click Add, then OK.
Calculated field name: AvgPrice
Formula: =Sales/UnitsExcel adds AvgPrice to the field list, usually in the Values area as "Sum of AvgPrice". Format it with number formatting so it reads as currency.
How the calculation works
The most important rule is this: a calculated field operates on the sums of its input fields, not on individual source rows.
For the East region and Widget product, the PivotTable first computes the sum of Sales (250 + 125 = 375) and the sum of Units (10 + 5 = 15). It then evaluates =Sales/Units, which gives 25.
It does not compute 250/10 and 125/5 and then average them. For this data both approaches give 25, but they differ when the prices differ between rows. The result is a weighted average price, which is usually what you want.
The same rule applies to totals. The Grand Total cell divides the total sales by the total units. It does not add up the AvgPrice values shown above it. A ratio calculated this way stays correct at every level.
The table shows the expected values for the sample data.
| Region | Product | Sum of Sales | Sum of Units | AvgPrice |
|---|---|---|---|---|
| East | Widget | 375 | 15 | 25 |
| East | Gadget | 315 | 7 | 45 |
| West | Widget | 300 | 12 | 25 |
| West | Gadget | 180 | 4 | 45 |
What formulas can and cannot do
Calculated field formulas accept:
- Field names, which stand for the sum of that field.
- Constants and the operators
+ - * / ^. - Comparison operators and a small set of worksheet functions that do not need cell references, such as
IF.
They cannot use:
- Cell references or defined names. The formula cannot point at
B4or at a named range. - Array formulas or functions that need a range, such as
VLOOKUP. - Anything that needs a row-by-row calculation. Because inputs are always sums, a formula like
=Sales*Unitsmultiplies the two totals together. It does not give the sum of per-row products.
If a field name contains spaces, put it in single quotes, for example ='Unit Cost'*Units.
Some useful formulas:
| Goal | Formula |
|---|---|
| Average price | `=Sales/Units` |
| Sales with 10% tax | `=Sales*1.1` |
| Commission of 5% | `=Sales*0.05` |
| Flag large sales | `=IF(Sales>300, 1, 0)` |
The last one is a trap. The condition tests the summed Sales in each cell, not each source row, so it does not count large individual transactions. For row-level logic, add a column to the source data instead.
Common problems
Wrong totals
Because the formula runs on sums, subtotals follow the same logic. This is correct for ratios and margins, but wrong for formulas that are not meant to be applied to sums. If a total looks odd, check whether your formula is a true ratio of totals.
Division by zero
If a cell has Units of 0, =Sales/Units returns #DIV/0!. Guard against it in the formula:
Calculated field name: SafePrice
Formula: =IF(Units=0, 0, Sales/Units)You can also hide errors by opening PivotTable Options and entering a value for "For error values show".
The field is missing for some sources
Calculated fields are not available when the PivotTable is built from the Data Model (the "Add this data to the Data Model" option) or from an OLAP source. In those cases you write a measure with DAX instead. If the Calculated Field command is greyed out, check how the PivotTable was created.
The result is stuck after changing the data
Calculated fields recalculate when the PivotTable refreshes. If the source changes, refresh the PivotTable. The macro from the refresh chapter does this for every PivotTable in the workbook:
Sub RefreshAllPivots()
ThisWorkbook.RefreshAll
End SubEditing and deleting
To change a formula, open Calculated Field again, pick the name from the Name drop-down, edit the formula, and click Modify. To remove it, pick the name and click Delete. Deleting removes the field from the PivotTable and from the field list. Simply unchecking it in the field list only hides it.
Choosing between alternatives
A calculated field is not always the best choice.
| Situation | Better option |
|---|---|
| Row-level logic, such as per-transaction margin | A calculated column in the source Excel Table |
| Ratio of two totals that must stay correct in every subtotal | Calculated field |
| One-off percentage of a total | Show Values As |
| Pull a single result into a report cell | `GETPIVOTDATA` |
| Data Model source | DAX measure |
You can combine a calculated field with GETPIVOTDATA. This formula returns the Sales total for the West region from the PivotTable whose top-left cell is A3, and it keeps working if the layout changes:
=GETPIVOTDATA("Sales",$A$3,"Region","West")Replace "Sales" with "AvgPrice" to read the calculated field. Use the name as it appears in the field list, which is not necessarily the caption shown in the PivotTable.
Summary of rules
- A calculated field is defined once and evaluated for every cell.
- Inputs are sums of source fields, not individual rows.
- Formulas cannot use cell references or lookup functions.
- Guard divisions against zero.
- The feature is unavailable on Data Model sources.
Exercise
A PivotTable shows Sum of Sales and Sum of Units for each Region. You add a calculated field =Sales/Units. Region East has two source rows: Sales 250 with Units 10, and Sales 100 with Units 20. What does the calculated field show for East, and why is it not the average of 25 and 5?
Show answer
It shows 350/30, which is about 11.67. The PivotTable first sums Sales (350) and Units (30), then applies the formula to those sums. The average of the two row prices would be 15, but that ignores that the second row sold twice as many units. The result is a weighted average price.
Exercise
You want a field that counts how many individual transactions had Sales above 200. Why does a calculated field with =IF(Sales>200,1,0) give the wrong answer, and what should you do instead?
Show answer
The formula tests the summed Sales in each PivotTable cell, not each source row. Add a helper column to the source Excel Table, such as =IF([@Sales]>200,1,0), refresh the PivotTable, and use Sum of that column.