Aggregation Expressions
Summary
- Aggregated Expressions are evaluated against grouped data and can return any value
- 3 specialised use cases are also available:
- Limiting scope of Expression to a group of rows by using the
GROUP_BYfunction - Performing cumulative aggregations by using the
CUMULandOVERfunctions - Creating buckets (e.g. Quartiles, Percentiles) using the
QUANTfunction
- Limiting scope of Expression to a group of rows by using the
Aggregation Scalar Expressions are special Expressions which run against aggregated data.
They can be of 2 types:
| Type | Returns | Available in Modules |
|---|---|---|
| Scalar | Any value | Calculated Column |
| Boolean | True or False | Alerts |
Find Out More
AdapTable also provides 2 specialist Aggregation Scalar Expressions: Cumulative and Quantile
The Expression evaluates to a single value for each row, but each value is dynamically derived by aggregating multiple rows and columns of the grid.
Scalar Aggregtion Expressions
Scalar Aggregation Expressions are the most basic form of Aggregation.
Note
- By default Scalar Aggregated Expressions will compute against the whole grid
- See Grouped Aggregation for more advanced used cases
Scalar Aggregation Expressions are of type Aggregated Scalar Query, and require just 2 elements:
<AGGREGATION_FUNCTION>
This is the AdapTableQL Aggregated Expression Function used to aggregate the data.
The Aggregation Function can be one of multiple types:
SUM: sums the values of the specified columnPERCENTAGE: calculates the percentage of the specified column
Note
The default denominator for Percentage is the SUM of the specified columns, but this can be overridden
AVG: calculates the average of the specified columnMIN: returns the minimum value of the specified columnMAX: returns the maximum value of the specified columnCOUNT: returns the number of items in the specified columnMEDIAN: returns the middle value in the specified columnMODE: returns the most popular item in the specified columnDISTINCT: returns the number of distinct items in the specified columnONLY: returns the the specified column's value if all are the sameSTD_DEVIATION: returns the standard deviation for the specified column
Note
- Further Aggregation Functions will be added in future releases in response to user requests
- Developers can provide their own Custom Aggregation Functions to supplement this list
COLUMN
This is the Adaptable Column that contains the values being aggregated.
Examples
At its most basic just an Aggregation Function and a Column is required:
// Return sum of all PnL cells
SUM([PnL]) // Return the lowest Price in the Grid
MIN([Price])The Column that is being aggregated can itself be a Calculated Column (i.e. one which itself is evaluated using an Aggregated Expression)
// Create a Calculated Column which displays the Price plus VAT
CalculatedColumns: [{ ColumnId: 'totalPrice', Query: { ScalarExpression: '[price] + [VAT]'}]
// Expression returns sum of all totalPrice Columns i.e. all Prices in the Grid with VAT
SUM([totalPrice])The AVG Aggregation Function can receive an optional WEIGHT parameter to create Weighted Averages:
// Return the Weighted Average for the Price Column (using Index column for weighting)
AVG([Price], WEIGHT([index]))- This Demo contains 2 Calculated Columns that use Aggregated Scalar Expressions
Popularity Ratio- displays a Percentage of
PopularityColumn Popularityis itself a Calculated Column that sumsGitHub StarsandGitHub Watchers
- displays a Percentage of
Total Stars- shows a Sum of of the GitHub Stars column
- because
GROUP_BYis not added, is the same value in each row
Add a GROUP_BY to the Total Stars Expression to Sum according to Language
Boolean Aggregation Expressions
Boolean Aggregation Expressions (of type Aggregated Boolean Query) are similar to Scalar Aggregation Expressions but only return a true / false value.
Hint
- This is ideal for Limits Management and related scenarios
- For instance where you want to check that a sum comprising multiple cell values has not been exceeded
In addition to Aggregation Function and Column, 2 addtional elements are typically required:
<OPERATOR>
This defines the operator which is used to compare the aggregated value with the threshold.
At present the following comparison operators are supported:
=- Equals!=- Not Equals>- Greater Than<- Less Than>=- Greater Than or Equals<=- Less Than or Equals
<THRESHOLD>
The Threshold is the (numeric) value that is being compared against the aggregated value.
aa
In place of absolute numbers, <THRESHOLD> can comprise a number with a string abbreviation.
The following shortcuts can be used:
K: thousand (e.g. '5K')M: million (e.g. '2M')B: billion (e.g. '4B')
Caution
When using a threshold shortcut the value must be placed in quotation marks
Examples
// Is total value of the 'PnL' column in all rows > 5 Million
SUM([PnL]) > '5000000' // Alternate form of above Expression but using abbreviated threshold notation
SUM([PnL]) > '5M' // Is the lowest Bid worth 40,000
MIN([Bid]) = '40K' // Is the average Price over 3 Million
AVG([Price]) > '3M'- This demo contains 3 Alerts that fire when an Aggregated Boolean Expression is triggered
- if the lowest
Github Watchersvalues is under 50 - when the total of all the
Github Starsrows is over 10,000 - when the total of all
Open Issuesrows is over 6,000
- if the lowest
- In the first row change the value of:
Github Watcherscolumn to 45 and see theInfoAlert be firedGithub Starscolumn to 279429 and see theSuccessAlert be firedOpen Issuescolumn to 1220 and see theWarningAlert be fired
Grouped Aggregation
An Aggregation Scalar Function can be applied just to a group of rows, rather than to the entire grid.
This is done by using the GROUP_BY AdapTableQL Expression Function.
Note
The GROUP_BY function is always only used together with an Aggregation Function
The GROUP_BY function typically receives a single parameter - the name of a Column:
// Return Sum of all Pnl cells for rows which have this row's Currency value
SUM([PnL], GROUP_BY([Currency]) )It is particularly useful when used with the COUNT function to give the numbers in each group:
// Return count of all rows in the Grid which have the same Currency as this one
COUNT([Currency], GROUP_BY([Currency]) )For more advanced use cases, the GROUP_BY function can receive multiple Columns:
// Return Sum of all Pnl cells for rows which have this row's Currency and Counterparty values
SUM([PnL], GROUP_BY([Currency], [Counterparty]) )Deep Dive
How Grouped Aggregation Works
- This Demo contains 4 Calculated Columns that use Aggregated Scalar Expressions with Grouping
Watchers By Lang- Sum of of the GitHub Watchers column, grouped by LanguageAvg. Lang Popularity- Average of Github Stars popularity, grouped by languageOpen/Closed Issues % by Lang- Percentage of Open and Closed issues, grouped by languageLang Count- Count of each language, also grouped language
Expand to see the Aggregated and Grouped Scalar Expressions
WHERE Clause
Optionally, a WHERE clause may be appended to an Aggregated Boolean Expression.
This is used to narrow down the scope of the 'main' part of the Expression.
The WHERE Clause takes the form of a Standard Expression.
// Are all PnL values in rows with Dollar Currency over 5M
SUM([PnL]) > '5M' WHERE [Currency] = 'USD' // Similar Expression to above but with more complex WHERE Clause
SUM([PnL]) > '5M' WHERE [Currency] = 'USD' AND [RequiredDate] < ADD_DAYS(CURRENT_DAY, 30)- This demo contains 2 Alerts that fire when an Aggregated Boolean Expression is triggered.
- Both are the same Aggregation Boolean Expressions as in the demo above but with a
WHEREclause added:- when the total of
Github Starsis over 10,000 - only for rows whereLicenceis 'MIT License' - when the total of
Open Issuesis over 6,000 - only for rows whereLanguageis 'JavaScript'
- when the total of
- In the first row change the value of:
Github Starscolumn to 279429 and see theSuccessAlert be fired (becauseLicencein first row is "MIT License")Open Issuescolumn to 1220 and see theWarningAlert be fired (becauseLanguagein first row is "JavaScript")
FAQ
Can Aggregated Scalar Functions reference Calculated Columns Yes, this was added in AdapTable Version 12.1
Does Aggregation take Pagination into account? No. All the data in the AG Grid data source is used when aggregating and not solely what is on the current page.