# Cumulative Calculated Columns

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

- Cumulative Calculated Columns perform cumulative aggregations
- The aggregation operation is applied to each row cumulatively  in a specific, given order

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

These are an advanced form of [Aggregation Expression](https://www.adaptabletools.com/docs/adaptable-ql-expression-aggregation/index.md).

They are similar in that the values derive from multiple rows but with one fundamental difference: they perform **cumulative aggregations**.

This means that the aggregation operation (e.g., `SUM`, `MIN`, `MAX`) is applied to each row **cumulatively** in a specific, given order.

This is particularly useful, for example, when we want to calculate a Running Total of a column

With the [Server-Side Row Model](https://www.adaptabletools.com/docs/dev-guide-row-models-server-overview/index.md), evaluate Cumulative 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 Cumulative Calculated Column

There are 5 properties that you need to provide when defining a Cumulative 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 [Cumulative Expression](https://www.adaptabletools.com/docs/adaptable-ql-expression-cumulative/index.md) evaluated by [AdapTableQL](https://www.adaptabletools.com/docs/adaptable-ql-expression/index.md) cumulatively across the grid in a specified order.

It is the `Expression` string of the Calculated Column and uses an aggregation (`SUM`, `MIN`, `MAX`, etc.) with a `CUMULATIVE` clause that sets the ordering.

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: 'cumulated-stars-count',
      FriendlyName: 'Cumulated Stars Count',
      Expression: 'SUM{[github_stars], CUMULATIVE([created_at])}',
      CalculatedColumnSettings: {
        DataType: 'number',
        Filterable: true,
      },
    },
    {
      ColumnId: 'max-tech-debt-over-stars',
      FriendlyName: 'Maximum Technical Debt',
      Expression: 'MAX{[open_issues_count], CUMULATIVE([github_stars])}',
      CalculatedColumnSettings: {
        DataType: 'number',
        Sortable: true,
      },
    },
  ],
},
```

<AiSkill skill="adaptable-dev" embedded>

Add 2 Cumulative Calculated Columns to the current Layout:
(a) "Running Notional" which will show a running total of the Notional column, with rows ordered by Trade Date
(b) "Peak PnL So Far" which will show the maximum PnL seen so far when rows are ordered by Trade Date

</AiSkill>

**Example: Calculated Columns: Cumulative**

Calculated Columns: Cumulative Aggregated Expressions

- This demo contains 2 Calculated Columns that use [Cumulative Aggregation Scalar Expressions](https://www.adaptabletools.com/docs/adaptable-ql-expression-cumulative/index.md):
  - `Cumulated Stars Count` - displays the cumulative sum of stars count of all repos in the grid, aggregated over the `Created` date
  - `Maximum Technical Debt` - shows the cumulative maxima of open issues of all repos in the grid, aggregated over the `GitHub Stars` count
- The Calculated Columns are formatted using [Column Formatting](https://www.adaptabletools.com/docs/technical-reference-styling-formatting/index.md).
### Expand to see the Cumulative Aggregation Scalar Expressions

```ts
CalculatedColumn: {
  CalculatedColumns: [
    {
      ColumnId: 'cumulated-stars-count',
      FriendlyName: 'Cumulated Stars Count',
      Expression: 'SUM{[github_stars], CUMULATIVE([created_at])}',
      CalculatedColumnSettings: {
        DataType: 'number',
      },
    },
    {
      ColumnId: 'max-tech-debt-over-stars',
      FriendlyName: 'Maximum Technical Debt',
      Expression: 'MAX{[open_issues_count], CUMULATIVE([github_stars])}',
      CalculatedColumnSettings: {
        DataType: 'number',
      },
    },
  ],
},
```

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: 'Cumulative 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: [
            'name',
            'created_at',
            'github_stars',
            'open_issues_count',
            'cumulated-stars-count',
            'max-tech-debt-over-stars',
          ],
          ColumnSorts: [{ColumnId: 'created_at', SortOrder: 'Asc'}],
        },
      ],
    },
    CalculatedColumn: {
      CalculatedColumns: [
        {
          ColumnId: 'cumulated-stars-count',
          FriendlyName: 'Cumulated Stars Count',
          Expression: 'SUM{[github_stars], CUMULATIVE([created_at])}',
          CalculatedColumnSettings: {
            DataType: 'number',
          },
        },
        {
          ColumnId: 'max-tech-debt-over-stars',
          FriendlyName: 'Maximum Technical Debt',
          Expression: 'MAX{[open_issues_count], CUMULATIVE([github_stars])}',
          CalculatedColumnSettings: {
            DataType: 'number',
          },
        },
      ],
    },
    FormatColumn: {
      FormatColumns: [
        {
          Name: 'formatColumn-created_at',
          Scope: {
            ColumnIds: ['created_at'],
          },
          DisplayFormat: {
            Formatter: 'DateFormatter',
            Options: {
              Pattern: 'yyyy/MM/dd',
            },
          },
        },
      ],
    },
  },
};
```
