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

How to calculate a cumulative field with group by?

Hello,

I really need help with figuring out how to create a cumulative field for this case:

Order Item KeyOrderItem

Quantity Ordered

Quantity ArrivedArrival DateCumulative Quantity for Item per Order
1a1a502528/08/201625
1a1a502531/08/201650
2b2b600010002/08/2016100
2b2b600015005/08/2016250
2b2b600020009/08/2016450
2b2b600015015/08/2016600
2b2b600015022/08/2016750
2c2c50015005/08/2016150
2c2c50015015/08/2016300
2c2c50010025/08/2016400
3d3d200050016/08/2016500
3d3d200050017/08/20161000
3d3d200050018/08/20161500
3d3d200050019/08/20162000


The purpose is to calculate the time that takes for X% of an Order to arrive ('X' will be dynamic).

My question is how to calculate the Cumulative Quantity for Item per Order

when my keys will be Order + Item.


I tried this:


MYtable:

LOAD

  *,

  if([Order Item Key] = previous([Order Item Key]) , rangesum([Quantity Arrived], peek([Cumulative Quantity])),[Quantity Arrived]) as [Cumulative Quantity]

RESIDENT OrdersTable

ORDER BY [Order Item Key],[Arrival Date];

But the "Cumulative Quantity" creates multiplication in the table and i can't find the solution.

I also tried with 'group by' (order, item)  but it didn't work here because i needed more fields except these i used for the group by.

Thank you all!

Inbar.

9 Replies
sunny_talwar

Try this:

Table:

LOAD [Order Item Key],

    Order,

    Item,

    [Quantity Ordered],

    [Quantity Arrived],

    [Arrival Date]

FROM

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

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

Where Len(Trim([Order Item Key])) > 0;

FinalTable:

LOAD *,

  If([Order Item Key] = Previous([Order Item Key]), RangeSum(Peek('Cumulative Quantity for Item per Order'), [Quantity Arrived]), [Quantity Arrived]) as [Cumulative Quantity for Item per Order]

Resident Table

Order By [Order Item Key], [Arrival Date];

DROP Table Table;

Not applicable
Author

Thank you for this suggestion,

but the 'if' still duplicates the rows in the second table.

sunny_talwar

Duplicates the row? I am not sure I understand your concern here. What exactly do you mean here? Is this something happening in the sample or is this something you are seeing in your actual application?

Not applicable
Author

in my actual application.

"Table1" works just fine but "Table" multiplies each row several times

Table1:

LOAD

  Company,

  [ID],

  [Order Item Key],

  [Arrival Date],

  [Quantity Arrived],

  [Type],

  [Order Item],

  [Receive Doc No],

FROM "$(QvdPath)TEMP.qvd"(qvd);

WHERE Len(Trim([Order Item])) > 0;

Table:

Load

  *,

  If([Order Item Key] = Previous([Order Item Key]) AND [Type] = 'blabla', RangeSum(Peek('Cumulative Quantity'), [Quantity Arrived]), [Quantity Arrived]) as [Cumulative Quantity]

Resident Table1

ORDER BY [Order Item Key],[Arrival Date];

DROP TABLE Table1;

sunny_talwar

Not sure how you can you get more number of rows than your resident table. How many observations increase when you do this?

Anil_Babu_Samineni

Are you getting problem here

RangeSum(Peek('Cumulative Quantity'), [Quantity Arrived]), [Quantity Arrived]) as [Cumulative Quantity]


if Yes,


Perhaps this


RangeSum('Cumulative Quantity', Peek([Quantity Arrived]), [Quantity Arrived]) as [Cumulative Quantity]

Best Anil, When applicable please mark the correct/appropriate replies as "solution" (you can mark up to 3 "solutions". Please LIKE threads if the provided solution is helpful
Not applicable
Author

It's really weird

when i'm using the same table in a new file it works just fine

but when i run it on my whole Module each row increases different number of times. it can be 11 or 96.

Not applicable
Author

Thank you, but i need the opposite

sunny_talwar

How exactly are you using this? Would you be able to show screenshots?