# Configuring Calculated Columns

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

- Calculated Columns can be provided by developers through Calculated Column Initial State
- The definition includes an Id, the Column's Expression and any associated behaviour

## AG Grid Definitions

Calculated columns are typically **not** provided in the `ColDefs` property in AG Grid GridOptions.

Instead, the definition provided in Initial State suffices for AdapTable to be able to create the associated AG Grid column automatically.

However sometimes a developer might want to add an AG Grid element to the column.

For instance a tooltip might be needed, or there might be a requirement to put the Calculated Column inside a Column Group (which is an AG Grid feature).

This is possible: a column can be provided in AG Grid ColDefs but by specifying a [Column Type](https://www.adaptabletools.com/docs/dev-guide-columns-column-types/index.md) of `calculatedColumn`, AdapTable will automatically wire it up with an associated Calculated Column definition.

- Set the [Column Type](https://www.adaptabletools.com/docs/dev-guide-columns-column-types/index.md) to be `calculatedColumn`
- The `ColId` in the Column Definition and `ColumnId` in the Calculated Column definition must be the same value

**Example: Calculated Columns: Defining in AG Grid**

Calculated Columns: Adding AG Grid properties

- This demo contains an AG Grid Column Group - `Github Averages`
- The group contains 2 Calculated Columns `Github Avg By Language` and `Github Avg By Licence`
- Both the [Column Group](https://www.adaptabletools.com/docs/handbook-grouping-columns/index.md) and the 2 containing Columns were defined in AG Grid Column Defs
- The 2 columns in the Group were given AG Grid Header and Cell ToolTips
- They were also given a type of `calculatedColumn` which is what allowed AdapTable to wire everything together

- Hover over a cell in the Calculated Column and see the Tooltip which AG Grid provides

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           [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: 'Defining Calculated Columns in AG Grid',
  initialState: {
    Dashboard: {
      ModuleButtons: ['CalculatedColumn', 'FormatColumn'],
    },
    StatusBar: {
      StatusBars: [
        {
          Key: 'Center Panel',
          StatusBarPanels: ['Layout', 'CalculatedColumn'],
        },
      ],
    },
    Theme: {CurrentTheme: 'dark'},
    Layout: {
      CurrentLayout: 'Standard Layout',
      Layouts: [
        {
          TableColumns: [
            'name',
            'github_stars',
            'license',
            'language',
            'starsAvgByLanguage',
            'starsAvgByLicence',
            'github_watchers',
          ],
          Name: 'Standard Layout',
        },
      ],
    },
    CalculatedColumn: {
      CalculatedColumns: [
        {
          ColumnId: 'starsAvgByLanguage',
          Expression: 'AVG{[github_stars], GROUP_BY([language])}',
          FriendlyName: 'Github Avg By Language',
          CalculatedColumnSettings: {DataType: 'number'},
        },
        {
          ColumnId: 'starsAvgByLicence',
          Expression: 'AVG{[github_stars], GROUP_BY([license])}',
          FriendlyName: 'Github Avg By Licence',
          CalculatedColumnSettings: {DataType: 'number'},
        },
      ],
    },
    FormatColumn: {
      FormatColumns: [
        {
          Name: 'formatColumn-starsAvgByLanguage',
          Scope: {
            ColumnIds: ['starsAvgByLanguage', 'starsAvgByLicence'],
          },
          DisplayFormat: {
            Formatter: 'NumberFormatter',
            Options: {
              FractionDigits: 2,
            },
          },
        },
      ],
    },
  },
};
```
**`src/columnDefs.ts`**

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

export const columnDefs: (ColDef | ColGroupDef)[] = [
  {
    field: 'name',
    cellDataType: 'text',
    sortable: true,
  },

  {
    field: 'language',
    cellDataType: 'text',
    enablePivot: true,
    enableRowGroup: true,
  },
  {
    field: 'github_stars',
    headerName: 'GitHub Stars',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'license',
    cellDataType: 'text',
    editable: false,
    enablePivot: true,
    enableRowGroup: true,
  },
  {
    headerName: 'Github Averages',
    children: [
      {
        colId: 'starsAvgByLanguage',
        type: ['calculatedColumn'],
        cellDataType: 'number',
        headerTooltip:
          "Shows all the Github Stars for the framework's language",
        tooltip: (params: ITooltipParams) => {
          return 'All Stars for ' + params.data.language;
        },
      },
      {
        colId: 'starsAvgByLicence',
        type: ['calculatedColumn'],
        cellDataType: 'number',
        headerTooltip: "Shows all the Github Stars for the framework's licence",
        tooltip: (params: ITooltipParams) => {
          return 'All Stars for ' + params.data.license;
        },
      },
    ],
  },
  {
    field: 'week_issue_change',
    headerName: 'Issue Change',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'created_at',
    headerName: 'Created',
    cellDataType: 'date',
  },
  {
    field: 'has_wiki',
    headerName: 'Has Wiki',
    cellDataType: 'boolean',
    enablePivot: true,
    enableRowGroup: true,
  },
  {
    field: 'updated_at',
    headerName: 'Updated',
    cellDataType: 'date',
  },
  {
    field: 'pushed_at',
    headerName: 'Pushed',
    cellDataType: 'date',
  },
  {
    field: 'github_watchers',
    headerName: 'GitHub Watchers',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'description',
    cellDataType: 'text',
    sortable: false,
  },
  {
    field: 'open_issues_count',
    headerName: 'Open Issues',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'closed_issues_count',
    headerName: 'Closed Issues',
    cellDataType: 'number',
    enableValue: true,
  },

  {
    field: 'open_pr_count',
    headerName: 'Open PRs',
    cellDataType: 'number',
    enableValue: true,
  },
  {
    field: 'closed_pr_count',
    headerName: 'Closed PRs',
    cellDataType: 'number',
    enableValue: true,
  },
  {field: 'has_projects', headerName: 'Has Projects', cellDataType: 'boolean'},
  {field: 'has_pages', headerName: 'Has Pages', cellDataType: 'boolean'},
  {
    field: 'topics',
    cellDataType: 'textArray',
    editable: false,
    sortable: false,
  },
  {
    field: 'id',
    cellDataType: 'number',
    hide: true,
    editable: false,
  },
];
```

See [Adding Column Types for Special Columns](https://www.adaptabletools.com/docs/dev-guide-columns-column-types/index.md) for more details and a demo
