Sample
Sample
Bill Jelen
Special Sales
For information about buying this title in bulk quantities, or for special sales
opportunities (which may include electronic versions; custom cover designs; and
content particular to your business, training goals, marketing focus, or branding
interests), please contact our corporate sales department at corpsales@pearsoned.com
or (800) 382-3419.
For government sales inquiries, please contact governmentsales@pearsoned.com.
For questions about sales outside the U.S., please contact intlcs@pearson.com.
Education is a powerful force for equity and change in our world. It has the potential to
deliver opportunities that improve lives and enable economic mobility. As we work with
authors to create content for every product and service, we acknowledge our responsibility
to demonstrate inclusivity and incorporate diverse scholarship so that everyone can achieve
their potential through learning. As the world’s leading learning company, we have a duty
to help drive change and live up to our purpose to help more people create a better life for
themselves and to create a better world.
■■ Our educational products and services are inclusive and represent the rich diversity
of learners.
■■ Our educational content accurately reflects the histories and experiences of the
learners we serve.
■■ Our educational content prompts deeper discussions with learners and motivates
them to expand their own learning (and worldview).
While we work hard to present unbiased content, we want to hear from you about any
concerns or needs with this Pearson product so that we can investigate and address them.
iii
Acknowledgments xxi
About the Author xxiii
Introduction xxv
CHAPTER 1 Pivot table fundamentals 1
CHAPTER 2 Creating a basic pivot table 11
CHAPTER 3 Customizing a pivot table 43
CHAPTER 4 Grouping, sorting, and filtering pivot data 77
CHAPTER 5 Performing calculations in pivot tables 121
CHAPTER 6 Using pivot charts and other visualizations 149
CHAPTER 7 Analyzing disparate data sources with pivot tables 175
CHAPTER 8 Sharing dashboards with Power BI 207
CHAPTER 9 Using cube formulas with the Data Model or OLAP data 225
CHAPTER 10 Unlocking features with the Data Model and Power Pivot 257
CHAPTER 11 Analyzing geographic data with 3D Map 285
CHAPTER 12 Enhancing pivot table reports with macros 301
CHAPTER 13 Using VBA or TypeScript to create pivot tables 321
CHAPTER 14 Advanced pivot table tips and techniques 389
CHAPTER 15 Dr. Jekyll and Mr. GetPivotData 435
CHAPTER 16 Creating pivot tables in Excel Online 453
CHAPTER 17
Pivoting without a pivot table using dynamic arrays
or Power Query 465
CHAPTER 18 Unpivoting in Power Query 479
Afterword 495
Index 497
vi
vii
viii Contents
Contents ix
x Contents
Contents xi
xii Contents
Contents xiii
xiv Contents
Contents xv
xvi Contents
Contents xvii
xviii Contents
Afterword 495
Index 497
Contents xix
A t Microsoft, thanks to the Excel team for always being willing to answer questions
about various features. At MrExcel.com, thanks to an entire community of people
who are passionate about Excel. Thanks to Bob Umlas for his tech editing of this book
and to the Kughens for their project management. Finally, thanks to my wife, Mary Ellen,
for her support during the writing process.
—Bill Jelen
xxi
xxiii
T he pivot table is the single most powerful tool in all of Excel. Pivot tables came along
during the 1990s, when Microsoft and Lotus were locked in a bitter battle for domi-
nance of the spreadsheet market. The race to continually add enhanced features to their
respective products during the mid-1990s led to many incredible features, but none as
powerful as the pivot table.
With a pivot table, you can transform one million rows of transactional data into a
summary report in seconds. If you can drag a mouse, you can create a pivot table. In
addition to quickly summarizing and calculating data, pivot tables enable you to change
your analysis on the fly by simply moving fields from one area of a report to another.
No other tool in Excel gives you the flexibility and analytical power of a pivot table.
The Power Query tools that debuted between Excel 2013 and Excel 2016 come close to
the power of a pivot table. You will see some Power Query examples in Chapter 17,
“Pivoting without a pivot table using dynamic arrays or Power Query,” and Chapter 18,
“Unpivoting in Power Query.”
Having picked up this book, you are savvy enough to have heard of pivot tables—and
you have perhaps even used them on occasion. You have a sense that pivot tables pro-
vide a power that you are not using, and you want to learn how to leverage that power to
increase your productivity quickly.
Within the first two chapters, you will be able to create basic pivot tables, increase
your productivity, and produce reports in minutes instead of hours. Within the first seven
chapters, you will be able to output complex pivot reports with drill-down capabilities
and accompanying charts. By the end of the book, you will be able to build a dynamic
pivot table reporting system.
xxv
Office 365 offers a new Analyze Data feature that is powered by Artificial Intelligence.
Select a data set with up to 250,000 cells and ask Excel to analyze the data. Excel will sug-
gest about 30 interesting analyses, including several pivot tables.
The Analyze Data feature allows you to ask a question about your data. There is a
fairly high chance that the answer will be provided as a pivot table or a pivot chart. So,
Analyze Data and Ask a Question become new entry points for creating pivot tables.
If you missed the 2019 edition of this book, these features were new since Excel 2016:
■■ You can specify default settings for all future pivot tables.
■■ The automatic date grouping introduced in Excel 2016 pivot tables can now be
turned off. A mix of empty cells and numeric cells will be treated like a numeric
column and will default to Sum instead of Count.
■■ Power Pivot is now included in all Windows versions of Excel 2019 and Office 365.
This sample data set (included with the download files for the book—see page xxxii)
has headings in row 1 and data in rows 2 through 564 and columns in column A through
column I.
First, you have to get a sorted, unique list of products down the left side of the summary
report and a sorted unique list of products across the top. In the past, this might involve an
Advanced Filter or the Remove Duplicates command. But today, it is easiest with a formula.
1. nter =SORT(UNIQUE(B2:B564)) in cell K2. You will have a list of the unique
E
region names spilling to K2:K5.
2. To get a list of unique products across the top of the report, enter =TRANSPOSE(
SORT(UNIQUE(C2:C564))) in cell L1.
xxvi Introduction
Next, you need to build a SUMIFS function to total the revenue for each Region and
Product. As shown in Figure I-2, the formula =SUMIFS(G2:G564,B2:B564,K2#,C2:C56
4,L1#) does the trick. It takes 40 characters plus the Enter key to finish the formula
FIGURE I-2 If Before Dynamic Arrays were introduced, the formula in L2 would have needed dollar
signs and then been copied to all sixteen cells showing numbers.
Enter the heading Total for the total row and for the total column. You can do this in
nine keystrokes if you type the first heading, press Ctrl+Enter to stay in the same cell,
and then use Copy, select the cell for the second heading, and use Paste.
If you select K1:P6 and press Alt+= (that is, Alt and the equal sign key), you can add the
total formula in three keystrokes.
With this method, which takes 110 clicks or keystrokes, you end up with a nice sum-
mary report, as shown in Figure I-3. If you could pull this off in 5 or 10 minutes, you
would probably be fairly proud of your Excel prowess; there are some good tricks
among those 110 operations.
FIGURE I-3 A mere 110 operations later, you have a summary report.
Introduction xxvii
■■ Could you put products down the side and regions across the top?
■■ Could you show me the same report for only the manufacturing customers?
■■ Could you show profit instead of revenue?
■■ Could you copy this report for each of the customers?
The core concept behind a pivot table is that the data, formulas, and data views are
stored separately. Each column has a name, and you can group and rearrange the data
by dragging field names to various positions on the report.
4. Drag the Product field from the PivotTable Fields list to the Columns area (see
Figure I-5).
xxviii Introduction
FIGURE I-5 To finish the report, drag the Product heading to the Columns area.
With just four clicks of the mouse, you have the report shown in Figure I-6.
Introduction xxix
In addition, when your manager comes back with a request like the ones near the
end of the prior case study, you can easily use the pivot table to make the changes.
Here’s a quick overview of the changes you’ll learn to make in the chapters that
follow:
■■ Could you put products down the side and regions across the top? (This change will
take you 10 seconds: Drag Product to Rows and Region to Columns.)
■■ Could you show me the same report for only the manufacturing customers?
(15 seconds: Select Insert Slicer, Sector; click OK; click Manufacturing.)
■■ Could you show profit instead of revenue? (10 seconds: Clear the check box for
Revenue, select the check box for Profit.)
■■ Could you copy this report for each of the customers? (30 seconds: Move Customer
to Report Filter, open the tiny drop-down menu next to the Options button, choose
Show Report Filter Pages, click OK.)
With one cell in your data set selected, choose the Analyze Data command on the
right side of the Home tab. The Analyze Data pane appears with several suggested
analyses. In the Ask a Question box at the top, type Revenue by Product and Region
as Table and press Enter.
Excel will draw a thumbnail of your report. Click +Insert PivotTable at the bottom of
the thumbnail.
xxx Introduction
We assume that you are comfortable navigating in Excel and that you have some
large data sets that you need to summarize.
Introduction xxxi
MicrosoftPressStore.com/Excel365pivotdata/downloads
System requirements
You need the following software and hardware to build and run the code samples for
this book:
MicrosoftPressStore.com/Excel365pivotdata/errata
If you discover an error that is not already listed, please submit it to us at the
same page.
MicrosoftPressStore.com/Support
Please note that product support for Microsoft software and hardware is not offered
through the previous addresses. For help with Microsoft software or hardware, go to
http://support.microsoft.com.
Stay in touch
Let’s keep the conversation going! We’re on Twitter:
http://twitter.com/MicrosoftPress
http://twitter.com/MrExcel
xxxii Introduction
Although pivot tables provide an extremely fast way to summarize data, sometimes the pivot table
defaults are not exactly what you need. In such cases, you can use many powerful settings to tweak
pivot tables. These tweaks range from making cosmetic changes to changing the underlying calcula-
tion used in the pivot table.
Many of the changes in this chapter can be customized for all future pivot tables using the new pivot
table default settings. If you find yourself always making the same changes to a pivot table, consider
making that change in the pivot table defaults.
In Excel, you find controls to customize a pivot table in myriad places: the PivotTable Analyze tab,
Design tab, Field Settings dialog box, Data Field Settings dialog box, PivotTable Options dialog box,
and context menus.
Rather than cover each set of controls sequentially, this chapter covers the following functional areas
in making pivot table customizations:
■■ Minor cosmetic changes—Change blanks to zeros, adjust the number format, and rename a
field. If you find yourself making the same changes to each pivot table, see Tip 5: Use Pivot Table
Defaults To Change Behavior Of All Future Pivot Tables in Chapter 14.
■■ Layout changes—Compare three possible layouts, show/hide subtotals and totals, and repeat
row labels.
■■ Major cosmetic changes—Use pivot table styles to format a pivot table quickly.
43
■■ Other options—Review some of the obscure options found throughout the Excel interface.
This default pivot table contains several annoying items that you might want to change quickly:
■■ The default table style uses no gridlines, which makes it difficult to follow the rows and columns
across and down.
■■ Numbers in the Values area are in a general number format. There are no commas, currency
symbols, and so on.
■■ Excel renames fields in the Values area with the unimaginative name Sum Of Revenue. You can
change this name.
You can correct each of these annoyances with just a few mouse clicks. The following sections
address each issue.
Tip Excel MVP Debra Dalgleish sells a Pivot Power Premium add-in that fixes most of
the issues listed here. Debra’s add-in offers a few more features than the new PivotTable
Defaults. This add-in is great if you will be creating pivot tables frequently. For more infor-
mation, visit http://mrx.cl/pivpow16.
2. From the ribbon, select the Design tab. Three arrows appear at the right side of the PivotTable
Style gallery.
3. Click the bottom arrow to open the complete gallery, which is shown in Figure 3-2.
4. Choose any style other than the first style from the drop-down menu. Styles toward the
bottom of the gallery tend to have more formatting.
5. Select the check box for Banded Rows to the left of the PivotTable Styles gallery. This draws
gridlines in light styles and adds row stripes in dark styles.
Note For more details about customizing styles, see “Customizing a pivot table’s appear-
ance with styles and themes,” later in this chapter.
Note For more about Power Pivot, read Chapter 10, “Unlocking features with the Data
Model and Power Pivot.”
For example, in the figures in this chapter, the numbers are in the thousands or tens of thousands.
At this level of sales, you would normally have a thousands separator and probably no decimal places.
Although the original data had a numeric format applied, the pivot table routinely formats your num-
bers in an ugly general style.
What is the fastest way to change the number format? It depends if you have a single value field or
multiple value fields.
In Figure 3-3, the pivot table Values area contains Revenue. There are many columns in the pivot
table because Product is in the Columns area. In this case, right-click any number and choose Number
Format.
FIGURE 3-3 For a single value field, right-click any number and choose Number Format.
1. Select from the first numeric cell to the last numeric cell, including the Grand Total row or
column if it is present.
3. Choose the Number tab across the top of the dialog box.
5. Click OK.
Until a coding change in Excel 2010, the preceding steps would not change the number format
in cases where the pivot table became taller. However, provided you include the Grand Total in your
selection, these steps will change the number format for all the fields in the Values area.
FIGURE 3-4 With multiple fields in the Values area, select all number cells as shown here and change the format
using the Format Cells dialog box.
A blank tells you that there were no sales for a particular combination of labels. In the default view,
an actual zero is used to indicate that there was activity, but the total sales were zero. This value might
mean that a customer bought something and then returned it, resulting in net sales of zero. Although
Follow these steps to change this setting for the current pivot table:
1. Right-click any cell in the pivot table and choose PivotTable Options.
2. On the Layout & Format tab in the Format section, type 0 next to the field labeled For Empty
Cells Show (see Figure 3-5). Alternatively, you can unselect the For Empty Cells Show option.
Or, you can type anything here, such as a dash or even the words zip, nada, nothing.
FIGURE 3-5 Enter a zero in the For Empty Cells Show box to replace the blank cells with zero.
The result is that the pivot table is filled with zeros instead of blanks, as shown in Figure 3-6.
FIGURE 3-6 Your report is now a solid contiguous block of non-blank cells.
Tip Although many of the names are inherited from headings in the original data set,
when your data is from an external data source, you might not have control over field
names. In these cases, you might want to change the names of the fields as well.
1. Select a cell in the pivot table that contains the appropriate type of value. You might have a
pivot table with both Sum Of Quantity and Sum Of Revenue in the Values area. Choose a cell
that contains a Sum Of Revenue value.
2. Go to the PivotTable Analyze tab in the ribbon. A Pivot Field Name text box appears below the
heading Active Field. The box currently contains Sum Of Revenue.
3. Type a new name in the box, as shown in Figure 3-7. Click a cell in your pivot table to complete
the entry and have the heading in A3 change. The name of the field title in the Values area
also changes to reflect the new name.
FIGURE 3-7 The name typed in the Custom Name box appears in the pivot table. Although names should be
unique, you can trick Excel into accepting a name that’s similar to an existing name by adding a space to the
end of it.
Note One common frustration occurs when you would like to rename Sum Of Revenue to
Revenue. The problem is that this name is not allowed because it is not unique; you already
have a Revenue field in the source data. To work around this limitation, you can name the
field and add a space to the end of the name. Excel considers “Revenue ” (with a space) to be
different from “Revenue” (with no space). Because this change is only cosmetic, the readers
of your spreadsheet do not notice the space after the name.
If you consider three report layouts, and the ability to show subtotals at the top or bottom, plus
choices for blank rows and Repeat All Item Labels, you have 16 different layout possibilities available.
Layout changes are controlled in the Layout group of the Design tab, as shown in Figure 3-8. This
group offers four icons:
■■ Subtotals—Moves subtotals to the top or bottom of each group or turns them off.
■■ Grand Totals—Turns the grand totals on or off for rows and columns.
■■ Report Layout—Uses the Compact, Outline, or Tabular forms. Offers an option to repeat item
labels.
Note You mathematicians in the audience might think that 3 layouts × 2 repeat options
× 2 subtotal location options × 2 blank row options would be 24 layouts. However, choos-
ing Repeat All Item Labels does not work with the Compact layout, thus eliminating 4 of
the combinations. In addition, Subtotals At The Top Of Each Group does not work with the
Tabular layout, eliminating another 4 combinations.
The Compact form is suited for using the Expand and Collapse icons. If you select one of the Sector
value cells such as Associations in A5 and then click the Collapse Field icon on the Analyze tab, Excel
hides all the customer details and shows only the sectors, as shown in Figure 3-9.
FIGURE 3-9 Click the Collapse Field icon to hide levels of detail.
After a field is collapsed, you can show detail for individual items by using the plus icons in column
A, or you can click Expand Field on the PivotTable Analyze tab to see the detail again.
FIGURE 3-10 When you attempt to expand the innermost field, Excel offers to add a new innermost field.
The Excel team added the Repeat All Item Labels option to the Report Layout tab starting in Excel
2010. This alleviated a lot of busy work because it takes just two clicks to fill in all the blank cells along
the outer row fields. Choosing to repeat the item labels causes values to appear in cells A6:A7, A9:A14,
as shown in Figure 3-11.
Figure 3-11 shows the same pivot table from before, now in Outline form and with labels repeated.
Caution This layout is suitable if you plan to copy the values from the pivot table to a
new location for further analysis. Although the Compact layout offers a clever approach by
squeezing multiple fields into one column, it is not ideal for reusing the data later.
By default, both the Compact and Outline layouts put the subtotals at the top of each group. You
can use the Subtotals drop-down menu on the Design tab to move the totals to the bottom of each
group, as shown in Figure 3-12. In Outline view, this causes a not-really-useful heading row to appear at
the top of each group. Cell A5 contains “Associations” without any additional data in the columns to the
right. Consequently, the pivot table occupies 44 rows instead of 37 rows because each of the 7 sector
categories has an extra header.
FIGURE 3-12 With subtotals at the bottom of each group, the pivot table occupies several more rows.
FIGURE 3-13 The Tabular layout is similar to pivot tables in legacy versions of Excel.
The Tabular layout is the best layout if you expect to use the resulting summary data in a subsequent
analysis. If you wanted to reuse the table in Figure 3-13, you would do additional “flattening” of the pivot
table by choosing Subtotals, Do Not Show Subtotals and Grand Totals, Off For Rows And Columns.
2. From the Design tab, select Grand Totals, Off For Rows And Columns.
4. Drag the Region tile from the Columns area in the PivotTable Fields list. Drop this field
between Sector and Customer in the Rows area.
FIGURE 3-14 The pivot table now contains a solid block of data.
6. Select one cell in the pivot table. Press Ctrl+* to select all the data in the pivot table.
9. Right-click. To the right of the words “Paste Special” is a greater-than sign that leads to a
flyout with 14 ways to Paste Special Choose Paste Values And Number Formatting, as shown in
Figure 3-15. Excel pastes a static copy of the report to the worksheet.
FIGURE 3-15 Use Paste Values And Number Formatting to create a static version of the data.
10. If you no longer need the original pivot table, select the entire pivot table and press the Delete
key to clear the cells from the pivot table and free up the area of memory that was holding
the pivot table cache. One way to select the entire pivot table is to use the Select drop-down
menu on the PivotTable Analyze tab and choose Entire PivotTable.
The Blank Rows drop-down menu offers the choice Insert Blank Line After Each Item. This setting
applies only to pivot tables with two or more row fields. Blank rows are not added after each item in
the inner row field. You see a blank row after each group of items in the outer row fields. As shown in
Figure 3-16, the blank row after each sector makes the report easier to read. However, if you remove
Sector from the report, you have only Customer in the row fields, and no blank rows appear (see
Figure 3-17).
FIGURE 3-16 The Blank Rows setting makes the report easier to read.
FIGURE 3-17 Blank rows will not appear when there is only one item in the row field.
FIGURE 3-18 The wording is confusing, but you can toggle off the grand total column, row, or both.
If you want a grand total column but no grand total at the bottom, choose On For Rows Only, as
shown at the top of Figure 3-18. To me, this seems backward. To keep the grand total column, you have
to choose to turn on grand totals for rows only. I guess the rationale is that each cell in F5:F8 is a grand
total of the row to the left of the cell. Hence, you are showing the grand totals for all the rows but not
for the columns. Perhaps someday Microsoft will ship a version of Excel in English-Midwest where this
setting would be called “Keep the Grand Total Column.” But for now, it remains confusing.
In a similar fashion, to show a grand total row but no grand total column, you open the Grand Totals
menu and choose On For Columns Only. Again, in some twisted version of the English language, cell
B18 is totaling the cells in the column above it.
The final choice, Off For Rows And Columns, is simple enough. Excel shows neither a grand total
column nor a grand total row.
Back in Excel 2003, pivot tables were shown in Tabular layout and logical headings such as Region
and Product would appear in the pivot table, as shown in the top pivot table in Figure 3-19. When the
Excel team switched to Compact form, they replaced those headings with Row Labels and Column
Labels. These add nothing to the report. To toggle off those headings, look on the far-right side of the
PivotTable Analyze tab for an icon called Field Headers and click it to remove Row Labels and Column
Labels from your pivot tables in Compact form.
FIGURE 3-19 The Compact form replaces useful headings with Row Labels. You can turn these off.
Caution When you arrange several pivot tables vertically, as in Figure 3-19, you’ll notice
that changes in one pivot table change the column widths for the entire column, often caus-
ing #### to appear in the other pivot tables. By default, Excel changes the column width
to AutoFit the pivot table but ignores anything else in the column. To turn off this default
behavior, right-click each pivot table and choose PivotTable Options. In the first tab of the
Options dialog box, the second-to-last check box is AutoFit Column Widths On Update.
Clear this check box. This is another setting that you might want to adjust in Pivot Table
Defaults. See Tip 5 in Chapter 14.
Start with the four check boxes in the PivotTable Style Options group of the Design tab of the rib-
bon. You can choose to apply special formatting to the row headers, column headers, banded rows, or
banded columns. My favorite choice here is banded rows because it makes it easier for the reader’s eye
to follow a row across a wide report. You should choose from these settings first because the choices
here will modify the thumbnails shown in the Styles gallery.
As mentioned earlier, the PivotTable Styles gallery on the Design tab offers 85 built-in styles.
Grouped into 28 styles each of Light, Medium, and Dark, the gallery offers variations on the accent
colors used in the current theme. In Figure 3-20, you can see which styles in the gallery truly support
banded rows and which just offer a bottom border between rows.
FIGURE 3-20 The styles are shown here with accents for row headers, column headers, and alternating colors in the
columns.
Tip Note that you can modify the thumbnails for the 85 styles shown in the gallery by
using the four check boxes in the PivotTable Style Options group.
The Live Preview feature in Excel works in the Styles gallery. As you hover your mouse cursor over
style thumbnails, the worksheet shows a preview of the style.
Say that you want to create a pivot table style in which the banded colors are three rows high. Fol-
low these steps to create the new style:
1. Find an existing style in the PivotTable Styles gallery that supports banded rows. Right-click
the style in the gallery and select Duplicate. Excel displays the Modify PivotTable Quick Style
dialog box.
2. Choose a new name for the style. Excel initially appends a 2 to the existing style name, which
means you have a name such as PivotStyleDark3 2. Type a better name, such as Greenbar.
3. In the Table Element list, click First Row Stripe. A new section called Stripe Size appears in the
dialog box.
5. To change the stripe color, click the Format button. The Format Cells dialog box appears. Click
the Fill tab and then choose a fill color. If you want to be truly authentic, choose More Colors,
Custom and use Red=200, Green=225, and Blue=204 to simulate 1980s-era greenbar paper.
Click OK to accept the color and return to the Modify PivotTable Quick Style dialog box.
6. In the Table Element List, click Second Row Stripe. Select 3 from the Stripe Size drop-down.
Modify the format to use a lighter color, such as white.
7. If you plan on creating more pivot tables in this workbook, choose the Set As Default Pivot-
Table Style For This Document check box in the lower left.
8. Optionally, edit the colors for Header Row and Grand Total Row.
9. Click OK to finish building the style. Strangely, Excel doesn’t automatically apply this new style
to the pivot table. After you put in a few minutes of work to tweak the style, the pivot table
does not change.
10. Your new style should be the first thumbnail visible in the Styles gallery. Click that style to
apply it to the pivot table.
Tip If you have not added more than seven custom styles, the thumbnail should be visible
in the closed gallery, so you can choose it without reopening the gallery.
In case you become tired of these combinations, you can visit the Themes drop-down menu on
the Page Layout tab, where many built-in themes are available. Each theme has a new combination of
accent colors, fonts, and shape effects.
To change a document theme, open the Themes drop-down menu on the Page Layout tab. Choose
a new theme, and the colors used in the pivot table change to match the theme.
Caution Changing the theme affects the entire workbook. It changes the colors and fonts
and affects all charts, shapes, tables, and pivot tables on all worksheets of the active work-
book. If you have several other pivot tables in the workbook, changing the theme will apply
new colors to all the pivot tables.
This oddity would have been right up his alley. Excel MVP Wyn Hopkins from Access Ana-
lytic recently uncovered a feature in pivot tables that allows you to quickly apply a border to
several sections inside the pivot table.
Say that you have a pivot table such as the one in Figure 3-23. Leave a blank row and
column on each side of the pivot table, but then completely surround the pivot table with a
border. The border shown in Figure 3-23 has a different style on each side to illustrate how
each side of the border will be translated to the pivot table.
To transfer the outer border to the pivot table, right-click any cell in the pivot table and
choose Refresh. The borders from outside the pivot table are applied to various sections
inside the pivot table.
To transfer the outer border to the pivot table, right-click any cell in the pivot table and choose
Refresh. The borders from outside the pivot table are applied to various sections inside the pivot table.
FIGURE 3-24 The border styles from the outer border are applied to the pivot table.
In a very specific pattern, the border styles used in the outer border are applied throughout
the pivot table. The dashed border used above row 2 is applied above rows 3, 4 and above cell
C11. The dotted border used below row 12 is applied in the pivot table below row 11. A similar
pattern happens with the left border being applied to most vertical lines in the pivot table. The
right border is applied to the right edge of the pivot table.
Why is this feature in Excel? It had to pre-date the Formatting Gallery. It must have been a
way to build your own Pivot Table Styles before there were styles.
One Excel customer wrote a letter to the Excel team describing this bug: “Why are you treating
empty cells as text? Treat them like any other formula would treat them and consider them to be zero.
A mix of numbers and zeroes should not cause a Count of that field.”
Without a single vote on Excel.UserVoice.com, someone on the Excel team patched the bug. If
you have a mix of numbers and blank cells, you will no longer get a Count Of Revenue. This is a nice
improvement.
There are several ways to open the Value Field Settings dialog box:
■■ If you have multiple fields in the Values area, double-click the heading for any value field.
■■ Choose any number in the Values area and click the Field Settings button in the PivotTable
Analyze tab of the ribbon.
■■ In the PivotTable Fields list, open the drop-down menu for any item in the Values area. The last
item will be Value Field Settings.
■■ If you right-click any number in the Values area, you have a choice for Value Field Settings, plus
two additional flyout menus offering six of the eleven choices for Summarize Values By and
most of the choices for Show Values As (see Figure 3-25).
To change the calculation for a field, select one Value cell for the field and click the Field Settings
button on the PivotTable Analyze tab of the ribbon. The Value Field Settings dialog box is similar to the
Field Settings dialog box, but it has two tabs. The first tab, Summarize Values By, contains Sum, Count,
Average, Max, Min, Product, Count Numbers, StdDev, StdDevP, Var, and VarP. You can choose 1 of these 11
calculation options to change the data in the column. In Figure 3-26, columns B through D show vari-
ous settings from the Summarize Values By tab.
FIGURE 3-25 The right-click menu for any number in the Values area offers a flyout menu with the popular
calculation options.
The following examples show how to use the various calculation options. To contrast the settings,
you can build a pivot table where you drag the Revenue field to the Values area nine separate times.
Each one shows up as a new column in the pivot table. Over the next several examples, you will see the
settings required for the calculations in each column.
To change the calculation for a field, select one Value cell for the field and click the Field Settings
button on the PivotTable Analyze tab of the ribbon. The Value Field Settings dialog box is similar to the
Field Settings dialog box, but it has two tabs. The first tab, Summarize Values By, contains Sum, Count,
Average, Max, Min, Product, Count Numbers, StdDev, StdDevP, Var, and VarP. You can choose 1 of these
11 calculation options to change the data in the column. In Figure 3-26, columns B through D show
various settings from the Summarize Values By tab.
Column B is the default Sum calculation. It shows the total of all records for a given market. Column C
shows the average order for each item by market. Column D shows a count of the records. You can
change the heading to say # Of Orders or # Of Records or whatever is appropriate. Note that the count
is the actual count of records, not the count of distinct items.
Note Counting distinct items has been difficult in pivot tables but is now easier using
PowerPivot. See “Unlocking hidden features with the data model” in Chapter 10 for more
details.
Far more interesting options appear on the Show Values As tab of the Value Field Settings dialog
box, as shown in Figure 3-27. Fifteen options appear in the drop-down. Depending on the option
you choose, you might need to specify either a base field or a base field and a base item. Columns E
through J in Figures 3-26 and 3-27 show some of the calculations possible using Show Values As.
FIGURE 3-27 Fifteen different ways to show that data is available on this tab.
No Calculation None
% of Grand Total None Shows percentages so all the detail cells in the pivot
table total 100%.
% of Column Total None Shows percentages that total up and down the pivot
table to 100%.
% of Row Total None Shows percentages that total across the pivot table to
100%.
% of Parent Row Total None With multiple row fields, shows a percentage of the par-
ent item’s total row.
% of Parent Column Total None With multiple column fields, shows a percentage of the
parent column’s total.
% of Parent Total Base Field only With multiple row and/or column fields, calculates a
cell’s percentage of the parent item’s total.
% Running Total In Base Field only Calculates a running total as a percentage of the total.
Rank Smallest to Largest Base Field only Provides a numeric rank, with 1 as the smallest item.
Rank Largest to Smallest Base Field only Provides a numeric rank, with 1 as the largest item.
% of Base Field and Base Item Expresses the values for one item as a percentage of
another item.
Difference From Base Field and Base Item Shows the difference of one item compared to another
item or to the previous item.
% Difference From Base Field and Base Item Shows the percentage difference of one item compared
to another item or to the previous item.
The capability to create custom calculations is another example of the unique flexibility of pivot
table reports. With the Show Values As setting, you can change the calculation for a particular data
field to be based on other cells in the Values area.
FIGURE 3-28 This report is created using the % Of option with Anne Troy as the Base Item.
To set up this calculation, choose Show Values As, % Of. For Base Field, choose Rep because this is
the only field in the Rows area. For Base Item, choose Anne Troy. The result is shown in Figure 3-26.
To set up a rank, choose Value Field Settings, Show Values As, Rank Largest To Smallest. You are
required to choose Base Field. In this example, because Rep is the only row field, it is the selection
under Base Field.
These rank options show that pivot tables have a strange way of dealing with ties. I say strange
because they do not match any of the methods already established by the Excel functions =RANK(),
=RANK.AVG(), and =RANK.EQ(). For example, if the top two markets have a tie, they are both assigned a
rank of 1, and the third market is assigned a rank of 2.
In Figure 3-30, cell I8 shows that the top four sales reps account for 76.97% of the total sales.
Note To produce this figure, you have to use the Sort feature, which is discussed in depth
in Chapter 4, “Grouping, sorting, and filtering pivot data.” To create a similar analysis with
the sample file, go to the drop-down menu in A4 and choose More Sort Options, Descend-
ing, By Total. Also note that the % Change From calculation shown in the next example is not
compatible with sorting.
To specify Running Total In (as shown in Column H) or % Running Total In (Column I), select Field
Settings, Show Values As, Running Total In. You have to specify a Base Field, which in this case is the
Row Field: Rep.
FIGURE 3-31 The % Difference From options enable you to compare each row to the previous or next row.
FIGURE 3-32 An option in Excel enables you to calculate a percentage of the parent row.
To set up this calculation in Excel, use Field Settings, Show Values As, % Of Parent Row Total. Cell D4
in Figure 3-32 shows that Chicago’s $184,425 is 10.59% of the Midwest total of $1,741,424.
Although it makes sense, the calculation on the subtotal rows might seem confusing. D4:D8 shows
the percentage of each market as compared to the Midwest total. The values in D9, D11, D16, and D19
compare the region total to the grand total. For example, the 31.67% in D11 says that the Northeast
region’s $2.1 million is a little less than a third of the $6.7 million grand total.
1. First, divide Georgia peaches by the Georgia total. This is 180/210, or 0.857.
2. Next, divide total peach production (285) by total fruit production (847). This shows that
peaches have an importance ratio of 0.336.
In Ohio, apples have an index of 4.91, so an apple blight would be bad for the Ohio fruit industry.
I have to admit that, even after writing about this calculation for 10 years, there are parts that I don’t
quite comprehend. What if a state like Hawaii relied on productions of lychees, but lychees were nearly
immaterial to U.S. fruit production? If lychees were half of Hawaii’s fruit production but 0.001 of U.S.
fruit production, the Index calculation would skyrocket to 500.
If you used the Subtotals drop-down menu on the Design tab, you would turn off all subtotals,
including the Region subtotals and the Market subtotals. The Region subtotals are still providing good
information, so you want to use the Subtotals setting in the Field Settings dialog box. Choose one cell
in the Market column. On the PivotTable Analyze tab, choose Field Settings. Change the Subtotals
setting from Automatic to None (see Figure 3-35).
FIGURE 3-35 Use the Subtotals setting in the Field list to turn off subtotals for one field.
To remove subtotals for the Market field, click the Market field in the bottom section of the
PivotTable Fields list. Select Field Settings. In the Field Settings dialog box, select None under Subtotals,
as shown in Figure 3-33. Alternatively, right-click a cell that contains a Market and remove the check
mark from Subtotal Market.
FIGURE 3-36 By selecting the Custom option in the Subtotals section, you can specify multiple subtotals for
one field.
Tip If you need to calculate the average of the four regions, you can do it with the DAX
formula language and Power Pivot. See Chapter 10.
For example, in Figure 3-37, a white font on dark fill has been applied to Revenue for Doodads sales
to the Software sector.
If you rearrange the pivot table, the dark formatting will follow the cell in the pivot table. While cell
B9 was formatted in Figure 3-37, the formatting has moved to D5 in Figure 3-38.
Note that the formatting will persist if you remove the cell due to a filter. If you unselect Software
from the Slicer and then reselect Software, the formatting will return.
However, if you remove Sector, Product, or Revenue from the pivot table, the individual cell format-
ting will be lost.
FIGURE 3-38 Rearrange the pivot table and the formatting moves so that revenue for Doodads sales to the
Software sector stays formatted.
In Figure 3-39, a Customer field has been added as the inner-row field. The dark formatting applied
to Doodads sales of Software is now expanded to C5:C8 to encompass all four customers in this group.
Note that in cell C9, the subtotal for Doodads sold to the Software sector is not formatted.
Next steps
Note that the following pivot table customizations are covered in subsequent chapters:
■■ Sorting a pivot table is covered in Chapter 4, “Grouping, sorting, and filtering pivot data.”
■■ Adding new calculated fields is covered in Chapter 5, “Performing calculations in pivot tables.”
■■ Using data visualizations and conditional formatting in a pivot table is covered in Chapter 4.
497
498
499
Data Model, 176, 225, 226, 257, 379. See also Add This deleting
DataTo The Data Model checkbox calculations, 146–147
adding tables to, 379–380 data from pivot tables, 335
linked tables, 275–276 pivot table(s), 162
new tables, 180 dialog boxes
big data and, 273–275 Create PivotTable, 20–21
building out your first, 176–179 Custom Lists, 87–89
creating a pivot table from, 278, 282–283 Grouping, 107–111
defining the pivot cache, 381 Insert Slicers, 31
Distinct Count, 262–264 Macro, 309
Excel Online and, 462 Manage Relationships, 179–180
limitations of, 180 Measure, 266–267
model fields, adding to the pivot table, 381 Modify Slicer, 433
numeric fields, adding to the Values area, 381–382 Move PivotTable, 41
overcoming limitations of, 281–282 Record Macro, 302
Power Pivot and, 257 Report Connections, 33
primary key, 177–178 Select Table, 183
relationships Sort, 84–85. See also sorting
creating, 259–260, 380–381 Value Field Settings, 64–68
managing, 179–180 adding and removing subtotals, 72–74
replacing VLOOKUP with, 257–261 displaying a change from a previous field, 70–71
unlocking hidden features with, 261–262 Index option, 71–72
VBA and, 382–384 running total calculations, 69–70
data ranges, automatically expanding, 401–402 Show Values As tab, 360–362
data source(s), 11, 479–481. See also external data sources showing percentage of total, 68
adding calculated fields to, 122–123 showing rank, 69
cleaning up, 15–19 tracking the percentage of a parent item, 71
deleting, 401 using % Of to compare one line to another line, 68
external, building a pivot table using, 180–181 Dickerman, Howie, 456
Microsoft Access, building a pivot table using, 181–184 dimensions, 2266
Power Query and, 202 disparate data sources, 175. See also Data Model; data
preparing source(s)
applying formatting to fields, 14 docking the Fields list, 79
avoid repeating groups as columns, 13–14 document themes, 61–64
eliminating gaps and blank cells, 14 dragging and dropping, 26–27
ensuring the data is in Tabular layout, 12, 14–15 drill-down hierarchy, creating, 219–220
section headings, 12–13 DSUM function, 468–470
preparing for 3D Map, 285–286 Dynamic Arrays, 495
transposing, 202–204 dynamic reports, creating with GetPivotData
updating, 37 build the shell report, 446–447
database centric layout, 12 create a pivot table, 443–446
Date filters, 97–98. See also timelines populate the shell report, 448–451
dates, summarizing, 342–344 updating the report in future months, 451–452
DAX (Data Analysis Expressions), 121, 144, 145
CALCULATE function, 280
median calculation, 266–268 E
deferring updates, 40–41 editing, calculations, 146–147
Excel, versions, 328–329
500
Excel Online, 384, 453, 462 filter(s), 89–90. See also slicers
Data Model and, 462 Advanced, 465–467
Google Sheets and, 453 check box, 90–91
Home screen, 454–455 conceptual, 372–375
icons, 458–459 Data, 404–406
pivot tables Date, 97–98
creating, 455–458 fields, 367
layouts, 459–461 including filtered items in totals, 264–265
PivotTable Settings task pane, 459 label, 93–94
Alt Text, 461–462 pivot table report, 28
options, 461 report, 100–102
signing in, 454–455 for row and column fields, 90
executive overviews, creating in VBA, 363–365 search, 375–376
exploding a data set search box, 92
to different tabs, 414–415 setting up in VBA, 367
to different workbooks, 418–420 slicers, 102–104
external data sources controlling data from two different data sets,
building a pivot table using, 180–181 428–431
Microsoft Access, building a pivot table using, 181–184 driving multiple pivot tables from, 105–107
Power Query and, 187–188 formatting, 432–434
SQL, building a pivot table using, 184–187 Timeline, 104–105, 159–160
VBA and, 376–379
timelines, 104–105
F value, 94–95
fields. See also cells Top 10, 95–97, 264
adding to a pivot table report, 22 VBA and, 371–372
adding to Filters area, 99 Filters area, 6–7, 99
calculated. See calculated fields adding fields to, 99
date, grouping, 111–112 choosing items from, 99–100
dragging and dropping, 26–27 fine-tuning, 3D Map, 293
filter, 367 form controls
formatting, 14 creating a user interface, 304–306
geographic, 287 scrollbar, 307–310
grouping, 107–111 formatting
pivot chart, 154–156 cells, 74–76
rank number, adding to a pivot table, 399–400 conditional, 165–167
renaming, 49–50 custom rules, 168–173
Fields list, 77–79, 132–133 preprogrammed scenarios for condition levels,
Areas section, 81–82 167–168
docking and undocking, 79 numbers
minimizing, 80 adding thousands separator, 46–47
OLAP pivot tables, 238, 239 default, changing, 345–346
rearranging, 80–81 pivot charts, 156
sorting pivot tables, 332–334
high-to-low, 82–85 sections of a pivot table, 410–412
manual, 85–87 slicers, 432–434
FILTER function, 233 values, 408–410
VBA and, 348
501
G J-K-L
geocoding, 286–287 joining two data sets, 425–428
geographic data, 286 label filters, 93–94
GetPivotData function, 164, 226, 435–436, 441–442 laying out a pivot table report, 23–25
avoiding, 439 layout
building dynamic reports Compact, 51–52, 58, 77–79, 332
build the shell report, 446–447 customizing, 50
creating a pivot table, 443–446 for Excel Online, 459–461
populate the shell report, 448–451 Outline, 52–53
updating the report in future months, 451–452 Tabular, 54–56, 332, 342
calculated items and, 436–439 levels, 238
speculation on, 440–441 lookup tables, 258–260
502
503
P Columns area, 6
conditional formatting, 165–167
Photoshop, 296–299
converting to cube formulas, 226–233
pie charts, 290–291
converting to values, 337–340
pivot cache, 7, 8
creating, 19–22
formatting, 156
from the Data Model, 278, 282–283
refreshing, 7
using Excel Online, 455–458
sharing, 38–39
using TypeScript, 384–387
starting from scratch and, 41
data source(s), updating, 37–38
VBA and, 381
default settings, 43, 394–395
pivot charts, 149–150
deleting, 162
alternatives to using, 160–161
deleting data from, 335
delete the underlying pivot table, 162
Distinct Count, 262–264
distribute a picture of the pivot chart, 162–163
filling blank cells
turn the pivot table into hard values, 161–162
in the data area, 334
use cells linked back to the pivot table as the data
in the row area, 335
source, 163–165
filter fields, 367
creating, 150–152, 153–154
Filters area, 6–7, 99
fields, placement, 154–156
adding fields to, 99
pivot field buttons, 152–153
choosing items from, 99–100
pivot tables and, 154
formatting
reports and, 157–159
selecting different areas, 410–412
pivot field buttons, 152–153
with VBA, 332–334
pivot table reports, 1. See also reports
formulas, 129
creating a filter, 28
calculated items, 135–137
fields, adding, 22
creating a calculated field, 123–124
laying out, 23–25
documenting, 148
macros, 301–302. See also macros
gridlines, adding, 45–46
adding functionality, 306–307
including filtered items in totals, 264–265
recording, 302–303
layout
pivot table(s). See also advanced tips; data source(s); VBA
(Visual Basic for Applications) Compact, 51–52, 58
adding layers to, 25 Outline, 52–53
backward compatibility, 7 Tabular, 54–56
borders, 63–64 limitations, 8
building from external data sources, 180–181 naming, 34
building with VBA, 329–331 OLAP
adding fields to the data area, 331–332 adding calculations to, 245–246
calculated fields limitations of, 240
creating, 125–133 performing what-if analysis, 253–254
inserting, 124–125 pivot charts and, 154
rules, 139–145 preparing the data
calculated items applying formatting to fields, 14
creating, 134–137 avoid repeating groups as columns, 13–14
rules, 145–146 eliminating gaps and blank cells, 14
calculations, 146 ensuring the data is in Tabular layout, 12, 14–15
cell ranges and, 139 section headings, 12–13
editing and deleting, 146–147 producing a static summary from, 338–340
named ranges and, 139 rearranging, 25–27
order of operations, 138 reasons for using, 2–3
504
505
S
scrollbar form control, inserting, 307–310 T
search box, filtering and, 92 tables. See also relationships
search filter, 375–376 adding to the Data Model, 180, 275–276
section headings, 12–13 comparing using a pivot table, 402–404
security, macros and, 303 relationships, creating, 259–260
Select Table dialog box, 183 Tabular layout, 12, 14–15, 54–56, 332
settings, 3D Map, 292 Excel Online and, 459–461
sharing, pivot cache, 38–39 totals and, 332
Show Report Filter Pages feature, 414–415 VBA and, 342
ShowDetail property, filtering recordsets with, 365–367 themes, 61–64
ShowPages method, 368 time intelligence, 280–281
signing in to Excel Online, 454–455 Timeline slicer, 159–160
SlicerCache method, 377 backward compatibility and, 36
slicers, 7, 31, 102–104 creating, 34–36
controlling data from two different data sets, timelines, 104–105
428–431 tools, Visual Basic, 324
506
507
508