# Pivot Result Columns

Canonical page: https://www.adaptabletools.com/docs/handbook-layouts-pivot-result-columns

- Pivot Result Columns are dynamically created by AG Grid when Pivot Layouts are rendered
- Each Pivot Result Columns is a unique combination of Pivot Column value and Aggregation Column
- AdapTable allows users to format, filter, sort and size Pivot Result Columns independently
- AdapTable allows enables saving of Pivot Result Columns order, and reapplies the order when the Layout next loads

[Layouts](https://www.adaptabletools.com/docs/handbook-layouts/index.md) · [Pivot Layouts](https://www.adaptabletools.com/docs/handbook-layouts-pivot/index.md) · [All Pivot Layout Topics](https://www.adaptabletools.com/docs/handbook-layouts-pivot/index.md#pivot-layout-topics).

Pivot Result Columns are the columns created dynamically by AG Grid when pivoting occurs.

Each [AG Grid created Pivot Result Column](https://www.ag-grid.com/javascript-data-grid/pivoting-result-columns/) is the unique permutation of 2 values:

- each distinct value in a Pivot Column
- each Aggregation Column

For instance if you Pivot on a column which has 3 distinct values, and you have 2 Aggregation columns, then 6 Pivot Result Columns will be created by AG Grid.

If there are multiple Pivot Columns, then a Pivot Result Column is created for every unique permutation

## Generated Ids

AG Grid will give each Pivot Result Column a unique Id.

These Ids are what AdapTable uses to filter, sort, size and position Pivot Result Columns as seen below

The generated Id is typically made up of 4 elements, each separated by an underscore:

- the word "pivot"
- the name of the pivot column
- the pivot column's value
- the name of the aggregation value

For example, if you had a Pivot Column of `currency` (with 2 unique values of 'USD' and 'EUR') and 2 aggregation column of `price` and `amount`, the 4 dynamically generated columns would be:

- `pivot_currency_USD_price`
- `pivot_currency_EUR_price`
- `pivot_currency_USD_amount`
- `pivot_currency_EUR_amount`

## Referencing

AdapTable uses the Ids dynamically generated by AG Grid to reference each Pivot Result Column.

This allows the Pivot Result Columns to be individually referenced in the Layout.

Pivot Result Columns can be uniquely identifed for the purposes of:

- Column Formatting
- Filtering
- Sorting
- Column Sizing

For more details on each of these together with examples see the pages in this section on:
- [Filtering](https://www.adaptabletools.com/docs/handbook-layouts-pivot-formatting/index.md#pivot-result-columns)
- [Formatting](https://www.adaptabletools.com/docs/handbook-layouts-pivot-filtering/index.md#pivot-result-columns)
- [Sizing](https://www.adaptabletools.com/docs/handbook-layouts-pivot-sizing/index.md#pivot-result-columns)
- [Sorting](https://www.adaptabletools.com/docs/handbook-layouts-pivot-sorting/index.md#pivot-result-columns)

## Ordering

AG Grid decides Pivot Result Column order based on the unique permutations at the time of their creation.

This is **non-deterministic** because the values in a Pivot Column can change between application loads

By default AdapTable will **not** save this order, which has 2 consequences:

- users will see the order decided by AG Grid based on values available when Layout loads
- any changes made by users are not persisted (and essentially overturned when the Layout next loads)

Where this is not the desired behaviour, AdapTable provides an alternative.

This is done by using the `PivotResultColumnsOrder` property which is available in each Layout.

The property can be set to one of 3 values:

| Value    | Behaviour                                                                          |
| -------- | ---------------------------------------------------------------------------------- |
| `false`  | No changes to Pivot Result Column Order are saved (default value)                  |
| `true`   | Any changes to Pivot Result Column Order will be persisted and available on reload |
| [string] | List of Column Ids for initial order (and any changes to that order are persisted) |

With the Server-Side Row Model, Pivot Result Columns are displayed in the order in which the datasource supplies them, and that order replaces the one saved in `PivotResultColumnsOrder` - see [Server-Side Pivoting](https://www.adaptabletools.com/docs/dev-guide-row-models-server-pivoting/index.md#column-order)

Changing the Pivot Aggregation Columns (e.g. reordering them in the Columns Tool Panel) makes AG Grid recreate the Pivot Result Columns in its own order, so a custom order is lost and the new order is saved in `PivotResultColumnsOrder`

**Example: Pivot Result Columns**

Using Pivot Result Columns

- This example shows how AdapTable Layouts can be configured at the per-Pivot Result Column Level, by setting some behaviours, including:
  - 3 different Formats for the different Github Stars columns (red, green and blue fonts respectively)
  - The `pivot_language_JavaScript_github_stars` Column is given its own sort order and a larger size
  - There are 3 different Layouts provided to show the different behaviours for Column Order based on the `PivotResultColumnsOrder` property:
    - `Default Order` - set to *false* so that it shows Pivot Result Columns in the order created by AG Grid, and no changes are persisted
    - `Tracked Order` - set to *true* so that it also shows Pivot Result Columns in the order created by AG Grid, but now changes are persisted
    - `Configured Order` - a default list is provided (of the 2 TypeScript columns first), and subsequent changes are persisted

Demo project (Vanilla); the files without a marker are shown below:

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts        [template]
  gridOptions.ts       [template]
  index.ts             [template]
  onAdaptableReady.ts  [template]
  rowData.ts           [template]
  styles.css           [template]
index.html             [template]
package.json           [template]
tsconfig.json          [template]
```

Files marked `[template]` are the same in every demo; see the [demo template](https://www.adaptabletools.com/llms/demo-template.md#vanilla).

**`src/adaptableOptions.ts`**

```ts
import {AdaptableOptions} from '@adaptabletools/adaptable';
import {WebFramework} from './rowData';

export const adaptableOptions: AdaptableOptions<WebFramework> = {
  primaryKey: 'id',
  adaptableId: 'Pivot Result Columns',
  initialState: {
    Dashboard: {
      Tabs: [{Name: 'Demo', Toolbars: ['Layout']}],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Layout'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    FormatColumn: {
      FormatColumns: [
        {
          Name: 'style-html-red',
          Scope: {
            ColumnIds: ['pivot_language_HTML_github_stars'],
          },
          Style: {
            ForeColor: 'var(--ab-color-palette-30)',
          },
        },
        {
          Name: 'style-html-green',
          Scope: {
            ColumnIds: ['pivot_language_JavaScript_github_stars'],
          },
          Style: {
            ForeColor: 'var(--ab-color-palette-19)',
          },
        },
        {
          Name: 'style-html-blue',
          Scope: {
            ColumnIds: ['pivot_language_TypeScript_github_stars'],
          },
          Style: {
            ForeColor: 'var(--ab-color-palette-24)',
          },
        },
      ],
    },
    Layout: {
      CurrentLayout: 'Default Order',
      Layouts: [
        {
          Name: 'Default Order',
          PivotColumns: ['language'],
          PivotGroupedColumns: ['license'],
          PivotAggregationColumns: [
            {
              ColumnId: 'github_watchers',
              AggFunc: 'count',
            },
            {
              ColumnId: 'github_stars',
              AggFunc: 'sum',
            },
          ],
          ColumnSorts: [
            {
              ColumnId: 'pivot_language_JavaScript_github_stars',
              SortOrder: 'Asc',
            },
          ],
          ColumnSizing: {
            pivot_language_JavaScript_github_stars: {Width: 300},
          },
          SuppressAggFuncInHeader: true,
          AutoSizeColumns: true,
        },
        {
          Name: 'Tracked Order',
          PivotColumns: ['language'],
          PivotGroupedColumns: ['license'],
          PivotAggregationColumns: [
            {
              ColumnId: 'github_watchers',
              AggFunc: 'count',
            },
            {
              ColumnId: 'github_stars',
              AggFunc: 'sum',
            },
          ],
          ColumnSorts: [
            {
              ColumnId: 'pivot_language_JavaScript_github_stars',
              SortOrder: 'Asc',
            },
          ],
          ColumnSizing: {
            pivot_language_JavaScript_github_stars: {Width: 300},
          },
          PivotResultColumnsOrder: true,
          AutoSizeColumns: true,
          SuppressAggFuncInHeader: true,
        },
        {
          Name: 'Configured Order',
          PivotColumns: ['language'],
          PivotGroupedColumns: ['license'],
          PivotAggregationColumns: [
            {
              ColumnId: 'github_watchers',
              AggFunc: 'count',
            },
            {
              ColumnId: 'github_stars',
              AggFunc: 'sum',
            },
          ],
          ColumnSorts: [
            {
              ColumnId: 'pivot_language_JavaScript_github_stars',
              SortOrder: 'Asc',
            },
          ],
          ColumnSizing: {
            pivot_language_JavaScript_github_stars: {Width: 300},
          },
          SuppressAggFuncInHeader: true,
          PivotResultColumnsOrder: [
            'pivot_language_TypeScript_github_stars',
            'pivot_language_TypeScript_github_watchers',
          ],
          AutoSizeColumns: true,
        },
      ],
    },
  },
};
```
