Using Pivot Tables

1. Overview

Pivot tables allow you to quickly compute and visualize statistical summaries from flat source tables. Typical operations include counting, summing, grouping, sorting, and computing minimum/maximum values.

In GS-Calc, pivot tables are created using the pivotData() array formula. Because this formula may require several parameters, it is often easier to use the Insert → Pivot Table composer.

2. Usage Examples

2.1 Customers Table

Consider a table containing fields such as Country and City:

Customers table

Count customers by country

  1. Open Insert → Pivot Table.
  2. Select the customers table and click Source Range.
  3. Select Country as the only Row field.
  4. Select Count as the default function.
  5. Choose a destination cell and click Insert.
Pivot example 1

Count customers by country and city

  1. Open Insert → Pivot Table.
  2. Select the customers table and click Source Range.
  3. Select Country as the first Row field.
  4. Select City as the second Row field.
  5. Select Count as the default function.
  6. Choose a destination cell and click Insert.
Pivot example 2

2.2 Products Table

Consider a table containing fields such as ProductName, Date, Qty, and Total:

Products table

Sales volume by day

  1. Open Insert → Pivot Table.
  2. Select the products table and click Source Range.
  3. Select ProductName as the only Row field.
  4. Select Date as the only Column field.
  5. Select Qty as the only Data field and choose Sum.
  6. Choose a destination cell and click Insert.
Pivot example 3

Sales volume and quantity by day

  1. Open Insert → Pivot Table.
  2. Select the products table and click Source Range.
  3. Select ProductName as the only Row field.
  4. Select Date as the only Column field.
  5. Select Total as the first Data field and choose Sum.
  6. Select Qty as the second Data field and choose Sum.
  7. Choose a destination cell and click Insert.
Pivot example 4

3. pivotData() Formula

PIVOT formulas have the following form:

pivotData(source, rows, columns, data, functions, options [, field1, filter1, field2, filter2, ...])

This formula creates and returns a pivot table for the data in the source range.

3.1 Row, Column, and Data Fields

The rows, columns, and data parameters are arrays containing field indices. Indices are 1‑based and relative to the top‑left corner of the source range.

Examples:

{1, 5}, {4}, {1}

Incorrect indices cause #VALUE!. In GS-Calc 9.0, the columns array may contain only one element. If data fields are omitted, the last row field and the default pivot function are used.

3.2 Functions

The functions argument is an array of predefined constants:

Examples:

{PIVOT::Sum}

{PIVOT::Sum, PIVOT::Count}

3.3 Required Column Field or Grand Totals

You must specify either:

Otherwise the function returns #NUM!.

3.4 Options (FULL descriptions preserved)

The options parameter may be a sum of:

Examples:

{PIVOT::ColumnGrandTotals}

{PIVOT::ColumnGrandTotals + PIVOT::RowGrandTotals + PIVOT::SubTotals}

3.5 Filters

Optional [field, filter] pairs specify filtering conditions. Only records matching all conditions are included.

Filters may be:

3.6 Examples

=pivotData(C3:H19, {1},,, {PIVOT::Sum}, PIVOT::ColumnGrandTotals)

=pivotData(sheet1!C3:H19, {1}, {2}, {3, 4}, {PIVOT::Sum, PIVOT::Count}, PIVOT::RowGrandTotals + PIVOT::ColumnGrandTotals + PIVOT::SubTotals)

=pivotData(sheet1!B3:F8, {5, 1}, {3},, {PIVOT::Sum}, PIVOT::RowGrandTotals + PIVOT::ColumnGrandTotals + PIVOT::SubTotals, 2, "Jones", 4, ">2010-01-01")