Referencing Calculated Columns

View as Markdown

Summary

  • Once created a Calculated Column can be used and managed like any other Column in AG Grid
  • Calculated Columns can also be referenced in AG Grid Charts
  • Calculated Columns can also be referenced in other Calculated Columns
  • AdapTable provides 2 UI components to allow run-time users to follow (and edit) a Calculated Column's formula
    • Formula Trace shows - Excel-style - how a Calculated Column value is derived
    • The Compact Expression Editor lets the Expression be edited inline

Calculated Columns are treated like any other AdapTable (or AG Grid) Column.

Accordingly, a Calculated Column's Expression can reference other Calculated Columns in the Grid if required.

Caution

  • There is no limitation on how many Calculated Columns are chained - AdapTableQL will evaluate each in turn
  • However at some point performance might be impacted if you have a large chain with complicated calculations
Calculated Columns: Referencing
Fork
  • This demo contains 3 Calculated Columns, one of which is evaluated based on the other 2
    • Issues Open/Total Ratio - shows ratio between open and total number of issues
    • PR Open/Total Ratio - shows ratio between open and total number of pull-requests
    • Score - is calculated by computing the average value of the previous 2 ratio Calculated Columns (a higher score is better)
Try It Out
  • Change a value in Open Issues or Closed Issues and see how the Score Calculated Column also updates
  • Change Score to take the maximum value between 'Issues Open/Total Ratio' and 'PR Open/Total Ratio'
Loading demo…

This page covers referencing other Calculated Columns, Formula Trace, and using calculated values across the Grid. For creating columns, the Wizard, and Initial State, see Calculated Columns.

Tracing Calculated Columns

AdapTable includes an Excel-style Formula Trace which lets run-time users see - and inline edit - how a Calculated Column value is derived.

There are 2 ways to open Formula Trace for a Calculated Column cell:

  • Double-click the cell
  • Select the Show Formula item in the cell's Context Menu

Note

Both triggers are enabled by default, but each can be turned off - see Formula Trace Options below

When opened, Formula Trace displays a popup - anchored to the cell - containing the Column's Expression with every referenced Column shown in its own colour.

At the same time, AdapTable highlights the referenced data in the Grid using the same colours (in the manner of Excel):

  • for Standard Calculated Columns the referenced cells in the row are outlined and tinted
  • for Aggregated (and other multi-row) Calculated Columns the referenced Column headers are highlighted

Hint

For Standard Calculated Columns the popup also shows each referenced Column's name above its actual cell value for that row, followed by the computed result (e.g. = 42) - so you can see the calculation "worked through" for the current row

Editing a Formula

The Formula Trace popup contains an Edit button which opens the Compact Expression Editor.

This is a small, moveable and resizable version of the full Expression Editor, giving quick access to the Expression without opening the full Wizard.

It provides the same power as the full Editor, including:

  • the Functions dropdown listing all available Expression Functions
  • live validation
  • colour-coded Column references (matching the Formula Trace highlights)

Edits are applied live as you type - the Calculated Column re-evaluates immediately.

Note

An invalid Expression will show #INVALID in the cells until it is corrected

The Compact Editor also provides 2 buttons:

  • Revert - restores the Expression to how it was when the Editor was opened
  • Open Full Editor - closes the Compact Editor and opens the full Calculated Column Wizard

Hint

The Compact Expression Editor is available for all 6 Calculated Column types: Standard, Aggregated, Cumulative, Quantile, Window and Observable

Showing Formula Trace via the API

Formula Trace can also be opened programmatically using the Calculated Column API:

  • showFormulaTrace(columnId, primaryKeyValue) - opens the Formula Trace popup for the given Calculated Column cell
  • hideFormulaTrace() - closes the popup (and clears the Grid highlights)
Using Calculated Columns: Formula Trace
Fork
  • This demo contains 3 Calculated Columns:
    • Total PRs (Standard) - sums the Open PRs and Closed PRs Columns
    • Issues Open/Total Ratio (Standard) - divides Open Issues by total Issues
    • Sum Pop by Lang (Aggregated) - sums Github Watchers grouped by Language
  • After 5 seconds the demo calls showFormulaTrace to open the Formula Trace popup for the first Total PRs cell (see onAdaptableReady.ts)
Try It Out
  • Wait for the Formula Trace popup to appear, or double-click any Total PRs cell yourself
  • Note how the two referenced cells (Open PRs, Closed PRs) are highlighted in matching colours
  • Click the Edit button in the popup and change the Expression in the Compact Editor - the Column updates live
  • Double-click a Sum Pop by Lang cell and note the referenced Column header is highlighted instead
Loading demo…

Configuring Formula Trace

Formula Trace is configured via the formulaTrace property in Calculated Column Options.

There are 2 properties available to set the formula trace behaviour:

enableDoubleClick

Default: true
Boolean
Whether double-clicking a Calculated Column cell opens Formula Trace

showContextMenuItem

Default: true
Boolean
Whether the "Show Formula" item appears in the cell Context Menu

Flashing Calculated Columns

Calculated Columns can flash their cells when their contents change.

Note

Calculated Columns are not directly editable but they will update if a referenced column's value changes

It is possible for one Calculated Column to reference another Calculated Column and still flash when changed.

Calculated Columns: Flashing
Fork
  • This example shows 2 Calculated Columns which flash when values in their referenced columns change:
    • Total Stars + Watchers has an ANY_CHANGE() rule and flashes when one of its referenced Columns (Github Stars and Github Watchers) tick
    • Stars Index (÷ 1000) references the first Calculated Column with a rule [stars-index] > 50, so the chained column flashes when its value changes (and the rule matches)
Loading demo…

Calculated Columns and Alerts

Calculated Columns can trigger Alerts.

Note

This is most commonly a Data Change Alert with a Boolean Expression evaluated against the changed cell value

Similar to Flashing Cells its possible for one Calculated Column to reference another Calculated Column and still trigger the Alert.

Calculated Columns: Alerts
Fork
  • This example shows 2 Calculated Columns which trigger Data Change Alerts when values in their referenced columns change:
    • Total Stars + Watchers triggers an Alert (with a Notification) using the rule [total-stars-watchers] > 200000
    • Stars Index (÷ 1000) references the first Calculated Column and a second Alert uses the rule [stars-index] > 150 on that chained column
Try It Out
  • Watch notifications appear when github_stars or github_watchers tick and their calculated values satisfy the Alert rules
Loading demo…
Deep Dive

Recalculating Dependent Calculated Columns

Calculated Columns in Charts

As "normal" Columns, Calculated Columns can also be referenced in Charts.

If a Chart contains a Calculated Column, then any changes to a Column which is referenced in the Calculated Columns's Expression, will cause the chart to re-render.

Charts Calculated Column
Fork
  • This example demonstrates how Calculated Columns can be used in Charts
  • We create a Calculated Column called Total PRs which sums the Open PRs and Closed PRs columns
  • We then create a small Pie Chart which displays the names of the first 5 frameworks and the Total PRs
  • Any change to the underlying value of one of the Calculated Column Expression's constituent columns will cause the Chart to redraw
Try It Out
  • Set the Open PRs Column in the first row (for Vue) to be 22400 and see how the Chart changes
Loading demo…