AGGR Function in QV

7
How to Use AGGR () function in Qlikview? The main purpose behind creating any dashboard is to summarize and aggregate information in a manner that can be communicated visually and clearly. Traditionally, this summarizing and agreegating used to happen outside the purview of dashboarding tool. This is how a traditional BI architecture looks like: Input sources are typically transactional in nature and all the summarizing typically used to happen in Datamarts. However, Qlikview works on a different architecture, it connects directly to the input systems and can add layers internally so that you do not need datamarts or data warehouse. Here is a representation of how it might look:

Transcript of AGGR Function in QV

8/10/2019 AGGR Function in QV

http://slidepdf.com/reader/full/aggr-function-in-qv 1/7

How to Use AGGR () function in Qlikview?The main purpose behind creating any dashboard is to summarize and aggregate information in a manner that canbe communicated visually and clearly. Traditionally, this summarizing and agreegating used to happen outside thepurview of dashboarding tool.

This is how a traditional BI architecture looks like:

Input sources are typically transactional in nature and all the summarizing typically used to happen in Datamarts.However, Qlikview works on a different architecture, it connects directly to the input systems and can add layersinternally so that you do not need datamarts or data warehouse. Here is a representation of how it might look:

8/10/2019 AGGR Function in QV

http://slidepdf.com/reader/full/aggr-function-in-qv 2/7

Since, Qlikview provides an option to perform aggregation with in, it needs strong aggregation function. The idea ofthis post is to introduce you to usage of AGGR() function in Qlikview.

Aggregate function(s) is developed in a way such that we can use it for multiple applications like conditional valuereturn, multiple value return or many more. These advance aggregate function can improve our dashboards andreports multi-fold.

What is AGGR? What is AGGR?

AGGR is a very powerful aggregation function in QLikview that is sometimes overlooked due to lack of understandingor familiarity. AGGR statements creates a temporary table of results, with one expression which can be grouped byone or more dimensions. It is similar to “Group by” in SQL.

Results of virtual table can be used by outer aggregate function(s). It can be used in expression and dimension both(calculated dimension).

Syntax : aggr ([ distinct | nodistinct ] [{set_expression}]expression {, dimension})

When should we use AGGR? Whenever we do aggregation in two steps like Sum of sales of top selling product by company

or Highest average sale by Product and Company, in both statements we are doing two aggregation.

We want to identify top/ bottom population in segment

8/10/2019 AGGR Function in QV

http://slidepdf.com/reader/full/aggr-function-in-qv 3/7

It becomes more powerful, when it is used with Set analysis, Rank(), FirstSortedValue().

Example Dataset:

Please note that I have chosen a transactional data to bring out the aspects of aggregation in better manner.

AGGR Example AGG Examples:

Now, we will explore how to use simple AGGR function in CHART, Pivot Table or Straight tables. Below areexamples where we can use AGGR function:

Sales of top selling product

Highest average sale of Product

8/10/2019 AGGR Function in QV

http://slidepdf.com/reader/full/aggr-function-in-qv 4/7

Total sales of the company can be calculated by using function SUM() simply (as SUM(Sale)). However, we need tocalculate the sale of top selling product by a company.To solve this we will use AGGR function by doing twoaggregations in one step.

To achieve sale for the top selling product for each company, we need to have those dimensions(Company_Nameand Product) in our AGGR statement….

=Max ( aggr ( sum ( Sale ), Company_Name , Product ) )

As mentioned before, when do we use AGGR function, it creates a temporary table. In this case, QlikView hascalculated the sale for each Product by Company. From this virtual table we have to identify maximum sales valueand it is done by MAX() function.

Next scenario is to calculate highest average sale of product by company. To solve this, we need to have expressionfor average sale as “SUM(Sale)/Count(Product)” and required dimensions within our AGGR statement:

8/10/2019 AGGR Function in QV

http://slidepdf.com/reader/full/aggr-function-in-qv 5/7

=Max( aggr ( sum(Sale)/Count(Product) , Company_Name, Product ) )

In this case also, Qlikview will generate temporary table for average sale by Product and Company and post thatMAX function will identify the largest value.

AGGR With Set Analysis :

AGGR with SET AnalysisIn above examples, expression were working with current selection sets where as if we want to compare previousyear sale of top selling product with current year then we have to use set analysis methodology in our expression.

In case you are new t o Set Analysis, please refer to my previous post “Set Analysis in QlikView – simplified! .”

To return the sales of top selling product for previous year, we need to write a set expression:

=Max({<Year={$(=Max(Year)-1)}>} aggr (sum( {<Year={$(=Max(Year)-1)}>} Sale),Company_Name,Product))

To understand more clearly, it will look like:

=Max({<Year={2012}>} aggr (sum( {<Year={2012}>} Sale),Company_Name,Product))

In above expression, i have used set expression twice, one for MAX function and another one for AGGR.AGGR With

AGGR with Firstsortedvalue

Let us take a scenario, where we have to identify Product Name that sold the most during selected period bycompany. To do so, we have to use AGGR function in conjunction with function FIRSTSORTEDVALUE().

FIRSTSORTEDVALUE function return the first value of expression sorted by corresponding sort-weight. Sort weightshould be numeric value and it returns NULL value if more than one expression value share the same sort order so ifthere is tie in sorting it returns NULL value unless you do some thing else to overcome this issue.

To extract the last sorted value which is opposite to first sorted value, we need to add a minus sign (-) to sort weightexpression. We can also use set analysis and Total in FIRSTSORTEDVALUE function.

8/10/2019 AGGR Function in QV

http://slidepdf.com/reader/full/aggr-function-in-qv 6/7

Syntax:- FIRSTSORTVALUE( Field, Sort-Weight)

As mentioned above, to identify name of Product based on highest sales product by company, we have to writeexpression like :

=FIRSTSORTEDVALUE (Product, – aggr(sum(Sale),Company_Name,Product) )

In above expression, AGGR statement generates a virtual table that has total sales by company and product. Then,FIRSTSORTEDVALUE function identifies the Product Name from the virtual table for highest sale. Because aggrfunction is preceded by minus sign (-), it will return product name against highest sales.

Identify Top/Bottom or Specific Segment

One of the best use case scenario where I use AGGR function is to identify the top/ Bottom segment. Here, we will

identify top 2 product across year 2012 & 2013 within a text object as seen below:

To achieve this we will use Concat() and Rank() functions in conjunction with AGGR statement. Completedexpression is as follows:

=concat ( IF( aggr ( Rank(sum(Sale)),Product)<=2, Product & chr(10)))

Expression “ aggr(Rank(Sum(Sale)),Product)”, creates a virtual table with Product and Rank against their sale.Thenext step is to add in our IF statement to request that we only receive the top 2 product in the result. So, if the rank inour virtual AGGR table is <=2 then show the product else return null().

Finally, we want to show the results of this in a text object. We have used CONCAT() function to concatenate the 2Product values together. To add a separate line between both product, we have used chr(10).

8/10/2019 AGGR Function in QV

http://slidepdf.com/reader/full/aggr-function-in-qv 7/7

This post was an example where we have brought out methods to use AGGR in Qlikview. Do you have more tricks tomake AGGR more interesting? If not, do you think this article will enable you to use AGGR function in your nextdashboard?

Do let me know your thoughts on using this feature in QlikView.

http://www.analyticsvidhya.com/blog/2014/02/aggr/