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: 
tonytang
Contributor II
Contributor II

How to create a new column with accumulate sum in loading script

Hi, 

I would like to know how can I create a new column with accumulated transaction amount in loading script? I wanna create the table in data model. Please see the below. 

The existing table has three columns, which are Customer Name, Order Date, and Transaction Amount

tonytang_0-1636824003363.png

Now, I want to create a new column called "Accumulated Transaction amount", which is calculated on the Customer Level and  Year(Order date Level). Please see my expected output below

tonytang_1-1636824529900.png

The transaction amount will keep accumulated until the year is changed to new year or the existing customer is changed to next customer name. For example, the first customer as above is accumulated from 4233 to 4275 in 2009, but once the year is updated to 2010, then the accumulated transaction amount will be reset to zero and then it will be accumulated again for every transaction amount in 2010. The value highlighted in red means the accumulated value is reset.

I would like to create this table with 4 columns in the loading script . From my initial thoughts, it may be accomplished with using Autogenerate or Do-loop function, but i am unable to think of a way to resolve it. 

Could everyone can give me some help? I am struggling for a few days.

Thanks so much in advance

 

2 Solutions

Accepted Solutions
MarcoWedel

Hi,

one solution might be:

QlikCommunity_Thread_t5_QlikView-App-Dev_How-to-create-a-new-column-with-accumulate-sum-in-loading-script_m-p_1858973_Pic1.png

table1:
LOAD 'Customer'&Ceil(Rand()*5)		as CustomerName,
     Dayname(Today()-Rand()*1500)	as OrderDate,
     Ceil(Rand()*1000)				as TransactionAmount
AutoGenerate 100;


table2:
LOAD *,
     RangeSum(TransactionAmount,If(CustomerName=Previous(CustomerName) and Year(OrderDate)=Previous(Year(OrderDate)),Peek(AccumulatedTransactionAmount))) as AccumulatedTransactionAmount
Resident table1
Order By CustomerName, OrderDate;


DROP Table table1;



hope this helps

Marco 

View solution in original post

AshutoshBhumkar
Partner - Specialist
Partner - Specialist

Hello,

Maybe below script can help. I have created a sample dataset.

 

Table:
Load Customer,Date(Date#(Date,'D/MM/YYYY')) as Date, Amount Inline [
Customer,Date,Amount
AB,1/7/2009,4233
AB,7/7/2009,42
AB,27/7/2010,5002
AB,9/11/2010,2337
AB,28/5/2010,14
AH,15/8/2009,460
AH,13/12/2009,13255

];

Table1:
Load * ,
If(Customer = Previous(Customer) and Year(Date)=Previous(Year(Date)),
RangeSum(Peek(Accumulated_Amount), Amount),Amount) as Accumulated_Amount

Resident Table Order by Customer,Date;
Drop Table Table;

Output :

AshutoshBhumkar_0-1636894017824.png

 

Thanks,
Ashutosh

View solution in original post

5 Replies
MarcoWedel

Hi,

one solution might be:

QlikCommunity_Thread_t5_QlikView-App-Dev_How-to-create-a-new-column-with-accumulate-sum-in-loading-script_m-p_1858973_Pic1.png

table1:
LOAD 'Customer'&Ceil(Rand()*5)		as CustomerName,
     Dayname(Today()-Rand()*1500)	as OrderDate,
     Ceil(Rand()*1000)				as TransactionAmount
AutoGenerate 100;


table2:
LOAD *,
     RangeSum(TransactionAmount,If(CustomerName=Previous(CustomerName) and Year(OrderDate)=Previous(Year(OrderDate)),Peek(AccumulatedTransactionAmount))) as AccumulatedTransactionAmount
Resident table1
Order By CustomerName, OrderDate;


DROP Table table1;



hope this helps

Marco 

AshutoshBhumkar
Partner - Specialist
Partner - Specialist

Hello,

Maybe below script can help. I have created a sample dataset.

 

Table:
Load Customer,Date(Date#(Date,'D/MM/YYYY')) as Date, Amount Inline [
Customer,Date,Amount
AB,1/7/2009,4233
AB,7/7/2009,42
AB,27/7/2010,5002
AB,9/11/2010,2337
AB,28/5/2010,14
AH,15/8/2009,460
AH,13/12/2009,13255

];

Table1:
Load * ,
If(Customer = Previous(Customer) and Year(Date)=Previous(Year(Date)),
RangeSum(Peek(Accumulated_Amount), Amount),Amount) as Accumulated_Amount

Resident Table Order by Customer,Date;
Drop Table Table;

Output :

AshutoshBhumkar_0-1636894017824.png

 

Thanks,
Ashutosh

tonytang
Contributor II
Contributor II
Author

Thanks both. It works.

MarcoWedel

you're welcome

AshutoshBhumkar
Partner - Specialist
Partner - Specialist

Great 🙂