Beruflich Dokumente
Kultur Dokumente
Database
by Charley Kyd on August 11, 2012
You can use a Pivot Table as a database in the same way that you can use Simple Tables or Excel
Tables.
However, Ive never seen a description of how to do it. Thats too bad, because Excel 2010 gave
Excel users the ability to use one or more Pivot Tables as a massive and powerful spreadsheet
database.
If you dont set up your Pivot Table as a database, you typically must use the GETPIVOTDATA
function to return data from it. That limits your power, because GETPIVOTDATA is a screenscraper function. That is, it only can return the numbers and text you see on your screen.
On the other hand, if you do set up your Pivot Table as a database, you can use Excels more
powerful functions with it, functions like SUMIFS, SUMPRODUCT, INDEX, MATCH, and so
on. In future posts, Ill show you how these functions can give you significantly more power to
return results from your spreadsheet database.
Using Pivot Tables as a database offers many general advantages. But theres one specific
advantage thats worth considering
3. Set up your report workbooks to return data from the PivotTable database in each
PowerPivot workbook.
With this approach, you need to maintain only one workbook to contain each PowerPivot
dataset. But you can create any number of report workbooks that can return data from any
number of PivotTable databases contained in any number of PowerPivot workbooks.
With this approach, you can give each report workbook access to a massive amount of data! And
you only need to manage one copy of each PowerPivot dataset.
But for now, lets set up a PivotTable database in a spreadsheet
For convenience, I defined this Pivot Table to get its data from
the Excel Table I explained in Introducing Excels Three Types of Spreadsheet Databases. But
your Pivot Table probably will be linked to your data warehouse, to Access, or to some other
real database.
By default, your initial Pivot Table will look something like this figure.
You cant use this version of the Pivot Table as a spreadsheet database, because the layout is too
complex; its not in simple rows and columns. Here, using GETPIVOTDATA is your only
practical option for returning its data.
But Excel 2010 gives you the option to change the format of this PivotTable into one you CAN
use as a database. To do so, first select any cell in the Pivot Table. Then in Pivot Table Tools,
choose
1. Design, Layout, Subtotals, Do Not Show Subtotals
2. Design, Layout, Grand Totals, Off for Rows and Columns
3. Design, Layout, Report Layout, Show in Tabular Form
4. Design, Layout, Report Layout, Repeat All Item Labels
5. Design, Layout, Blank Rows, Remove Blank Line After Each Item
6. Options, Show, +/- Buttons (hiding the buttons)
Dynamic range names are defined using spreadsheet functions that return references, usually the
OFFSET or INDEX functions. The references that these functions return point to new areas of
your worksheet in response to changes in the values of their arguments.
In the following figure, Ill show you how I defined the Sales.DateTime range name using the
OFFSET function. But first, I needed to set up a formula that returns the number of rows in the
Pivot Table.
This formula relies on two characteristics of Excel. First, the COUNT function counts only
numbers in cells, not text. (COUNTA, on the other hand, counts both numbers and text.) Second,
dates are numbers.
Therefore, because every row in this table has a date, we can count the number of rows in the
Pivot Table by counting the number of dates in column A.
Here are three additional things you should know about this setup:
First, notice that the dates are in a different column than the COUNTA formula. Thats necessary
to avoid a circular calculation error.
Second, its okay if your data doesnt include dates. Often, you can count the numbers in a
column of numeric values. If thats not possible, you can use the COUNTA function to count the
numbers and text in a column. In this case, however, you should subtract the number of cells
with content thats not in your database.
To illustrate, you also could use this formula in the cell shown:
B1: =COUNTA($A:$A)-3
Here, I subtract 3 to back out the counts for the content in cells A1, A3, and A4.
Third, notice that I assigned the name Sales.NumRows to cell B1. By starting all names
associated with this Pivot Table with Sales.where Sales is the Pivot Tables nameyour
range names will be easier to manage in Excel. Also, when you add another Pivot Table to your
workbook, you can have similar ranges based on the new tables name. For example, you could
have Costs.NumRows, SKUs.NumRows, and so on.
Here are answers to three questions that might occur to you
Is it safe to use periods in range names?
Yes. Excel treats periods in range names just like any other character. But by using them as
shown, you make it much easier to identify the names associated with each Pivot Table database.
If we change the name of a Pivot Table database, do we need to change the names of our
range names?
Not necessarily. However, your formulas will be easier to understand if you do keep the names in
sync. This is because your formulas are more clear if you return data from a Pivot Table named
Sales using range names that begin with Sales.particularly when you look at your
formulas several months after you create them. But it really doesnt matter to Excel what name
you assign to your range names.
Why do I need to set up the COUNT or COUNTA calculation in a separate cell? Cant I
just use it as part of the formula that defines my dynamic range?
Because COUNT and COUNTA must examine every cell in the range they reference, they take a
comparatively long time to calculate. That is, if we were to use them in the following formulas
they would need to recalculate each time the formula is used. But when we put the calculation in
one cell, it needs to calculate only oncewhich could reduce calculation time considerably.
=OFFSET(Data!$A$4,1,0,Sales.NumRows,1)
in the Refers-to edit box. Then choose OK.
In this formula, the function starts in cell A4, which is the top-left cell of the Pivot Table. The
reference points down one row (the second argument), and stays in the same column (the third
argument). Then the reference expands vertically by the number of rows specified by the
Sales.NumRows value. And finally, the reference is set to one column wide.
You set up the other rows similarly. When youre done, your names for this sheet will be defined
something like this:
Sales.DateTime =OFFSET(Data!$A$4,1,0,Sales.NumRows,1)
Sales.Product =OFFSET(Data!$B$4,1,0,Sales.NumRows,1)
Sales.Color =OFFSET(Data!$C$4,1,0,Sales.NumRows,1)
Sales.Amount =OFFSET(Data!$D$4,1,0,Sales.NumRows,1)
(Heres an easy way to set up the formulas for each name: When you set up the first name, copy
the OFFSET formula in the Refers-to box of the New Name dialog. Next, when you create the
other names, paste the formula into each new names Refers-to box, then edit the column letter in
the first argument as needed.)
But, of course, youll typically need to use more spreadsheet functions than merely SUM.
So thats what Ill cover in my next post.