The Analysis Sheets
I use several worksheets and chart sheets to analyze what’s going on with the sales of my books. For an analysis that looks at the data over time, a pivot table (and pivot chart) is a nearly perfect solution. It collapses the hourly figures that I get from my web queries into more manageable time slices—I find that summarizing by day strikes a good balance between the overwhelming amount of data in an hourly analysis and the lack of detail in a monthly or even weekly analysis. Of course, when your baseline extends over several hundred days, it’s time to start thinking about weeks or months as your unit of analysis.
Figure 1.10 shows one of the pivot tables that I rely on to synthesize the data on sales rankings.
Figure 1.10. When you connect this pivot table to a pivot chart, you can start making real sense of the data.
As powerful as pivot tables are—and I believe that pivot tables are the most powerful and flexible tool for data synthesis and analysis available in Excel—they can’t tell when you have changed the underlying data, and (without help) they can’t tell that their underlying data range has added another row.
In contrast, something as simple as the SUM() function can update itself when the underlying values change. If you have entered this formula
in some cell, the value it returns changes immediately if you change any of the values in A1:A5. That’s not true of a pivot table that’s based on those cells or any other cell. You have to do something special to refresh the table when the underlying data changes.
But even SUM() won’t change its own argument. If you now put a new value in A6, SUM() doesn’t change itself from SUM(A1:A5) to SUM(A1:A6).
The way I prefer to handle that is by means of dynamic range names. You might already know that you can assign a name to a worksheet range and use that name instead of a range address. If you have given A1:A5 the name Addends, you can use this instead of SUM(A1:A5):
I show you how to name a range shortly. First, you should know that if you define a range properly, you can get it to change its own dimensions when you add a new row or column to it. (Tables, new in Excel 2007, do that automatically, and you should probably investigate their capabilities. I still prefer dynamic range names because of a possibly irrational belief that I can control them better.)
Defining a Dynamic Range Name
Begin by selecting the worksheet that contains the range that you want to name. This step is not strictly necessary, but I usually find it helpful. Then follow these steps:
- Click the Ribbon’s Formulas tab.
- Click the Define Name button in the Defined Names group. The New Name dialog box shown in Figure 1.11 appears.
Figure 1.11. You can use a formula instead of an address in the Refers To box.
- Enter the name you want to establish in the Name box.
- In this case, leave the Scope with the default value of Workbook.
- I base the pivot table on the data in my Summary sheet, shown in Figure 1.7. With the Summary sheet’s layout as shown there, type the following formula in the Refers To box:
- Click OK.
Here’s a quick explanation of the formula. The OFFSET() function returns a range of cells that are offset from an anchor cell. Here, the anchor cell is defined as A2 on the Summary sheet. Notice in Figure 1.7 that cell A2 is where the label “When” is entered, and more labels follow it in row 2, columns B through I. I want to include those labels in the defined range because they’re needed for the pivot table.
The two zeros that follow $A$2 in the OFFSET() function tell Excel how many rows and how many columns away from A2 the resulting range should begin. In this case, because both values are zero, the resulting range is offset by zero rows and zero columns: that is, the range begins at cell A2.
The fourth argument to the OFFSET() function, COUNTA(Summary!$B:$B), tells Excel how many rows to include in the offset range. The COUNTA() function tells Excel to count the number of values in (here) column B on the Summary worksheet. So when the VBA code runs and adds a new value in column B, the COUNTA function counts an additional value and redefines the number of rows in the range. That’s how adding a new row at the bottom of the range causes the definition of the range to increase by one row.
The final argument to the OFFSET() function tells Excel how many columns to include in the result range. Here I want to include three columns: one for the date, one for the Stats book rankings, and one for the BAXL book rankings.
It’s called a dynamic range name because the dimensions of the range can change depending on the number of records or fields, or both, that are added to or removed from the range.
Using the Dynamic Range Name
With the dynamic range name defined, you can use it when you create a pivot table. Here’s how to do that:
- Begin by activating a blank worksheet and then click the Ribbon’s Insert tab.
- Click Pivot Table in the Tables group. The dialog box shown in Figure 1.12 appears.
Figure 1.12. Use the dynamic range name you defined in the Table/Range box.
- Type the name you supplied in Step 3 of the prior numbered list into the Table/Range box. This causes Excel to use that range as the data source for the pivot table.
- Click OK. The PivotTable Field List appears.
- Drag the When field into the Row Labels box.
- Drag the StatsR field into the Σ Values box.
- Drag the BAXLR field into the Σ Values box.
- Dismiss the PivotTable Field List by clicking its Close box. The pivot table now appears as shown in Figure 1.13.
Figure 1.13. You still need to group on date and to show the average instead of the sum.
- Right-click in the date column of the pivot table and choose Group from the shortcut menu.
- Grouping by Month is the default selection. Click it to deselect it, and click Day to select it instead. Click OK.
- The pivot table might seem to disappear from the visible worksheet. That can happen when the grouping operation causes the pivot table to occupy many fewer rows. Just scroll up to it and then right-click in the column for the first book’s rankings. Choose Value Field Settings from the shortcut menu.
- Change the summary type from Sum to Average and click OK.
- Right-click in the column for the second book’s rankings, choose Value Field Settings, and change the summary type from Sum to Average. Click OK.
The pivot table should now appear as shown in Figure 1.14.
Figure 1.14. You can also use the Value Field Settings to select a display format for the summary values in the pivot table.
A pivot table such as the one in Figure 1.14 isn’t very informative by itself. There are too many data points to make any useful inferences from it. Fortunately, it’s easy to create a pivot chart from a pivot table. Just select any cell in the pivot table, click the Ribbon’s Insert tab, and choose (say) a Line chart from the Charts group.
You get a chart embedded in the active worksheet, and you can usually tell more about your data, at least on a macro level, from the chart than directly from the pivot table (see Figure 1.15).
Figure 1.15. You usually need to do some tinkering with the pivot chart before it starts to show you what you need to know.
One of the useful aspects of pivot charts is that they don’t need any special handling to keep up with the underlying pivot table. When the pivot table’s data changes, or gets more rows, the pivot chart updates automatically.
You do need a way to force a refresh of the pivot table. There are many ways to handle that. Some are mildly complicated, such as writing a special Worksheet Activate event handler using VBA. That event handler would force a refresh any time you activate the worksheet that contains the pivot table. Probably the simplest way is to right-click any cell in the pivot table and to choose Refresh from the shortcut menu.
Among the tweaks I like to apply to a newly created pivot chart are the following:
- Right-click an embedded chart, click Move in the shortcut menu, and choose to move the chart to a new sheet.
- Right-click a data series in the chart and choose to format it. Reduce the width of the lines and change any obnoxious color to a more acceptable one—black, for example.
- Add a trendline. Right-click a data series and choose Add Trendline from the shortcut menu. In line charts such as the one in Figure 1.15, linear trendlines and moving average trendlines are often very informative. Tinker with the length of a moving average trendline until you get something that smooths out the rough in the series but preserves its basic movement. Chapter 3, “Forecasting with Moving Averages,” and Chapter 4, “Forecasting a Time Series: Smoothing,” provide guidance on selecting the length of a moving average.