# QlikView App Dev

Discussion Board for collaboration related to QlikView App Development.

Announcements
Action-Packed Learning Awaits! QlikWorld 2023. April 17 - 20 in Las Vegas: REGISTER NOW
cancel
Showing results for
Did you mean:
Not applicable

## Aggr function on avg

Hi,

I need average of  sales at 'geography' and 'level' and want to compare ID sales with that average. I have tried aggr function but it's giving null for some values. Please help.

Regards,

Navdeep

7 Replies
MVP

Hi Navdeep,

You either need to remove the ID as a dimension from the table, or add ID as a parameter to the AGGR function.

How you have it at the moment where Level and Geography have two IDs; the correct average is being put on one of the rows and the null value is being shown on the other.

Hope that helps.

Steve

avg(total <Geography,Level>Sales)

MVP

Thinking about it, whilst you have Level and Geography as dimensions, the aggr function is doing absolutely nothing, you can just do avg(Sales) to get the average.

Aggr functions are only useful if you intend to use the output as a dimension, or if you need to put another expression around it, for example:

sum(aggr(avg(Sales),Geography,Level))

This will give you the total of the averages by those dimensions.

Steve

Not applicable
Author

Hi Kush,

Seems perfect I have some set conditions also, how to include those in this expression and if possible please briefly explain the avg(total <Geography,Level>Sales) expression.

Thanks,

Navdeep

Not applicable
Author

Ok, yes I understand your point, m always little confused about the functionality of aggr(). 🙂

avg(total <Geography,Level>Sales) - It will return the avg of  Sales within selection of each Geography and level.

If you have set expression then please use the solution suggested by Steve.Because due to total qualifier Set expression will not give you accurate results

MVP

The way to think of AGGR is allowing you to create a temporary table and then do further calculations over it.  So, say you wanted the sum of values for field A and B in a table, but as the total you wanted to show the Average of all those rows; you would simply create a table with A and B as dimensions, have your expression as sum(Value) and then change the Total Mode to average.  All very simple and no AGGR statements involved.

Now, if you wanted to show the average value in a text box or in the caption of a chart, you would not have a table with all the rows in - so you would need to create a virtual table to work over.  The expression avg(aggr(sum(Value), A, B)) will do this for you.  Essentially, the table you have in the first example is made in memory, for the average function to work over.

That is the basic premise of AGGR, but it can be used in other scenarios also.  One that I use quite frequently is when calculating exceptions and having them emailed out by a QlikView alert.  So, perhaps where a Customer has gone over their credit limit.  I use an AGGR on the CustomerID with an IF statement in, returning Customer Name for those over the limit and NULL for those under.  Around the AGGR I put a Concat DISTINCT statement, that then turns this into a list of items.

Hope that clears it up for you?

Steve

Community Browser