Sub topic 17
Reading PivotTable Values with GETPIVOTDATA
This chapter assumes you can build a PivotTable with Rows, Columns and Values areas, and that you know how a PivotTable refreshes. You should also be comfortable writing formulas that refer to cells on another part of a sheet. The examples use the sales data from earlier chapters.
Why a plain cell reference is risky
Suppose you want a report cell that shows West region sales from a PivotTable. You could click the PivotTable cell and build a formula such as =B6. That formula points to a position, not to a meaning. If someone adds a new region, sorts the rows, or applies a filter, the cell at B6 can show a different region or a different product. The formula still works, but it now returns the wrong number.
GETPIVOTDATA solves this by asking for a value by name. You tell it which field to total and which items to match. Excel then finds that cell wherever it currently sits in the PivotTable.
The example PivotTable
This chapter uses a PivotTable with Region in Rows, Product in Columns and Sum of Sales in Values. Its top-left cell is A3.
| Cell | A | B | C | D |
|---|---|---|---|---|
| 3 | Sum of Sales | Column Labels | ||
| 4 | Row Labels | Gadget | Widget | Grand Total |
| 5 | East | 315 | 375 | 690 |
| 6 | West | 180 | 300 | 480 |
| 7 | Grand Total | 495 | 675 | 1170 |
Syntax
GETPIVOTDATA(data_field, pivot_table, [field1, item1], [field2, item2], ...)data_fieldis the value field to read, as text. You can use the source field name, such as"Sales", or the name shown in the PivotTable, such as"Sum of Sales".pivot_tableis any cell inside the PivotTable. By convention you use the top-left cell, with absolute references.- Each
field, itempair narrows the result. The field is a row, column or filter field name. The item is a value in that field.
The pairs can come in any order. You can give none, one, or several.
Examples
=GETPIVOTDATA("Sales",$A$3,"Region","West")
=GETPIVOTDATA("Sales",$A$3,"Region","West","Product","Gadget")
=GETPIVOTDATA("Sales",$A$3,"Product","Widget")
=GETPIVOTDATA("Sales",$A$3)Against the table above, these return 480, 180, 675 and 1170.
- The first formula returns the West row total.
- The second returns one cell of the grid.
- The third returns a column total.
- The fourth has no pairs, so it returns the grand total.
Letting Excel write the formula
You rarely need to type these by hand. Type = in a cell outside the PivotTable, then click a value cell inside it. Excel writes the GETPIVOTDATA formula for you, with the correct field names and items.
This behaviour is controlled by a setting. Select a cell in the PivotTable, open the PivotTable Analyze tab, open the Options dropdown, and switch Generate GETPIVOTDATA on or off. If you turn it off, clicking a cell gives a plain reference like =B6. Many people turn it off by accident and then wonder why their formulas break after a layout change.
Replacing typed items with cell references
Typed text such as "West" is fixed. To build a report grid, store the items in cells and point to them. Suppose a report has region names in column F (F2 and below) and product names in row 1 (G1 and across).
=GETPIVOTDATA("Sales",$A$3,"Region",$F2,"Product",G$1)The $F2 and G$1 references lock the column and row respectively. You can fill this formula right and down, and each cell asks for its own region and product. You can build a report with a different layout from the PivotTable this way, for example one with the products listed down the side.
Handling dates and numbers
Items that are not text need care. A date item must be a real date value, not a text string that looks like one.
=GETPIVOTDATA("Sales",$A$3,"Date",DATE(2025,1,5))This works only if Date is a field in the PivotTable with individual dates showing. If you grouped the dates into months or years, the field names and items change, and a typed guess often fails. In that case let Excel generate the formula by clicking the cell. Numeric items work as plain numbers.
When it returns #REF!
GETPIVOTDATA returns #REF! when it cannot find the requested value in the PivotTable as currently displayed. The most common causes are:
- The item is hidden. A filter, a slicer or a collapsed group removes the item from view. The formula only sees what is on screen.
- The field is not in the PivotTable. For example, you ask for
"Product"after removing Product from the layout. - The item is misspelled or has the wrong type.
"west "with a trailing space, or a text date, does not match. - The data field is not in the Values area. You ask for
"Units"but only Sales is displayed. - The
pivot_tablereference is not inside a PivotTable. This happens after you move or delete the PivotTable. - A subtotal or total is not shown. If you hid grand totals, the formula without pairs has nothing to return.
Returning a friendly result for missing items
A report is often built for items that may legitimately be absent, such as a product with no sales in a given region. Wrap the formula so the report shows zero instead of an error.
=IFERROR(GETPIVOTDATA("Sales",$A$3,"Region",$F2,"Product",G$1),0)Use this with care. IFERROR also hides real mistakes, such as a misspelled field name, and turns them into zeros. Test the formula without the wrapper first, and add it once the plain version returns the right numbers.
Behaviour to know about
- Displayed values, not source values. If a value field uses Show Values As, such as percent of column total, the formula returns the displayed percentage.
- Value field names. If you rename a value field to something like
"Total Sales", use that new name or the source field name. Renaming the field later can break formulas that use the old display name, so the source name is the safer choice. - Refresh. After you refresh the PivotTable, the formulas recalculate. A stale PivotTable gives stale results, so refresh before you read a report. The macro
ThisWorkbook.RefreshAllrefreshes every PivotTable and data connection in the workbook. - Layout changes are safe. Sorting, adding fields to other areas, or moving the PivotTable does not break a formula that names fields and items. This is the point of using it.
- Several PivotTables. The
pivot_tableargument decides which one is read. Two PivotTables on one sheet need two different anchor cells.
When not to use it
If you want a simple lookup that the PivotTable layout should drive, a plain reference is fine. If you need calculations on the source data that do not depend on the PivotTable, use SUMIFS on the source Table. GETPIVOTDATA is the right choice when the number must match the PivotTable exactly, including its filters.
Exercise
Using the example PivotTable with its top-left cell at A3, write a formula that returns the East Widget sales, and state the value it returns.
Show answer
=GETPIVOTDATA("Sales",$A$3,"Region","East","Product","Widget") returns 375.
Exercise
A report formula =GETPIVOTDATA("Sales",$A$3,"Region","West") worked yesterday and now returns #REF!. A colleague added a slicer on Region. What is the likely cause, and what are two ways to fix it?
Show answer
The slicer is hiding West, so the item is not displayed in the PivotTable and the formula cannot find it. Clear the slicer selection so West is visible again, or change the report to read from a source that does not depend on the slicer, such as SUMIFS on the source Table.