Aggr() function is a very powerful function in QlikView.
It comes under advanced Aggregation and provides great flexiblity to the developer and user.
What is Aggr( )
- Let you perform Aggregation over a set of dimensions
- It creates kind of a temp table of results from which you can select and perform more aggregations
- Can be used with expressions and dimensions
- Result of Aggr can be used in other aggregated functions
- Similar to Group By clause in SQL
- Similar to chart expression based on different chart dimensions
1. let us find out the Cost per product per customer in a given month.
We will create this using normal aggregation in a chart with Dimensions as Month,CustomerName and Product with expression as Sum(Cost). We will also create this using Aggr(). Both will give same output
Aggr() expression will look like
Normal aggregation Sum(Cost) and Advanced Aggr() produce same results.
This is also similar to SQL statement
Select Month, CustomerName, ProductName, Sum(Cost) from myTable
Group By Month,CustomerName,ProductName;
2. To understand more of Aggr(), let us find out Cost of a product for each customer and month and also the maximum Cost for any Customer for the same product and month. In this query, we are discarding Customer dimension to get Maximum cost by Month and Product.
To achieve the above results, we need to first calculate Cost per month,per customer and product and then from this result get the Maximum cost per month and product.
First we will calculate Aggr(Sum(Cost),Month,CustomerName,ProductName)
which is similar to the above output
then we will get the Maximum of this result i.e.
This calculation will not change the result as Max is also calculating within the dimension of the chart i.e. for Month,Customer and Product but for our requirement we need to calculate Maximum Cost per month and Product
For this we will use Total qualifier with Max.Total qualifier will disregard any or all chart dimension. Since we have to just disregard Customer dimension, we will explicitly state which dimensions we want to include with the Total aggregation. To include Month and ProductName and disregard Customer, our expression will be
Total<Month,ProductName> – will make sure that in the calculation of Max, Month and ProductName dimensions are included.
Our Output will look like
From the above examples, we can see that
– Aggr() function is similar to Group by function in Sql.
– It is also similar to applying aggregation in a chart with set of dimensions.
– The power of Aggr() lies in the fact that it provides a temp table from which you can select the results and apply further aggregation to arrive at meaningful results.
– In the absence of Aggr you may need to write complex scripts or sql queries to achieve similar results.