Cumulative (Aggregation) Expressions
Summary
- AdapTableQL performs cumulative aggregations by using the
CUMULandOVERfunctions
Cumulative Expressions, as the name suggests, perform cumulative Aggregations.
Hint
This is particularly useful when calculating Running Totals
In this use case, the Aggregation Operation is applied to each row in the set cumulatively.
Note
Cumulative Expressions are actually a special case of Aggregation Expression
Aggregation Operations
There are 5 Aggregation Operations available when using Cumulative Aggregations:
SUMMINMAXAVGPERCENTAGE
Using OVER to set Order
The Aggregation Operation in a Cumulative Aggregation is applied in a specific order.
That order is specified in the Expression by the OVER keyword operator.
This takes the name of an Adaptable Column as its sole parameter.
Note
The OVER operator only works with Columns of type date or number
// Derive cumulative sum of all PnL columns according to TradeDate order
CUMUL(SUM([PnL]), OVER([TradeDate])) // Calculate cumulative maximum value of all PnL columns according to TradeDate order
CUMUL(MAX([PnL]), OVER([TradeDate]))Hint
- There is no need physically to sort the Grid using the column specified by
OVER - Indeed, the beauty of
OVERis that ignores the current Grid Sort and uses the order in the given Column
- This demo contains 2 Calculated Columns that use Cumulative Aggregation Scalar Expressions:
Cumul Stars- displays the cumulative sum of stars count of all repos in the grid, aggregated over theCreateddateMax Technical Debt- shows the cumulative maxima of open issues of all repos in the grid, aggregated over theGitHub Starscount
Expand to see the Aggregated Cumulative Scalar Expressions
Grouped Cumulative Aggregation
Like with Standard Scalar Aggregations, Cumulative Aggregations can be grouped.
Aging this is accomplished by using the GROUP_BY function.
Caution
GROUP_BY is applied as a parameter to the Aggregation operator being used (e.g. SUM) and not to CUMUL
// Cumulative sum of all PnL columns ordering by TradeDate, grouping by Currency
CUMUL(SUM([PnL], GROUP_BY([currency])), OVER([TradeDate]))Similar to Standard Scalar Aggregations, there is no limit on the number of Columns which can be grouped.
// Cumulative sum of all PnL columns ordering by TradeDate, grouping by Currency and Country
CUMUL(SUM([PnL], GROUP_BY([currency], [country])), OVER([TradeDate]))- This demo contains 2 Calculated Columns that use Cumulative Aggregation Scalar Expressions, one with grouping:
Cumul Stars- displays the cumulative sum of stars count of all repos in the grid, aggregated over theCreateddate (same as in demo above)Cumul Lang Stars- displays the cumulative sum of stars count in the grid, but grouped byLanguage, and aggregated over theCreateddate