Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
Double_XYZ
Contributor
Contributor

YTD calculation - with some dimensions

YTD Calculation respect to some dimensions
 

Hey, i have the following problem:

My data is like 

Customer,  Date,          Earnings
A,                   01/2022,  10
B,                   01/2022,  10
B,                   02/2022,  10
C,                   01/2022,  10
C,                   02/2022,  10
C,                   03/2022,  10

and i want to calculate a ytd dimension like

Customer,  Date,          Earnings,  Earnings_YTD
A,                   01/2022,  10,                10
B,                   01/2022,  10,                10
B,                   02/2022,  10,                20
C,                   01/2022,  10,                10
C,                   02/2022,  10,                20
C,                   03/2022,  10,                30

 

Important: I want to do this in the script.

Any ideas?

Labels (2)
1 Solution

Accepted Solutions
BrunPierre
Partner - Master II
Partner - Master II

As below

DataTable:
LOAD *,
RowNo() as RN; 

LOAD * Inline[
Customer,Date,Earnings
A,01/2022,10
B,01/2022,10
B,02/2022,10
C,01/2022,10
C,02/2022,10
C,03/2022,10];

FinalTable:
LOAD *,
IF(RN = 1, Earnings,
IF(Peek('Customer') <> Customer, Earnings, Earnings + Peek('Earnings_YTD'))) as Earnings_YTD

Resident DataTable
Order By Customer,RN;

Drop Table DataTable;

DROP Field RN From FinalTable;

EXIT SCRIPT;

 

View solution in original post

2 Replies
BrunPierre
Partner - Master II
Partner - Master II

As below

DataTable:
LOAD *,
RowNo() as RN; 

LOAD * Inline[
Customer,Date,Earnings
A,01/2022,10
B,01/2022,10
B,02/2022,10
C,01/2022,10
C,02/2022,10
C,03/2022,10];

FinalTable:
LOAD *,
IF(RN = 1, Earnings,
IF(Peek('Customer') <> Customer, Earnings, Earnings + Peek('Earnings_YTD'))) as Earnings_YTD

Resident DataTable
Order By Customer,RN;

Drop Table DataTable;

DROP Field RN From FinalTable;

EXIT SCRIPT;

 

Double_XYZ
Contributor
Contributor
Author

Works! Thanks a lot.