Excel – Pivot tables

[Excel is an extremely powerful tool for the manipulation and analysis of numerical data.  And one of the most powerful features in Excel is the PIVOT TABLE.  It is extremely easy to use, but there are lots of subtle ways in which it can be used to reveal patterns in your data that can lead you to interesting conclusions.  I subscribe to this blog and received this tutorial recently.  I thought it was worth passing along.  You may also subscribe to the blog here.   Pat Lynch


This article explains the basics of excel pivot tables, I have included vba code for the most common actions.

Table of contents

1.    What is an excel pivot table?

2.    Prepare your source data

1.    Rearrange values

2.    Use an excel table as a data source

3.    How to build a pivot table

4.    Configuring a pivot table

1.    Report Filter

2.    Column Labels

3.    Row Labels

4.    Values

5.    Pivot table features

1.    Summarize and analyze

2.    Count

3.    Unique distinct list

4.    Sort data

5.    Group data

6.    Multiple levels

7.    See data behind a pivot table cell

8.    Row and column grand totals

6.    Insert a pivot chart

7.    Slicers

8.    Refresh a pivot table

9.    Copy a pivot table without link to source data

10.                    Customize the layout

11.                    Consolidate data from multiple cell ranges

What is an excel pivot table?

A pivot table allows you to summarize huge amounts of values amazingly fast in groups and sub-groups you specify.

You can then analyze the data with ease, compare values by date or by group and see important trends.

It is one of the best and most powerful excel feature and also one of the least known.

Pivot table charts is a great tool for visualizing your data.

Slicers allows you to quickly filter data, however the report filter has the same functionality but perhaps not as elegant.

Prepare your source data

Before you build your first pivot table make sure your data source table follows some simple rules.

1.    Use unique header names for all your columns.

2.    Make sure you have no blank cells.

3.    Check your spelling. If you have one cell “West” and another “Westt” they will both show up in the pivot table. You can correct this later.

4.    Convert your data source to an excel defined table (optional).

Rearrange values

This picture shows you a table with bad data structure, you can’t use it in a pivot table.

The table below is much better, all values in this table are not shown for obvious reasons. A couple of things are missing though, can you see it?

Unique table header names and the data table is not an excel defined table. You don’t have to use an excel defined table but it will make it a lot easier if you add more values later on to your table.

An excel defined table is dynamic and it will save you time not needing to adjust the pivot table source range.

I have made a macro/udf that can help you rearrange your data, see this post: Vba macro: Normalize data and this: Normalize data, part 2

Use an excel defined table as a data source

This is how you convert a generic data table to an excel defined table.

1.    Select a cell in your data table

2.    Go to tab “Insert” on the ribbon

3.    Click “Table” button

You can also use these shortcut keys: CTRL + T This dialog box shows up.

Excel finds the entire data table automatically =$A$1:$C$48, do make sure this is correct. My table does not have table headers so I don’t select “My table has headers” check box. Click OK button.

Excel has now converted your data table to an excel defined table. It has also inserted new column headers, see picture above. I don’t want to use these table header names as they are not descriptive of each column.

This table is much better, later on these table header names will make it much easier for us when working with the pivot table.

How to build a pivot table

1.    Select a cell in your data table

2.    Go to tab “Insert” on the ribbon

3.    Click “Pivot table” button

This dialog box appears.

It uses the excel defined table name in the first field, very good. It also allows you to choose where you want your new pivot table. I am going to place the pivot table on a new sheet. Click OK button.

Excel creates a blank pivot table for us on a new worksheet, see picture above. This is what the macro recorder returns while inserting a pivot table.

ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _ “Source data!R1C1:R365C7”, Version:=6).CreatePivotTable TableDestination:= _ “Sheet17!R3C1″, TableName:=”PivotTable7”, DefaultVersion:=6 Sheets(“Sheet17”).Select Cells(3, 1).Select

Configuring a pivot table

The pivot table on the worksheet is blank and it tells us “To build a report, choose fields from the PivotTable Field List”. You can find our fields in the blue box named 1, see picture below.

The fields are Region, Date and Amount the same as your header names in your data source table, now you understand why it is important to name your data source headers.

The way this works is that you can click and hold with left mouse button on one of the fields and then drag it to an area. The areas are Filters, Columns, Rows and Values and I have drawn a blue box around them with the name 2, see above picture.

Drag Region to Filters, Date to Rows and Amount to Values, see picture below. Excel is trying to help me out here, it automatically grouped my dates into months se column “Row Labels”. Don’t worry, I will show you later on how to group and ungroup dates.

This is what the macro recorder returns when I drag Region to Filters area.

With ActiveSheet.PivotTables(“PivotTable3”).PivotFields(“Region”)
.Orientation = xlPageField
.Position = 1
End With

Here is the output when I drag Region to Columns area.

With ActiveSheet.PivotTables(“PivotTable3”).PivotFields(“Region”)
.Orientation = xlColumnField
.Position = 1
End With

This happens while recording Date to Rows area.

With ActiveSheet.PivotTables(“PivotTable3”).PivotFields(“Date”)
.Orientation = xlRowField
.Position = 1
End With

Finally Amount to Values area.

ActiveSheet.PivotTables(“PivotTable3”).AddDataField ActiveSheet.PivotTables( _ “PivotTable3”).PivotFields(“Amount”), “Sum of Amount”, xlSum

Report Filters

The report filter allows you to select a subset of your data. In this case I want to work with values in region “West”. Simply click the arrow and select a Region. Click OK. See animated picture below.

If you are interested in the vba code for this action here it is.

ActiveSheet.PivotTables(“PivotTable3”).PivotFields(“Region”).ClearAllFilters ActiveSheet.PivotTables(“PivotTable3”).PivotFields(“Region”).CurrentPage = _ “West”

Column Labels

Click and drag Region to Columns area.

Unique values from column Region in your source data table shows up horizontally on the pivot table (cell range C4:E4, pic above).

Row Labels

The following picture shows Region in Rows area and Date in Filters area.


The last area is Values. You are not limited to numbers (Amount) here, you can drag Dates or Region here also. Notice that you can’t sum text values but you can count hem.

Pivot table features

Here is a list of the most used functionalities.

Summarize and analyze

The pivot table is phenomenal at processing huge amounts of data very quickly. Since calculations are done in almost no time you can easily drill down in every detail very quickly.

I have shown you earlier in this article that if you drag a field to Values area, the pivot table sums your values into groups depending on what specific fields you have in the Columns and Rows area.

It also allows you to see trends in your numbers, follow this example. Drag “Amount” once again to Values area, you should now have two “Amount” there.

Left click on the down pointing arrow next to the second “Amount” and then click on “Value Field Settings…”. Click on tab “Show Values As”

Select Date in Base field: and (previous) in Base item:, see picture above. Click OK

There is now a new column next to the sums. It shows the % difference compared to the previous values. The number in cell B8 compared to the number in cell B7 is -4.86% less. (8410/8840) -1 = -4.86%


The following picture shows Region in both Values area and Rows area. Date in Columns area grouped by month.

Unique distinct list

You can use the a pivot table to extract a unique distinct list from a column in a large list. The picture shows countries, simply drag Country to Rows area.

Sort data

It is possible to sort almost anything in the pivot table. This picture shows a pivot table and I want to sort column East from smallest to largest. Right click on a cell in a column you want to sort. Click on “Sort” and then sort Smallest to Largest.

Group data

A pivot table allows you to group dates. You can group dates (and time) by seconds, minutes, hours, days, months, quarters and years.

Right click on a column or row you want to group, then click group and this dialog box appears.

Below Group is Ungroup and I don’t think I have to explain that.

Multiple levels

You can rearrange the pivot table so it has multiple levels on Rows area or Columns area or both. Drag more than one field to an area to create multiple levels.

The picture shows you data grouped by quarter and then by region. You can also drag fields in an area to sort them, the order is important.

The Date and Region field switched places.

See data behind a pivot table cell

To see the data behind a pivot table cell just double click on a cell that interests you and excel creates a new sheet with the corresponding data shown.

Row and column grand totals

If you don’t need the grand totals, turn them off by going to tab “Design”, click “Grand Totals” button and pick a setting you want. The choices are:

  • Off for Rows and Columns
  • On for Rows and Columns
  • On for Rows only
  • On for Columns only

Insert a pivot chart

A pivot chart helps you visualize data.

1.    Make sure you have a cell selected on your pivot table

2.    Go to tab “Analyze” on the ribbon

3.    Click “Pivot chart” button

From here you can pick a variety of charts, the preview helps you in your decision.

The chart will change if you apply a filter or sort pivot table.


Excel 2010 and later excel versions lets you insert slicers, they are no different than the Report Filter except that they look different and take up more space on your worksheet. Go to tab “Insert” and click “Slicers” button.

From here you can pick a field, I will select region and then press OK button. The slicer appears on your worksheet, you may need a new location for it.

Refresh a pivot table

If you add, remove or edit values in your source data table you must update the pivot table to reflect the changes made, every time. This is easy to forget, here is how to do it. Right click on a pivot table cell.

Click “Refresh”. You can automate this with a macro.

Private Sub Worksheet_Activate()
Sheets(“Pivot table”).PivotTables(“PivotTable1”).RefreshTable
End Sub

Put it in your worksheet code module, see this post for more details.

Copy a pivot table without

If you want to share your pivot table but not the source data, follow these steps.

1.    Select the pivot table you want to copy

2.    Press Ctrl +C or press copy button on tab “Home” on the ribbon

3.    Right click on a cell where you want to paste the pivot table

4.    Click on “Paste Special…”

5.    Select “Values”

6.    Click OK button

7.    Repeat step 3 and 4 and then select “Formats”

8.    Click OK button

You can tell that the copy has no link to source data by first selecting a single cell in the copied pivot table and then a single cell in the original pivot table.

The are two more tabs (Analyze and Design) on the ribbon if the original pivot table is selected.

Customize the layout

You can easily change the pivot table layout with one of the pivot table styles or create a entirely new one. Select a cell in the pivot table. Go to tab design. Click on a style and the pivot table is instantly changed.

You can hover with mouse pointer over different styles and see the changes to your pivot table change before you make up your mind.

What happens if I record a macro while changing pivot table style?

Sub Macro2()
ActiveSheet.PivotTables(“PivotTable1”).TableStyle2 = “PivotStyleLight14”
End Sub

To create a new pivot table style, go to tab Design. Click the arrow in the lower right corner of pivottable styles window, see picture below.

Select a table element and click “Format” button.

Change formatting and click OK button.

Repeat with the remaining table elements you want to change.

Consolidate data from multiple cell ranges

Excel has a feature that lets you consolidate data from multiple pivot tables or cell ranges. Here are my two pivot tables for 2015 and 2014.

I want to consolidate both pivot tables into one, notice that they share the same structure Pivot table 1 – 2015

Pivot table 2 – 2014

Here is how to do it.

1.    Press Alt + D + P (This opens the pivot table and pivot chart wizard)

2.    Click on “Multiple consolidation ranges”

3.    Click Next

4.    Select “Create a single page field for me”

5.    Click Next button

6.    Select the first worksheet range you want to consolidate

7.    Notice that I select the row and column labels as well but not the grand totals.

8.    Click Add button

9.    Repeat step 6 and 7 with your remaining ranges.

10.                    Click Next button

11.                    Select if you want to create a new pivot table on an existing sheet or a new worksheet

12.                    Click Finish

13.                    Left click on arrow next to Count of Value

14.                    Click “Value Field Settings…”

15.                    Click “Sum”

16.                    Click OK


Take care and see you next week.

Oscar Cronquist
Get Digital Help