The Aggr function has, as an optional clause, the possibility of stating that the aggregation will be either distinct or nodistinct.
The default option is distinct and, as such, is rarely ever stated. In this default operation, the aggregation will only product distinct results for every combination of dimensions - just as you would expect from a normal chart or straight table.
The nodistinct option only makes sense within a chart, one that has more dimensions than are in the Aggr statement. In that case, the granularity of the chart is lower than the granularity of the Aggr and therefore QlikView will only calculate that Aggr for the first occurrence of lower granularity dimensions and will return null for the other rows. If we specify nodistinct then the same result will be calculated across all of the lower granularity dimensions.
That can be difficult to understand without seeing an example, so let us look at a common use case for this option. We will start with a data set:
Load * Inline [
Product, Territory, Year, Sales
Product A, Territory A, 2013, 100
Product B, Territory A, 2013, 110
Product A, Territory B, 2013, 120
Product B, Territory B, 2013, 130
Product A, Territory A, 2014, 140
Product B, Territory A, 2014, 150
Product A, Territory B, 2014, 160
Product B, Territory B, 2014, 170
We will build a report from this data using a pivot table:
Now, we want to bring the value in the Total column into a new column under each year, perhaps to calculate a percentage for each year. We might think that, because the total is the sum for each Product and Territory then we might use an Aggr like this:
Sum(Aggr(Sum(Sales), Product, Territory))
However, as stated above, because the chart includes an additional dimension (Year) than the Aggr, then the expression will only be calculated for the first occurrence of each of the lower granularity dimensions (in this case, for Year = 2013):
The commonly suggested fix for this is to use the Aggr without the Sum and with a nodistinct like this:
Aggr(NoDistinct Sum(Sales), Product, Territory)
At first, this will appear to solve the problem:
The problem occurs when we decide to have a total row on this chart:
Because there is no aggregation function surrounding the Aggr, it does not total correctly at the Product or Territory dimensions. And we can't add an aggregation function - like Sum - because it will break one of the other totals.
There is, however, something different that we can do - something that doesn't involve Aggr at all! We can use our old friend Total:
Sum(Total<Product, Territory> Sales)
This will calculate correctly at all the levels:
Stephen Redmond is author of Mastering QlikView, the QlikView for Developer's Cookbook and QlikView Server and Publisher
He is CTO of CapricornVentis a QlikView Elite Partner. We are always looking for the right people to join our team.
Follow me on Twitter: @stephencredmond