Skip to main content
hic
Former Employee
Former Employee

The Aggr() functions is one of the most advanced functions in the QIX engine, and it is not always easy to use. It does not get easier when you put set analysis expressions in it.

In one of my previous posts (Pitfalls of the Aggr function) I recommended having the set analysis expression in both the inner and the outer aggregation function when using set analysis in the Aggr() function. This is a good rule of thumb, because in most cases doing so will generate the result that you want.

But this is not always correct.

In more complex calculations you often need to use the condition in one place only – sometimes in the inner aggregation, sometimes in the outer. It depends on how the condition is formulated. Then it is important to understand the difference between the two positions.

The evaluation of the Aggr() function is a two-step process: In the first step, an intermediary virtual table is created using the inner aggregation and the dimension of the Aggr(). In the second step, this virtual table is aggregated using the outer aggregation.

For example, say that you want to find the largest order value per year. Then you would need to first calculate the sales value per order, and in a second step find the largest of these values. Hence

Formula1.png

The first step aggregates the source data (with multiple records per Order ID) into a virtual table with one record per Order ID, and the second step finds the largest values in the virtual table.

Two-steps.png

However, there is not yet any set analysis in the expression. So, let us use the following requirement instead:

  1. Show the largest order value per year
  2. Include only products from the product group “Shoes” in the order value
  3. Calculate these numbers only for 2014 and 2015

The two conditions correspond to the following set analysis expression:

Formula2.png

But where should this expression be written? In the outer or in the inner aggregation?

To answer this question, we must ask ourselves in which step the conditions should be used. Then it becomes obvious that the condition in product group must be used in step one – in the inner aggregation. If it is used in the outer aggregation only, the order values will be incorrect – they will be calculated from all products.

The condition in year, however, can be put in either place. Hence, the following expression will work fine:

Formula6.png

From the above example one might draw the conclusion that you always should put the condition in the inner aggregation. But this is not the case. Sometimes you have a condition that cannot be put in the inner aggregation. The following requirement can serve as example:

  1. Per year, show the bestselling product within the product group “Shoes”
  2. Show how this product ranks compared to all products, also non-shoes

The solution is the following table

Ranks.png

The condition in product group should be evaluated in step two, so the expressions used for Product and Rank are:

Formula5.png

Here it is not possible to have the condition on product group in the inner aggregation, since this would interfere with the calculation of the rank. You must have it in the outer aggregation.

Bottom lines are:

  • You need to figure out if your condition should be evaluated in step one (in the inner aggregation) or in step two (in the outer aggregation). This will determine where to put the set analysis expression. You may need to use The Feynman Problem Solving Algorithm.
  • If you can’t figure out where to put your set analysis expression – try putting it in both the outer and the inner aggregation function, and keep your fingers crossed. Afterwards you should however verify that the numbers are what you want.

HIC

Further reading related to this topic:

Pitfalls of the Aggr function

A Primer on Set Analysis

21 Comments
upaliwije
Creator II
Creator II

Great Post as always

0 Likes
32,466 Views
luciancotea
Specialist
Specialist

Hi hic,

Can you please include in the article also the Set Analysis of the aggr() function itself? It would be great.

Thanks,

Lucian

0 Likes
32,466 Views
Gysbert_Wassenaar

And where does the set expression for the aggr function itself fit in this?

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

0 Likes
32,466 Views
hic
Former Employee
Former Employee

Gysbert, Lucian

The Set Analysis of the Aggr() itself defines a scope for the aggregations inside the Aggr(). Or differently put: The Set Analysis expression of the Aggr() is injected into the relevant aggregation functions before they are calculated. This means that the two expressions below should return the same result.

   Aggr( {<SetExpression>} Sum(X)/Sum(Y), Dim)  

   Aggr( Sum({<SetExpression>} X)/Sum({<SetExpression>} Y), Dim)

Eventually there will come a blog post on this.

HIC

32,466 Views
swuehl
MVP
MVP

Henric, this (set analysis of the aggr() ) has been a recent change, right?

As far as I remember, it's been in the HELP for ages, but I don't remember it working until QV12.

[Just in case people wondering about the above syntax Gysbert showed:

The sortable Aggr function is finally here!

]

32,466 Views
Gysbert_Wassenaar

Does that mean that these two are equivalent too?

  • Aggr( {<SetExpressionA>} Sum({<SetExpressionB>} X)/Sum(Y), Dim) 
  • Aggr( Sum({<SetExpressionA>*<SetExpressionB>} X)/Sum({<SetExpressionA>} Y), Dim) 
32,466 Views
hic
Former Employee
Former Employee

Stefan

Yes, it is a recent change. Qlik Sense and and QlikView 12 only. I am frankly surprised that the older help files have a set analysis option in the Aggr() syntax...

HIC

0 Likes
26,560 Views
hic
Former Employee
Former Employee

Gysbert

Yes, the two expressions are equivalent.

In other places, there is a difference between {$<...>} and {<...>} where the first is a modification of the default selection, and the latter is a modification of the selection in that specific scope. Which makes a difference when you use alternate states. The idea is that you should have the same distinction inside an Aggr().

If you test it you will however find that it doesn't always work - the inner aggregation is always treated as a {<...>} even if you've written {$<...>}. So, we'll see if we can fix this.

HIC

26,560 Views
Gysbert_Wassenaar

Good, that probably means I understand it. I'm not sure that this 'shortcut' is worth the extra complexity though. Set analysis is hard enough to learn as it is. Bigger can, more worms

0 Likes
26,560 Views
luciancotea
Specialist
Specialist

Well, in fact it's older than that. I've been using it in v11.2 for a long time.

Edit: Just tested on 11.2 SR8 and it works.

0 Likes
26,560 Views