Skip to main content Skip to complementary content

Aggr - chart function

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 advanced aggregations, in which the Aggr function is enclosed in another 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:  

Argument Description
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 aggregations on multiple levels.

Limitations:  

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

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-type Allowed Ordering types
NUMERIC ASCENDING, DESCENDING, or REVERSE
TEXT ASCENDING, A2Z, DESCENDING, REVERSE, or Z2A
FREQUENCY DESCENDING, REVERSE or ASCENDING
LOAD_ORDER ASCENDING, 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 and results:  

Example Result
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).

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.

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 '|');

 

Examples and results: Structured parameters

Example Result
Sum(Aggr( Rangesum(Above(Sum(Sales),0,12)), (Year, (Numeric, Ascending)), (Month, (Numeric, Ascending)) ))

This measure calculates the year-to-date sales for every month using sorting criteria in the structured parameter argument in the expression.

Without sorting criteria, the result of the expression Sum(Aggr( Rangesum(Above(Sum(Sales),0,12)), (Year), (Month) )) depends on how the dimensions Year and Month are sorted. We may 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: (Year, (Numeric, Ascending)), (Month, (Numeric, Ascending)). The sort type NUMERIC and ordering ASCENDING determine that Year and Month are sorted in ascending numerical order.

Data used in examples:

The following load script generates a table of orders with order lines, to be used in the example for structured parameters.

Set vNumberOfOrders = 1000; OrderLines: Load RowNo() as OrderLineID, OrderID, OrderDate, Round((Year(OrderDate)-2005)*1000*Rand()*Rand()*Rand1) as Sales While Rand()<=0.5 or IterNo()=1; Load * Where OrderDate<=Today(); Load Rand() as Rand1, Date(MakeDate(2013)+Floor((365*4+1)*Rand())) as OrderDate, RecNo() as OrderID Autogenerate vNumberOfOrders; Calendar: Load distinct Year(OrderDate) as Year, Month(OrderDate) as Month, OrderDate Resident OrderLines;

You can compare the difference between these measures in a table or in separate line charts:

  • Sum(Aggr( Rangesum(Above(Sum(Sales),0,12)), (Year), (Month) ))
  • Sum(Aggr( Rangesum(Above(Sum(Sales),0,12)), (Year, (Numeric, Ascending)), (Month, (Numeric, Ascending)) ))

The second measure gives the correct year-to-date sales for each month.

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!