Skip to main content
Announcements
Customer Spotlight: Discover what’s possible with embedded analytics Oct. 16 at 10:00 AM ET: REGISTER NOW
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Cummulative Calculation based on condition

Hi All,

I am stuck in one of the requirement and I am not able to find the right solution.

  

IDSeqCategoryAmount
11a100
12a200
13a300
14b200
15c150
26c300
27a100
28b100
29b300
210a175

I need output like below

  

IDSeqCategoryAmountCummulative
11a100100
12a200200
13a300300
14b200400“(100+200+300)-200”
15c150850“(100+200+300+400)-150”
26c300300
27a1002050“(100+200+300+400+850+300)-100”
28b1004400
29b300300
210a1758725

So basically when Category is Same, amount will be same, but when category changed sequentially (eg. Seq=4) , amount should be calculated as (sum of all previous category amount - current category amount)

Hope I have explained the logic well.

gwassenaar  swuehlsunindiakush141087

1 Solution

Accepted Solutions
swuehl
MVP
MVP

Maybe like

INPUT:

LOAD ID,

    Seq,

    Category,

    Amount

FROM

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

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

LOAD *,

  Rangesum(Cumulative, Peek(Tmp1)) as Tmp1;

LOAD ID,

    Seq,

    Category,

    Amount,

    If(ID <> previous(ID) or Category=Previous(Category), Amount, Rangesum(Peek(Tmp1),-Amount)) as Cumulative

Resident INPUT

ORDER BY ID, Seq;

DROP TABLE INPUT;

edit: there are some minor differences in the last few lines, but I think the calculations are correct:

ID Seq Category Amount Cumulative Tmp1
11a100100100
12a200200300
13a300300600
14b2004001000
15c1508501850
26c3003002150
27a10020504200
28b10041008300
29b3003008600
210a175842517025

View solution in original post

11 Replies
sunny_talwar

This is needed in the script or the front end of the application?

Not applicable
Author

Hi sunny,

I need this in script

swuehl
MVP
MVP

Maybe like

INPUT:

LOAD ID,

    Seq,

    Category,

    Amount

FROM

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

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

LOAD *,

  Rangesum(Cumulative, Peek(Tmp1)) as Tmp1;

LOAD ID,

    Seq,

    Category,

    Amount,

    If(ID <> previous(ID) or Category=Previous(Category), Amount, Rangesum(Peek(Tmp1),-Amount)) as Cumulative

Resident INPUT

ORDER BY ID, Seq;

DROP TABLE INPUT;

edit: there are some minor differences in the last few lines, but I think the calculations are correct:

ID Seq Category Amount Cumulative Tmp1
11a100100100
12a200200300
13a300300600
14b2004001000
15c1508501850
26c3003002150
27a10020504200
28b10041008300
29b3003008600
210a175842517025
swuehl
MVP
MVP

I am little bit unsure about your logic when ID change, so instead

  If(ID <> previous(ID) or Category=Previous(Category), ..


you might want a different logic (I believe my current logic is not correct)? Do you need to look at ID at all?

  If(Recno() = 1 or Category=Previous(Category), ...

sunny_talwar

So Stefan, preceding load is the key here right, there no way to have accomplished this without the preceding load or do you think it is still possible?

swuehl
MVP
MVP

Well, I think it should be possible to get everything in one load, by replacing the field reference with the full expression.

.

sunny_talwar

Something like this?

INPUT: 

LOAD ID, 

    Seq, 

    Category, 

    Amount 

FROM 

[https://community.qlik.com/thread/213114

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

 

 

LOAD *, 

  as Tmp1; 

LOAD ID, 

    Seq, 

    Category, 

    Amount, 

    If(ID <> previous(ID) or Category=Previous(Category), Amount, Rangesum(Peek(Rangesum(Cumulative, Peek(Tmp1))),-Amount)) as Cumulative 

Resident INPUT 

ORDER BY ID, Seq; 

 

DROP TABLE INPUT; 

Peek allows for using functions such as RangeSum and another Peek within it?

sunny_talwar

But I guess this won't work because we don't have Tmp1 available within the inner peek. And when I tried this:

INPUT:

LOAD ID,

     Seq,

     Category,

     Amount

FROM

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

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

//LOAD *,

//  Rangesum(Cumulative, Peek(Tmp1)) as Tmp1;

LOAD ID,

     Seq,

     Category,

     Amount,

    Rangesum(Cumulative, Peek(Tmp1)) as Tmp1,

     If(ID <> previous(ID) or Category=Previous(Category), Amount, Rangesum(Peek(Tmp1),-Amount)) as Cumulative

Resident INPUT

ORDER BY ID, Seq;

DROP TABLE INPUT;

Again it throw me an error that Cumulative isn't there. So I guess cross referencing seems only possible with a preceding load here.

swuehl
MVP
MVP

Well, I was considering replacing the preceding load, not getting everything into one field expression:

LOAD ID,

     Seq,

     Category,

     Amount,

     If(Recno()=1 or Category=Previous(Category), Amount, Rangesum(Peek(Tmp1),-Amount)) as Cumulative,

     Rangesum( If(Recno()=1 or Category=Previous(Category), Amount, Rangesum(Peek(Tmp1),-Amount)), Peek(Tmp1)) as Tmp1

Resident INPUT

ORDER BY ID, Seq;