Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
didierodayo
Partner - Creator III
Partner - Creator III

Complexe sum script

Hi Experts,

I am faced with a small challenge. I have the table below which holds ID;DATE; PRICE.

IDDATEPRICE
0023415/02/2016125
0023421/03/2016250
0023422/03/2016300
0023428/03/2016400
0023417/04/2016900
0023425/04/20163500
0023405/05/20164000
0023415/05/20166000
0023421/05/2016750
0023406/06/2016200
0023420/06/2016

120

0023401/07/201645

question1: while loading the table I would like to return only the id;date;price While ignoring the minimum dates up to where the SUM of price is >=600 and <=700. The result will be :

600.PNG

Question2: From the original I would like to load only the id;date;price where the Sum of Price for any consecutive 3 dates is >=10000.

the expected result will be.

10000.PNG

I would appreciate any help.

Regards

1 Solution

Accepted Solutions
Gysbert_Wassenaar

I think you want a cumulative sum of price:

Temp1:

LOAD

     ID,

     DATE,

     PRICE

FROM

     ...source...

     ;

Temp2:

LOAD

     ID,

     DATE,

     PRICE,

     If(ID = Previous(ID), Rangesum(PRICE, peek(CUM_PRICE)) , PRICE) as CUM_PRICE,

     If(ID = Previous(ID), Rangesum(PRICE, Peek(PRICE, -1), Peek(PRICE, -2))) as CONSEC_3DAY_SUM

RESIDENT

     Temp1

ORDER BY

     ID, DATE

     ;

DROP TABLE Temp1;

Result:

LOAD

     *,

     If(CONSEC_3DAY_SUM>=10000 or Peek(CONSEC_3DAY_SUM,-1) >= 10000 or Peek(CONSEC_3DAY_SUM,-2) >= 10000, 1, 0) as CONSEC_3DAY_FLAG

RESIDENT

     Temp2

ORDER BY

     ID, DATE desc

     ;

DROP TABLE Temp2;

Once you have this you should do the rest in chart objects imho. For example a straight table with ID and DATE as dimensions and as expression sum({<CUM_PRICE={'>700'}>}PRICE). For the second question use sum({<CONSEC_3DAY_FLAG={1}>}PRICE)


talk is cheap, supply exceeds demand

View solution in original post

15 Replies
sunny_talwar

I think its difficult to understand your output here. How is 45 included in your first screenshot. Can you elaborate on the thinking behind the two questions you have here?

tresesco
MVP
MVP

I could not get your question1:

Didier Agonvonon wrote:

...While ignoring the minimum dates up to where the SUM of price is >=600 and <=700. The result will be :

600.PNG

How does your logic give the output? Could you please explain?

didierodayo
Partner - Creator III
Partner - Creator III
Author

Hi Sunny,

I am trying this is actually a sample of a very large table. so I would like to ignore the FIRST dates for all IDs up to >=600 and <=700. So the load will ignore 125;250;300 (with their corresponding id and dates)

Secondly it is suspicious to have consecutive amount up to >=10000 . so I would like to identify any consecutive sum that equals to >=10000 which is the result is the 3rd snapshot.

I hope it is clearer now otherwise please let me know.

Thanks

didierodayo
Partner - Creator III
Partner - Creator III
Author

Hi Tresesco,

Please check my reply to Sunny, I am only ignoring the beginning (first rows) up to the sum of >=600 and <=700.

tresesco
MVP
MVP

But the first value - (after 125;250;300) 400 doesn't satisfy the condition >=600 and <=700  ??

sunny_talwar

I guess 125;250;300 are getting ignored, but how is 400 meeting the condition? >=600 and  <= 700?

didierodayo
Partner - Creator III
Partner - Creator III
Author

the sum of 125;250;300 is 675. so am ignoring those 3 rows before the load.

didierodayo
Partner - Creator III
Partner - Creator III
Author

Hi Sunny, am not ignoring 400. you can see it is showing up the result in the second snapshot( result 1)

Gysbert_Wassenaar

I think you want a cumulative sum of price:

Temp1:

LOAD

     ID,

     DATE,

     PRICE

FROM

     ...source...

     ;

Temp2:

LOAD

     ID,

     DATE,

     PRICE,

     If(ID = Previous(ID), Rangesum(PRICE, peek(CUM_PRICE)) , PRICE) as CUM_PRICE,

     If(ID = Previous(ID), Rangesum(PRICE, Peek(PRICE, -1), Peek(PRICE, -2))) as CONSEC_3DAY_SUM

RESIDENT

     Temp1

ORDER BY

     ID, DATE

     ;

DROP TABLE Temp1;

Result:

LOAD

     *,

     If(CONSEC_3DAY_SUM>=10000 or Peek(CONSEC_3DAY_SUM,-1) >= 10000 or Peek(CONSEC_3DAY_SUM,-2) >= 10000, 1, 0) as CONSEC_3DAY_FLAG

RESIDENT

     Temp2

ORDER BY

     ID, DATE desc

     ;

DROP TABLE Temp2;

Once you have this you should do the rest in chart objects imho. For example a straight table with ID and DATE as dimensions and as expression sum({<CUM_PRICE={'>700'}>}PRICE). For the second question use sum({<CONSEC_3DAY_FLAG={1}>}PRICE)


talk is cheap, supply exceeds demand