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

Historical Stock Level from Current Stock and Transaction History

Hi there,

I have two tables, one detailing current stock levels and the other detailing stock transaction history. I would like to generate a script whereby I can view the stock level on any particular day in say the past 12 months.

Table1:

%_StockIDCurrent Stock Level

0001

10
000210
0003

10

 

Table2:

%_StockIDMovement DateMovement Quantity
000128/04/20215
000127/04/2021-10
000228/04/2021-10

 

Desired Result:

%_StockIDDateStock Level
000129/04/202110
000128/04/20215
000127/04/202115
000229/04/202110
000228/04/202120
000227/04/202120
000329/04/202110
000328/04/202110
000327/04/202110

 

Hopefully that makes sense, let me know if any more info is required.

Thanks,

Will

1 Solution

Accepted Solutions
Dalton_Ruer
Support
Support

I have to run but at the end you can remove two of the columns from Calendar

 


StockData:
Load * Inline [
StockID, StockAmount
0001, 10
0002, 20
0003, 40
];

Movement:
Load * Inline [
StockID, Movement Date, Movement
0001, 28/04/2021, 5
0001, 23/04/2021, -10
0002, 28/04/2021, -10
0002, 26/04/2021, 17
0003, 25/04/2021, 15
0003, 24/04/2021, -17
];

Calendar:
Load * Inline [
CalDate
29/04/2021
28/04/2021
27/04/2021
26/04/2021
25/04/2021
24/04/2021
23/04/2021
];

// Add Every stock item to every day
Join (Calendar)
Load StockID
Resident StockData;

Join (Calendar)
Load RowNo() as CalendarKey,
CalDate, StockID
Resident Calendar order by StockID, CalDate DESC;


// Get the current stock level for the first day
Join (Calendar)
Load CalendarKey,
Lookup('StockAmount', 'StockID', StockID, 'StockData') as CurrentStockLevel
Resident Calendar
where CalDate = '29/04/2021';

// Now bring over all of the movement transactions
Join (Calendar)
Load "Movement Date" as CalDate,
StockID,
Movement
Resident Movement;

// We don't need the movement transactions anymore because they are useless now
drop table Movement;


Join (Calendar)
Load
CalendarKey,
IF(IsNull(CurrentStockLevel), Peek(NewStockLevel)+IF(IsNull(Movement), 0, Movement), CurrentStockLevel) as NewStockLevel
Resident Calendar
order by CalendarKey

View solution in original post

1 Reply
Dalton_Ruer
Support
Support

I have to run but at the end you can remove two of the columns from Calendar

 


StockData:
Load * Inline [
StockID, StockAmount
0001, 10
0002, 20
0003, 40
];

Movement:
Load * Inline [
StockID, Movement Date, Movement
0001, 28/04/2021, 5
0001, 23/04/2021, -10
0002, 28/04/2021, -10
0002, 26/04/2021, 17
0003, 25/04/2021, 15
0003, 24/04/2021, -17
];

Calendar:
Load * Inline [
CalDate
29/04/2021
28/04/2021
27/04/2021
26/04/2021
25/04/2021
24/04/2021
23/04/2021
];

// Add Every stock item to every day
Join (Calendar)
Load StockID
Resident StockData;

Join (Calendar)
Load RowNo() as CalendarKey,
CalDate, StockID
Resident Calendar order by StockID, CalDate DESC;


// Get the current stock level for the first day
Join (Calendar)
Load CalendarKey,
Lookup('StockAmount', 'StockID', StockID, 'StockData') as CurrentStockLevel
Resident Calendar
where CalDate = '29/04/2021';

// Now bring over all of the movement transactions
Join (Calendar)
Load "Movement Date" as CalDate,
StockID,
Movement
Resident Movement;

// We don't need the movement transactions anymore because they are useless now
drop table Movement;


Join (Calendar)
Load
CalendarKey,
IF(IsNull(CurrentStockLevel), Peek(NewStockLevel)+IF(IsNull(Movement), 0, Movement), CurrentStockLevel) as NewStockLevel
Resident Calendar
order by CalendarKey