Skip to main content
Announcements
Join us at Qlik Connect for 3 magical days of learning, networking,and inspiration! REGISTER TODAY and save!
cancel
Showing results for 
Search instead for 
Did you mean: 
ahmd
Contributor II
Contributor II

Subtract values from previous values

Hi,

How to subtract "PL" values from "Previous ST" and each time new ST to be caluclated.

For example on 22.01.2021 I have 1 "Previous ST" in table 1, but the SL in table 2 takes place on different dates and they are subtracting mostly in each dates. So, I have to subtract Previous Day ST - Today SL each time.

 

The table 1 consist on ST with 3 dimensions.

Table 1
Dim1Dim2DatePrevious ST
AA122.01.202118
AA222.01.20213
BB122.01.202111
BB222.01.202117
CC122.01.202114
CC222.01.202113

 

Table 2 consist on SL which are taking place on different dates.

Table 2
Dim1Dim2DateSL
AA123.01.20211
AA124.01.20222
AA125.01.20222
AA126.01.20234
AA127.01.20235
AA128.01.20241
AA223.01.20211
AA224.01.20222
BB123.01.20212
BB124.01.20221
BB225.01.20221

 

Output required: Formula: New ST = Previous ST - SL

Table 2
Dim1Dim2DatePrevious STSLNew ST
AA122.01.202119  
AA123.01.2021 118
AA124.01.2022 216
AA125.01.2022 214
AA126.01.2023 410
AA127.01.2023 55
AA128.01.2024 14
AA222.01.202131 
AA223.01.2021 12
AA224.01.2022 20
1 Solution
4 Replies
Dalton_Ruer
Support
Support

Check out this post it was a similar question and I think the solution will work for you as well. 

ahmd
Contributor II
Contributor II
Author

Hi,

It works for specific ST Date with lookup. For example date is 22.01.2021 in table 1.

If the dates are changed with ST then it doesn't handle. 

Dim1, Dim2, Date, Previous ST
A, A1, 22.01.2021, 18
A, A1, 27.01.2021, 5

MayilVahanan

T1:
LOAD * INLINE [
Dim1, Dim2, Date, Previous ST
A, A1, 22.01.2021, 19
A, A2, 22.01.2021, 3
B, B1, 22.01.2021, 11
B, B2, 22.01.2021, 17
C, C1, 22.01.2021, 14
C, C2, 22.01.2021, 13
];

Concatenate
LOAD * INLINE [
Dim1, Dim2, Date, SL
A, A1, 23.01.2021, 1
A, A1, 24.01.2022, 2
A, A1, 25.01.2022, 2
A, A1, 26.01.2023, 4
A, A1, 27.01.2023, 5
A, A1, 28.01.2024, 1
A, A2, 23.01.2021, 1
A, A2, 24.01.2022, 2
B, B1, 23.01.2021, 2
B, B1, 24.01.2022, 1
B, B2, 25.01.2022, 1
];

FinalTemp:
Load *, If(Len(Trim(SL))>0, Peek(NewST)-SL, ([Previous ST])) as NewST Resident T1 order by Dim1, Dim2, Date;

Final:

NoConcatenate
LOAD Dim1, Dim2, Date, [Previous ST], SL, if(Len(Trim([Previous ST]))=0,NewST)as NewST Resident FinalTemp;
DROP Table T1, FinalTemp;

 

O/P:

MayilVahanan_0-1620016535663.png

 

Thanks & Regards, Mayil Vahanan R
Please close the thread by marking correct answer & give likes if you like the post.