In a pivot table, you can create calculated fields, using formulas that work with the sum of other pivot fields. There are limitations to what a calculated field can do, but they let you add more power to your pivot tables. Also, see the difference between Calculated Items and Calculated Fields
In a pivot table, you can create a new field that performs a calculation on the sum of other pivot fields. For example, in the screen shot below, a calculated field, named Bonus, has been created, and it will calculate 3% of the Total, if the sum of Units is greater than 100.
Here are some of the features and limitations of calculated fields:
Watch this video to see the steps for creating a simple calculated field. The written instructtions are below the video. To get the sample file for this video, go to the download section, below.
In this example, the pivot table shows the total sales for each sales representative per product, and the Units field summarizes the number of units sold.
You can download the sample file for this video, in the download section, below.
The sales reps will earn a 3 percent bonus on their Total Sales. To show the bonuses, you can add a calculated field to the pivot table. In this example, the formula will multiply the Total field by 3%.
In this example, the pivot table shows the total sales for each sales representative per product, and the Units field summarizes the number of units sold. We'll create a calculated field that uses these two fields, and the IF function.
The sales reps will earn a 3 percent bonus if they have sold more than 100 units of any product. To show the bonuses, you can add a calculated field to the pivot table. In this example, the formula will test the Units field, to see if more than 100 units were sold, and multiply the Total field by 3%.
You can download the sample file for this tutorial, in the download section, below.
When you create a calculated field, you might expect to see a sum of the calculated amounts, in the pivot table's subtotal and grand total rows. However, the calculated field uses the same calculation in the subtotal and grand total rows, instead of showing a sum.
For example, in the Bonus calculated field, shown above, the total bonus for Andrews is 164.23, instead of 70.13 (23.65+33.83+12.65). This happens because Excel uses the same calculation in the Total and Subtotal rows, instead of summing.
The total Units for Andrews is greater than 100, so the total Bonus is calculated as 3% of Andrews' Total.
You can download the sample file for this tutorial, in the download section, below.
There isn't a pivot table setting that you can change, so it will sum the calculated fields, instead of using the calculated field formula on the totals. However, you could use one of the following workarounds:
To hide the rows that don't qualify for a bonus, filter the product field for Units > 100, to match the Bonus calculation. With the filter applied, the subtotals and Grand Total are correct.
Another workaround is to remove the Subtotals and Grand Totals from the Sales Rep field.
If your pivot table layout won't change, another workaround is to calculate the Subtotals and Totals, outside of the pivot table, in columns to the right. For this technique to work correctly, change the pivot table layout from Compact to Outline Form.
In the screen shot below, the bonus is a calculated field, in column P. Formulas have been added in columns Q, R and S, and column Q has conditional formatting, so it matches the pivot table style.
Copy the formulas down to row 22, where the Grand Total is located.
Then, columns P, R and S could be hidden, leaving only the calculated Bonus in column Q.
After you create a calculated field, you might need to change its formula. In this example, we'll change the Bonus percentage for the calculated field formula that was created in the previous section.
Watch the video to see the steps, and the written instructions are below the video. To get the sample file for this video, go to the download section, below.
In this example, the pivot table has a calculated field named Bonus. It appears in the Values area as Sum of Bonus. You could temporarily hide the Bonus calculated field, or permanently delete it from the pivot table.
To temporarily remove a calculated field from a pivot table, follow these steps:
The calculated field is removed from the pivot table layout, but remains in the PivotTable Field List.
Later, you can add a check mark to the calculated field in the PivotTable Field List, to return it to the pivot table layout.
To permanently remove a calculated field, follow these steps to delete it:
In Excel VBA, if you try to change the Orientation for a calculated field, Excel displays the error message "Run-time error '1004': Unable to set the Orientation property of the PivotField class"
You can manually uncheck the calculated field boxes, and remove them from the pivot table, then check the box again, to put it back into the layout. However, if you record code while removing the calculated field, that recorded code shows the same error message when you try to run it.
Watch this video to see a macro that will remove all the calculated fields, without showing an error message. The download link and sample code are below the video.
The following code removes all the calculated fields by changing the Visible property to False. You can download the sample file below, to test the code.. Thanks to Rory Archibald for suggesting this solution.
You must have a pivot table cell selected, and only that pivot table is affected.
Sub RemoveALLCalculatedFields() Dim pt As PivotTable Dim pf As PivotField Dim df As PivotField On Error Resume Next Set pt = ActiveCell.PivotTable If pt Is Nothing Then MsgBox "Select a pivot table cell" Exit Sub End If For Each pf In pt.CalculatedFields For Each df In pt.DataFields If df.SourceName = pf.Name Then With df .Parent.PivotItems(.Name) _ .Visible = False End With Exit For End If Next df Next pf End Sub
With a built-in command, you can quickly create a list of the calculated fields and calculated items in the selected pivot table. Watch this short video to see the steps, and the written instructions are below the video..
With a built-in pivot table command, you can quickly create a list of the calculated fields and calculated items in the selected pivot table.
NOTE: All pivot tables that share the same pivot cache will also share the same calculated fields and calculated items.
A new sheet is inserted in the workbook, with a list of the calculated fields and a list of the calculated items.
A new sheet is inserted in the workbook, with a list of the calculated fields and calculated items (see the Excel 2007 example above).
To create a list of all the formulas in a specific pivot table, you can use the List Formulas command, as shown above.
There is no built-in command that will list the formulas for all of the pivot tables in a workbook, but you can use a macro to do that.
In the sample code shown below, a new worksheet is added to the active workbook, with a list of all the calculated items and calculated fields, in all of the pivot tables.
To download the sample file, which contains the code, go to the Download section, below.
Sub ListAllPivotFormulas() 'www.contextures.com 'print all the pivot table formulas 'in the active workbook Dim lRow As Long Dim wb As Workbook Dim ws As Worksheet Dim wsFP As Worksheet Dim pt As PivotTable Dim pf As PivotField Dim cf As Variant 'calculated field Dim ci As Variant 'calculated item Dim strSh As String Dim lPI As Long On Error Resume Next Application.DisplayAlerts = False Set wb = ActiveWorkbook strSh = "FP_" & Format(Date, "yyyymmdd") On Error Resume Next Worksheets(strSh).Delete On Error GoTo exitHandler Set wsFP = Worksheets.Add With wsFP .Name = strSh .Columns("A:E").NumberFormat = "@" 'text format .Range(.Cells(1, 1), .Cells(1, 7)).Value _ = Array("ID", "Sheet", "PivotTable", _ "Type", "Field", "Name", "Formula") .Rows(1).Font.Bold = True End With lRow = 2 For Each ws In wb.Worksheets If ws.PivotTables.Count > 0 Then For Each pt In ws.PivotTables For Each cf In pt.CalculatedFields wsFP.Range(wsFP.Cells(lRow, 1), _ wsFP.Cells(lRow, 7)).Value _ = Array(lRow - 1, _ ws.Name, pt.Name, _ "Calc Field", , cf.Name, _ " " & cf.Formula) lRow = lRow + 1 Next cf For Each pf In pt.PivotFields On Error Resume Next lPI = 0 lPI = pf.CalculatedItems.Count On Error GoTo errHandler If lPI > 0 Then For Each ci In pf.CalculatedItems wsFP.Range(wsFP.Cells(lRow, 1), _ wsFP.Cells(lRow, 7)).Value _ = Array(lRow - 1, _ ws.Name, pt.Name, _ "Calc Item", pf.Name, _ ci.Name, " " & ci.Formula) lRow = lRow + 1 Next ci End If Next pf Next pt End If Next ws wsFP.Columns("A:G").EntireColumn.AutoFit exitHandler: Application.DisplayAlerts = True Exit Sub errHandler: MsgBox "Could not list formulas" Resume exitHandler End Sub
Last updated: March 22, 2017 11:34 AM