Skip to main content
Announcements
See what Drew Clarke has to say about the Qlik Talend Cloud launch! READ THE BLOG
cancel
Showing results for 
Search instead for 
Did you mean: 
cjliew
Contributor III
Contributor III

Accumulative Average Margin % Scripting

Dear All, 

Any idea what scripting we can write in order to get the result of the "Avg Margin" as below?

The "Avg Margin" is calculated based on the accumulative of Margin divide by accumulative of Sales.

cjliew_0-1605774728638.png

So far, I have tried the following script, but the return result is by year, instead of period:

cjliew_1-1605774905482.png

cjliew_2-1605774936928.png

 

Labels (1)
1 Solution

Accepted Solutions
lironbaram
Partner - Master III
Partner - Master III

hi 

assuming you loaded the data field to table named Table1 
this script should give you the accumulated Sales and cost for each date so you can calculate the margin

Table2: 
load *,
     if(Previous(company)=company, rangesum(peek('accSales'),Sales),Sales) as accSales,
 if(Previous(company)=company, rangesum(peek('accCost'),Cost),Cost) as accCost
Resident Table1
Order By Company,Year,Period; 
drop Table1;

 

View solution in original post

3 Replies
lironbaram
Partner - Master III
Partner - Master III

hi 

assuming you loaded the data field to table named Table1 
this script should give you the accumulated Sales and cost for each date so you can calculate the margin

Table2: 
load *,
     if(Previous(company)=company, rangesum(peek('accSales'),Sales),Sales) as accSales,
 if(Previous(company)=company, rangesum(peek('accCost'),Cost),Cost) as accCost
Resident Table1
Order By Company,Year,Period; 
drop Table1;

 

cjliew
Contributor III
Contributor III
Author

Thanks a lot, it is the result that I'm looking for.

cjliew
Contributor III
Contributor III
Author

Hi Lironbaram, 

If my data have a duplicate row with different sales figure, any idea how to resolve it?

cjliew_0-1606121032607.png