---
title: "Pivot Result Columns"
enterprise: true
framework: javascript
version: "36.1.0"
---

# Pivot Result Columns

The grid generates pivot result columns to display the aggregated values for each unique permutation of pivot values.

#### Pivot Result Column Summary Example

```ts
import {
  ClientSideRowModelModule,
  GridApi,
  GridOptions,
  ModuleRegistry,
  createGrid,
  enableDevValidations,
} from "ag-grid-community";
import {
  ColumnMenuModule,
  ColumnsToolPanelModule,
  ContextMenuModule,
  PivotModule,
} from "ag-grid-enterprise";
import { IOlympicData } from "./interfaces";

// Enable extended validations only for development
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  ClientSideRowModelModule,
  ColumnsToolPanelModule,
  ColumnMenuModule,
  ContextMenuModule,
  PivotModule,
]);

let gridApi: GridApi<IOlympicData>;

const gridOptions: GridOptions<IOlympicData> = {
  columnDefs: [
    { field: "country", rowGroup: true },
    { field: "sport", pivot: true },
    { field: "gold", aggFunc: "sum" },
    { field: "silver", aggFunc: "sum" },
    { field: "bronze", aggFunc: "sum" },
  ],
  defaultColDef: {
    flex: 1,
    minWidth: 130,
  },
  autoGroupColumnDef: {
    minWidth: 200,
  },
  pivotMode: true,
};

const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);

fetch("https://www.ag-grid.com/example-assets/olympic-winners.json")
  .then((response) => response.json())
  .then((data: IOlympicData[]) => gridApi!.setGridOption("rowData", data));
```

[Live example: Pivot Result Column Summary Example](https://www.ag-grid.com/examples/pivoting-result-columns/pivot-result-summary/typescript)

## Column Definitions

Pivot Result Columns inherit [Column Definitions](https://www.ag-grid.com/javascript-data-grid/column-definitions/) from the value column that they were created from. It is also possible to extend this definition further to specifically customise pivot result columns using the `processPivotResultColDef` grid option.

| Property | Type | Required | Default | Description |
| --- | --- | --- | --- | --- |
| `processPivotResultColDef` | `ProcessPivotResultColDef` |  |  | Callback for the mutation of the generated pivot result column definitions Module: [`PivotModule`](https://www.ag-grid.com/javascript-data-grid/modules/). |

In the example below, the `Gold` column has `cellStyle: { backgroundColor: '#f2e287' }` applied, this is then inherited by the pivot result columns, causing all of the `sum(Gold)` columns to have a gold background. Note that the `Silver` column does not have this background so neither do the `sum(Silver)` columns.

The grid option `processPivotResultColDef` is then also used, which sets the text colour of all the pivot result columns to `#2f73ff`.

#### Column Definitions Example

```ts
import {
  CellStyleModule,
  ClientSideRowModelModule,
  GridApi,
  GridOptions,
  ModuleRegistry,
  createGrid,
  enableDevValidations,
} from "ag-grid-community";
import {
  ColumnMenuModule,
  ColumnsToolPanelModule,
  ContextMenuModule,
  PivotModule,
} from "ag-grid-enterprise";
import { IOlympicData } from "./interfaces";

// Enable extended validations only for development
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  CellStyleModule,
  ClientSideRowModelModule,
  ColumnsToolPanelModule,
  ColumnMenuModule,
  ContextMenuModule,
  PivotModule,
]);

let gridApi: GridApi<IOlympicData>;

const gridOptions: GridOptions<IOlympicData> = {
  columnDefs: [
    { field: "country", rowGroup: true },
    { field: "sport", pivot: true },
    {
      field: "gold",
      aggFunc: "sum",
      cellStyle: { backgroundColor: "#f2e287" },
    },
    { field: "silver", aggFunc: "sum", cellStyle: {} },
  ],
  defaultColDef: {
    flex: 1,
    minWidth: 130,
  },
  autoGroupColumnDef: {
    minWidth: 200,
  },
  pivotMode: true,
  processPivotResultColDef: (colDef) => {
    if (typeof colDef.cellStyle === "object") {
      colDef.cellStyle.color = "#2f73ff";
    }
  },
};

const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);

fetch("https://www.ag-grid.com/example-assets/olympic-winners.json")
  .then((response) => response.json())
  .then((data: IOlympicData[]) => gridApi!.setGridOption("rowData", data));
```

[Live example: Column Definitions Example](https://www.ag-grid.com/examples/pivoting-result-columns/column-definitions-example/typescript)

This uses the following configuration to both inherit and modify column definitions on the pivot result columns:

```js
const gridOptions = {
    columnDefs: [
        // ...other column definitions
        { field: 'gold', aggFunc: 'sum', cellStyle: { backgroundColor: '#f2e287' } },
        { field: 'silver', aggFunc: 'sum', cellStyle: {} },
    ],
    pivotMode: true,
    processPivotResultColDef: (colDef) => {
        colDef.cellStyle.color = '#2f73ff'; // the params are mutated directly, not returned
    },

    // other grid options ...
}
```

## Filtering

When pivot mode is enabled, you can [Filter](https://www.ag-grid.com/javascript-data-grid/filtering-overview/) on the pivot result columns by setting the `filter` attribute on your value column.

#### Filtering Pivot Result Columns

```ts
import {
  ClientSideRowModelModule,
  GridApi,
  GridOptions,
  ModuleRegistry,
  NumberFilterModule,
  createGrid,
  enableDevValidations,
} from "ag-grid-community";
import {
  ColumnMenuModule,
  ContextMenuModule,
  FiltersToolPanelModule,
  PivotModule,
  SetFilterModule,
} from "ag-grid-enterprise";
import { IOlympicData } from "./interfaces";

// Enable extended validations only for development
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  ClientSideRowModelModule,
  FiltersToolPanelModule,
  ColumnMenuModule,
  ContextMenuModule,
  PivotModule,
  SetFilterModule,
  NumberFilterModule,
]);

let gridApi: GridApi<IOlympicData>;

const gridOptions: GridOptions<IOlympicData> = {
  columnDefs: [
    { field: "country", rowGroup: true },
    { field: "athlete", rowGroup: true },
    { field: "year", pivot: true },
    { field: "gold", aggFunc: "sum", filter: "agNumberColumnFilter" },
    { field: "silver", aggFunc: "sum", filter: "agNumberColumnFilter" },
    { field: "bronze", aggFunc: "sum", filter: "agNumberColumnFilter" },
  ],
  defaultColDef: {
    flex: 1,
    minWidth: 130,
    floatingFilter: true,
  },
  autoGroupColumnDef: {
    minWidth: 200,
  },
  pivotMode: true,
};

const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);

fetch("https://www.ag-grid.com/example-assets/small-olympic-winners.json")
  .then((response) => response.json())
  .then((data: IOlympicData[]) => gridApi!.setGridOption("rowData", data));
```

[Live example: Filtering Pivot Result Columns](https://www.ag-grid.com/examples/pivoting-result-columns/secondary-columns-filter/typescript)

As pivot values are all aggregates, filtering out rows will not re-aggregate the parent, group and grand total rows. Refer to [Filtering Aggregated Values](https://www.ag-grid.com/javascript-data-grid/aggregation-filtering/#filtering-for-aggregated-values) for more information.

> **Note**
>
> Pivot result columns inherit the properties of the value column from which they are generated. However, setting `filter: true` will instead default to a [Number Filter](https://www.ag-grid.com/javascript-data-grid/filter-number/) in the case of a pivot result column. The [Set Filter](https://www.ag-grid.com/javascript-data-grid/filter-set/) cannot be used for filtering pivot result columns.

## Best Practices

### Limiting Column Generation

When pivoting, changes in data, aggregation or pivot columns can cause the number of generated columns to scale exponentially. This can cause performance issues such as long delays in rendering, and often the resulting view would be unmanageable for the user.

To prevent this from happening, you can set the `pivotMaxGeneratedColumns` option. When the grid generates a number of pivot columns exceeding this value, it halts column generation, clears the view, and fires the `onPivotMaxColumnsExceeded` event to allow your application to intervene.

#### Extreme Pivot Handling

```ts
import {
  ClientSideRowModelModule,
  GridApi,
  GridOptions,
  ModuleRegistry,
  createGrid,
  enableDevValidations,
} from "ag-grid-community";
import {
  ColumnsToolPanelModule,
  FiltersToolPanelModule,
  PivotModule,
  SideBarModule,
} from "ag-grid-enterprise";
import { IOlympicData } from "./interfaces";

// Enable extended validations only for development
if (process.env.NODE_ENV !== "production") {
  enableDevValidations();
}

ModuleRegistry.registerModules([
  ClientSideRowModelModule,
  SideBarModule,
  ColumnsToolPanelModule,
  FiltersToolPanelModule,
  PivotModule,
]);

let gridApi: GridApi<IOlympicData>;

const gridOptions: GridOptions<IOlympicData> = {
  columnDefs: [
    { field: "country", rowGroup: true, enableRowGroup: true },
    { field: "athlete", enablePivot: true },
    { field: "year", enablePivot: true },
    { field: "sport", enablePivot: true },
    { field: "gold", aggFunc: "sum" },
    { field: "silver", aggFunc: "sum" },
    { field: "bronze", aggFunc: "sum" },
  ],
  defaultColDef: {
    flex: 1,
    minWidth: 130,
  },
  autoGroupColumnDef: {
    minWidth: 200,
  },
  pivotMode: true,
  sideBar: "columns",
  pivotMaxGeneratedColumns: 1000,
  onPivotMaxColumnsExceeded: () => {
    console.warn(
      "The limit of 1000 generated columns has been exceeded. Either remove pivot or aggregations from some columns or increase the limit.",
    );
  },
};

const gridDiv = document.querySelector<HTMLElement>("#myGrid")!;
gridApi = createGrid(gridDiv, gridOptions);

fetch("https://www.ag-grid.com/example-assets/olympic-winners.json")
  .then((response) => response.json())
  .then((data: IOlympicData[]) => gridApi!.setGridOption("rowData", data));
```

[Live example: Extreme Pivot Handling](https://www.ag-grid.com/examples/pivoting-result-columns/extreme-pivot/typescript)

In the example above, pivoting by the `Athlete` column will instead trigger the `pivotMaxColumnsExceeded` event, which logs an error in the browser console.

The example above demonstrates the following configuration:

```js
const gridOptions = {
    pivotMode: true,
    pivotMaxGeneratedColumns: 1000,
    onPivotMaxColumnsExceeded: () => {
        console.error(
            'The limit of 1000 generated columns has been exceeded. Either remove pivot or aggregations from some columns or increase the limit.'
        );
    },

    // other grid options ...
}
```
