Sub topic 17
Reading PivotTable values with GETPIVOTDATA
This chapter assumes you can build a PivotTable with Region in Rows, Product in Columns and Sum of Sales in Values. It also assumes you can write a basic Excel formula. You will pull a single number out of a PivotTable and keep it correct when the layout changes.
Why not a plain cell reference?
Suppose the West region total sits in cell D5. A formula such as =D5 returns it. But if you add a filter, sort the rows, or a new region appears, the West total moves and =D5 now points at a different number. The formula still works, but it returns the wrong answer.
GETPIVOTDATA asks for a value by field name and item name instead of by position. Excel finds the matching cell wherever it currently is.
The syntax
=GETPIVOTDATA("Sales",$A$3,"Region","West")The arguments are:
"Sales": the name of the value field to read.$A$3: any cell inside the PivotTable. The top-left cell is the usual choice."Region","West": a field name and the item to match. You can add more pairs.
This call returns the Sales total for West, and it keeps working if the PivotTable layout changes.
Letting Excel write it for you
Excel generates the formula when you click a PivotTable cell while typing a formula.
- Select an empty cell outside the PivotTable.
- Type
=. - Click the PivotTable cell you want, such as the West grand total.
- Press Enter.
Excel inserts a GETPIVOTDATA formula in place of a plain reference. You can then edit the text arguments or replace them with cell references.
Using cell references for the criteria
Hard-coded text makes a formula fixed. If the region name is in cell F1, use it instead:
=GETPIVOTDATA("Sales",$A$3,"Region",F1)Now changing F1 to East changes the result. To match two fields, add a second pair:
=GETPIVOTDATA("Sales",$A$3,"Region","West","Product","Widget")This returns the sales of Widgets in the West only.
Common errors
The function returns #REF! when it cannot find the requested value. The usual causes are:
- The field or item is not visible in the PivotTable. A filtered-out region or a field not placed in the layout cannot be read.
- The name is misspelled, or does not match the label in the PivotTable. Value fields are named like "Sum of Sales" unless you renamed them, so use the name shown in the PivotTable.
- The pairs ask for a combination that does not exist in the current layout.
- The PivotTable reference points at a cell that is not in a PivotTable.
Exercise
A PivotTable has its top-left cell at B4. Region is in Rows, Product is in Columns, and the value field is named "Sales". Write a formula that returns the sales of Gadgets in the East, and say why it is safer than clicking the cell with =D7`.
Show answer
=GETPIVOTDATA("Sales",$B$4,"Region","East","Product","Gadget"). It finds the value by field and item names, so it still returns the East Gadget total after the table is sorted, filtered or gains new rows. A plain reference such as =D7 follows the cell position and would return a different number once the layout changes.