Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
tomelmslie
Contributor
Contributor

Calculating moving average/date order issue

Hi,

I am trying to calculate a moving average for a data set with no luck.

I have tried to use the rangeavg(above(sum(sales),0,18)) code, but have encountered an issue with the date order.  It appears to be ordering by the value of the measure rather than by the date order (dimension). This seems to be preventing me from calculating an average (sales value returns zero).

I believe an AGGR function might be able to help here, but not sure exactly how to use it. Is there a way I can wrap the rangeavg formula with an AGGR to make this work? Many Thanks!

This is the data I am working with:

SalesDate
672017-06
652017-09
622017-10
592017-08
582017-11
572017-05
572017-07
522017-12
482018-01
472018-02
422018-03
352018-06
352018-07
322018-05
312018-04
232017-02
232017-04
162017-03
Labels (3)
7 Replies
sunny_talwar

If your date is sorted in the script... you can do this

Aggr(RangeAvg(Above(Sum(sales), 0, 18)), Date)

If it isn't, but you have QV12 or above, you can use this

Aggr(RangeAvg(Above(Sum(sales), 0, 18)), (Date, (Numeric)))

If your date isn't sorted and you are still using QV11.2 or lower, you will need to make sure that your Date field is sorted in ascending order in the script and then you can use the first expression

tomelmslie
Contributor
Contributor
Author

Hi Sunny,

Thanks for your help.

I tried both options but no luck - strangely my date order appears correctly now, but the sales column returns zero value for each date point. Might I have a problem with the sales measure in this case?

I am using Qlik Sense 2018 - not sure if this helps...

Tom

sunny_talwar


@tomelmslie wrote:

 

I am using Qlik Sense 2018 - not sure if this helps...


You posted your question in QlikView section of the community which is what confused me. But if you are using Qlik Sense 2018, you should def. be able to use this

Aggr(RangeAvg(Above(Sum(sales), 0, 18)), (Date, (Numeric)))

Few things to check if this became 0. Is your Date field called Date or something else? Make sure to use the correct name. Also, does this happen when you make a selection or does it no work with or without selections?

tomelmslie
Contributor
Contributor
Author

Apologies for that! The field name is definitely correct. I have tried this in a table as well as different graphs, and each time zero is returned. I have tested the code with other measures other than sales, and it works - I suspect it is an issue with the construction of measure in that case?
sunny_talwar

I am not sure why you think sales is an issue, but if this worked

RangeAvg(Above(Sum(sales), 0, 18))

I don't see why you think sales is causing the problem.

But having said that, I don't really have a way to test unless you are able to share your app for me to look at.

tomelmslie
Contributor
Contributor
Author

I played around with the formula and RangeAvg(Above(Count(Sales), 0, 18)) seems to have done the trick which is strange!
sunny_talwar

Good to know 🙂