Updated 16 May 2023
Note: This does not apply to Power Pivot aka Data Model PivotTables.
When you create a PivotTable Excel takes a copy of the source data and stores it in the Pivot Cache. The Pivot Cache is held in Excel’s memory. You can’t see it but that’s the data the PivotTable references when you build your PivotTable, change a Slicer selection or move rows/columns around.
This enables Excel to be very responsive to changes in the PivotTable but it also increases the size of your file. Don't panic though as the Pivot Cache is very good at compressing the data, so your file size shouldn't double. For example, below you can see 3 files and their respective sizes:
- The first file only contains the data in the worksheet.
- The second file has the data in the worksheet and loaded to the Pivot Cache.
- The third file uses Power Query to get the data from an external file and load it to the Pivot Cache.
You can see that the Pivot Cache uses less than 700 KB to store the data vs nearly 3MB when it's stored in the worksheet.
Now, while PivotTables in Excel 2007 onwards have become more efficient at re-using the same Pivot Cache when you build PivotTables referencing the same source data, you can still accidentally create multiple Pivot Caches which can quickly blowout your file size.
Avoid Duplicating Pivot Caches
Although it’s more difficult to accidentally create duplicate Pivot Caches nowadays, I still like to create additional PivotTables (that reference the same data source) using the Copy & Paste technique.
That is you copy and paste an existing PivotTable and then edit the pasted one according to your needs, as opposed to selecting the source data and inserting a new PivotTable.
Tip: Preferably paste your new PivotTable on a different sheet to avoid them running over one another when the PivotTables expand.
Detecting the Number of Pivot Caches
To see how many Pivot Caches there are in a workbook open the VB Editor (ALT+F11), then open the Immediate window of the VB Editor; View Menu > Immediate Window (or CTRL+G):
Then type the following in the Immediate window:
?ActiveWorkbook.PivotCaches.Count
Press Enter to see the count result:
If you only have one data source and you have more than one Pivot Cache then your file size will be bigger than necessary.
Another side effect is you won’t be able to control both PivotTables with the same Slicers. Slicers can only control PivotTables which share the same Pivot Cache.
Notes:
- The Pivot Cache for a PivotTable connected to an Online Analytical Processing (OLAP) data source cannot be shared because it is used in a different way than non-OLAP data sources.
- To be absolutely clear, if you have two separate data sources then they cannot share a cache.
Removing Duplicate Pivot Caches
So, you’ve found you have duplicate caches and now you want to remove them. One way is to delete the offending PivotTable and build it again using the Copy and Paste method above.
Alternatively, you can use this macro to eliminate them. It comes with warnings though, so be careful!
Excel Pivot Cache Limitations
Ideally, we want to have as few caches in our file as possible to keep the file size small, however this means putting up with the following restrictions:
- Any Grouping applied to dates or other fields is applied in all PivotTables which share the cache
- Calculated fields are available in all PivotTables that share the cache
- Refreshing one PivotTable refreshes all PivotTables which share the cache
Intentionally Duplicating Pivot Caches
We have a few options to get around the limitations above.
New PivotTables
Option 1: You can create a new PivotTable using the PivotTable Wizard which will automatically create a new cache. To start the Wizard first select your source data > ALT + D + P and follow the prompts. At the warning you want to select ‘No’ so that Excel creates a new cache.
Note: If your source data is referencing an external data source then you will need to create a unique data connection for each PivotTable to ensure they don’t share a cache.
Option 2: You can insert a new PivotTable by selecting the source data > Insert tab > PivotTable. This PivotTable will share the same cache but it won't automatically inherit the groupings of the previous PivotTables. However, if you later make grouping changes to any PivotTables sharing the same cache, this PivotTable will also become grouped in the same way. See the next section on 'Existing PivotTables' for how to fix them if this happens.
Note: keep in mind that dates are automatically grouped when you add a date field to a PivotTable. You can CTRL+Z to undo this grouping before adding any other fields. This will only undo the grouping for the new PivotTable.
Existing PivotTables
If you have an existing PivotTable that you no longer want grouped the same as the other PivotTables sharing the cache, you can temporarily create a new Pivot Cache by changing the source data range so that it’s just one row bigger than the actual range you want. To change the data source, simply select a cell in the PivotTable > Analyze > increase the range by 1 row:
Note: This will result in ‘blank’ values in your PivotTable, but don't worry as they will go again when you change the source data range back.
Change the grouping to suit your needs, then edit the source data range to remove the extra row. The PivotTable will now share the cache again, AND it will retain the new grouping settings.
Note: if you subsequently change the groupings for any PivotTables sharing the cache, they will be applied to all PivotTables again, so you'll have to repeat the steps above to temporarily change the source data range.
Reducing the Pivot Cache Overhead
PivotTables were designed to summarise huge amounts of data so sometimes the duplication of data in the Pivot Cache can tip your file over the edge of reasonableness when it comes to size.
If so, there are a few things you can do:
Delete the Source Data: If your source data is static, i.e. it’s not changing or being updated then you can delete it. Yes, you can delete the source data that occupies a worksheet in your file. It’s already in the Pivot cache so you don’t also need it in the workbook.
You can still filter and edit the PivotTable because it will use the data in the Pivot cache. You just can’t refresh it as the link to the source is broken once you delete it.
If you want to get the source data back then all you need to do is double click on the Grand Total cell in your (unfiltered) PivotTable and it will dump the source data in another sheet.
- Put your source data in another Excel file: use Power Query to bring the data into the Pivot Cache without loading it to the worksheet first.
For extra points make sure you only import the data you need by filtering out unnecessary columns and rows.
Caution: don't use this if you used Power Query to get your data or if your source data was in your Excel file and you've since removed it as suggested in tip 1 above. You must retain a connection to the original source of the data, otherwise your PivotTable becomes nothing more than a static report. All ability to update or pivot it further will be lost.
Philippe Van Reybrouck
Hi Mynda, I have tried to detect the Pivot Cache following your instructions and get the following message: Window name “Microsoft Visual Basic for Applications” Compile error: Invalid outside procedure. What does that mean please?
Sincerely,
Philippe Van Reybrouck
Catalin Bombea
Hi Philippe,
You might have code outside of Sub-End Sub instructions, you have to check it. Normally, the compiler selects the wrong statements, is it pointing to something in code?
You can try uploading a sample to our forum.
Robert Winder
I am using Excel 2019. I was getting date grouping issues across my PivotTables when I came across your article. I followed the directions above and successfully created two caches which fixed my date grouping problem across multiple PivotTables and charts. I added an extra row to the data source but if I change it back my problem arises again. My data source is a named Table. In 2019 does changing the data source back to the original automatically delete the second cache? In addition, I am guessing the PivotTables using the named Table source will properly update but the PivotTables using the “extra row”, which is outside the named Table will not. This may be an issue if I cannot have separate caches where one does not grow with the Table.
Mynda Treacy
Hi Robert,
Yes, more recent versions of Excel are smart enough to clean up duplicate caches where they’re no longer necessary. If you require different grouping and automatic inclusion of new data in a table, then you might be better off adding additional columns to your source data for month, quarter, year etc. so you don’t need the automatic date grouping.
Mynda
Chris
hi mynda
Great article:
is there a way to find the maximum of a field of a pivot cache from an external database (SQL-Server-View) with vba?
Cheers, Chris
Catalin Bombea
Hi Chris,
Not sure what you mean, an external database does not have a pivot cache. You want to know how many unique items are in a specific column from an sql database table? You can find that in SQL, with a simple query, the same query can be executed from VBA if needed.
Rosann Gilman
Is it possible to limit a user’s access to the pivot cache? So if I have data for 8 regions but want each region to be able to see their data only, can I put security on to accomplish this?
Mynda Treacy
Hi Rosann,
You can use Worksheet Protection to some degree, but this is not user proof.
Mynda
Lukasz
Hi,
I’ve just realized that:
– if I create a pivot table from the source data stored in the excel worksheet and then copy it -> the pivotcache index is the same
– if I copy the pivot table created from the PowerPivot Data model -> the pivotcache index is always different.
Does it mean the cache is unnecessary duplicated?
BR,
Lukasz
Mynda Treacy
Hi Lukasz,
The Data Model only has one Pivot Cache, which is the data model, so you can’t duplicate it. I’m not sure where you’re seeing the index.
Mynda
Alicia
Amazing! Thank you Mynda!!!
Mynda Treacy
Thanks, Alicia. I’m glad it was helpful.
Mynda
Jay Blevins
Mynda,
This is a great tip! I’ve always wondered why some files with pivot tables suddenly grew in size.
I was playing with the other properties available with the Pivotcaches element, but haven’t been able to get them to work in the Immediate window. For example, how do I show the MemoryUsed for each cache?
Thanks – I’ll be using this tip often!
Jay
Mynda Treacy
Hi Jay,
I’m glad I was able to demystify the Pivot Cache for you. Debra Dalgleish explains
how to detect memory used by the Pivot cache using VBA
Mynda
Kevin Lehrbass
Great explanation of Pivot cache Mynda. I’ll be sharing this for sure!
Mynda Treacy
Cheers, Kevin 🙂
Nick F
Yet another fantastic article! I’ve seen those very large files before and couldn’t figure out the cause.
Mynda Treacy
Thanks, Nick! Glad you found it useful.
Mynda