Skip to main content
Announcements
Introducing Qlik Answers: A plug-and-play, Generative AI powered RAG solution. READ ALL ABOUT IT!
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

How to calculate accumulated amount in script?

Hi!

YTD-questions seems to be a common question but I can't find a previous solution for my YTD-problem.

I got a table that inclueds Unit, Period and Amount_Month. I like to calculate a new field in the script with Amount_YTD as shown in the table below.

UnitPeriodAmount_MonthAmount_YTD
C2009102525
C2009115075
C200912-600-525
A201001400400
A201002-200200
A2010030200
A201004300500
A201005-5000
A201006-100-100
A201007200100
A201008300400
A2010090400
A201010100500
A201011250750
A201012-150600
B201001200200
B201002300500
B2010030500
B201004100600
B201005250850
B201006-150700
B2010074001100
B201008-200900
B2010090900
B2010102001100
B2010113001400
B201012-1501250


Any ideas?

1 Solution

Accepted Solutions
Miguel_Angel_Baeyens

Hello Kristofer,

Using peek() and previous() functions will help you to do that. My untested guess for your new field is

If(Unit = Previous(Unit), Rangesum(Amount_Month, Peek(Amount_Month))) AS Amount_YTD,


Of course you can add new conditionals to compare with previous periods or previous fields.

Anyway, the more complex your field is, the longer the load will take.

Hope that helps!

View solution in original post

2 Replies
Miguel_Angel_Baeyens

Hello Kristofer,

Using peek() and previous() functions will help you to do that. My untested guess for your new field is

If(Unit = Previous(Unit), Rangesum(Amount_Month, Peek(Amount_Month))) AS Amount_YTD,


Of course you can add new conditionals to compare with previous periods or previous fields.

Anyway, the more complex your field is, the longer the load will take.

Hope that helps!

Not applicable
Author

Thanx for your answer. Your tip got me on the right track. I had to adjust the last peek to Amount_YTD. This soleved my problem:




If

(Unit = Previous(Unit) and left(Period,4) = previous(left(Period,4)), rangesum(Amount_Month, Peek('Amount_YTD')),Amount_Month) as Amount_YTD