Skip to main content Skip to complementary content

Aggr

Aggr() returns an array of values for the expression calculated over the stated dimension or dimensions. For example, the maximum value of sales, per customer, per region.

The Aggr function is used for nested aggregations, in which its first parameter (the inner aggregation) is calculated once per dimensional value. The dimensions are specified in the second parameter (and subsequent parameters).

In addition, the Aggr function should be enclosed in an outer aggregation function, using the array of results from the Aggr function as input to the aggregation in which it is nested

Syntax:  

Aggr({SetExpression}[DISTINCT] [NODISTINCT ] expr, StructuredParameter{, StructuredParameter})

Return data type: dual

Arguments:  

  • expr: An expression consisting of an aggregation function. By default, the aggregation function will aggregate over the set of possible records defined by the selection.
  • StructuredParameter: StructuredParameter consists of a dimension and optionally, sorting criteria in the format: (Dimension(Sort-type, Ordering)). The dimension is a single field and cannot be an expression. The dimension is used to determine the array of values the Aggr expression is calculated for.

    If sorting criteria are included, the array of values created by the Aggr function, calculated for the dimension, is sorted. This is important when the sort order affects the result of the expression the Aggr function is enclosed in. For details of how to use sorting criteria, see Adding sorting criteria to the dimension in the structured parameter.

  • SetExpression: By default, the aggregation function will aggregate over the set of possible records defined by the selection. An alternative set of records can be defined by a set analysis expression.
  • DISTINCT: If the expression argument is preceded by the distinct qualifier or if no qualifier is used at all, each distinct combination of dimension values will generate only one return value. This is the normal way aggregations are made – each distinct combination of dimension values will render one line in the chart.
  • NODISTINCT: If the expression argument is preceded by the nodistinct qualifier, each combination of dimension values may generate more than one return value, depending on underlying data structure. If there is only one dimension, the aggr function will return an array with the same number of elements as there are rows in the source data.
 

Basic aggregation functions, such as Sum, Min, and Avg, return a single numerical value whereas the Aggr() function can be compared to creating a temporary staged result set (a virtual table) over which another aggregation can be made. For example, by computing an average sales value by summing the sales by customer in an Aggr() statement and then calculating the average of the summed results: Avg(TOTAL Aggr(Sum(Sales), Customer)).

Tip noteUse the Aggr() function in calculated dimensions if you want to create nested chart aggregation in multiple levels.

Limitations:  

Each dimension in an Aggr() function must be a single field, and cannot be an expression (calculated dimension).

Adding sorting criteria to the dimension in the structured parameter

In its basic form, the argument StructuredParameter in the Aggr function syntax is a single dimension. The expression: Aggr(Sum(Sales, Month)) finds the total value of sales for each month. However, when enclosed in another aggregation function, there can be unexpected results unless sorting criteria are used. This is because some dimensions can be sorted numerically or alphabetically, and so on.

In the StructuredParameter argument in the Aggr function, you can specify sorting criteria on the dimension in your expression. This way, you impose a sort order on the virtual table that is produced by the Aggr function.

The argument StructuredParameter has the following syntax:

(FieldName, (Sort-type, Ordering))

Structured parameters can be nested:

(FieldName, (FieldName2, (Sort-type, Ordering)))

Sort-type can be: NUMERIC, TEXT, FREQUENCY, or LOAD_ORDER.

The ordering types associated with each Sort-type are as follows:

Sort ordering types
Sort typeAvailable ordering types
NUMERICASCENDING, DESCENDING, or REVERSE
TEXTASCENDING, A2Z, DESCENDING, REVERSE, or Z2A
FREQUENCYDESCENDING, REVERSE, or ASCENDING
LOAD_ORDERASCENDING, ORIGINAL, DESCENDING, or REVERSE

The ordering types REVERSE and DESCENDING are equivalent.

For Sort-type TEXT, the ordering types ASCENDING and A2Z are equivalent, and DESCENDING, REVERSE, and Z2A are equivalent.

For Sort-type LOAD_ORDER, the ordering types ASCENDING and ORIGINAL are equivalent.

Examples

Example 1:  

Avg(Aggr(Sum(UnitSales*UnitPrice), Customer))

The expression Aggr(Sum(UnitSales*UnitPrice), Customer) finds the total value of sales by Customer, and returns an array of values: 295, 715, and 120 for the three Customer values.

Effectively, we have built a temporary list of values without having to create an explicit table or column containing those values. These values are used as input to the Avg() function to find the average value of sales, 376.6667. (You must have Totals selected under Presentation in the properties panel.

Example 2:  

Aggr(NODISTINCT Max(UnitPrice), Customer)

An array of values: 16, 16, 16, 25, 25, 25, 19, and 19. The nodistinct qualifier means that the array contains one element for each row in the source data: each is the maximum UnitPrice for each Customer and Product.

Example 3:  

max(aggr(sum(Customers)-above(Sum(Customers)), (MonthYear,(NUMERIC, ASCENDING))))

Using sorting criteria in the StructuredParameter argument in the expression: max(aggr(sum(Customers)-above(Sum(Customers)), (MonthYear,(NUMERIC, ASCENDING))))

Without sorting criteria the result of the expression max(aggr(sum(Customers)-above(Sum(Customers)), (MonthYear))) depends on how the dimension MonthYear is sorted. We might not get the result we want. By adding values for sort type and ordering type to the dimension, we give sorting criteria to the structured parameter: (MonthYear, (NUMERIC, ASCENDING)), where the sort type NUMERIC and ordering ASCENDING determine that MonthYear is sorted in ascending numerical order.

Here, we are looking to find the greatest increase in number of customers, month-on-month. This could be used in a KPI visualization, for example.

The Aggr part of the expression compares the total number of customers in one month (given by MonthYear) to the total number in the previous month. Because we use sorting criteria with the dimension: (MonthYear,(NUMERIC, ASCENDING)), we are sure that Aggr compares the numbers of customers in consecutive months in the virtual table by sorting the months in ascending numerical order, and not in ascending alphabetical order.

Data used in examples:

Create a table with Customer, Product, UnitPrice, and UnitSales as dimensions. Add the expression to the table, as a measure.

ProductData:

LOAD * inline [

Customer|Product|UnitSales|UnitPrice

Astrida|AA|4|16

Astrida|AA|10|15

Astrida|BB|9|9

Betacab|BB|5|10

Betacab|CC|2|20

Betacab|DD|25|25

Canutility|AA|8|15

Canutility|CC||19

] (delimiter is '|');

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – let us know how we can improve!

Join the Analytics Modernization Program

Remove banner from view

Modernize without compromising your valuable QlikView apps with the Analytics Modernization Program. Click here for more information or reach out: ampquestions@qlik.com