Sub topic 12
Adding Calculated Fields to a Pivot Table
What a calculated field does
This chapter assumes that you can create a pivot table, place fields in Rows and Values, and use basic functions such as SUM and AVERAGE.
A calculated field is a new value created inside a pivot table from existing source fields. It is useful when the source table does not already contain the measure you need. For example, if your source has Revenue and Cost, a calculated field can show Profit without adding a Profit column to the source data.
Calculated fields are evaluated for each group in the pivot table. If the pivot table has one row per region, the formula produces one result for each region.
Add a calculated field
Suppose the source data has these columns:
| Region | Product | Units | Revenue | Cost |
|---|---|---|---|---|
| West | Desk | 10 | 2500 | 1500 |
Create a pivot table with Region in Rows and Revenue and Cost in Values. Then:
- In the pivot table editor, under Values, select Add.
- Choose Calculated field.
- Enter a name such as
Gross profit. - Enter this formula:
=SUM(Revenue)-SUM(Cost)- Confirm the formula.
The pivot table now calculates gross profit for every region. If West has $20,000 in revenue and $12,000 in cost, its calculated gross profit is $8,000.
You can also calculate a rate. For example, this formula calculates the margin percentage:
=SUM(Revenue)-SUM(Cost)To calculate the percentage itself, divide the result by revenue:
=(SUM(Revenue)-SUM(Cost))/SUM(Revenue)Format the resulting values as percentages when appropriate.
Understand aggregation order
A calculated field normally works with summarized field values. This affects which formula you should use.
| Question | Suitable formula pattern |
|---|---|
| Total profit | `=SUM(Revenue)-SUM(Cost)` |
| Margin for each group | `=(SUM(Revenue)-SUM(Cost))/SUM(Revenue)` |
| Average unit price | `=SUM(Revenue)/SUM(Units)` |
For average unit price, dividing total revenue by total units often gives a meaningful weighted average. Averaging individual row prices can give a different answer because every row receives equal weight.
Common errors and trade-offs
Field names in the formula must match the source headers. Rename a header only after checking the calculated field, because the formula may stop working.
Avoid dividing by a field that can total zero. A zero denominator produces an error or an unusable percentage. Filter out invalid records or fix the source data before calculating the rate.
Calculated fields keep a summary in one pivot table, but they are less flexible than a normal sheet formula. Use a calculated field when the measure should respond to the pivot table's rows, columns, and filters. Use a source column when the value must be reused in many places or audited record by record.
Exercise
A pivot table shows total Revenue and total Units for each product. Which formula calculates the average revenue per unit for each product?
Show answer
Use =SUM(Revenue)/SUM(Units). This divides the product's total revenue by its total units.