Group and aggregate
Aggregation is two steps: groupBy collects the rows that share a key into one row per key, and list reducers turn each column’s list of member values back into one value.
groupBy(prop("desk"), prop("ccy"))setProp("desk", prop("desk").first())setProp("ccy", prop("ccy").first())setProp("trades", prop("trade_id").count())setProp("largest", prop("notional").max())setProp("notional", prop("notional").sum())keepProps("desk", "ccy", "trades", "notional", "largest")largest reads the member list before notional is summed: after its setProp, notional holds one number, not a list.
What groupBy produces
Section titled “What groupBy produces”groupBy(key, …) takes one expression per key column and emits one row per distinct combination of key values. Every column becomes the list of its members’ values, the key columns included. Groups appear in the order their key was first seen, and members keep their input order.
groupBy(prop("owner"), prop("ISIN"))turns three rows into group rows shaped like this:
{ __groupKey: ["alice", "GB00B16NNR78"], __groupIndices: [0, 1], owner: ["alice", "alice"], ISIN: ["GB00B16NNR78", "GB00B16NNR78"], price: [3, 6]}- Key matching: rows match when every key value is equal under
==. Empty text and""fall into one group. - Helper fields:
__groupKeyis the list of key values and__groupIndicesthe zero-based positions of the members. Later statements in the same formula can read them; the rows written onward leave them out. To keep the key, copy it first:setProp("groupKey", prop("__groupKey")). - Computed keys: a key can be any expression, such as
groupBy(formatDate(prop("trade_date"), "YYYY-MM"))for one row per month. - Placement:
groupBystands on its own line, never insideifordo.
Reduce each column
Section titled “Reduce each column”| Reducer | Gives |
|---|---|
sum() |
The total |
count() |
The number of members |
min(), max() |
The smallest and largest value |
mean(), median() |
The average and the middle value |
first(), last() |
The first and last member’s value, in input order |
unique() |
The distinct values, as a list |
join(", ") |
The values as one text |
After a reducer, the column holds a single value again, with the member type. A column you do not reduce stays a list and is written as one. Drop the columns you do not need with keepProps.
Conditional and weighted aggregates
Section titled “Conditional and weighted aggregates”Compute a helper column per row before grouping, then reduce it:
# Bought and sold quantity per ISINsetProp("bought", if(prop("side") == "BUY", prop("qty"), 0))setProp("sold", if(prop("side") == "SELL", prop("qty"), 0))
# Quantity-weighted average pricesetProp("qty_x_price", prop("qty") * prop("price"))
groupBy(prop("ISIN"))setProp("ISIN", prop("ISIN").first())setProp("bought", prop("bought").sum())setProp("sold", prop("sold").sum())setProp("avg_price", round(prop("qty_x_price").sum() / prop("qty").sum(), 4))keepProps("ISIN", "bought", "sold", "avg_price")List functions work on the member lists too: prop("tags").flat().unique() gives every tag used in the group, and prop("amount").filter(not(empty(current))).count() counts the members that have an amount.
Count distinct values
Section titled “Count distinct values”setProp("counterparties", prop("counterparty").unique().count())Share of the total
Section titled “Share of the total”To compare each row with a total over all rows without collapsing them, sum into context() first. A statement finishes every row before the next starts, so the third line reads the full total:
context().setProp("total", 0)context().setProp("total", context().prop("total") + prop("notional"))setProp("share", round(prop("notional") / context().prop("total"), 4))A single total row
Section titled “A single total row”Group on a constant to collapse every row into one:
groupBy("all")setProp("trades", prop("trade_id").count())setProp("notional", prop("notional").sum())keepProps("trades", "notional")Aggregate several wires
Section titled “Aggregate several wires”A node with several wires stacks them with appendRows, then groups:
appendRows(inputs().prop("eu_trades"))appendRows(inputs().prop("us_trades"))groupBy(prop("ISIN"))setProp("ISIN", prop("ISIN").first())setProp("qty", prop("qty").sum())See Combine tables.
Styles and lineage through a group
Section titled “Styles and lineage through a group”A group row records every member row as its source, so lineage traces an aggregate back to the rows it summed, and the group keeps the union of its members’ data styles. See Styles and view formulas.
Related
Section titled “Related”- Filter and sort: removing duplicates and keeping the latest row per key
- Lists functions
- Worked examples:
groupBy-then-copy-key,normalise-drop-raise-group-aggregate