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: 
ZimaBlue
Creator
Creator

Cumulative Sum in load scipt

Hello!,

I'm trying to find a way to calculate the cumulative total in the load script. I found an article with a formula like Range Sum(Above(sum(Sales),0,Rovno())), but as far as I understand, this does not work in the load script, because qlik sees Above as a field name, not as a function.

What I need: count for each customer|date row the sum of all customer orders for the last 30 days. The database is sorted as order by user_id, order_date asc.

I need to check that in all the previous 29 rows the user_id is the same and calculate the cumulative total in each row (data is needed for every day). So something like: 

user1| order_date1 | sum of all orders for last 30 days from this date

user1| order_date2| sum of all orders for last 30 days from this date

Also, if there is less than 30, I want to see the cumulative total for the period that we have, within 30 days. Unfortunately, our sql version is very old, so I can't calculate this sum in the sql script. I need to calculate this in qlik sense.

Thank you for any help!

Labels (1)
1 Solution

Accepted Solutions
cristianj23a
Partner - Creator III
Partner - Creator III

Hi ZimaBlue.

Here's an example script that might help you. This example assumes you have a table called 'Orders' that contains a 'User_Id', 'Order_Date', and 'Order_Amount' fields:

RawData:
LOAD
User_Id,
Order_Date,
Order_Amount
FROM
Orders
ORDER BY
User_Id,
Order_Date ASC;

TempTable:
LOAD
User_Id,
Order_Date,
Order_Amount,
IterNo() as RowNum
RESIDENT
RawData
WHILE IterNo() <= Peek('RowNum', -1, 'RawData') + 30 AND Peek('User_Id', -IterNo()) = User_Id;

FinalTable:
LOAD
User_Id,
Order_Date,
Sum(Order_Amount) as RollingOrderSum
RESIDENT
TempTable
GROUP BY
User_Id,
Order_Date;

DROP TABLES
RawData,
TempTable;

The script first loads the original data into a temporary table 'RawData' in the order of 'User_Id' and 'Order_Date'. Then, it creates a second temporary table 'TempTable' that replicates each row from 'RawData' into as many rows as there are within the previous 30 rows (or fewer if there are less than 30) with the same 'User_Id'. Finally, it creates 'FinalTable' by aggregating 'TempTable' to get the sum of 'Order_Amount' for each 'User_Id' and 'Order_Date'. The 'RawData' and 'TempTable' are then dropped as they're no longer needed.

Please note that this script assumes that you have a single record for each user and date. If you have multiple records for the same user and date, you should pre-aggregate them before using this script. Also, be aware that if your data is large, this script may take a long time to execute.

 

Regarts.

 

https://www.linkedin.com/in/cristianjorge/
Do not forget to mark as "Accepted Solution" the comment that resolves the doubt.

View solution in original post

3 Replies
cristianj23a
Partner - Creator III
Partner - Creator III

Hi ZimaBlue.

Here's an example script that might help you. This example assumes you have a table called 'Orders' that contains a 'User_Id', 'Order_Date', and 'Order_Amount' fields:

RawData:
LOAD
User_Id,
Order_Date,
Order_Amount
FROM
Orders
ORDER BY
User_Id,
Order_Date ASC;

TempTable:
LOAD
User_Id,
Order_Date,
Order_Amount,
IterNo() as RowNum
RESIDENT
RawData
WHILE IterNo() <= Peek('RowNum', -1, 'RawData') + 30 AND Peek('User_Id', -IterNo()) = User_Id;

FinalTable:
LOAD
User_Id,
Order_Date,
Sum(Order_Amount) as RollingOrderSum
RESIDENT
TempTable
GROUP BY
User_Id,
Order_Date;

DROP TABLES
RawData,
TempTable;

The script first loads the original data into a temporary table 'RawData' in the order of 'User_Id' and 'Order_Date'. Then, it creates a second temporary table 'TempTable' that replicates each row from 'RawData' into as many rows as there are within the previous 30 rows (or fewer if there are less than 30) with the same 'User_Id'. Finally, it creates 'FinalTable' by aggregating 'TempTable' to get the sum of 'Order_Amount' for each 'User_Id' and 'Order_Date'. The 'RawData' and 'TempTable' are then dropped as they're no longer needed.

Please note that this script assumes that you have a single record for each user and date. If you have multiple records for the same user and date, you should pre-aggregate them before using this script. Also, be aware that if your data is large, this script may take a long time to execute.

 

Regarts.

 

https://www.linkedin.com/in/cristianjorge/
Do not forget to mark as "Accepted Solution" the comment that resolves the doubt.
ZimaBlue
Creator
Creator
Author

Thank you for such a detailed answer! Sorry for the long answer. I get an empty table at this stage.

TempTable:
LOAD
User_Id,
Order_Date,
Order_Amount,
IterNo() as RowNum
RESIDENT
RawData
WHILE IterNo() <= Peek('RowNum', -1, 'RawData') + 30 AND Peek('User_Id', -IterNo()) = User_Id;

I also tried to remove the quotes for the field names, but it didn't help. Can I do something wrong?

cristianj23a
Partner - Creator III
Partner - Creator III

Hello I don't see the quotes in your data or I don't understand your question well, but one way to remove values that you don't want is using REPLACE or SUBFIELD.

https://help.qlik.com/en-US/qlikview/May2023/Subsystems/Client/Content/QV_QlikView/Scripting/StringF....

https://help.qlik.com/en-US/sense/May2023/Subsystems/Hub/Content/Sense_Hub/Scripting/StringFunctions...

 

Regarts.

 
https://www.linkedin.com/in/cristianjorge/
Do not forget to mark as "Accepted Solution" the comment that resolves the doubt.