Skip to main content
Announcements
Qlik Introduces a New Era of Visualization! READ ALL ABOUT IT
cancel
Showing results for 
Search instead for 
Did you mean: 
rajat2392
Partner - Creator III
Partner - Creator III

Calculate Opening Balance in Load script

Hi Everyone,

I am stuck in a situation for calculating Closing Stock month on month in load script.

Below is the scenario.

YearMonthSalesProduction
2016Jan10001500
2016Feb8001300
2016Mar10001400
2016Apr9001500
2016May5001400

Stock in hand at the start of the year is 0.

I need the below output.

YearMonthSalesProductionOpening
2016Jan100015000
2016Feb8001300500
2016Mar100014001000
2016Apr90015001400
2016May50014002000

Please some one help me with the script.

I tried a lot but it isn't happening.

Thanks in Advance

Rajat Arora

1 Solution

Accepted Solutions
Kushal_Chawda

Data:
LOAD *,
Date(Date#(Year&Month,'YYYYMMM'),'YYYYMM') as MonthYear;
LOAD Year,
Month,
Sales,
Production
FROM
[https://community.qlik.com/thread/218884]
(
html, codepage is 1252, embedded labels, table is @1);


New:
LOAD *,
if(RowNo()=1,0, RangeSum((Previous(Production)-Previous(Sales)),Peek('Opening'))) as Opening
Resident Data
Order By MonthYear;

DROP Table Data;

View solution in original post

6 Replies
Oleg_Troyansky
Partner Ambassador/MVP
Partner Ambassador/MVP

Hi Rajat,

I'm describing exactly this process in my book QlikView Your Business - check it out!

best,

Oleg Troyansky

Kushal_Chawda

Data:
LOAD *,
Date(Date#(Year&Month,'YYYYMMM'),'YYYYMM') as MonthYear;
LOAD Year,
Month,
Sales,
Production
FROM
[https://community.qlik.com/thread/218884]
(
html, codepage is 1252, embedded labels, table is @1);


New:
LOAD *,
if(RowNo()=1,0, RangeSum((Previous(Production)-Previous(Sales)),Peek('Opening'))) as Opening
Resident Data
Order By MonthYear;

DROP Table Data;

miguelbraga
Partner - Specialist III
Partner - Specialist III

Hey Rajat,

See my qvw

Regards,

MB

sunny_talwar

May be this:

Table:

LOAD *,

  Date(Date#(Month & '-' & Year, 'MMM-YYYY'), 'MMM-YYYY') as MonthYear;

LOAD Year,

    Month,

    Sales,

    Production

FROM

[https://community.qlik.com/thread/218884]

(html, codepage is 1252, embedded labels, table is @1);

FinalTable:

LOAD *,

  If(RowNo() = 1, 0, RangeSum(Peek('Opening'), Peek('Production'), -Peek('Sales'))) as Opening

Resident Table

Order By MonthYear;

DROP Table Table;

petter
Partner - Champion III
Partner - Champion III

If your source records are already sorted by date and aggregated monthly this load statement should do the calculation for you:

LOAD

  Year, Month, Sales, Production,

  If ( RecNo () = 1, 0 , Peek('Opening')-Peek ('Sales')+Peek ('Production') ) AS Opening

FROM

  .......;

kashifsaifi
Contributor
Contributor

Hi There,

What if I want to break it down by year. Meaning, for 2017 onwards, the opening balance should start with 0.