Adaptable

#Calculated Columns

A calculated column doesn't read its value from the data. Its value is derived from an AdaptableQL expression that typically references other columns in the row — for example [salary] / 12.

  • The expression returns a single value per row and is re-evaluated as the data changes.
  • Once defined, a calculated column behaves like any other column: you can sort, filter, group and aggregate by it, style it and export it.
  • Calculated columns are read-only — their cells are never editable.

#Defining calculated columns

A calculated column is a regular column definition with an expression instead of a field or valueGetter:

Calculated columns in availableColumns
globalEntities: {
  availableColumns: {
    salary: { field: 'salary', dataType: 'number' },
    monthlySalary: {
      label: 'Monthly salary',
      dataType: 'number',
      expression: 'ROUND([salary] / 12, 0)',
    },
  },
},

The dataType is mandatory and should match what the expression returns: 'number', 'text', 'date' or 'boolean'. When users create a calculated column in the UI, the editor infers the data type from the expression.

Expressions reference other columns by their column id, in square brackets. Any column available to the current view can be referenced — it doesn't have to be visible or part of the view's columnOrder.

Tip

Use CONCAT to join text: CONCAT([firstName], " ", [lastName]). The + operator is meant for numbers.

#
Basic calculated columns

Three calculated columns:

  • Full name — CONCAT([firstName], " ", [lastName])
  • Monthly salary — ROUND([salary] / 12, 0)
  • Salary band — [salary] > 150000 ? "High" : [salary] > 100000 ? "Medium" : "Low"

Double-click a salary cell and change the value: the calculated columns update. Open the settings panel from the dashboard to edit the calculated columns or create new ones.

open
loading demo…

#Referencing other calculated columns

An expression can reference other calculated columns, which in turn can reference others.

#
Calculated columns referencing calculated columns
  • Monthly salary (not shown in the view) — [salary] / 12
  • Monthly after tax — ROUND([monthlySalary] * 0.7, 0)
  • Daily after tax — ROUND([monthlyAfterTax] / 21, 0)
open
loading demo…

#Aggregated, cumulative and window expressions

Besides standard (per-row) expressions, calculated columns accept expressions that look at other rows too:

KindExampleValue for each row
Aggregated[salary] - AVG{[salary]}Difference from the average salary
Aggregated, groupedAVG{[salary], GROUP_BY([country])}Average salary in the row's country
CumulativeSUM{[salary], CUMULATIVE([id])}Running total, ordered by id
QuantileQUARTILE{[salary]}Which quartile (1–4) the salary falls in
WindowRANK{OVER([salary])}Rank of the row, ordered by salary

Aggregations scan the full data set (not only the rows that pass the current filters) and are recomputed when the data changes. See Expressions for the syntax and the available functions.

#
Calculated columns with aggregations
  • Vs. average — [salary] - ROUND(AVG{[salary]}, 0)
  • Country average — ROUND(AVG{[salary], GROUP_BY([country])}, 0)
  • Share % — ROUND([salary] / SUM{[salary]} * 100, 1)
  • Rank — RANK{OVER([salary])} (1 is the lowest salary)
  • Running total — SUM{[salary], CUMULATIVE([id])}

Edit a salary and watch every row's aggregated values update.

open
loading demo…

#Styling calculated columns

Calculated columns can be formatted and styled like any other column — both as the target of a styled cell and inside its condition.

#
Styling calculated columns
  • Monthly salary is formatted as a currency and bold.
  • Salary band is uppercased and highlighted where [salaryBand] = "High".
  • Rows are highlighted where [monthlySalary] > 12000 — a condition on a calculated column.
open
loading demo…

#Managing calculated columns in the UI

The settings panel has a Calculated Columns page, listing the calculated columns defined in globalEntities.availableColumns — those in the current view first, then the others, ready to be added to the view. Its editor walks the user through:

  1. the kind of expression (standard, aggregated, cumulative, quantile or window);
  2. the column id and label;
  3. the expression, in an AdaptableQL editor with completions and a live preview on sample rows;
  4. column properties — filterable, sortable, resizable, groupable, pivotable, aggregatable, exportable — and sizing;
  5. a review step, with the option to add the new column to the current view.

The header menu of a calculated column also has an Edit Calculated Column item. The calculatedColumn dashboard widget opens the page directly.

The manager is also available standalone as <adaptable-calculated-columns-manager> (it only needs an <adaptable-provider> ancestor):

<adaptable-provider
  adaptableId="my-grid"
  primaryKey="id"
  [data]="data"
  [defaultState]="defaultState"
>
  <adaptable-ui />
  <adaptable-calculated-columns-manager />
</adaptable-provider>

Who can create, edit, delete or add calculated columns to a view is controlled by the calculatedColumn_* permissions — see Permissioning.

#The injectCalculatedColumns function

injectCalculatedColumns() is the Angular counterpart of the useCalculatedColumns hook. For custom UIs, it exposes the same operations: call it in a component rendered inside <adaptable-provider>; it returns a signal of an object with them:

import { injectCalculatedColumns } from '@adaptabletools/datagrid-angular';

// a component rendered inside <adaptable-provider>
@Component({ selector: 'my-calculated-columns-toolbar', template: `<!-- ... -->` })
class MyCalculatedColumnsToolbar {
  // a signal: read it where you use it, so it is always current
  private readonly calculatedColumnsModel = injectCalculatedColumns();

  onToolbarAction() {
    const {
      calculatedColumns, // all calculated columns, with their ids
      isInCurrentView, // (columnId) => boolean
      createCalculatedColumn, // ({ id, ...config }, { addToCurrentView? }) => id
      updateCalculatedColumn, // (columnId, config) => void
      deleteCalculatedColumn, // (columnId) => void — also removed from every table view
      addToCurrentView,
      removeFromCurrentView,
      getDataTypeFromExpression, // (expression) => data type, when it can be inferred
    } = this.calculatedColumnsModel();

    // ...
  }
}

createCalculatedColumn adds the new column to the current table view unless you pass { addToCurrentView: false }.