#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:
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.
Use CONCAT to join text: CONCAT([firstName], " ", [lastName]). The +
operator is meant for numbers.
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.
#Referencing other calculated columns
An expression can reference other calculated columns, which in turn can reference others.
- Monthly salary (not shown in the view) —
[salary] / 12 - Monthly after tax —
ROUND([monthlySalary] * 0.7, 0) - Daily after tax —
ROUND([monthlyAfterTax] / 21, 0)
#Aggregated, cumulative and window expressions
Besides standard (per-row) expressions, calculated columns accept expressions that look at other rows too:
| Kind | Example | Value for each row |
|---|---|---|
| Aggregated | [salary] - AVG{[salary]} | Difference from the average salary |
| Aggregated, grouped | AVG{[salary], GROUP_BY([country])} | Average salary in the row's country |
| Cumulative | SUM{[salary], CUMULATIVE([id])} | Running total, ordered by id |
| Quantile | QUARTILE{[salary]} | Which quartile (1–4) the salary falls in |
| Window | RANK{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.
- 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.
#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.
- 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.
#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:
- the kind of expression (standard, aggregated, cumulative, quantile or window);
- the column id and label;
- the expression, in an AdaptableQL editor with completions and a live preview on sample rows;
- column properties — filterable, sortable, resizable, groupable, pivotable, aggregatable, exportable — and sizing;
- 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 }.