Chapter 8. Creating Custom PivotTable Calculations

Introducing Custom Calculations

A custom calculation is a formula that you define yourself to produce PivotTable values that would not otherwise appear in the report if you used only the source data fields and Excel's built-in summary calculations. Custom calculations enable you to extend your data analysis to include results that are specific to your needs.

For example, suppose your PivotTable shows employee sales by quarter and you want to award a 10 percent bonus to each employee with sales of more than $25,000 in any quarter. You can create a custom calculation that checks for sales greater than $25,000 and then multiplies those by 0.1 to get the bonus number.

A custom calculation is simply an Excel formula that gets applied to your source data to produce a summary result. In other words, in most cases the custom calculation is just like Excel's built-in PivotTable summary calculations, except that you define the specifics of the calculation yourself. Because you are creating a formula, you can use most of Excel's formula power, which gives you tremendous flexibility to create custom calculations that suit your data analysis needs. And by placing these calculations within the PivotTable itself — as opposed to, for example, adding the calculations to your source data — you can easily update the calculation as needed and refresh the report results.

Formula Basics

Custom calculations are formulas with certain restrictions imposed; see the next section, "Understanding Custom Calculation Limitations." You need to understand the basics of an Excel formula before you can create your own calculations. For much more detail on this topic, see Appendix A. A formula always begins with an equals sign (=), followed by one or more operands and operators.

Operands

The operands are the values that the formula uses as the raw material for the calculation. In a custom PivotTable calculation, the operands can be numbers, worksheet functions, or fields from your data source.

Operators

The operators are the symbols that the formula uses to perform the calculation. In a custom PivotTable calculation, the available operators include addition (+), subtraction (-), multiplication (*), division (/), comparison operators such as greater than (>) and less than (<) or equal to (<=), and more.

Custom Calculation Types

When building a custom calculation for a PivotTable, Excel offers two different types: a calculated field and a calculated item.

Calculated Field

A calculated field is a new data field in which the values are the result of a custom calculation formula. You can display the calculated field along with another data field or on its own. A calculated field is really a custom summary calculation, so in almost all cases the calculated field references one or more fields in the source data. See the task "Insert a Custom Calculated Field," later in this chapter.

Calculated Item

A calculated item is a new item in a row or column field in which the values are the result of a custom calculation. In this case, the calculated item's formula references one or more items in the same field. See the task "Insert a Custom Calculated Item."

Understanding Custom Calculation Limitations

Custom calculations — whether they are calculated fields or calculated items — are powerful additions to your PivotTable analysis toolbox. However, although custom calculation formulas look like regular worksheet formulas, you cannot assume that everything you do with a worksheet formula you can also do with a custom PivotTable formula. In fact, there are a number of limitations that Excel imposes on custom formulas, such as not being able to reference data outside the pivot cache, and not being able to use custom items in conjunction with grouping.

General Limitations

The major limitation inherent in custom calculations is that, with the exception of constant values, such as numbers, you cannot reference anything outside the PivotTable's source data:

  • You cannot use a cell reference, range address, or range name as an operand in a custom calculation formula.

  • You cannot use any worksheet function that requires a cell reference, range, or defined name. However, you can still use many of Excel's worksheet functions by substituting either a field or an item in place of a cell reference or range name. For example, if you want a calculated item that returns the average of items named Jan, Feb, and Mar, you could use the following formula:

    =AVERAGE(Jan, Feb, Mar)
  • You cannot use the PivotTable's subtotals, row totals, column totals, or Grand Total as an operand in a custom calculation formula.

Calculated Field Limitations

When you are working with calculated fields, it is important to understand how references to other PivotTable fields work within your calculations and what limitations you face when using field references.

Field References

It is important to understand that when you reference a field in your formula, Excel interprets this reference as the sum of that field's values. For example, the formula =Sales + 1 does not add 1 to each Sales value and return the sum of these results; that is, Excel does not interpret the formula as =Sum of (Sales + 1). Instead, the formula adds 1 to the sum of the Sales values — Excel interprets the formula as =(Sum of Sales) + 1.

Field Reference Problems

The fact that Excel defaults to a Sum calculation when you reference another field in your custom calculation can lead to problems. The trouble is that it does not make sense to sum certain types of data. For example, suppose you have inventory source data with UnitsInStock and UnitPrice fields. You want to calculate the total value of the inventory, so you create a custom field based on the following formula:

=UnitsInStock * UnitPrice

Unfortunately, this formula does not work because Excel treats the UnitPrice operand as Sum of UnitPrice. Of course, it does not make sense to "add" the prices together, so your formula produces an incorrect result.

Calculated Item Limitations

Excel imposes the following limitations on the use of calculated items:

  • A formula for a calculated item cannot reference items from any field except the one in which the calculated item resides.

  • You cannot insert a calculated item into a PivotTable that has at least one grouped field. You must ungroup all the PivotTable fields before you can insert a calculated item.

  • You cannot group a field in a PivotTable that has at least one calculated item.

  • You cannot insert a calculated item into a page field. Also, you cannot move a row or column field that has a calculated item into the page area.

  • You cannot insert a calculated item into a PivotTable in which a field has been used more than once.

  • You cannot insert a calculated item into a PivotTable that uses the Average, StdDev, StdDevp, Var, or Varp summary calculations.

Calculated Field Limitations

When you are working with calculated fields, it is important to understand how references to other PivotTable fields work within your calculations and what limitations you face when using field references.

Field References

It is important to understand that when you reference a field in your formula, Excel interprets this reference as the sum of that field's values. For example, the formula =Sales + 1 does not add 1 to each Sales value and return the sum of these results; that is, Excel does not interpret the formula as =Sum of (Sales + 1). Instead, the formula adds 1 to the sum of the Sales values — Excel interprets the formula as =(Sum of Sales) + 1.

Field Reference Problems

The fact that Excel defaults to a Sum calculation when you reference another field in your custom calculation can lead to problems. The trouble is that it does not make sense to sum certain types of data. For example, suppose you have inventory source data with UnitsInStock and UnitPrice fields. You want to calculate the total value of the inventory, so you create a custom field based on the following formula:

=UnitsInStock * UnitPrice

Unfortunately, this formula does not work because Excel treats the UnitPrice operand as Sum of UnitPrice. Of course, it does not make sense to "add" the prices together, so your formula produces an incorrect result.

Insert a Custom Calculated Field

If your data analysis requires PivotTable results that are not available using just the data source fields and Excel's built-in summary calculations, you can insert a calculated field that uses a custom formula to derive the results you need.

A custom calculated field is based on a formula that looks much like an Excel worksheet formula; see the section "Introducing Custom Calculations," earlier in this chapter. However, you do not enter the formula for a calculated field into a worksheet cell. Instead, Excel offers the Calculated Field feature which provides a dialog box for you to name the field and construct the formula. Excel then stores the formula along with the rest of the PivotTable data in the pivot cache.

Having the calculated field stored in the pivot cache is handy because the pivot cache is often shared by other PivotTables that you have built using the same source data. When you build a second PivotTable based on the same source data as the first, Excel asks if you want to reuse the original data in the new PivotTable. Therefore, you can reuse calculated fields in other PivotTable reports, which can save time.

Insert a Custom Calculated Field

Note

This chapter uses the PivotTables.xls spreadsheet, available at www.wiley.com/go/pivottablesvb, or you can create your own sample database.

Insert a Custom Calculated Field
Insert a Custom Calculated Field
Insert a Custom Calculated Field

You can also click Insert→Calculated Field.

The Insert Calculated Field dialog box appears.

Insert a Custom Calculated Field
Insert a Custom Calculated Field
Insert a Custom Calculated Field
Insert a Custom Calculated Field
Insert a Custom Calculated Field
Insert a Custom Calculated Field
Insert a Custom Calculated Field
Insert a Custom Calculated Field
Insert a Custom Calculated Field

Insert a Custom Calculated Item

If your data analysis requires PivotTable results that are not available using just the data source fields and Excel's built-in summary calculations, you can insert a calculated item that uses a custom formula to derive the results you need.

As with a calculated field, a calculated item uses a formula much like an Excel worksheet formula; see the section "Introducing Custom Calculations," earlier in this chapter. Again, however, you do not enter the formula for a calculated item into a worksheet cell. Instead, Excel offers the Calculated Item command that displays a dialog box where you name the item and construct the formula. Excel then stores the formula along with the rest of the PivotTable data in the pivot cache.

Remember that the Calculated Item feature creates just a single item in a field. However, you are free to add as many calculated items as you need. For example, suppose you want to compare the performance of male and female sales representatives. One way to do that would be to create one calculated item that returns the average sales of the men, and a second calculated item that returns the average sales of the women.

Before you create a calculated item, be sure to remove all groupings from your PivotTable; see the section "Understanding Custom Calculation Limitations," earlier in this chapter. Note that it is not enough to simply remove the grouped field from the PivotTable. Instead, you must run the Ungroup command on the field, as described in the Chapter 4 task "Ungroup Values."

Insert a Custom Calculated Item

Insert a Custom Calculated Item
Insert a Custom Calculated Item
Insert a Custom Calculated Item

The Insert Calculated Item dialog box appears.

Insert a Custom Calculated Item
Insert a Custom Calculated Item
Insert a Custom Calculated Item
Insert a Custom Calculated Item

You can also double-click the field.

Insert a Custom Calculated Item
Insert a Custom Calculated Item
Insert a Custom Calculated Item

You can also double-click the item.

Insert a Custom Calculated Item
Insert a Custom Calculated Item
Insert a Custom Calculated Item
Insert a Custom Calculated Item
Insert a Custom Calculated Item
Insert a Custom Calculated Item

Edit a Custom Calculation

If you notice an error in a custom calculation, or if your data analysis needs changed, you can modify the formula used by a calculated field or calculated item.

When you add a custom calculation to a PivotTable, Excel first checks the formula to make sure that it contains no syntax errors — such as a missing comma or parenthesis — or illegal operands — such as cell addresses, unknown field or item names, or functions not supported by custom calculations. If Excel finds an error, it displays a dialog box to let you know and does not add the custom calculation to the PivotTable.

However, just because a formula contains no syntax errors or illegal operands does not necessarily mean that the formula's results are correct. In a calculated field, you may have used the wrong function for the result you are seeking. In a calculated item involving several field items, you may have accidentally missed an item.

Alternatively, your formula may be working perfectly, but it may no longer be the result you need if your data analysis needs have changed. For example, you might have a calculated field that determines whether employees get paid a bonus by looking for sales greater than $50,000. If that threshold changes to $75,000, then your calculated field will no longer produce the results you want.

Whether your custom calculation contains an error or your data analysis needs have changed, Excel enables you to edit the formula to produce the result you want.

Edit a Custom Calculation

EDIT A CALCULATED FIELD

EDIT A CALCULATED FIELD
EDIT A CALCULATED FIELD
EDIT A CALCULATED FIELD

You can also click Insert→Calculated Field.

The Insert Calculated Field dialog box appears.

EDIT A CALCULATED FIELD
EDIT A CALCULATED FIELD
EDIT A CALCULATED FIELD
EDIT A CALCULATED FIELD

Excel updates the calculated field's results.

EDIT A CALCULATED ITEM

EDIT A CALCULATED ITEM
EDIT A CALCULATED ITEM
EDIT A CALCULATED ITEM

The Insert Calculated Item dialog box appears.

EDIT A CALCULATED ITEM
EDIT A CALCULATED ITEM
EDIT A CALCULATED ITEM
EDIT A CALCULATED ITEM

You can also edit a calculated item by clicking the item's result. The formula appears in Excel's formula bar, and you can edit it from there.

Excel updates the calculated item's results.

Change the Solve Order of Calculated Items

You can ensure that your calculated items return the correct results by adjusting the order in which Excel solves the items.

If you have multiple calculated items in a PivotTable, you may end up with cells that have values that rely on two or more formulas. For example, you may have one calculated item in a row field and another in a column field. In the PivotTable cell that lies at the intersection of these two items, the value will be the result of Excel applying one formula and then the other. The default order is the order in which you added the items to the PivotTable. Most of the time, this order does not matter. However, the order that Excel solves calculated items can make a difference.

For example, suppose you have a PivotTable that shows the number of units that customers ordered based on two different promotional offers — 1 Free with 10 and Extra Discount — broken down by advertisement — Direct Mail, Magazine, and Newspaper. Suppose further that you have added a calculated item in the row field that returns the percentage of units ordered for each promotion. For example, with the Magazine advertisement, you might find that 52.5% of units were ordered via the 1 Free with 10 promotion, and the other 47.5% were ordered via the Extra Discount promotion.

If you also want to know the overall percentages of each promotion, you run into a problem because Excel's default Grand Total calculation will add the percentages. To work around this problem, you could turn off the Grand Total calculation and create a new calculated item in the column field that adds the various Advertisement items together. If this calculated item is solved after the first one, you end up with the same problem: Excel adds the percentages. To fix this, you need to change the solve order so that Excel adds the Advertisement items first, and then calculates the percentages.

Change the Solve Order of Calculated Items

Change the Solve Order of Calculated Items
Change the Solve Order of Calculated Items
Change the Solve Order of Calculated Items
Change the Solve Order of Calculated Items
Change the Solve Order of Calculated Items
Change the Solve Order of Calculated Items

The Calculated Item Solve Order dialog box appears.

Change the Solve Order of Calculated Items
Change the Solve Order of Calculated Items

Excel moves the item up.

Change the Solve Order of Calculated Items
Change the Solve Order of Calculated Items
Change the Solve Order of Calculated Items

Excel adjusts the solve order and recalculates the results.

Change the Solve Order of Calculated Items

List Your Custom Calculations

You can document your PivotTable's custom calculations by displaying the formulas for each calculated field and calculated item on a separate worksheet.

When you add several custom calculations to a PivotTable, the report can become difficult to decipher. This is particularly true with calculated fields because there is nothing in the PivotTable that shows the field's underlying formula to the reader. Calculated fields are more transparent because you can see the formula by selecting the cell, but you have no way of knowing whether there are multiple calculations that determine the cell's value.

To help the reader decipher a PivotTable's custom calculations — whether that reader is another person not familiar with the PivotTable or yourself a few months from now — Excel offers a feature that enables you to document all the custom calculations, including the formulas and solve order, in a separate worksheet.

You can use a VBA macro to list the custom calculations for a PivotTable. To do this, run the PivotTable object's ListFormulas method, as shown in the following macro:

Sub ListPivotTableFormulas()
     Dim objPT As PivotTable
     '
     ' Work with the first PivotTable
     Set objPT = ActiveSheet.PivotTables(1)
     '
     ' List the PivotTable's formulas
     objPT.ListFormulas
End Sub

List Your Custom Calculations

List Your Custom Calculations
List Your Custom Calculations
List Your Custom Calculations

Excel inserts a new worksheet and displays the solve order, name, and formula for each calculated field and item.

Delete a Custom Calculation

When you no longer need a calculated field or calculated item, you can delete the calculation from the PivotTable.

Custom calculations do not always remain a permanent part of a PivotTable report. For example, it is common to add a calculated field or item temporarily to the PivotTable to test the data or get a number to use elsewhere. Similarly, you may find that you create several versions of a custom calculation and you only want to keep the final version. Finally, although custom calculations are a powerful tool, they cannot do everything, so you may find that a calculation does not provide the answer you seek or help you with your data analysis.

For all these situations, Excel enables you to delete those calculated fields or items that you no longer need.

The following VBA macro deletes all the custom calculations in a PivotTable:

Sub DeleteAllCustomCalculations()
Dim objPF As PivotField, objCF As PivotField
Dim objCI As PivotItem
With ActiveSheet.PivotTables(1)
    For Each objCF In .CalculatedFields
        objCF.Delete
    Next 'objCF
    For Each objPF In .PivotFields
        For Each objCI In objPF.CalculatedItems
            objCI.Delete
        Next 'objCI
    Next 'objPF
End With
End Sub

Delete a Custom Calculation

Delete a Custom Calculation
Delete a Custom Calculation
Delete a Custom Calculation

To delete a calculated item, instead, click PivotTable→Formulas→Calculated Item.

The Insert Calculated Field dialog box appears.

Delete a Custom Calculation
Delete a Custom Calculation
Delete a Custom Calculation

Excel removes the custom calculation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.211.165