Skip to main content
Announcements
Qlik Introduces a New Era of Visualization! READ ALL ABOUT IT
cancel
Showing results for 
Search instead for 
Did you mean: 
siva0606
Contributor III
Contributor III

Calculating Cumulative Sum with Duplicate Values

Hi All,

I have the below data set to achieve the output and below is the example.

INPUT:

ID                      DATE                        CODE                VALUE

127400            11/22/2019           A12                    1.5

127400            11/22/2019           A13                    1.5

127400            12/24/2019           A12                    2.5

127400            12/24/2019           A13                    2.5

127400            12/30/2019           A12                    3.5

127400            12/30/2019           A13                    3.5

 

OUTPUT:

 

ID                      DATE                      CODE             VALUE           CUMULATIVE_VALUE

127400            11/22/2019         A12                 1.5                   1.5 (Just VALUE)

127400            11/22/2019         A13                 1.5                   1.5 (Just VALUE)

127400            12/24/2019         A12                 2.5                   4.0 (VALUE+peek(CUMULATIVE_VALUE)

127400            12/24/2019         A13                 2.5                   4.0 (VALUE+peek(CUMULATIVE_VALUE)

127400            12/30/2019         A12                 3.5                   7.5 (VALUE+peek(CUMULATIVE_VALUE)

127400            12/30/2019         A13                 3.5                   7.5 (VALUE+peek(CUMULATIVE_VALUE)

Kindly help me on achieving the CUMULATIVE_VALUE column .

 

Best Regards,

Siva

Labels (1)
1 Solution

Accepted Solutions
Saravanan_Desingh

Please check this.

tab1:
LOAD *, If(ID=Peek(ID), If(VALUE <> Peek(VALUE), RangeSum(VALUE,Peek(CUMULATIVE_VALUE)), Alt(Peek(CUMULATIVE_VALUE),VALUE)),VALUE) As CUMULATIVE_VALUE;
LOAD RecNo() As RowID, * INLINE [
ID, DATE, CODE, VALUE
127400, 11/22/2019, A12, 1.5
127400, 11/22/2019, A13, 1.5
127400, 12/24/2019, A12, 2.5
127400, 12/24/2019, A13, 2.5
127400, 12/30/2019, A12, 3.5
127400, 12/30/2019, A13, 3.5
273400, 11/22/2019, A12, 1.5
];

View solution in original post

8 Replies
Anil_Babu_Samineni

Perhaps this?

RangeSum(Above(TOTAL Aggr(Sum(distinct VALUE), VALUE), 0, RowNo(TOTAL)))

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
siva0606
Contributor III
Contributor III
Author

Hi Anil,

Thanks much for your reply.

I forgot to mention one thing. I need the calculation to be done on script level.

Best Regards,

Siva

Saravanan_Desingh

One Solution is.

tab1:
LOAD *, If(ID=Peek(ID) And VALUE <> Peek(VALUE), RangeSum(VALUE,Peek(CUMULATIVE_VALUE)), Alt(Peek(CUMULATIVE_VALUE),VALUE)) As CUMULATIVE_VALUE;
LOAD RecNo() As RowID, * INLINE [
ID, DATE, CODE, VALUE
127400, 11/22/2019, A12, 1.5
127400, 11/22/2019, A13, 1.5
127400, 12/24/2019, A12, 2.5
127400, 12/24/2019, A13, 2.5
127400, 12/30/2019, A12, 3.5
127400, 12/30/2019, A13, 3.5
];
Saravanan_Desingh

Output.

commQV51.PNG

siva0606
Contributor III
Contributor III
Author

Hi Saran,

Thank you very much for your quick response.

and the logic is working as expected if we have one Id . If the other id comes , then facing the below issue in cumulative column.

I have shared the screenshot below.

In RowIdD =7, the cumulative value should be 1.5 and not 7.5.  Kindly check and let me know.

siva0606_0-1595350650866.png

Thanks much in advance.

Best Regards,
Siva

Saravanan_Desingh

Please check this.

tab1:
LOAD *, If(ID=Peek(ID), If(VALUE <> Peek(VALUE), RangeSum(VALUE,Peek(CUMULATIVE_VALUE)), Alt(Peek(CUMULATIVE_VALUE),VALUE)),VALUE) As CUMULATIVE_VALUE;
LOAD RecNo() As RowID, * INLINE [
ID, DATE, CODE, VALUE
127400, 11/22/2019, A12, 1.5
127400, 11/22/2019, A13, 1.5
127400, 12/24/2019, A12, 2.5
127400, 12/24/2019, A13, 2.5
127400, 12/30/2019, A12, 3.5
127400, 12/30/2019, A13, 3.5
273400, 11/22/2019, A12, 1.5
];
Saravanan_Desingh

Output.

commQV52.PNG

siva0606
Contributor III
Contributor III
Author

Thank you so much and I have implemented the logic and it is working as expected.

 

Best Regards,

Siva