Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
zhj0735
Contributor II
Contributor II

Calculate the average of a column with below() across YRMON rows

Hi, I have a problem trying to calculate the average of a column inside the Vizlib pivot table, specifically with the below() function inside. 

First I start with average calculations of actual sales  AVG(vol_act), it works correctly and shows the average on the top of the "Total" row perfectly.  Then, I tried to calculate the average of the lagged "vol_act" without luck:

1. I firstly put Agg(Below(vol_act),YRMON), the lagged monthly output are weird. Instead of showing the previous month, it picked the random month of sales.

2. Though avg(Agg(Below(vol_act),YRMON) is calculated right, it does not calculate the right lagged month. 

Labels (1)
1 Solution

Accepted Solutions
Oleg_Troyansky
Partner Ambassador/MVP
Partner Ambassador/MVP

Hi,

You must be referring to the AGGR() function... When you use Below() within AGGR(), the order of months is determined by the load order of the field YRMON, not by the chart's sort order, which appears to be sorted by month in the descending order. So in your case, you should sort the data within AGGR() by month in the descending order too.

Also, the AGGR() function returns an array of values, which may cause problems. Enclosing it in the Avg() function is a good idea. So, I believe this should work better:

avg(

       Aggr(

                    Below(vol_act),

                    (YRMON, (NUMERIC, DESC))

      )

)

Cheers,

View solution in original post

2 Replies
Oleg_Troyansky
Partner Ambassador/MVP
Partner Ambassador/MVP

Hi,

You must be referring to the AGGR() function... When you use Below() within AGGR(), the order of months is determined by the load order of the field YRMON, not by the chart's sort order, which appears to be sorted by month in the descending order. So in your case, you should sort the data within AGGR() by month in the descending order too.

Also, the AGGR() function returns an array of values, which may cause problems. Enclosing it in the Avg() function is a good idea. So, I believe this should work better:

avg(

       Aggr(

                    Below(vol_act),

                    (YRMON, (NUMERIC, DESC))

      )

)

Cheers,

zhj0735
Contributor II
Contributor II
Author

That works perfectly! Thanks a lot Oleg!