Window Calculated Columns
Summary
- Window Calculated Columns look up values from other rows in an ordered series
- Each cell's value is derived by ordering the rows (via
OVER) and referencing a row at a relative or absolute position
Calculated Columns are able to display Window Expressions.
These are an advanced form of Aggregation Expression, closely related to Cumulative Expressions.
The value of each cell derives from multiple rows, and the Window Expression orders the rows and then looks up a value from another row in that ordering.
This makes them ideal for row-relative comparisons - e.g. "the value in the previous row", "the value in the next row", or "this row's rank".
Window Functions
Window Expressions use ordered-series functions.
Each function takes an OVER clause that defines the ordering (a number or date column):
| Function | Returns | Example |
|---|---|---|
LAG | The value from a previous row (SQL LAG) | LAG{[github_stars], 1, OVER([created_at])} |
LEAD | The value from a later row (SQL LEAD) | LEAD{[github_stars], 1, OVER([created_at])} |
FIRST | The value from the first row in the ordering | FIRST{[github_stars], OVER([created_at])} |
LAST | The value from the last row in the ordering | LAST{[github_stars], OVER([created_at])} |
RANK | The 1-based rank of the row in the ordering (ties share a rank) | RANK{OVER([github_stars], 'DESC')} |
Hint
- Each function also accepts an optional
GROUP_BY([col])clause - This means the lookup (or rank) is computed within each partition rather than across the whole grid
Developer Guide
Defining a Window Calculated Column
There are 5 properties that you need to provide when defining a Window Calculated Column:
CalculatedColumn: {
CalculatedColumns: [
{
ColumnId: 'prev-stars',
FriendlyName: 'Previous Stars',
Expression: 'LAG{[github_stars], 1, OVER([created_at])}',
CalculatedColumnSettings: {
DataType: 'number',
Filterable: true,
},
},
{
ColumnId: 'stars-rank',
FriendlyName: 'Stars Rank',
Expression: "RANK{OVER([github_stars], 'DESC')}",
CalculatedColumnSettings: {
DataType: 'number',
Sortable: true,
},
},
{
ColumnId: 'first-stars',
FriendlyName: 'First Stars',
Expression: 'FIRST{[github_stars], OVER([created_at])}',
CalculatedColumnSettings: {
DataType: 'number',
},
},
],
},This ColumnId value is used to reference the Column in AdapTable State and in other objects (e.g. Layouts).
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 Window Expression evaluated by AdapTableQL by ordering the rows and looking up a value from another row. It contains:
- a window function (
LAG,LEAD,FIRST,LASTorRANK) - an
OVERclause that sets the ordering - optionally, a
GROUP_BYclause that partitions the series
The Data Type of the Calculated Column should be provided in CalculatedColumnSettings.
It is a Cell Data Type 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:
FilterablePivotableSortableGroupable
Series
The "Series" set of 4 Window Functions return data based on a series of rows.
LAGandLEADreturn the stars of the neighbouring repo in creation orderFIRSTandLASTreturn the stars of the earliest and latest repo
Note
All four use OVER([created_at])
- This demo contains 4 Calculated Columns that read another row's
Github Stars, all orderedOVER([created_at]):Previous Stars-LAGwith offset 1, the repo created immediately before this oneNext Stars-LEADwith offset 1, the repo created immediately after this oneFirst Stars-FIRST, the earliest repo (the same value on every row)Last Stars-LAST, the latest repo (the same value on every row)
- The Grid is sorted by
Createdascending, soPrevious StarsandNext Starsline up with the rows above and below
- Compare
Previous StarsandNext Starswith theGithub Starsof the rows immediately above and below First StarsandLast Starsstay the same on every row
Rank
Another use case for Window functions is to return a row's place in the ordering.
This is done using the RANK function.
It has 2 additional arguments:
'DESC'- reverses the order (e.g. the repo with the most stars is rank 1)GROUP_BY- creates 1 ranking per element, e.g.GROUP_BY([language])creates a ranking for each language
- This demo contains 2 Calculated Columns that use
RANK:Stars Rank- every repo ranked byGithub Stars, so the most stars is rank 1 (ties share a rank)Rank within Language- the same ranking, restarted inside eachLanguagewithGROUP_BY([language])
- The Grid is sorted by
Github Starsdescending, so rank 1 is the top row
- Group the Grid by
Languageand seeRank within Languagerestart for each Language
Find Out More
See Window Expressions for more information