PivotTables Tutorial
PivotTables Tutorial
Chapter 8
Using Pivot Tables
Copyright
This document is Copyright 20092012 by its contributors as listed below. You may distribute it
and/or modify it under the terms of the Creative Commons Attribution-Share Alike License
(http://creativecommons.org/licenses/by-sa/3.0/), version 3.0 or later.
All trademarks within this guide belong to their legitimate owners.
Authors
Barbara Duprey
Martin J Fox
Jean Hollis Weber
John A Smith
Feedback
Please direct any comments or suggestions about this document to:
[email protected]
Acknowledgments
This chapter is based on and updated from Chapter 8 of the OpenOffice.org 3.3 Calc Guide, which
was adapted from a German original written by Stefan Weigel and translated into English by Sigrid
Kronenberger. Other contributors to that chapter are:
Jean Hollis Weber
Claire Wood
Andy Brown
Martin Fox
Sharon Whiston
Mac equivalent
Effect
Right-click
Control+click
Ctrl (Control)
z (Command)
F5
Shift+z+F5
F11
z+T
Contents
Copyright.............................................................................................................................. 2
Note for Mac users...............................................................................................................2
Important note...................................................................................................................... 4
Introduction.......................................................................................................................... 4
Database preconditions............................................................................................................... 4
Data sources................................................................................................................................ 5
Calc spreadsheet.................................................................................................................... 5
Registered data source........................................................................................................... 5
Creating a Pivot Table.................................................................................................................. 6
Important note
Unfortunately, due to the complexities of modern software, the Pivot Table function is incompletely
implemented in this release (v3.4) of Calc.
The Group and Outline functionality for any time interval is not available.
The DataPilot dialog should have been renamed to Pivot Table to match the parent
function, but this was not done.
If you wish to use this function to its full extent, we strongly recommend that that you upgrade to
LibreOffice version 3.5, which fully implements Pivot Tables functionality.
Introduction
Many requests for software support are the result of using complicated formulas and solutions to
solve simple day to day problems. More efficient and effective solutions use the Pivot Table, a tool
for combining, comparing, and analyzing large amounts of data easily. Using Pivot Tables, you can
view different summaries of the source data, display the details of areas of interest, and create
reports, whether you are a beginner, an intermediate or advanced user.
Database preconditions
The first thing needed to work with the Pivot Table is a list of raw data, similar to a database table,
consisting of rows (data sets) and columns (data fields). The field names are in the first row above
the list.
The data source could be an external file or database. For the simplest case, where data is
contained in a Calc spreadsheet, Calc offers sorting functions that do not require the Pivot Table.
For processing data in lists, the program needs to know where in the spreadsheet the table is. The
table can be anywhere in the sheet, in any position. A spreadsheet can contains several unrelated
tables.
Calc recognizes your lists automatically. It uses the following logic: Starting from the cell youve
selected (which must be within your list), Calc checks the surrounding cells in all 4 directions (left,
right, above, below). The border is recognized if the program discovers an empty row or column, or
if it hits the left or upper border of the spreadsheet.
This means that the described functions can only work correctly if there are no empty rows or
columns in your list. Avoid empty lines (for example for formatting). You can format your list by
using cell formats.
Rule
If you select more than one single cell before you start sorting, filtering or calling the Pivot Table,
then the automatic list recognition is switched off. Calc assumes that the list matches exactly the
cells you have selected.
Rule
For sorting, filtering, or using the Pivot Table, always select only one cell.
A relatively common source of errors is to inadvertently declare a list by mistake and then sort the
list. If you select multiple cells (for example, a whole column) then the sorting mixes up the data
that should be together in one row.
In addition to these formal aspects, the logical structure of your table is very important when using
the Pivot Table.
Rule
Calc lists must have the normal form; that is, they must have a simple linear
structure.
When entering the data, do not add outlines, groups, or summaries. Here are some mistakes
commonly made by inexperienced spreadsheet users:
1) You made several sheets, for example, a sheet for each group of articles. Analyses are
then possible only within each group. Analyses for several groups would be a lot of work.
2) In the Sales list, instead of only one column for the amount, you made a column for the
amounts for each employee. The amounts then had to be entered into the appropriate
column. An analysis with the Pivot Table would not be possible any more. In contrast, one
result of the Pivot Table is that you can get results for each employee if you have entered
everything in one column.
3) You entered the amounts in chronological order. At the end of each month you made a
sum total. In this case, sorting the list for different criteria is not possible because the Pivot
Table will treat the sum totals the same as any other figure. Getting monthly results is one
of the very fast and easy features of the Pivot Table.
Data sources
At this time, the possible data sources for the Pivot Table are a Calc spreadsheet or an external
data source that has to be registered in LibreOffice.
Calc spreadsheet
The simplest and most often used case is analyzing a list in a Calc spreadsheet. The list might be
updated regularly or the data might be imported from a different application.
For example, a list can be copied from a different application and pasted into Calc. The behavior of
Calc while inserting the data depends on the format of the data. If the data is in a common
spreadsheet format, it is copied directly into Calc. However, if the data is in plain text, the Text
Import dialog (Figure 1) appears after you select the file containing the data; see Chapter 1,
Introducing Calc, for more more information about this dialog.
Calc can import data from a huge number of foreign data formats, for example from other
spreadsheets (Excel, Lotus 1, 2, 3), from databases (like dBase), and from simple text files
including CSV formats.
The drawback of copying or importing foreign data is that it will not update automatically if there are
changes in the source file. With a Calc file you were previously limited to 65,535 rows but this has
been expanded to 1,048,576 rows.
Introduction
Basic layout
In the DataPilot dialog (Figure 3) are four white areas that show the layout of the result. Beside
these white areas are buttons with the names of the fields in your data source. To choose a layout,
drag and drop the field buttons into the white areas.
The Data Fields area in the middle must contain at least one field. Advanced users can use more
than one field here. For the Data Field an aggregate function is used. For example, if you move the
sales field into the Data Fields area, it appears there as Sum sales.
Figure 4: DataPilot field order for analysis, and resulting layout in pivot table
Fields that are placed into the Page Fields area appear in the result above as a drop down list. The
summary in your result takes only that part of your base data into account that you have selected.
For example, if you use employee as a Page Field, you can filter the result shown for each
employee.
To remove a field from the white layout area, just drag it past the border and drop it (the cursor will
change to a crossed symbol), or select it and click the Remove button.
More options
To expand the DataPilot dialog and show more options, click More.
In this case the word - undefined is misleading because the output position is in fact defined.
Identify categories
With this option selected, if the source data has missing entries in a list and does not meet the
recommended data structure (see Figure 6), the Pivot Table adds it to the listed category above
it. If this option is not chosen, then the Pivot Table inserts (empty) (see Figure 8).
for employees for that field and displaying the totals for east. If this function is disabled, the
double-click will keep its usual edit function within a spreadsheet.
10
The table below lists the possible types of displayed value and associated base field and item,
together with a note on usage.
Type
Base field
Base item
Analysis
Normal
Difference
from
Selection of a field
from the data source
of the DataPilot (for
example,
employee).
Selection of an
element from the
selected base field (for
example, Brigitte).
% of
Selection of a field
from the data source
of the DataPilot (for
example, employee)
Selection of an
element from the
selected base field (for
example, Brigitte)
%
difference
from
Selection of a field
from the data source
of the DataPilot (for
example, employee)
Selection of an
element from the
selected base field (for
example. Brigitte)
Running
total in
Selection of a field
from the data source
of the DataPilot (for
example, date)
% of row
% of
column
% of total
Index
11
Figure 11: Original Pivot Table (top) and a Difference from example (below)
12
Figure 15: Division of the regions for employees (two row fields) without subtotals
Figure 16: Division of the regions for employees with subtotals (by region)
The DataPilot dialog
13
In the Subtotals section of the Data Field dialog, select the Automatic option to calculate subtotals
that can also be used for the data fields (see above).
You can choose the type of subtotal by selecting User-defined and then clicking the type of
subtotal you want to calculate from the list. Functions are only available when User-defined is
selected.
You can remove a column, row, or page field from the Pivot Table by clicking on it and dragging it
out of the table. The cursor changes to that shown in Figure 22. A field removed in error cannot be
recovered and it is necessary to return to the DataPilot to replace it.
15
determined mainly by the type of values that have to be grouped. You need to distinguish between
scalar values, or other values, such as text, that you want grouped.
Date and time values can not be grouped in this release of Calc (see Important note on page 4).
Note
Before you can group, you have to produce a Pivot Table with ungrouped data. The
time needed for creating a Pivot Table depends mostly on the number of columns
and rows and not on the size of the basic data. Through grouping you can produce
the Pivot Table with a small number of rows and columns. The Pivot Table can
contain a lot of categories, depending on your data source.
Choose Data > Group and Outline > Group from the menu bar or press F12; you get the
following dialog.
16
You can define in which value range (start/end) the grouping should take place. The default setting
is the whole range from smallest to largest value. In the field Group by you can enter the class
size, this means the interval size (in this example groups of 10 km/h each).
For grouping of non scalar categories, select in the result of the Pivot Table all the individual field
values that you want to put in the one group.
Tip
You can select several non-contiguous cells in one step by pressing and holding the
Control key while left-clicking with the mouse.
Given the input data shown in Figure 26, execute the Pivot Table with Department in the Row Field
and Sum (Sick Days) in the Data Field. The output should look like that in Figure 27. Select with
the mouse the Departments Accounting, Purchasing and Sales.
Choose the Data > Group and Outline > Group from the Menu bar or press F12. The output
should now look like that in Figure 28. Repeat this for all groups that you want to create from the
different categories (Select Assembly, Production and Warehouse and Group again. The output
should look like Figure 29.
17
You can change the default names for the groups and the newly created group field, by editing the
name in the input field (for example changing 'Group2' to 'Technical'). The Pivot Table will
remember these settings, even if you change the layout later on. For the following pictures, the
dialog was called again (with a right-click, Edit Layout) and by selecting the icon Department 2,
then Options, and finally from the preferences menu Automatic was selected. This generated the
partial sum results shown in Figure 30. Double clicking Group 1 and Technical collapses the
entries, as shown in Figure 31.
Note
A well-structured database makes manual sorting within the Pivot Table obsolete. In
the example shown, you could add another column with the name Department, that
has the correct entry for each person based on whether the employees department
belongs to the group Office or Technical. The mapping for this (1:n relationship) can
be done easily with the VLOOKUP function from Calc.
18
Sort automatically
To sort automatically, right-click within the Pivot Table and choose Edit Layout, to open the
DataPilot (Figure 3). Within the Layout area of the DataPilot, double-click the row or column field
you want to sort. In the Data Field dialog which opens (Figure 17), click Options to display the
Data Field Options dialog.
For Sort by choose either Ascending or Descending. On the left side is a drop-down list where you
can choose the field this setting should apply to. With this method you can specify that sorting does
not happen according to the categories but according to the results of the data field.
19
Figure 35: Before the drill down for the category golfing
A dialog appears allowing you to select the field to use for further subdivision. In this
example, employee.
20
21
Figure 38: New table sheet after the drill down for a value in a data field
Filtering
To limit the Pivot Table analysis to a subset of the information that is contained in the data basis,
you can filter with the Pivot Table.
Note
An Autofilter or default filter used on the sheet has no effect on the DataPilot analysis
process. The DataPilot always uses the complete list that was selected when it was
started.
To do this, click Filter on the top left side above the results.
Figure 39: Filter field in the upper left area of the Pivot Table
In the Filter dialog, you can define up to 3 filter options that are used in the same way as Calcs
default filter.
Note
22
Even if they are not called a filter, page fields are a practical way to filter the results.
The advantage is that the filtering criteria used are clearly visible.
Cell formatting
The cells in the results area of the Pivot Table are automatically formatted in a simple format by
Calc. You can change this formatting using all the tools in Calc, but note that if you make any
change in the design of the Pivot Table or any updates, the formatting will return to the format
applied automatically by Calc.
For the number format in the data field, Calc uses the number format that is used in the
corresponding cell in the source list. In most cases, this is useful (for example, if the values are in
the currency format, then the corresponding cell in the result area is also formatted as currency).
However, if the result is a fraction or a percentage, the Pivot Table does not recognize that this
might be a problem; such results must either be without a unit or be displayed as a percentage.
Although you can correct the number format manually, the correction stays in effect only until the
next update.
Using shortcuts
If you use the Pivot Table very often, you might find the frequent use of the menu paths (Data >
Pivot Table > Create and Data > Group and Outline > Group) inconvenient.
For grouping, a shortcut is already defined: F12. For starting the Pivot Table, you can define your
own keyboard shortcut. If you prefer to have toolbar icons instead of keyboard shortcuts, you can
create a user-defined symbol and add it to either your own custom made toolbar or the Standard
toolbar.
For an explanation how to create keyboard shortcuts or add icons to toolbars, see Chapter 14,
Setting Up and Customizing Calc.
23
Figure 42: The value that you really want to use can be found now in a different
location.
Syntax
The syntax has two variations:
GETPIVOTDATA(target field, Pivot Table, [ Field name / Element, ... ])
GETPIVOTDATA(Pivot Table, specification)
24
=GETPIVOTDATA("sales",A1,"employee","Hans","category","sailing")
25
26