21.2. Building Pivot Tables
Now that you've learned the role that pivot tables play in summarizing data, it's time to create your own. Before you begin, you need to have a long list of raw data that you want to summarize. You could use the customer list from the previous example, but it's too small to really demonstrate the benefits of pivot tables. A better example is something like the list of order information shown in Figure 21-4.
Not all data is suited for a pivot table. To work well, your data needs to meet a few criteria:
Note: It's technically possible to create subtotals without using a numeric column. In this case, the subtotals just count the number of values in the group. This approach is occasionally useful, but it's not as powerful as other types of subtotals.
The order information table is perfect for a pivot table because there are several columns you can use to group the order rows. These include:
A pivot table can handle all of these comparisons. You don't need to choose one column or another before you start building the pivot table.
Tip: The best way to learn about pivot tables is to perform the steps in this chapter, and then start experimenting. If you don't happen to have a table with hundreds of records on hand, you can download the workbook shown in Figure 21-4 from the "Missing CD" page at www.missingmanuals.com. It gives you 2,155 rows to summarize and pivot to your heart's content.
21.2.1. Preparing a Pivot Table
Creating a new pivot table is a two-step process. First you need to run the Pivot-Table and PivotChart wizard, which asks you to identify the data you want to summarize and select the location where you want to place the pivot table. The next step is to actually define the structure of the pivot table and try out different ways of organizing and grouping your data.
The following steps lead you through the first step in creating a new pivot table:
Tip: If you choose to create a new worksheet, Excel gives the worksheet an unhelpful name (like Sheet5), and then places it before all your other worksheets. You can rename the worksheet and drag it into a better spot using the techniques described in Chapter 4.
Note: When you create an empty pivot table in compatibility mode (Section 220.127.116.11), it looks a little different. Instead of the graphical box shown in Figure 21-6, you get a series of linked white boxes, with messages inviting you to drop fields in each one. Microsoft designed this strange appearance to mimic the way that blank pivot tables appeared in Excel 2003. But don't worryonce you create a pivot table, it'll look much more similar, and the instructions in this chapter work just as well whether or not you're in compatibility mode.
21.2.2. Pivot Table Regions
To build a pivot table, simply drag columns from the PivotTable Field List pane on the right side of the Excel window, and drop them into one of the four boxes underneath. As you work, Excel generates the pivot table, updating it dynamically as you add, rearrange, or remove columns.
Note: Excel refers to all your source data's columns as fields .
To understand how to fill up a pivot table with data, you need to know how each region works. Altogether, a pivot table includes four regions:
It really doesn't matter whether you use a field for row grouping or column grouping. The pivot table shows the same data either way, but one approach may be more readable than another. If you have a field with extremely long names , it probably works better as a row field than as a column field (where it would stretch out the width of the whole column).
Also, consider how many different groups you want to create. If you want your pivot table to compare sales by category and country, and your list features five categories and 20 countries , you'll probably be best off if you use the country field as a row field and the category field as a column field. That keeps the table long and narrow, which is easier to read and print.
21.2.3. Laying Out a Pivot Table
To get a better understanding of how to create a pivot table, it helps to follow along with a basic example. These steps lead you through the process of creating a summary that compares the products and shipping locations shown in Figure 21-4.
Tip: Pivot tables also calculate row and column subtotals. If you want to find the total number of units shipped for a given product across all countries, scroll to the far right end of the chart. If you want to find the total number of units sold in a given country, scroll to the totals at the bottom of the chart.
This example built a fairly sophisticated two-dimensional pivot table, which means that it compares two different groupings (one represented with rows, and the other represented with columns). Most of the pivot tables you'll see in real life are two-dimensional, but, there's no reason you can't create simpler one-dimensional pivot tables. All you need to do is leave out either the Column Labels or Row Labels. Figure 21-9 shows a pivot table that simply totals the number of units sold for particular products.
Unlike most other elements in Excel, pivot tables don't refresh themselves automatically. That means that if you change the source data, the pivot table may show out-of-date totals. To correct this problem, you can refresh the pivot table by moving to one of the cells in the pivot table and selecting PivotTable Tools Options Data Refresh (or the keyboard shortcut Alt+F5). This action tells Excel to scan the source data and regenerate the pivot table.
21.2.4. Formatting a Pivot Table
As you've probably already noticed, when you move into one of the cells of a pivot table, two new tabs spring up in the ribbon under the PivotTable Tools heading. These are similar to the tabs that appear when you select a chart, picture, or table.
You can use the PivotTable Tools Options tab to access a few advanced features that you've yet to consider, like grouping, pivot table formulas, and pivot charts . You'll consider these features in the rest of this chapter. The PivotTable Tools Design tab is more modest; you use it to format and otherwise fine-tune the appearance of your pivot table.
The PivotTable Tools Design tab is carved into three sections. At the far right, you'll find the PivotTables Styles section, with a familiar style gallery. If you choose one of the entries here, Excel adjusts your PivotTable automatically, giving it new colors and shading that can range from subtle to dramatic.
Tip: The colors that Excel uses in the pivot table styles actually come from your workbook theme (Section 6.2.4). So if you want to get a completely different set of accent colors, just choose a new theme from the Page Layout Themes Themes gallery.
PivotTable Style Options section of the ribbon. If you want to use a specific style but you dont want to apply its banding effects, clear the Banded Rows and Banded Columns checkboxes. Similarly, if you don't want to apply the style formatting to headers, clear the Column Headers or Row Headers checkboxes.
Finally, the PivotTable Tools Design Layout section lets you choose from various preset options that control spacing and subtotals. This section contains four submenus:
Note: In previous versions of Excel, pivot tables were apt to lose their formatting every time you refreshed them to include new data. But in Excel 2007, pivot tables are much better behaved. Even if you format the pivot table by hand (by applying different fonts and colors to individual cells, for instance), the formatting sticks when you refresh the pivot table.
21.2.5. Rearranging a Pivot Table
So far, you've seen how to use a pivot table to quickly build a summary table. However, pivot tables have another key benefit: flexibility. There's no limit to how many times you can move fields or recalculate your summary so that it performs different calculations.
To change a pivot table, you can use the following techniques in the PivotTable Field List pane:
Figure 21-10 shows one way you could rearrange the pivot table shown in Figure 21-8.