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: 
qliksensedlin
Contributor II
Contributor II

Rolling 30 Days Counts in 2 Dimensions

Hi,

I have a chart that I need to count the score over past 30 days by products. In table form, it looks like below.

submit_dateProduct_Master_CodeScore
2019-01-01A21
2019-01-01B48
2019-01-01C30
2019-01-02A26
2019-01-02B23
2019-01-03A24
2019-01-03C38
2019-01-04A34
2019-01-04B30
2019-01-14C39
.........

 

I tried with the Aggr expression below.

=Aggr(RangeSum(Above(Count(Score), 0, 30)),Product_Master_Code, (submit_date,(NUMERIC, ASCENDING)))

It could give me the rolling 30 days score count for any particular submit date with each product.

However, the problem is I do not have a record for all products on all dates. This would give me a miss count as the result is just 30 rows above current record regardless the dates. In effect, I could be counting over 2 month's scores if there are a few days missing in between.

What I hope to achieve is a range count between (submit_date-30) and submit_date. So I tried the following expression.

=Count({<submit_date = {">=(=submit_date-30)<=(=submit_date)"}>}Score)

This counts to 0. I assume some error with my expression.

Could any one help me with this rolling 30 days calculation?

Thank you in advance for the help.

5 Replies
ashishkalia
Partner - Creator
Partner - Creator

You need to have a master calendar.
Once you create master calendar you'll have a continuous date field and for the dates where you don't have and transaction you'll get zero and calculation will happen accordingly.
After That Formula Should be:
Count({<
submit_date = {">=$(=Date(MAX(submit_date)-30))<=(=Date(Max(submit_date)))"}
>}Score)
qliksensedlin
Contributor II
Contributor II
Author

Hi Ashishakalia,

Thanks for your reply. I tried with your formula. What it gives me is only a daily count on the last 30 days. I got counts on Jan 26 to Feb 25 only. And they are not rolling 30 days counts, but just a daily count. 

What could go wrong here?

=Count({<submit_date = {">=$(=Date(Max(submit_date)-30))<=$(=Date(Max(submit_date)))"}>}Score)

 

ashishkalia
Partner - Creator
Partner - Creator

Can you pl share the output which you want and the one which you are getting now.
qliksensedlin
Contributor II
Contributor II
Author

Hi Ashishkalia,

The output I would like to have:

submit_dateProduct_Master_CodeScoreRolling Count
2019-01-01A2121
2019-01-01B4848
2019-01-01C3030
2019-01-02A2647
2019-01-02B2371
2019-01-03A2471
2019-01-03C3868
2019-01-04A34105
2019-01-04B30101
2019-01-14C39107
............

 

The output I'm getting now:

submit_dateProduct_Master_CodeScoreRolling Count
2019-01-01A210
2019-01-01B480
2019-01-01C300
2019-01-02A260
2019-01-02B230
2019-01-03A240
2019-01-03C380
2019-01-04A340
2019-01-04B300
2019-01-14C390
............
2019-02-24A2020
2019-02-24B3535
2019-02-25A3232
2019-02-25C3030

 

The rolling count is 0 for dates more than 30 days ago.  And the counts are not accumulated for dates within 30 days.

qliksensedlin
Contributor II
Contributor II
Author

Also putting up my script for master calendar creation. The rolling 30 days still can't be solved after implementing master calendar.

Temp:
Load
 min(submit_date) as minDate,
 max(submit_date) as maxDate
Resident NPS;

Let varMinDate = Num(Peek('minDate', 0, 'Temp'));

Let varMaxDate = Num(Peek('maxDate', 0, 'Temp'));

DROP Table Temp;

TempCalendar:
LOAD
 $(varMinDate) + Iterno()-1 As Num,
 Date($(varMinDate) + IterNo() - 1) as TempDate
 AutoGenerate 1 While $(varMinDate) + IterNo() -1 <= $(varMaxDate);

MasterCalendar:
Load
 TempDate AS submit_date
Resident TempCalendar

Order By TempDate ASC;

Drop Table TempCalendar;