Skip to main content
Announcements
Introducing Qlik Answers: A plug-and-play, Generative AI powered RAG solution. READ ALL ABOUT IT!
cancel
Showing results for 
Search instead for 
Did you mean: 
New-Qlik
Creator II
Creator II

How to calculate Average of 4 hours and cumulative

Hi Everyone ,

I have to calculate average4 hour, average8 hour, Forecast - 12 Hours on Actual and cumulative .

I have two columns date and quantity.

Average of 4 hours will be : last four hour average , I need to display it wrt to time.

Please see attached excel for you reference.

Thanks In Advance.

Best Regards

Avneet

1 Solution

Accepted Solutions
Anonymous
Not applicable

Hi

In the editor:

LOAD Date,

    Quantity

FROM

[https://community.qlik.com/thread/224973]

(html, codepage is 1252, embedded labels, table is @3);

use following expressions

exp1:


sum(Quantity)

exp2:

for 4 hours average

if(rowno()<4,'',Rangeavg(above(Sum({1} Quantity),0,4)))

exp3:

for 8 hours avrage:

if(rowno()<8,'',Rangeavg(above(Sum({1} Quantity),0,8)))

:FYI , attached qvw as well......

Thanks

Allu

View solution in original post

11 Replies
Not applicable

 

Build an accumulation table to help your app understand how to accumulate the hours. The table will look like this:

 

Accum2hr:

 

HourOfTheDay

Accum2hour

1

2

2

2

2

3

3

3

3

4

4

4

4

5

5

5

5

6

6

6

6

7

7

7

7

8

8

8

Etc….

Etc ….

 

Accum4hr:

 

HourOfTheDay

Accum4hour

1

4

2

4

3

4

4

4

2

5

3

5

4

5

5

5

3

6

4

6

5

6

6

6

Etc….

Etc ….

 

Then join your data on the Hour column in your base data to the HourOfTheDay value in the table(s). What this does is tell your application that in the 2nd hour, the Accum2 values to accumulate are 1 & 2 and so forth. So you can now use the Accum<n> value(s) as a virtual dimension(s) to calc those values. Ie sum(<{Accum2Hour={[HourInYourDataField]}>  fieldtototal} – but be careful of potential data duplication if you use the accum<n>hour field in the dimensions of the chart.

 

You may also want to review the posts on “accumlations in pivot tables”.  Below is a code snippet I use to build a daily accumulation table

 

 

// Build the accumulation mapping table for the hours of a day
for vOuterLoop = 1 to 24
   for vInnerLoop = 1 to vOuterLoop
     HourAccumulation:
     LOAD
     $(vOuterLoop) as [AccumulatedHr],
     $(vInnerLoop) as [InvHr]
     AUTOGENERATE(1);   
   next
next

Hope this helps…

 

 

 

New-Qlik
Creator II
Creator II
Author

Thanks Greg for replying. But my requirement is little different

I have below databse column names

   

DateQuantity
20160226002
20160226013
20160226049
201602260521
201602260639
20160226076
20160226087
201602260924
201602261035

So average of 4 hours for Date = 2016022605 will be average of quantity for 2016022600, 2016022601, 2016022604, 2016022605 that means last four hours so  (2+3+9+21)/4 i.e 9

similarly for 2016022606 will be  average of 2016022601 +2016022604 +2016022605+ 2016022606 i.e (3+9+21+39)/4 = 18

so output should be like

    

DateQuantityAverage 4 hoursAverage 8 hours
20160226002
20160226013
20160226049
2016022605219
20160226063918
2016022607619
2016022608718
2016022609241914
2016022610351818

Kindly let me know if you have nay questions

Regards

Avneet

Anonymous
Not applicable

Hi

In the editor:

LOAD Date,

    Quantity

FROM

[https://community.qlik.com/thread/224973]

(html, codepage is 1252, embedded labels, table is @3);

use following expressions

exp1:


sum(Quantity)

exp2:

for 4 hours average

if(rowno()<4,'',Rangeavg(above(Sum({1} Quantity),0,4)))

exp3:

for 8 hours avrage:

if(rowno()<8,'',Rangeavg(above(Sum({1} Quantity),0,8)))

:FYI , attached qvw as well......

Thanks

Allu

New-Qlik
Creator II
Creator II
Author

Hi Allu,

Thanks for replying, but I am not able to open application ass you have created on personal edition.

Please guide.

Thanks

New-Qlik
Creator II
Creator II
Author

I checked its working Allu.. Thanks

can u also suggest how to find cumulative

     

Row LabelsSum of QtyAverage - 4 hoursAverage - 8 hoursCumulative Fcst Mvg Ave
20160226002
20160226013 5
20160226029 14
2016022603219 35
20160226043918 74
2016022605619 80
2016022606718 87
2016022607241914111
2016022608351818146
2016022609523024198
2016022610333627231
2016022611323829263
Anonymous
Not applicable

Hi

Add one more exp 4:

for cumulative sums  (Cumulative Fcst Mvg Ave)

Rangesum(above(Sum( Quantity),0,rowno()))

Note : use all the expressions and dimensions in PIVOT TABLE (for better visualization )

New-Qlik
Creator II
Creator II
Author

Thanks. Its working perfectly ..

New-Qlik
Creator II
Creator II
Author

Hello Allu,

I have got one more requirement for calculation Forecast value  ie for

1. Forecast - 12 Hours on Actual wrt to 2016022605 =   average(2016022600,2016022601,2016022604,2016022605)*0.75+ (Forecast - 12 Hours on Actual of previous date(2016022604) *0.25)

2. Forecast - 12 Hours on Actual wrt to 2016022606= average(2016022600,2016022601,2016022604,2016022605, 2016022606)*0.75+ (Forecast - 12 Hours on Actual of 2016022605 *0.25)

Kindly guide.

      

Row LabelsSum of QtyAverage - 12 HoursForecast - 12 Hours on ActualForecast - 12 Hours on Average
20160226002
20160226013
20160226049
20160226052197
2016022606391513
201602260761313
201602260871213
2016022609241413
2016022610351614
2016022611522016
2016022612332119
2016022613322220
2016022614292422
201602261588312412
201602261687382917
2016022617128473619
2016022618203604422

Thanks In Advance

Avneet

New-Qlik
Creator II
Creator II
Author

Hi Allu,

I tried cumulative expression today it was displaying null because inorder to calculate cumulative value for first date it has to pick up data from quantity.

please see attached file.

Regards

Avneet