Dynamic Chart using Pivot Table and Range Names
by Jon Peltier
Thursday, April 24th, 2008
Peltier Technical Services, Inc., Copyright © 2010.
Licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
In Using Pivot Table Data for a Chart with a Dual Category Axis, I showed how to make a Chart with a Dual Category Axis using a pivot table to properly arrange the source data. I generally prefer using a regular chart, because pivot charts are pretty inflexible when it comes to formatting. Unfortunately, a regular chart doesn’t know when the pivot table has been resized by a refresh. If you know that the pivot table will only grow in one direction, that is, the number of series remains the same but the number of points in a series may change, you can define dynamic ranges that stretch with the pivot table.
The data initially looks like this:
| A | B | C | D | |
| 1 | Main Category | Category | Sub Category | Defects |
| 2 | Mechanical | Mechanical | Gear | 11 |
| 3 | Mechanical | Mechanical | Bearing | 8 |
| 4 | Mechanical | Mechanical | Motor | 3 |
| 5 | Electrical | Electrical | Switch | 19 |
| 6 | Electrical | Electrical | Plug | 12 |
| 7 | Electrical | Electrical | Cord | 11 |
| 8 | Electrical | Electrical | Fuse | 3 |
| 9 | Electrical | Electrical | Bulb | 2 |
| 10 | Hydraulic | Hydraulic | Pump | 4 |
| 11 | Hydraulic | Hydraulic | Leak | 3 |
| 12 | Hydraulic | Hydraulic | Seals | 1 |
and the pivot table like this, with the top left corner of the pivot table in cell F2:

Let’s define the names according to this table:
| Name | Refers To |
| ‘PT Names’!PTDefectCount | =COUNT(‘PT Names’!$D:$D) |
| ‘PT Names’!PTData | =OFFSET(‘PT Names’!$A$1,0,0,1+’PT Names’!PTDefectCount,4) |
| ‘PT Names’!PTCatList | =’PT Names’!$F$3 |
| ‘PT Names’!PTCat1 | =’PT Names’!$H$3 |
| ‘PT Names’!PTCat2 | =’PT Names’!$I$3 |
| ‘PT Names’!PTCat3 | =’PT Names’!$J$3 |
| ‘PT Names’!PTCategories | =OFFSET(‘PT Names’!PTCatList,1,0,’PT Names’!PTDefectCount,2) |
| ‘PT Names’!PTValues1 | =OFFSET(‘PT Names’!PTCat1,1,0,’PT Names’!PTDefectCount,1) |
| ‘PT Names’!PTValues2 | =OFFSET(‘PT Names’!PTCat2,1,0,’PT Names’!PTDefectCount,1) |
| ‘PT Names’!PTValues3 | =OFFSET(‘PT Names’!PTCat3,1,0,’PT Names’!PTDefectCount,1) |
Note that all names are defined locally for the worksheet containing the data and pivot table. PTDefectCount is the number of rows of data in the original table and in the pivot table. PTData is the data range used as the pivot table’s source. PTCatList is an anchor for the PTCategories name, which in turn encompasses the two columns of main and sub categories, the category labels for the chart. PTCat1 through 3 are the names of the three series in the chart, and the anchors for PTValues 1 through 3, the Y value ranges for the three series.
The chart is constructed like that in Using Pivot Table Data for a Chart with a Dual Category Axis. The three series formulas are:
=SERIES(‘PT Names’!PTCat1,’PT Names’!PTCategories,’PT Names’!PTValues1,1)
=SERIES(‘PT Names’!PTCat2,’PT Names’!PTCategories,’PT Names’!PTValues2,2)
=SERIES(‘PT Names’!PTCat3,’PT Names’!PTCategories,’PT Names’!PTValues3,3)

This looks like the previous chart, but it’s now dynamic, ready to change as the data table and its pivot table are updated. Add some rows to the pivot table source data range:
| A | B | C | D | |
| 1 | Main Category | Category | Sub Category | Defects |
| 2 | Mechanical | Mechanical | Gear | 11 |
| 3 | Mechanical | Mechanical | Bearing | 8 |
| 4 | Mechanical | Mechanical | Motor | 3 |
| 5 | Electrical | Electrical | Switch | 19 |
| 6 | Electrical | Electrical | Plug | 12 |
| 7 | Electrical | Electrical | Cord | 11 |
| 8 | Electrical | Electrical | Fuse | 3 |
| 9 | Electrical | Electrical | Bulb | 2 |
| 10 | Hydraulic | Hydraulic | Pump | 4 |
| 11 | Hydraulic | Hydraulic | Leak | 3 |
| 12 | Hydraulic | Hydraulic | Seals | 1 |
| 13 | Mechanical | Mechanical | Vibration | 9 |
| 14 | Mechanical | Mechanical | Wear | 8 |
| 15 | Electrical | Electrical | Short | 2 |
| 16 | Mechanical | Mechanical | Torque | 14 |
The name PTData expands to include the added rows. Refresh the pivot table:

The chart data ranges also expand, and the chart reflects the added data:

The Mechanical category has moved to the left of Electrical, because its total is now highest. The Category field in the column area of the pivot table has been changed so it is not sorted by defect count. Therefore the series are plotted in the same order (electrical is first), and the colors of the series do not change.
This technique works because I know that the number of columns in the pivot table will not change, only the number of rows. If there is a chance that the number of main categories will change, then this technique will not work, because a dynamic name can only be used to define the X or Y values of a chart series, or its name. A dynamic name cannot be used to specify the number of series in the chart.
Note: In a chart that is not based on a pivot table, a dynamic name can be defined that grows in height and width, and such a name can be used for the chart’s entire source data range (X values, Y values for all series, and all series names). There are two shortcomings with this approach. When you use a name to define the source data range, excel replaces the name by its cell address, and you lose the benefit of having defined a dynamic name. Also, if you define the source data for a chart, and the range includes a pivot table, then the pivot table becomes the data source, and the chart becomes a pivot chart.
Third in a series
- Chart with a Dual Category Axis
- Using Pivot Table Data for a Chart with a Dual Category Axis
- Dynamic Chart using Pivot Table and Range Names
- Dynamic Chart using Pivot Table and VBA
Related Posts:
- Dynamic Chart using Pivot Table and VBA
- Using Pivot Table Data for a Chart with a Dual Category Axis
- Chart with a Dual Category Axis
- Referencing Pivot Table Ranges in VBA
- Update Regular Chart when Pivot Table Updates
- Pivot Table Conditional Formatting with VBA
- Preliminary Data Exploration with Excel Pivot Tables
- Dynamic Chart with Multiple Series
- Dynamic Chart Review
- Dynamic Chart Source Data
Posted: Thursday, April 24th, 2008 under Dynamic Charts.
Comments: 3
Comments
Comment from Peder Schmedling
Time: Friday, April 25, 2008, 2:05 am
Shouldn’t it be possible to further develop this method to handle change in number of columns by using some VBA?
This is just a suggestion, I’m not sure if it will work:
Write a class that “hooks” into the events of this chart (that is if it’s an embedded chart, if not just place the code in the code module of the chart), use the Calculate event of the chart to determine when “something changes”. Then use VBA to check if the number of columns changed and and take action if necessary..
Comment from Jon Peltier
Time: Friday, April 25, 2008, 6:48 am
Peder – It’s certainly possible, and I show how in the next post.
Comment from Eric
Time: Monday, September 22, 2008, 4:26 pm
Jon,
Should the last parameter in the ‘PT Names’!PTValues2 & ‘PT Names’!PTValues3 defined name OFFSET formulas be a 1?
i.e. =OFFSET(’PT Names’!PTCat2,1,0,’PT Names’!PTDefectCount,1)
& =OFFSET(’PT Names’!PTCat3,1,0,’PT Names’!PTDefectCount,1)


















Write a comment
I welcome comments from my readers. If you have an opinion on this post, if you have a question or if there is anything to add, I want to hear from you. Whether you agree or disagree, please join the discussion.
If you want to include an image in your comment, post it on your own site or on one of the many free image sharing sites, and include a link in your comment. I'll download your image and insert the necessary html to display the image inline.
Read the PTS Blog Comment Policy.