Skip to main content
Announcements
See what Drew Clarke has to say about the Qlik Talend Cloud launch! READ THE BLOG
cancel
Showing results for 
Search instead for 
Did you mean: 
KennyTan
Partner - Contributor II
Partner - Contributor II

pivot table calculation - avg vertically and agg horizontally

Hi Qlik community,

Would like to get help from the community on one of my use case. There is a requirement for a pivot table, whereby to calculate measure (sales) with apply average calculation vertically, while aggregate calculation horizontally.

I'm not sure whether this use case is possible? If possible can please shed on light on this ?

To illustrate, I attached a sample qvf.  For e.g. custA:

I would need the calculated figure for year (highlighted) to be the Average of the sales, i.e. 

year 2020: (78 + 95 + 46 + 75) /4 = 73.5 (currently is 294, not what I need)

year 2021: (15 + 95)/2 = 55

custA.png

 

 

 

Same behaviors should apply if the pivot table is collapse.

Below is the calculation formula I had tried:

SUM(AGGR( nodistinct
Sum(sales),year, month, customer, product));

AVG(AGGR( nodistinct
Sum(sales),year, month, customer, product))

But the problem here is whichever I used, it will end up applied either SUM or AVG for the whole table. What I need is to calculate Average vertically, while SUM aggregate horizontally.

Appreciate any input. Thanks.

Regards,

Kenny.

1 Solution

Accepted Solutions
MayilVahanan

Hi

Its looks correct only. 

what is the expected o/p for ur latest file?

Year total: =Sum(Sales)/ no of distinct month , so total is 99 which is correct.

Thanks & Regards, Mayil Vahanan R
Please close the thread by marking correct answer & give likes if you like the post.

View solution in original post

5 Replies
MayilVahanan

Hi 

Try like below

If(Dimensionality()=1,
AVG(sales),

SUM(sales))

MayilVahanan_0-1630297323381.png

 

Thanks & Regards, Mayil Vahanan R
Please close the thread by marking correct answer & give likes if you like the post.
KennyTan
Partner - Contributor II
Partner - Contributor II
Author

Hi @MayilVahanan ,

Thanks for the input, this work well for calculation when break down by product, but calculated figure seem not correct when it aggregate, for e.g. for custA below: 

custA.png

 

 

 

I expect to have the year calculation (highlighted) = (111 + 116 + 46 + 107)/4 = 95, but it calculate as 54.286 (not sure how this figure come from).

Is it possible  to have similar behavior even when the pivot table is group together?

Thanks and Regards,

Kenny.

 

MayilVahanan

Hi

Try like below

If(Dimensionality()=1,
Sum(sales)/Count(Distinct month),

SUM(sales))

It based on ur requirement. 

Thanks & Regards, Mayil Vahanan R
Please close the thread by marking correct answer & give likes if you like the post.
KennyTan
Partner - Contributor II
Partner - Contributor II
Author

Hi @MayilVahanan ,

Thanks, i didn't aware the level of dimension have impact on the calculation with using Dimensionality(), so didn't provide an accurate scenario to reflect my use case, but it was close.

What if i have 3 level of dimension? year, month and week. I tried below code using example provided by you, but the total average calculated year's figure seem a bit off (see highlighted):

custA1.png

 custA2.png

 

 

 

 

 

 

 

 

 

 

formula i had tried:

If( Dimensionality()=2, Sum(sales)/Count(Distinct week),
	If(Dimensionality()=1,Sum(sales)/Count(Distinct month),SUM(sales)))

Can you please help identify which section i getting it wrong? I would like to have the same behaviour, but with additional new "week" dimension as row.

I re-attached new set of qvf data just in case.

Thanks for your kind reply and time.

Kenny Tan.

MayilVahanan

Hi

Its looks correct only. 

what is the expected o/p for ur latest file?

Year total: =Sum(Sales)/ no of distinct month , so total is 99 which is correct.

Thanks & Regards, Mayil Vahanan R
Please close the thread by marking correct answer & give likes if you like the post.