Functions Utility

GroupBy

The GroupBy() function groups a range of data into sets and calculates values for each set using aggregation functions such as SUM, AVERAGE, and COUNT.

Related functions: Pivot


Syntax:

Qx.GroupBy(Range, Group Columns, Formulas, Calculate Error Value)
  • In Excel with Quintessence addin versions prior to 25.1, use: GroupBy()
  • In the Quintessence Editor (Studio) using Quintessence language, use: GroupBy()
  • In the Quintessence Editor (Studio) using Python language, use: Qx.GroupBy()
 See parameter descriptions

RangeThe range to manipulate.
Group ColumnsThe columns that the GroupBy() function will group by, in the specified order.
Formulas
(optional)
The aggregation functions used to calculate the grouped output, for example:
SUM(V4) — where 4 refers to the column number (i.e., sum the values in column 4 for each grouped set).
Calculate Error Value
(optional)
The value that will be returned if the aggregation fails.

Remarks:

  • Unless changed by an administrator, the default parameters are:
    • Formulas: No formula will be passed. In this case, GroupBy() behaves like a Unique() function applied to the Group Columns parameter.
    • Calculate Error Value: Aggregation failures will produce empty results.
  • An aggregate-type function is a utility function that performs aggregations on grouped data using Excel aggregate functions such as AVERAGE, COUNT, and SUM. GroupBy() is considered an aggregate-type function.

Examples:

The examples provided below are based on fictitious data for illustrative purposes only.

> Example 1: Use GroupBy() to calculate sum, average, and largest for each group


The following source data is in cell range A1:D13:

FundAssetYear% Return
AGRI PROVIDENT FUNDMBD201310
AGRI PROVIDENT FUNDKVJ201320
AGRI PROVIDENT FUNDGMP201317
NNN LIFE LIMITEDYKG201345
NNN LIFE LIMITEDPHO201312
NNN LIFE LIMITEDBSF201314
AGRI PROVIDENT FUNDMBD201412
AGRI PROVIDENT FUNDKVJ201415
AGRI PROVIDENT FUNDGMP201425
NNN LIFE LIMITEDYKG201432
NNN LIFE LIMITEDPHO201413
NNN LIFE LIMITEDBSF201411

We want to find the sum, average, and largest value for each fund in each year. This can be achieved using the following formula:

=Qx.GroupBy(A2:D13,"C1,C3","SUM(V4),AVERAGE(V4),LARGE(V4,1)")

Result:

AGRI PROVIDENT FUND20134715.6666720
AGRI PROVIDENT FUND20145217.3333325
NNN LIFE LIMITED20137123.6666745
NNN LIFE LIMITED20145618.6666732
> Example 2: Group and Sum at multiple levels – by Sector and by Asset within a Sector


The following source data is in cell range A1:D11:

SectorFund NoAssetValue
Basic Materials5000KVJ5250
Consumer Goods5010XDU9822
Basic Materials5020CEB1167
Consumer Goods5030XDU10311
Consumer Goods5040XDU410
Industrials5050ZQE1350
Consumer Goods5060NHP59
Consumer Goods5070QGW180
Consumer Goods5080QJU40
Industrials5090UAG15

We need to calculate:

  • The total value of each sector and asset (grouping level 2 – G2)
  • The total value of each sector (grouping level 1 – G1)

This can be achieved using the following formula:

=Qx.GroupBy(A2:D11,"C1,C3","SUM(G2V4),SUM(G1V4)")

Result:

Basic MaterialsCEB11676417
Basic MaterialsKVJ52506417
Consumer GoodsNHP5920822
Consumer GoodsQGW18020822
Consumer GoodsQJU4020822
Consumer GoodsXDU2054320822
IndustrialsUAG161366
IndustrialsZQE13501366