Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Qlik Open Lakehouse is Now Generally Available! Discover the key highlights and partner resources here.
cancel
Showing results for 
Search instead for 
Did you mean: 
tduarte
Partner - Creator II
Partner - Creator II

Cumulative 12 months Sum on load script

Hi all,

I need to create a cumulative 12 months Sum(Revenue) on the load script, aggregated by month, category and reseller.

Meaning that for each month, the sum should be based on up to the 12 previous months.

MonthRevenueCumulative12
Jan-121010
Feb-121020
Mar-121030
Apr-121040
May-121050
Jun-121060
Jul-121070
Aug-121080
Sep-121090
Oct-1210100
Nov-1210110
Dec-1210120
Jan-1310120
Feb-1310120
Mar-1310120
Apr-1310120
May-1310120
Jun-1310120
Jul-1310120
Aug-1310120
Sep-1310120
Oct-1310120

The table contains many fields but I want the cumulative12 Sum, that will be used for banding, to be aggregated by month, category and  reseller.

I had a look at rangesum and peek not sure it will work here. I think it can be achieve with joins and group by.

Thanks.

10 Replies
tduarte
Partner - Creator II
Partner - Creator II
Author

Thanks Erica,

Option 3 does the job but after I had to change the cumulative sum to a temp table then inner join it with the original one to remove records added with the outer that represented no sales:

//Add in the previous 12 months total

outer join (GenData) Load Cat1, Cat2,  Month + 12 as Month , Month  as MonthL12, Sales as SalesL12 resident GenData;

//Do the usual cumulative sum - but minus the value from 12 months ago

CumulativeTemp:

Load Cat1, Cat2, Month,

  if(Peek(Cat1)&peek(Cat2) = Cat1&Cat2, numsum(Sales,Peek(CumSum) ),Sales) as CumSum,

  if(Peek(Cat1)&peek(Cat2) = Cat1&Cat2, numsum(Sales,Peek(CumSumL12) ,-SalesL12 ),Sales) as CumSumL12,

  if(not(isnull(Sales)),1) as IsActualSales

resident GenData;

//Remove records where there were no actual sales (added with outer join for missing months)

Inner Join (GenData)

Load Cat1, Cat2, Month, CumSumL12

Resident CumulativeTemp

Where IsActualSales;

Drop Table CumulativeTemp;