Skip to main content
Woohoo! Qlik Community has won “Best in Class Community” in the 2024 Khoros Kudos awards!
Announcements
Nov. 20th, Qlik Insider - Lakehouses: Driving the Future of Data & AI - PICK A SESSION
cancel
Showing results for 
Search instead for 
Did you mean: 
marcel_olmo
Partner Ambassador
Partner Ambassador

Loading relative AVG

Hi guys, I'm trying to the following in Qlikview :

  

IDVALUERELATIVE AVG
110
220
330
440
552,50
663,50
774,50
885,50
996,50
10107,50

As you see, I want to calculate dynamically the average of the previous 4 numbers of the field VALUE.

For example in ID =5, the previous four numbers does a sum of 10, divided into 4 rows --> 2,50

I'm trying to do it with a resident table using rangeAvg() but i bet I'm doing something wrong. Someone can help me?

I attack an excel with the example.

Thanks in advance.

Regards, Marcel.

1 Solution

Accepted Solutions
afurtado
Partner Ambassador/MVP
Partner Ambassador/MVP

vQtRelative = 2

ID RELATIVE AVG VALUE REALATIVE AVG NEW
10,0010
20,0020
30,0031,5
40,0042,5
52,5053,5
63,5064,5
74,5075,5
85,5086,5
96,5097,5
107,50108,5
furtado@farolbi.com.br

View solution in original post

6 Replies
swuehl
MVP
MVP

Maybe something like

LOAD ID,

        VALUE,

       RangeAvg( Peek('VALUE', -1), Peek('VALUE', -2),Peek('VALUE', -3),Peek('VALUE', -4)) as [REALATIVE AVG]

FROM ...;

marcel_olmo
Partner Ambassador
Partner Ambassador
Author

Thanks swuehl for your quick response,

the point is that I want to do it dinamically. If i want to do it for the relative 20, I have to do it manually?

Regards, Marcel.

afurtado
Partner Ambassador/MVP
Partner Ambassador/MVP

Hi Marcel.

Using swuehl‌ expression ,  maybe this can help....

You can change vQtRelative for 4 or 20 or .....

vQtRelative = 4;

Let vPeek       = 'if(recno()>'&$(vQtRelative)&',RangeAvg(';

Let vFieldName  = 'VALUE';

For A = 1 to '$(vQtRelative)'

    vPeek   = '$(vPeek)' & 'peek('&chr(39)&'$(vFieldName)'&chr(39)&','&($(A)*-1)&')'&if('$(A)'='$(vQtRelative)','),0)',',');

Next

LOAD

     ID,

     VALUE,

     $(vPeek)          as [REALATIVE AVG NEW],

     [RELATIVE AVG]

FROM (ooxml, embedded labels, table is Hoja1);

furtado@farolbi.com.br
afurtado
Partner Ambassador/MVP
Partner Ambassador/MVP

ID RELATIVE AVG VALUE REALATIVE AVG NEW
10,0010
20,0020
30,0030
40,0040
52,5052,5
63,5063,5
74,5074,5
85,5085,5
96,5096,5
107,50107,5
furtado@farolbi.com.br
afurtado
Partner Ambassador/MVP
Partner Ambassador/MVP

vQtRelative = 2

ID RELATIVE AVG VALUE REALATIVE AVG NEW
10,0010
20,0020
30,0031,5
40,0042,5
52,5053,5
63,5064,5
74,5075,5
85,5086,5
96,5097,5
107,50108,5
furtado@farolbi.com.br
marcel_olmo
Partner Ambassador
Partner Ambassador
Author

Thanks Alessandro, it looks great! Regards, Marcel.