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: 
AleRods88
Contributor III
Contributor III

Cumulative Sum in Script

Hello,

I have the following table:

IDDATETIMETIME_SORTVALUE
11/1/2019611
11/1/20197217
11/1/2019835
11/1/2019945
11/1/20191057
11/1/201911613
11/1/20191270
11/1/20191383
11/1/20191498
11/1/2019151022
11/1/2019161123
11/1/2019171238
11/1/2019181331
11/1/2019191438
11/1/2019201530
11/1/2019211646
11/1/2019221746
11/1/2019231815
25/1/2019125
25/1/20191210

 

What I need is to add a colum named "cumulative" that makes a cumulative by ID and Date:

IDDATETIMETIME_SORTVALUECUMULATIVE
11/1/20196111
11/1/2019721718
11/1/201983523
11/1/201994528
11/1/2019105735
11/1/20191161348
11/1/2019127048
11/1/2019138351
11/1/2019149859
11/1/201915102281
11/1/2019161123104
11/1/2019171238142
11/1/2019181331173
11/1/2019191438211
11/1/2019201530241
11/1/2019211646287
11/1/2019221746333
11/1/2019231815348
25/1/20191255
25/1/2019121015

 

Is there any way to do that?

Thank you!!!

2 Replies
_ylc
Partner - Creator
Partner - Creator

Hi,

What you can do is create 2 keys.

1 for sorting and 1 for lookup

key1 will be ID,DATE, TIME and TIME_SORT

key2 will be ID and DATE

temp:
LOAD *,
ID & '_' & DATE&'_'& TIME & '_'* TIME_SORT as key1,

ID & '_' & DATE as key2
;
LOAD * INLINE [
ID, DATE, TIME, TIME_SORT, VALUE
1, 1/1/2019, 6, 1 ,1
1, 1/1/2019, 7 ,2 ,17
1, 1/1/2019, 8 ,3 ,5
1, 1/1/2019, 9 ,4 ,5
1, 1/1/2019, 10 ,5 ,7
1, 1/1/2019, 11 ,6 ,13
1, 1/1/2019, 12 ,7 ,0
1, 1/1/2019, 13 ,8 ,3
1, 1/1/2019, 14 ,9 ,8
1, 1/1/2019, 15 ,10 ,22
1, 1/1/2019, 16 ,11 ,23
1, 1/1/2019, 17 ,12 ,38
1, 1/1/2019, 18 ,13 ,31
1, 1/1/2019, 19 ,14 ,38
1, 1/1/2019, 20 ,15 ,30
1, 1/1/2019, 21 ,16 ,46
1, 1/1/2019, 22 ,17 ,46
1, 1/1/2019, 23 ,18 ,15
2, 5/1/2019, 1 ,2 ,5
2, 5/1/2019, 1 ,2 ,10
];

//Create a resident table that will order by your dataset using the key1.

//Then use the previous or peek function to compare the key2 if they are equal, if yes, then you should add the value. //if no then just get the value

final:
LOAD *,
if(peek(key2,-1)=key2, peek(CUMULATIVE,-1)+VALUE, VALUE) as CUMULATIVE//if(Previous(%key)=%key, previous(VALUE) + VALUE, VALUE) as testvalue,
;
LOAD * resident temp
order by key1 asc;


drop table temp;

Brett_Bleess
Former Employee
Former Employee

If the partners suggested solution worked for you, please be sure to return to your thread to properly close it out by using the Accept as Solution button on the partner's post to give them credit and let other Members know that worked.  If you require further assistance, please leave an update post.  We appreciate it if you do close out your threads, and if you did something different, you can post that and then use the button to mark that as the solution too.  

Check out the following Design Blog post if you are still working upon things, it may be of some further help too:

https://community.qlik.com/t5/Qlik-Design-Blog/Accumulations/ba-p/1466581

Regards,
Brett

To help users find verified answers, please do not forget to use the "Accept as Solution" button on any post(s) that helped you resolve your problem or question.
I now work a compressed schedule, Tuesday, Wednesday and Thursday, so those will be the days I will reply to any follow-up posts.