Chapter 10. Building More Advanced PivotTables

Create a PivotTable from Multiple Consolidation Ranges

If your source data exists in two or more ranges, Excel can consolidate all the ranges and then produce a PivotTable report based on the consolidated data.

Many businesses create worksheets for a specific task and then distribute them to various departments. The most common example is budgeting. Accounting might create a generic "budget" template that each department or division in the company must fill out and return. Similarly, you often see worksheets distributed for inventory requirements, sales forecasting, survey data, experiment results, and more.

Creating these worksheets, distributing them, and filling them in are all straightforward operations. The tricky part, however, comes when the sheets are returned to the originating department where all the new data must be combined into a summary report showing company-wide totals. This task is called consolidating the data, and it is often difficult and time-consuming, especially for large worksheets. However, Excel has a powerful PivotTable feature that can make it easy to consolidate the data and summarize it into a simple report.

Create a PivotTable from Multiple Consolidation Ranges

Note

This chapter uses the spreadsheets Division_I_Budget.xls, Division_II_Budget.xls, Division_III_Budget.xls, and PivotTables.xls, and the Web page Orders.htm, available at www.wiley.com/go/pivottablesvb. You can also create your own sample database.

Create a PivotTable from Multiple Consolidation Ranges

The PivotTable and PivotChart Wizard appears.

Create a PivotTable from Multiple Consolidation Ranges
Create a PivotTable from Multiple Consolidation Ranges
Create a PivotTable from Multiple Consolidation Ranges
Create a PivotTable from Multiple Consolidation Ranges

The next PivotTable and PivotChart Wizard dialog box appears.

Create a PivotTable from Multiple Consolidation Ranges
Create a PivotTable from Multiple Consolidation Ranges

The next PivotTable and PivotChart Wizard dialog box appears.

Create a PivotTable from Multiple Consolidation Ranges
Create a PivotTable from Multiple Consolidation Ranges

Excel collapses the dialog box to show just the Range input box.

In a PivotTable based on multiple consolidation ranges, Excel only offers a limited layout: a row field, a column field, a value (data) field, and up to four page fields. The items in the row field come from the leftmost columns of the source data ranges; the items in the column field come from the topmost row in the source data ranges; and the items in the value field come from the rest of the source data ranges.

For the page field, Excel sets up the report so that you can display the data from all the ranges or just the data from one of the ranges. In other words, Excel enables you to filter the PivotTable report based on the source ranges, and it uses the page field to do this.

However, if you let Excel set up this page field for you, it uses generic item names such as Item1, Item2, and Item3. To avoid the hassle of renaming these items after you create the PivotTable, you can specify them as you work with the PivotTable and PivotChart Wizard. That is why you selected the "I will create the page fields" option in Step 5. In Steps 12 to 15 of this task, you define the page field item names yourself.

Apply It
Apply It

Excel restores the PivotTable and PivotChart Wizard.

Apply It
Apply It
Apply It
Apply It
Apply It
Apply It
Apply It
Apply It

The final PivotTable and PivotChart Wizard dialog box appears.

Apply It
Apply It
Apply It
Apply It

Excel consolidates the ranges and creates the PivotTable.

Create a PivotTable from an Existing PivotTable

You can save time and effort by creating a new PivotTable based on the data in an existing PivotTable.

You learned in several places throughout this book that Excel maintains a pivot cache for each PivotTable. This pivot cache is a memory location that holds the source data and other information relating to the PivotTable. Keeping this data in memory means that your PivotTable recalculates quickly when you change the layout, grouping, filtering, or summary calculation. The price you pay for having the pivot cache is extra workbook size and less memory available for other tasks, but the tradeoff is usually worth it.

You can minimize the downside of the pivot cache by building new PivotTables based on existing PivotTables wherever possible. If you build a new PivotTable using the same data source as an existing PivotTable, Excel asks if you want to base the new report on the existing report. However, you can also build a new PivotTable from an existing PivotTable on purpose. Doing this not only means that the two PivotTables share the same pivot cache, but it also takes less time and effort to build the new PivotTable.

Create a PivotTable from an Existing PivotTable

Create a PivotTable from an Existing PivotTable

The PivotTable and PivotChart Wizard appears.

Create a PivotTable from an Existing PivotTable
Create a PivotTable from an Existing PivotTable
Create a PivotTable from an Existing PivotTable
Create a PivotTable from an Existing PivotTable

The second PivotTable and PivotChart Wizard dialog box appears.

Create a PivotTable from an Existing PivotTable
Create a PivotTable from an Existing PivotTable
Create a PivotTable from an Existing PivotTable

Excel creates an empty PivotTable.

Create a PivotTable from an Existing PivotTable
Create a PivotTable from an Existing PivotTable

Create a PivotTable from External Data

You can create a PivotTable using an external data source, which enables you to build reports from extremely large datasets and from relational database systems.

So far in this book you have learned about data sources that reside on Excel worksheets as ranges or lists. This is a convenient way to work with PivotTables because you have access to the source data, enabling you to easily change field names, add and delete fields, insert records, and so on. However, working with data in Excel suffers from two major drawbacks:

  • Excel offers only simple row-and-column database management. You cannot use Excel to perform relational database management where, when two or more datasets are related on a common field, you can combine those datasets in powerful ways.

  • Excel worksheets are limited to 65,536 rows, so that is the maximum number of records you can have in a range or list data source.

To overcome these limitations, you need to use a relational database management system (RDBMS) such as Microsoft Access or SQL Server. With these programs, you can set up a table, query, or other object that defines the data you want to work with. In most cases, the data object can be as complex and as large as you need. You can then build your PivotTable based on this external data source.

Create a PivotTable from External Data

Create a PivotTable from External Data

The PivotTable and PivotChart Wizard appears.

Create a PivotTable from External Data
Create a PivotTable from External Data
Create a PivotTable from External Data
Create a PivotTable from External Data

The second PivotTable and PivotChart Wizard dialog box appears.

Create a PivotTable from External Data

The Choose Data Source dialog box appears.

Create a PivotTable from External Data
Create a PivotTable from External Data

Note

To learn how to create data sources, see the Appendix B task "Define a Data Source."

Create a PivotTable from External Data

The Query Wizard's Choose Columns dialog box appears.

Create a PivotTable from External Data
Create a PivotTable from External Data
Create a PivotTable from External Data
Create a PivotTable from External Data

The Query Wizard's Filter Data dialog box appears.

The Choose Data Source dialog box and the various Query Wizard dialog boxes are not part of Excel. Instead, they are components of a program called Microsoft Query. You can use this program to work with external data. For more detail on how this program works, see Appendix B. For the purposes of this task, I assume that you have already defined the appropriate data source, as shown in the Appendix B task "Define a Data Source," and that you do not want to work with Microsoft Query directly; see the Appendix B task "Start Microsoft Query." Note, too, that Steps 10 and 11 essentially skip over the Query Wizard dialog boxes that enable you to filter and sort the external data, because this is not usually pertinent for a PivotTable report. For the details of these steps, in Appendix B, see the task "Define a Data Source."

The other assumption I made in this task is that you do not want the external data imported to Excel. Rather, in this task the external data resides only in the new PivotTable's pivot cache; you do not see the data itself in your workbook. This is particularly useful if the external data contains more than 65,536 records, because otherwise Excel would not allow you to import so much data. However, you can still easily refresh and rebuild your PivotTable, just like you can with a report based on a local range or list. If you want to learn how to import external data into Excel, see Appendix C.

Extra

The Query Wizard's Sort Order dialog box appears.

Extra
Extra

The Query Wizard's Finish dialog box appears.

Extra
Extra

The second PivotTable and PivotChart Wizard dialog box appears.

Extra

Excel creates an empty PivotTable.

Extra
Extra
Extra

Set Up a Server-Based Page Field

If the external source contains a large amount of data, Excel may run very slowly or it may display an out of memory error. You can work around these problems by setting up a server-based page field that retrieves page data only when you request it from the server.

When you connect to an external data source, and when you refresh a PivotTable based on an external data source, Excel retrieves all the data from the external file. If you are dealing with a database on a remote server, or if the source contains a large amount of data, retrieving all the data may take a very long time.

Part of the problem is that Excel retrieves all the data even if you are currently displaying the results for just a single page field: Excel still retrieves the data for those page fields that are not currently displayed. This increases the performance of the PivotTable when you switch pages, but it greatly increases the amount of memory that Excel uses and it slows down the refreshing of the PivotTable.

To overcome these problems, you can configure the page field as a server page field. This means that Excel only retrieves the data for the currently displayed page field. When you display a different page, Excel queries the server and retrieves the new data — this is called querying page-by-page. This means it takes longer to switch pages, but it greatly reduces the amount of memory used by Excel, and it speeds up the refreshing of the PivotTable.

Note that querying page-by-page is only available for certain types of external data, such as SQL Server data sources. This technique is unreliable when used with Access databases.

Set Up a Server-Based Page Field

Set Up a Server-Based Page Field
Set Up a Server-Based Page Field

You can also double-click the page field button.

The PivotTable Field dialog box appears.

Set Up a Server-Based Page Field
Set Up a Server-Based Page Field

The PivotTable Field Advanced Options dialog box appears.

Set Up a Server-Based Page Field
Set Up a Server-Based Page Field

Excel returns you to the PivotTable Field dialog box.

Set Up a Server-Based Page Field
Set Up a Server-Based Page Field
Set Up a Server-Based Page Field

Set External Data Options

Excel offers several options related to PivotTables built using external data sources. You can use these options to save time and effort and to reduce the amount of memory used by these PivotTables. In the PivotTable Options dialog box, there are three check boxes related to external data sources:

  • Save password — Some external data sources require a login name and password to access the data. By default, Excel does not save the login password in the pivot cache. If you close the workbook, reopen it, and then refresh the PivotTable, you must log in again to the external data source. To prevent this, activate the "Save password" check box, and Excel saves the login data with the PivotTable.

  • Background query — When you refresh the PivotTable, Excel queries the external data source to retrieve the latest data. While the query is in progress, you cannot do anything else in Excel. This is fine if the query takes only a few seconds, but it can be a problem for queries that take a very long time. If you activate the "Background query" check box, Excel continues the query in the background and allows you to perform other work in Excel.

  • Optimize memory — When Excel retrieves data from an external source, it stores the data in the pivot cache. By default, Excel adds each record to the cache as it is retrieved, and the size of the cache grows accordingly. However, Excel has the ability to reduce the size of the cache, but only if it knows in advance how many records the query is going to return. If you activate the "Optimize memory" check box, Excel interrogates the external data source to determine how many records the query retrieves, and uses that information to reduce the pivot cache size as much as possible. Note, however, that it takes Excel extra time to interrogate the external data source, so activating this option may slow down the PivotTable's performance.

Set External Data Options

Set External Data Options
Set External Data Options
Set External Data Options

The PivotTable Options dialog box appears.

Set External Data Options
Set External Data Options
Set External Data Options
Set External Data Options
Set External Data Options

Excel puts the external data source options into effect.

Export PivotTable Data from a Web Page to Excel

If you are working with a PivotTable list on a Web page, you can export that PivotTable's data from the Web page to Excel.

In Chapter 3, you learned how to publish a PivotTable to a Web page; see the task "Publish a PivotTable to a Web Page." The resulting Web page-based PivotTable is displayed using an Office Web component called a PivotTable list. If you are working with a PivotTable list on a Web page, you can also perform the opposite technique and export the PivotTable list to Excel. This gives you a regular Excel PivotTable in a read-only file, which you can then save to your computer.

Export PivotTable Data from a Web Page to Excel

Export PivotTable Data from a Web Page to Excel
Export PivotTable Data from a Web Page to Excel
Export PivotTable Data from a Web Page to Excel

If the PivotTable list contains features not supported by PivotTable reports, a warning dialog box appears.

Export PivotTable Data from a Web Page to Excel
Export PivotTable Data from a Web Page to Excel

The PivotTable appears in a temporary Excel worksheet.

Reduce the Size of PivotTable Workbooks

You can reduce the size of your Excel workbooks by not saving the PivotTable source data in the pivot cache.

If you build a PivotTable from data that resides in a different workbook or in an external data source, Excel stores the source data in the pivot cache. This greatly reduces the time it takes to refresh and recalculate the PivotTable. The downside is that it can increase both the size of the workbook and the amount of time it takes Excel to save the workbook. If your workbook has become too large or it takes too long to save, you can tell Excel not to save the source data in the pivot cache.

You can control whether Excel uses the pivot cache via VBA. To do this, set the PivotTable object's SaveData property to True, with the pivot cache on, or to False, with the pivot cache off, as shown in the following macro:

Sub TogglePivotCache()
    Dim objPT As PivotTable
    ' Work with the first PivotTable on the active worksheet
    Set objPT = ActiveSheet.PivotTables(1)
    ' Toggle the SaveData property
    objPT.SaveData = Not objPT.SaveData
    ' Display the current state
    MsgBox "The PivotTable's pivot cache is now " & _
           IIf(obj.SaveData, "on.", "off.")
End Sub

Reduce the Size of PivotTable Workbooks

Reduce the Size of PivotTable Workbooks
Reduce the Size of PivotTable Workbooks

The PivotTable Options dialog box appears.

Reduce the Size of PivotTable Workbooks
Reduce the Size of PivotTable Workbooks
Reduce the Size of PivotTable Workbooks

Excel puts the external data source options into effect.

Use a PivotTable Value in a Formula

When you reference a PivotTable report value in a formula, you can ensure that the reference remains accurate by using a special Excel worksheet function.

As part of your data analysis, you may need to use a value from a PivotTable report in a worksheet formula that resides outside the PivotTable. You normally reference a cell in a formula by using the cell's address. However, this does not work with PivotTables because the addresses of the report values change as you pivot, filter, group, and refresh the PivotTable.

To ensure accurate PivotTable references, use Excel's GETPIVOTDATA worksheet function. This function uses the data field, PivotTable location, and one or more (row or column) field/item pairs that specify the exact value you want to use. This way, no matter what the PivotTable layout, as long as the value remains visible in the report, your formula reference remains accurate.

If you want to reference a PivotTable value only temporarily, you might prefer that Excel not generate the GETPIVOTDATA function. To turn off this feature, click the Generate GetPivotData toolbar button (

Use a PivotTable Value in a Formula

Use a PivotTable Value in a Formula

Use a PivotTable Value in a Formula
Use a PivotTable Value in a Formula
Use a PivotTable Value in a Formula
Use a PivotTable Value in a Formula
Use a PivotTable Value in a Formula
Use a PivotTable Value in a Formula

Excel includes the PivotTable value in the formula result.

Note

The GETPIVOTDATA function looks complicated, but it really only contains a few parameters. The first parameter is the name of the data field ("Quantity" in the example); the second parameter is the location of the PivotTable ("$A$3"); subsequent parameters come in pairs: a field name ("ShipRegion") and an item in that field ("Oregon").

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.112.220