Sub topic 16
Adding a calculated field
This chapter assumes you can already build a PivotTable with Region and Product fields and a Sum of Sales value field. It also assumes you know that Excel summarises each PivotTable cell from the source rows behind it.
What a calculated field does
A calculated field is a new field that you define with a formula. The formula uses other fields from the source data. Excel adds the new field to the PivotTable Fields pane, and you can drag it into the Values area like any other field.
A common use is a ratio, such as average price. Suppose the source data has these columns:
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,125If you add Sales and Units as value fields, you get two totals. A ratio of the two totals gives the average price for each cell. You cannot get that by averaging the Sales column, because that would give average sales per row, not price per unit.
Create the field
- Click any cell inside the PivotTable.
- Open the PivotTable Analyze tab.
- Choose Fields, Items & Sets, then Calculated Field.
- Enter a name and a formula, as below.
- Click Add, then OK.
Calculated field name: AvgPrice
Formula: =Sales/UnitsExcel adds AvgPrice to the field list. Tick it, or drag it into Values. With Region in Rows and Product in Columns, each cell now shows the summed Sales divided by the summed Units for that combination.
For example, the East Widget cell has Sales of 250 + 125 = 375 and Units of 10 + 5 = 15. AvgPrice is 375 / 15 = 25.
How the calculation works
Excel sums each field first, then applies your formula to the sums. It does not calculate row by row and then add up the results. The order matters for ratios.
Common errors and limits
- Wrong results from row-level logic. A formula such as
=IF(Units>5, Sales, 0)tests the summed Units, not each row. If you need row-level logic, add a helper column to the source data and use that column instead. - Cell references are not allowed. The formula can use field names but not worksheet cell references like
B2. If a field name contains spaces, Excel wraps it in single quotes, for example='Unit Price'*Units. - Totals can look odd. The grand total is also the formula applied to the grand sums. For a ratio this is correct. For other formulas, check that it means what you intend.
- Dependence on the source. A calculated field exists only inside its PivotTable. If you change the source columns it refers to, update or remove the field.
- Division by zero. If the summed denominator is zero, the cell shows
#DIV/0!. Filter out those items or handle the case with a helper column.
Exercise
A PivotTable has Sum of Sales = 600 and Sum of Units = 20 in its grand total. The source has 5 rows. You add the calculated field =Sales/Units. What does the grand total show, and why is it not the average of the five row-level prices?
Show answer
It shows 30, which is 600 / 20. Excel sums Sales and Units first and then divides. The average of five row-level prices weights each row equally, regardless of how many units it covers, so it would generally give a different number.