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: 
elintham
Contributor
Contributor

Cumulative Sum in Script

Hi Qlik Community, 

I have the below data whereby I have two Backlog Types (CRS and RS) and I would need to add another Backlog Type "Cumulative Delta" where I pre-calculate the cumulative difference between RS and CRS in the load script so that I can do a pivot in Qlik NPrinting. For example I have the below set of data:

PNMPNCPNCustomerCountryFamilyOrderIDFiscal WeekBacklogTypeSum(Qty)
CP123AB2334CDF345JohnMALAYSIAA12345201912CRS2000
CP123AB2334CDF345JohnMALAYSIAA12345201912RS1000
CP123AB2334CDF345JohnMALAYSIAA12345201913CRS2000
CP123AB2334CDF345JohnMALAYSIAA12345201913RS2000
CP123AB2334CDF345JohnMALAYSIAA12345201914CRS2000
CP123AB2334CDF345JohnMALAYSIAA12345201914RS3000
CP123AB2334CDF345JohnMALAYSIAA12345201915CRS2000
CP123AB2334CDF345JohnMALAYSIAA12345201915RS2000
CP123AB2334CDF345JohnMALAYSIAA12345201916CRS2000
CP123AB2334CDF345JohnMALAYSIAA12345201916RS2000
CP123AB2334CDF345JohnMALAYSIAA12345201917CRS1000
CP123AB2334CDF345JohnMALAYSIAA12345201917RS1000
CDS345B234DBF889KarlGERMANYB98080201912CRS5000
CDS345B234DBF889KarlGERMANYB98080201912RS5000
CDS345B234DBF889KarlGERMANYB98080201913CRS7500
CDS345B234DBF889KarlGERMANYB98080201913RS7500
CDS345B234DBF889KarlGERMANYB98080201914CRS7500
CDS345B234DBF889KarlGERMANYB98080201914RS7500
CDS345B234DBF889KarlGERMANYB98080201915CRS7500
CDS345B234DBF889KarlGERMANYB98080201915RS7500
CDS345B234DBF889KarlGERMANYB98080201918CRS5000
CDS345B234DBF889KarlGERMANYB98080201918RS5000

 

I'd like to pre-calculate Cumulative Delta in the load script. The calculation should be -1*CRS+RS and accumulate by week, ordered by PN, MPN, CPN, Customer, Country, Family and OrderID. But the sum(Qty) of CRS and RS should not be cumulative. I've attached the calculation method of the Cumulative Delta in excel, the desired pivot table output and my current script in Nprinting in the excel file. I'm thinking if I should be using the If(Peek() function with Rangesum, but not sure how exactly to apply.  

PNMPNCPNCustomerCountryFamilyOrderIDFiscal WeekBacklogTypeSum(Qty)
CP123AB2334CDF345JohnMALAYSIAA12345201912CRS2000
CP123AB2334CDF345JohnMALAYSIAA12345201912RS1000
CP123AB2334CDF345JohnMALAYSIAA12345201912Cumulative Delta-1000
CP123AB2334CDF345JohnMALAYSIAA12345201913CRS2000
CP123AB2334CDF345JohnMALAYSIAA12345201913RS2000
CP123AB2334CDF345JohnMALAYSIAA12345201913Cumulative Delta-1000
CP123AB2334CDF345JohnMALAYSIAA12345201914CRS2000
CP123AB2334CDF345JohnMALAYSIAA12345201914RS3000
CP123AB2334CDF345JohnMALAYSIAA12345201914Cumulative Delta0
CP123AB2334CDF345JohnMALAYSIAA12345201915CRS2000
CP123AB2334CDF345JohnMALAYSIAA12345201915RS2000
CP123AB2334CDF345JohnMALAYSIAA12345201915Cumulative Delta0
CP123AB2334CDF345JohnMALAYSIAA12345201916CRS2000
CP123AB2334CDF345JohnMALAYSIAA12345201916RS2000
CP123AB2334CDF345JohnMALAYSIAA12345201916Cumulative Delta0
CP123AB2334CDF345JohnMALAYSIAA12345201917CRS1000
CP123AB2334CDF345JohnMALAYSIAA12345201917RS1000
CP123AB2334CDF345JohnMALAYSIAA12345201917Cumulative Delta0
CDS345B234DBF889KarlGERMANYB98080201912CRS5000
CDS345B234DBF889KarlGERMANYB98080201912RS5000
CDS345B234DBF889KarlGERMANYB98080201912Cumulative Delta0
CDS345B234DBF889KarlGERMANYB98080201913CRS7500
CDS345B234DBF889KarlGERMANYB98080201913RS7500
CDS345B234DBF889KarlGERMANYB98080201913Cumulative Delta0
CDS345B234DBF889KarlGERMANYB98080201914CRS7500
CDS345B234DBF889KarlGERMANYB98080201914RS7500
CDS345B234DBF889KarlGERMANYB98080201914Cumulative Delta0
CDS345B234DBF889KarlGERMANYB98080201915CRS7500
CDS345B234DBF889KarlGERMANYB98080201915RS7500
CDS345B234DBF889KarlGERMANYB98080201915Cumulative Delta0
CDS345B234DBF889KarlGERMANYB98080201918CRS5000
CDS345B234DBF889KarlGERMANYB98080201918RS5000
CDS345B234DBF889KarlGERMANYB98080201918Cumulative Delta0

 

Dear @sunny_talwar, you can ignore the previous thread about grouping historical data, I'll close it. But do you have any ideas how to solve this one? Thanks! 

 

Anyone else who knows the solution, please help as well! 

Thanks in advanced 🙂 

1 Solution

Accepted Solutions
kaanerisen
Creator III
Creator III

Hi elintham,

How about this,

Main:
LOAD
    PN,
    MPN,
    CPN,
    Customer,
    Country,
    Family,
    OrderID,
    "Fiscal Week",
    BacklogType,
    "Sum(Qty)" AS QTY
FROM [lib://comm_help]
(html, utf8, embedded labels, table is @1);

Concatenate

Calculated_Delta:
LOAD
    PN,
    MPN,
    CPN,
    Customer,
    Country,
    Family,
    OrderID,
    "Fiscal Week",
    BacklogType,
    IF(Previous(OrderID)=OrderID,RangeSum(Peek(QTY),QTY),QTY) AS QTY;
LOAD
    PN,
    MPN,
    CPN,
    Customer,
    Country,
    Family,
    OrderID,
    "Fiscal Week",
    'Cumulative Delta' AS BacklogType,
    sum(IF(BacklogType='RS',QTY,-QTY)) AS QTY
Resident Main
Group by     
    PN,
    MPN,
    CPN,
    Customer,
    Country,
    Family,
    OrderID,
    "Fiscal Week";

Untitled.png

Hope it helps..

View solution in original post

19 Replies
jonathandienst
Partner - Champion III
Partner - Champion III

How about this:

Data:
LOAD
    PN,
    MPN,
    CPN,
    Customer,
    Country,
    Family,
    OrderID,
    "Fiscal Week" as FiscalWeek,
    BacklogType,
    "Sum(Qty)" as Qty
FROM [lib://Post1554649]
(html, utf8, embedded labels, table is @1);

Cumul:
LOAD OrderID,
    FiscalWeek,
    If(BacklogType = 'CRS', If(OrderID = Previous(OrderID), RangeSum(Peek(CumCRS), Qty), Qty), 0) as CumCRS,
    If(BacklogType = 'RS', If(OrderID = Previous(OrderID), RangeSum(Peek(CumRS), Qty), Qty), 0) as CumRS
Resident Data
Order By BacklogType, OrderID, FiscalWeek;

Cumul2:
LOAD OrderID,
    FiscalWeek,
    Sum(CumRS) - Sum(CumCRS) as Qty
Resident Cumul
Group By OrderID,
    FiscalWeek;

Left Join (Cumul2)
LOAD PN,   
	MPN,
    CPN,
    Customer,
    Country,
    Family,
    OrderID,
    FiscalWeek,
    'Cumulative Delta' as BacklogType
Resident Data
Where BacklogType = 'CRS';

Concatenate(Data)
LOAD * Resident Cumul2;

DROP Table Cumul, Cumul2;

Where connection Post1554649 points to https://community.qlik.com/t5/New-to-Qlik-Sense/Cumulative-Sum-in-Script/m-p/1554649 (this posting).

Logic will get you from a to b. Imagination will take you everywhere. - A Einstein
elintham
Contributor
Contributor
Author

Hi, 

I added your script under my existing script and dropped the final table where the ApplyMap is as well but am getting an "invalid expression" error in the data table portion. I have to retain the Mapping Load and ApplyMap portion in my script as the two Backlog Types were differentiated using the Mapping Load and ApplyMap script. Any ideas on why there is an error?

The below is my current script: 

Backlog_All:

Load

*,

FROM [Lib:xxx] (qvd);

CRS:
Mapping Load
OrderID,
CRS_WEEK
Resident Backlog_All;

RS:
Mapping Load
OrderID,
RS_WEEK
Resident Backlog_All;

Final_Backlog_All:
Load
*,
ApplyMap('CRS',OrderID, 'OTHER') as FiscalWeek,
'CRS' as BacklogType,
1 as Status
Resident Backlog_All;

Load
*,
ApplyMap('RS',OrderID, 'OTHER') as FiscalWeek,
'RS' as BacklogType,
0 as Status
Resident Backlog_All;

Backlog_Table:
Load
PN,
MPN,
CPN,
Customer,
Country,
FAMILY,
OrderID,
FiscalWeek,
BacklogType,
Sum(QTY) as QTY
Resident Final_Backlog_All;

Cum1:
Load
OrderID,
FiscalWeek,
If(BacklogType = 'CRS', If(OrderID = Previous(OrderID), RangeSum(Peek(CumCRS), Qty), Qty), 0) as CumCRS,
If(BacklogType = 'RS', If(OrderID = Previous(OrderID), RangeSum(Peek(CumRS), Qty), Qty), 0) as CumRS
Resident Backlog_Table
Order By BacklogType, OrderID, FiscalWeek;

Cum2:
LOAD
OrderID,
FiscalWeek,
Sum(CumRS) - Sum(CumCRS) as Qty
Resident Cum1
Group By OrderID,
FiscalWeek;

Left Join (Cum2)
LOAD
PN,
CPN,
MPN,
Customer,
Country,
FAMILY,
OrderID,
FiscalWeek,
'Cumulative Delta' as BacklogType
Resident Backlog_Table
Where BacklogType = 'CRS';

Concatenate(Backlog_Table)
LOAD * Resident Cum2;

Drop Tables Backlog_All, Final_Backlog_All, Cum1, Cum2;

 

 

Channa
Specialist III
Specialist III

try this  attach QVF

 


Main:
LOAD
PN,
MPN,
CPN,
Customer,
Country,
Family,
OrderID,
"Fiscal Week" as FiscalWeek,
BacklogType,
"Sum(Qty)" as Qty
FROM [lib://TEST/SampleData.xlsx]
(ooxml, embedded labels, table is Sheet1);

Concatenate(Main)
COLUMNS:
LOAD
PN,
MPN,
CPN,
Customer,
Country,
Family,
OrderID,
FiscalWeek,
'TCumulate' as BacklogType,
sum(if(BacklogType='CRS', Qty)) -
sum(if(BacklogType='RS', Qty)) as Qty
Resident Main
Group by PN,
MPN,
CPN,
Customer,
Country,
Family,
OrderID,
FiscalWeek,'TCumulate'
Order By PN,
MPN,
CPN,
Customer,
Country,
Family,
OrderID,
FiscalWeek;

 

Channa
kaanerisen
Creator III
Creator III

Hi elintham,

How about this,

Main:
LOAD
    PN,
    MPN,
    CPN,
    Customer,
    Country,
    Family,
    OrderID,
    "Fiscal Week",
    BacklogType,
    "Sum(Qty)" AS QTY
FROM [lib://comm_help]
(html, utf8, embedded labels, table is @1);

Concatenate

Calculated_Delta:
LOAD
    PN,
    MPN,
    CPN,
    Customer,
    Country,
    Family,
    OrderID,
    "Fiscal Week",
    BacklogType,
    IF(Previous(OrderID)=OrderID,RangeSum(Peek(QTY),QTY),QTY) AS QTY;
LOAD
    PN,
    MPN,
    CPN,
    Customer,
    Country,
    Family,
    OrderID,
    "Fiscal Week",
    'Cumulative Delta' AS BacklogType,
    sum(IF(BacklogType='RS',QTY,-QTY)) AS QTY
Resident Main
Group by     
    PN,
    MPN,
    CPN,
    Customer,
    Country,
    Family,
    OrderID,
    "Fiscal Week";

Untitled.png

Hope it helps..

elintham
Contributor
Contributor
Author

Hi Channa, 

 

Thanks for your attachment but I'm unable to open it. I'm using QlikSense web and desktop. 

elintham
Contributor
Contributor
Author

Hi Kaanerisen, 

Thank you for your reply. The screenshot of the output looks like what I need but I'm getting a synthetic key when I load the data following your script. Should I be dropping the main table? I tried that but I'm not getting the Cumulative Delta as an added Backlog Type if I do so. 

kaanerisen
Creator III
Creator III

No you shouldn't drop the main table. main table and calculated table should be concatenated. can you share your script and I can look into.

elintham
Contributor
Contributor
Author

Hi @jonathandienst

I re-tried this with the same sample date in Qlik Sense Desktop but i'm getting this output which isn't what I was looking for. Not sure if I've done something wrong or if this is actually the output that should be generated using your script? 

qlik sample.PNG

 

Channa
Specialist III
Specialist III

past your script hear

Channa