# Quantile Calculated Columns

Canonical page: https://www.adaptabletools.com/docs/handbook-calculated-column-quantile

- Quantile Calculated Columns allow 'buckets' of values to be created
- Each cell is placed into a specific bucket based on its value relative to others in the group

Calculated Columns are able to display [Quantile Expressions](https://www.adaptabletools.com/docs/adaptable-ql-expression-quantile/index.md).

These are an advanced form of [Aggregation Expressions](https://www.adaptabletools.com/docs/adaptable-ql-expression-aggregation/index.md) that perform **quantile aggregations**.

The Calculated Column is evaluated by placing each cell's value into a different bucket based on its value relative to others in the group.

Quantile aggregations use the `QUANT` Expression Function.

This receives a value to evaluate (typically a column name) and the number of buckets to create.

With the [Server-Side Row Model](https://www.adaptabletools.com/docs/dev-guide-row-models-server-overview/index.md), evaluate Quantile Calculated Columns on the server — see [Server-Side Row Model - Calculated Columns](https://www.adaptabletools.com/docs/dev-guide-row-models-server-calculated-columns/index.md).

### Defining a Quantile Calculated Column

There are 5 properties that you need to provide when defining a Quantile Calculated Column:

This `ColumnId` value is used to reference the Column in AdapTable State and in other objects (e.g. [Layouts](https://www.adaptabletools.com/docs/handbook-layouts/index.md)).

This value is used to refer to the Column in AdapTable UI.
It only needs to be provided if the `ColumnId` value is unsuitable.

This is a [Quantile Expression](https://www.adaptabletools.com/docs/adaptable-ql-expression-quantile/index.md) evaluated by [AdapTableQL](https://www.adaptabletools.com/docs/adaptable-ql-expression/index.md) by placing each row's value into a bucket relative to other rows.

It is the `Expression` string of the Calculated Column and uses the `QUANT` function — a column reference, a number of buckets, and optionally a `GROUP_BY` clause.

The Data Type of the Calculated Column should be provided in `CalculatedColumnSettings`.

It is a [Cell Data Type](https://www.adaptabletools.com/docs/dev-guide-aggrid-cell-data-types/index.md) value and the object's only mandatory property. Common values are `number`, `text` or `date`.

Additional Settings for the Calculated Column (other than mandatory `DataType`) can be provided, including:

- `Filterable`
- `Pivotable`
- `Sortable`
- `Groupable`

```tsx [[1,4, "ColumnId"],[1,13, "ColumnId"],[2,5, "FriendlyName"],[2,14, "FriendlyName"],[3,6, "Expression"],[3,15, "Expression"],[4,8, "DataType"],[4,17, "DataType"],[5,9, "Filterable"],[6,18, "Sortable"]]
CalculatedColumn: {
  CalculatedColumns: [
    {
      ColumnId: 'quartile',
      FriendlyName: 'Quartile',
      Expression: 'QUANT{[value], 4}',
      CalculatedColumnSettings: {
        DataType: 'number',
        Filterable: true,
      },
    },
    {
      ColumnId: 'quartile-by-type',
      FriendlyName: 'Quartile by Type',
      Expression: 'QUANT{[value], 4, GROUP_BY([type])}',
      CalculatedColumnSettings: {
        DataType: 'number',
        Sortable: true,
      },
    },
  ],
},
```

<AiSkill skill="adaptable-dev" embedded>

Add 2 Quantile Calculated Columns to the current Layout:
(a) "Notional Quartile" which will place each row in quartile 1 to 4 based on the Notional column across the whole grid
(b) "Desk Size Quintile" which will place each row in quintile 1 to 5 based on the Size column, only comparing rows that share the same Desk

</AiSkill>

**Example: Quantile Calculated Columns**

Calculated Columns: Quantile Aggregations

- This Example demonstrates how to use the `QUANT` Expression Function to calculate Quantile functions
- It contains 100 Tickers each with a `Value` and a `Type`
- The provided Value is from 1 to 100 in ascending order (in order to demonstrate how Quantiles work)
- 4 Calculated Columns are provided in the Initial Adaptable State - each using `QUANT`:
  - `Quartile` - divides the 100 Ticker Values into 4 groups
  - `Quintile` - divides the 100 Ticker Values into 5 groups
  - `Decile` - divides the 100 Ticker Values into 10 groups
  - `Percentile` - divides the 100 Ticker Values into 100 groups (so there is therefore just 1 value per group)
- A 5th Calculated Column illustrates how to group Quantiles by levering the `GROUP_BY` keyword:
  - `Grouped by Type` - creates 3 bucket for each set of Values in each group of a distinct 'Type' (with a [Column Display Format](https://www.adaptabletools.com/docs/handbook-column-formatting/index.md))

- Sort the Grid by *Type* to see how the `Grouped by Type` column creates 3 buckets for each dinstinct Type
- Change some Values and note how this changes all the Quantiles it is placed in

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

```text
src/
  adaptableOptions.ts
  agGridModules.ts     [template]
  columnDefs.ts
  gridOptions.ts       [template]
  index.ts             [template]
  onAdaptableReady.ts  [template]
  rowData.ts
  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';

export const adaptableOptions: AdaptableOptions = {
  primaryKey: 'Id',
  adaptableId: 'Quantile Calculated Columns',
  initialState: {
    Dashboard: {
      ModuleButtons: ['CalculatedColumn'],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Layout', 'CalculatedColumn'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          Name: 'Standard Layout',
          TableColumns: [
            'Id',
            'Ticker',
            'Value',
            'Type',
            'Quartile',
            'Quintile',
            'Decile',
            'Percentile',
            'TypeGroup',
          ],
        },
      ],
    },
    CalculatedColumn: {
      CalculatedColumns: [
        {
          ColumnId: 'Quartile',
          Expression: 'QUANT{[Value], 4}',
          CalculatedColumnSettings: {
            DataType: 'number',
          },
        },
        {
          ColumnId: 'Quintile',
          Expression: 'QUANT{[Value], 5}',
          CalculatedColumnSettings: {
            DataType: 'number',
          },
        },
        {
          ColumnId: 'Decile',
          Expression: 'QUANT{[Value], 10}',
          CalculatedColumnSettings: {
            DataType: 'number',
          },
        },
        {
          ColumnId: 'Percentile',
          Expression: 'QUANT{[Value], 100}',
          CalculatedColumnSettings: {
            DataType: 'number',
          },
        },
        {
          ColumnId: 'TypeGroup',
          Expression: 'QUANT{[Value], 3, GROUP_BY([Type])}',
          FriendlyName: 'Grouped by Type',
          CalculatedColumnSettings: {
            DataType: 'number',
          },
        },
      ],
    },
    FormatColumn: {
      FormatColumns: [
        {
          Name: 'formatColumn-TypeGroup',
          Scope: {
            ColumnIds: ['TypeGroup'],
          },
          DisplayFormat: {
            Formatter: 'NumberFormatter',
            Options: {
              Prefix: 'Bucket: ',
            },
          },
        },
      ],
    },
  },
};
```

**`src/columnDefs.ts`**

```ts
import {ColDef} from 'ag-grid-enterprise';

export const columnDefs: ColDef[] = [
  {
    headerName: 'Id',
    field: 'Id',
    filter: true,
    editable: false,
    sortable: true,
    cellDataType: 'number',
  },
  {
    headerName: 'Value',
    field: 'Value',
    filter: true,
    editable: true,
    sortable: true,
    cellDataType: 'number',
  },
  {
    headerName: 'Ticker',
    field: 'Ticker',
    filter: true,
    editable: false,
    sortable: true,
    cellDataType: 'text',
  },
  {
    headerName: 'Type',
    field: 'Type',
    filter: true,
    editable: true,
    sortable: true,
    cellDataType: 'text',
  },
];
```

**`src/rowData.ts`**

```ts
export const rowData = [
  {
    Ticker: 'AAL',
    Value: 1,
    Id: 1,
    Type: 'Transport',
  },
  {
    Ticker: 'AAPL',
    Value: 2,
    Id: 2,
    Type: 'IT',
  },
  {
    Ticker: 'AB',
    Value: 3,
    Id: 3,
    Type: 'Finance',
  },
  {
    Ticker: 'ABBV',
    Value: 4,
    Id: 4,
    Type: 'Manufacturing',
  },
  {
    Ticker: 'ACER',
    Value: 5,
    Id: 5,
    Type: 'Other',
  },
  {
    Ticker: 'ACN',
    Value: 6,
    Id: 6,
    Type: 'IT',
  },

  {
    Ticker: 'ADI',
    Value: 7,
    Id: 7,
    Type: 'Manufacturing',
  },
  {
    Ticker: 'AEG',
    Value: 8,
    Id: 8,
    Type: 'Finance',
  },
  {
    Ticker: 'AMZN',
    Value: 9,
    Id: 9,
    Type: 'Consumer',
  },
  {
    Ticker: 'BA',
    Value: 10,
    Id: 10,
    Type: 'Transport',
  },
  {
    Ticker: 'BAC',
    Value: 11,
    Id: 11,
    Type: 'Finance',
  },
  {
    Ticker: 'BAH',
    Value: 12,
    Id: 12,
    Type: 'IT',
  },

  {
    Ticker: 'BB',
    Value: 13,
    Id: 13,
    Type: 'IT',
  },
  {
    Ticker: 'BBDC',
    Value: 14,
    Id: 14,
    Type: 'Finance',
  },
  {
    Ticker: 'BBIG',
    Value: 15,
    Id: 15,
    Type: 'Consumer',
  },
  {
    Ticker: 'BBQ',
    Value: 16,
    Id: 16,
    Type: 'Consumer',
  },
  {
    Ticker: 'BCS',
    Value: 17,
    Id: 17,
    Type: 'Finance',
  },
  {
    Ticker: 'BK',
    Value: 18,
    Id: 18,
    Type: 'Finance',
  },
  {
    Ticker: 'C',
    Value: 19,
    Id: 19,
    Type: 'Finance',
  },
  {
    Ticker: 'CAJ',
    Value: 20,
    Id: 20,
    Type: 'IT',
  },
  {
    Ticker: 'CALX',
    Value: 21,
    Id: 21,
    Type: 'IT',
  },
  {
    Ticker: 'CASH',
    Value: 22,
    Id: 22,
    Type: 'Finance',
  },
  {
    Ticker: 'CBAT',
    Value: 23,
    Id: 23,
    Type: 'Manufacturing',
  },
  {
    Ticker: 'CBIO',
    Value: 24,
    Id: 24,
    Type: 'Manufacturing',
  },
  {
    Ticker: 'CCAP',
    Value: 25,
    Id: 25,
    Type: 'Finance',
  },
  {
    Ticker: 'CCL',
    Value: 26,
    Id: 26,
    Type: 'Consumer',
  },
  {
    Ticker: 'CDR',
    Value: 27,
    Id: 27,
    Type: 'Finance',
  },
  {
    Ticker: 'CDTX',
    Value: 28,
    Id: 28,
    Type: 'Manufacturing',
  },
  {
    Ticker: 'CSCO',
    Value: 29,
    Id: 29,
    Type: 'IT',
  },
  {
    Ticker: 'DAL',
    Value: 30,
    Id: 30,
    Type: 'Transport',
  },
  {
    Ticker: 'DAO',
    Value: 31,
    Id: 31,
    Type: 'Consumer',
  },
  {
    Ticker: 'DAVA',
    Value: 32,
    Id: 32,
    Type: 'IT',
  },
  {
    Ticker: 'DB',
    Value: 33,
    Id: 33,
    Type: 'Finance',
  },
  {
    Ticker: 'DBRG',
    Value: 34,
    Id: 34,
    Type: 'Other',
  },
  {
    Ticker: 'DBX',
    Value: 35,
    Id: 35,
    Type: 'IT',
  },
  {
    Ticker: 'DCI',
    Value: 36,
    Id: 36,
    Type: 'Manufacturing',
  },
  {
    Ticker: 'DD',
    Value: 37,
    Id: 37,
    Type: 'Manufacturing',
  },
  {
    Ticker: 'DELL',
    Value: 38,
    Id: 38,
    Type: 'IT',
  },
  {
    Ticker: 'DEO',
    Value: 39,
    Id: 39,
    Type: 'Consumer',
  },
  {
    Ticker: 'DHBC',
    Value: 40,
    Id: 40,
    Type: 'Other',
  },
  {
    Ticker: 'DOW',
    Value: 41,
    Id: 41,
    Type: 'Manufacturing',
  },
  {
    Ticker: 'EVA',
    Value: 42,
    Id: 42,
    Type: 'Manufacturing',
  },
  {
    Ticker: 'EVGN',
    Value: 43,
    Id: 43,
    Type: 'Manufacturing',
  },
  {
    Ticker: 'EVH',
    Value: 44,
    Id: 44,
    Type: 'Other',
  },
  {
    Ticker: 'EVTC',
    Value: 45,
    Id: 45,
    Type: 'IT',
  },
  {
    Ticker: 'EZGO',
    Value: 46,
    Id: 46,
    Type: 'Transport',
  },
  {
    Ticker: 'F',
    Value: 47,
    Id: 47,
    Type: 'Transport',
  },
  {
    Ticker: 'FBP',
    Value: 48,
    Id: 48,
    Type: 'Finance',
  },
  {
    Ticker: 'FCBC',
    Value: 49,
    Id: 49,
    Type: 'Finance',
  },
  {
    Ticker: 'FDP',
    Value: 50,
    Id: 50,
    Type: 'Consumer',
  },
  {
    Ticker: 'FDS',
    Value: 51,
    Id: 51,
    Type: 'Finance',
  },

  {
    Ticker: 'FEMY',
    Value: 52,
    Id: 52,
    Type: 'Other',
  },
  {
    Ticker: 'FSP',
    Value: 53,
    Id: 53,
    Type: 'Finance',
  },
  {
    Ticker: 'GACQ',
    Value: 54,
    Id: 54,
    Type: 'Other',
  },
  {
    Ticker: 'GATX',
    Value: 55,
    Id: 55,
    Type: 'Finance',
  },
  {
    Ticker: 'GB',
    Value: 56,
    Id: 56,
    Type: 'IT',
  },
  {
    Ticker: 'GBR',
    Value: 57,
    Id: 57,
    Type: 'Manufacturing',
  },
  {
    Ticker: 'GCI',
    Value: 58,
    Id: 58,
    Type: 'Other',
  },
  {
    Ticker: 'GDDY',
    Value: 59,
    Id: 59,
    Type: 'IT',
  },
  {
    Ticker: 'GEO',
    Value: 60,
    Id: 60,
    Type: 'Finance',
  },
  {
    Ticker: 'GOOG',
    Value: 61,
    Id: 61,
    Type: 'Consumer',
  },
  {
    Ticker: 'HCP',
    Value: 62,
    Id: 62,
    Type: 'IT',
  },
  {
    Ticker: 'HEP',
    Value: 63,
    Id: 63,
    Type: 'Manufacturing',
  },
  {
    Ticker: 'HLGN',
    Value: 64,
    Id: 64,
    Type: 'Manufacturing',
  },
  {
    Ticker: 'HUM',
    Value: 65,
    Id: 65,
    Type: 'Other',
  },
  {
    Ticker: 'IBM',
    Value: 66,
    Id: 66,
    Type: 'IT',
  },
  {
    Ticker: 'ICE',
    Value: 67,
    Id: 67,
    Type: 'Finance',
  },
  {
    Ticker: 'JEF',
    Value: 68,
    Id: 68,
    Type: 'Finance',
  },
  {
    Ticker: 'JPM',
    Value: 69,
    Id: 69,
    Type: 'Finance',
  },
  {
    Ticker: 'KRO',
    Value: 70,
    Id: 70,
    Type: 'Manufacturing',
  },
  {
    Ticker: 'KTB',
    Value: 71,
    Id: 71,
    Type: 'Consumer',
  },
  {
    Ticker: 'LUMN',
    Value: 72,
    Id: 72,
    Type: 'IT',
  },
  {
    Ticker: 'LVO',
    Value: 73,
    Id: 73,
    Type: 'Consumer',
  },
  {
    Ticker: 'LX',
    Value: 74,
    Id: 74,
    Type: 'Consumer',
  },
  {
    Ticker: 'MAN',
    Value: 75,
    Id: 75,
    Type: 'Other',
  },
  {
    Ticker: 'MBI',
    Value: 76,
    Id: 76,
    Type: 'Finance',
  },
  {
    Ticker: 'MIT',
    Value: 77,
    Id: 77,
    Type: 'Other',
  },
  {
    Ticker: 'MSFT',
    Value: 78,
    Id: 78,
    Type: 'IT',
  },
  {
    Ticker: 'NDAQ',
    Value: 79,
    Id: 79,
    Type: 'Finance',
  },
  {
    Ticker: 'NE',
    Value: 80,
    Id: 80,
    Type: 'Manufacturing',
  },
  {
    Ticker: 'NGG',
    Value: 81,
    Id: 81,
    Type: 'Manufacturing',
  },
  {
    Ticker: 'NKE',
    Value: 82,
    Id: 82,
    Type: 'Consumer',
  },
  {
    Ticker: 'OGE',
    Value: 83,
    Id: 83,
    Type: 'Manufacturing',
  },
  {
    Ticker: 'ORCL',
    Value: 84,
    Id: 84,
    Type: 'IT',
  },
  {
    Ticker: 'PAFO',
    Value: 85,
    Id: 85,
    Type: 'Other',
  },
  {
    Ticker: 'PAYX',
    Value: 86,
    Id: 86,
    Type: 'IT',
  },
  {
    Ticker: 'PCSB',
    Value: 87,
    Id: 87,
    Type: 'Finance',
  },
  {
    Ticker: 'PEP',
    Value: 88,
    Id: 88,
    Type: 'Consumer',
  },
  {
    Ticker: 'SBUX',
    Value: 89,
    Id: 89,
    Type: 'Consumer',
  },
  {
    Ticker: 'SCS',
    Value: 90,
    Id: 90,
    Type: 'Other',
  },
  {
    Ticker: 'SENEA',
    Value: 91,
    Id: 91,
    Type: 'Consumer',
  },
  {
    Ticker: 'SIF',
    Value: 92,
    Id: 92,
    Type: 'Transport',
  },
  {
    Ticker: 'TM',
    Value: 93,
    Id: 93,
    Type: 'Transport',
  },
  {
    Ticker: 'TNK',
    Value: 94,
    Id: 94,
    Type: 'Manufacturing',
  },
  {
    Ticker: 'TSLA',
    Value: 95,
    Id: 95,
    Type: 'Transport',
  },

  {
    Ticker: 'USB',
    Value: 96,
    Id: 96,
    Type: 'Finance',
  },
  {
    Ticker: 'VCEL',
    Value: 97,
    Id: 97,
    Type: 'Manufacturing',
  },
  {
    Ticker: 'WMT',
    Value: 98,
    Id: 98,
    Type: 'Consumer',
  },
  {
    Ticker: 'YNDX',
    Value: 99,
    Id: 99,
    Type: 'Consumer',
  },
  {
    Ticker: 'ZYXI',
    Value: 100,
    Id: 100,
    Type: 'Other',
  },
];
```

See [Quantile Aggregation Scalar Expressions](https://www.adaptabletools.com/docs/adaptable-ql-expression-quantile/index.md) for more information
