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

Announcements
Join us to spark ideas for how to put the latest capabilities into action. Register here!
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Open and Close values for each month.

Hi all,

Have been trying to figure this out for a while. I have following load script

TEMP_HIST:

LOAD ID,

    [Date],

    Year([Date]) as Hist.Year,

    Month([Date]) as Hist.Month,

    Style,

    Color,

    Size,

    Bucket,

    Units,

    Dollars,

    Markdown,

    Deval,

    Style & '_' & Color & '_' & Size & '_' & Bucket as Hist.Key ;

SQL SELECT *

FROM StockOnHand;

and I want to generate table where Opening and Closing Qty are calculated from Units. For each month the Closing Qty is sum(Units) and Opening Qty is the colsing Qty for previous month. I tried using Order By and Sort by so that I can use FirstSortedValue(Units, -Date) but thats not working. Is there an easlier way to get this.

Is it possible to used a set analysis to almost reference the previous month for each of the months. For example if Month is dimension then in expression have something like sum({1<Month = {$(=addMonth([Date],-1))}, Month=>} Units)?

MonthJanJanFebFebMarMarAprAprMayMayJunJun
YearStyleOpeningClosingOpeningClosingOpeningClosingOpeningClosingOpeningClosingOpeningClosing
20127184N_179_M-M_2504605605504504349349504504203203504
20127184N_989_XL-TG_42522222222522521001002522523232252
20127184N_Y57_M-M_235934343593592332333593593333432

Thanks,

G

1 Solution

Accepted Solutions
Not applicable
Author

Hi try the below script:

TEMP_HIST:

LOAD ID,

    [Date],

    Year([Date]) as Hist.Year,

    Month([Date]) as Hist.Month,

    Style,

    Color,

    Size,

    Bucket,

    Units,

    Dollars,

    Markdown,

    Deval,

    Style & '_' & Color & '_' & Size & '_' & Bucket as Hist.Key ;

SQL SELECT *

FROM StockOnHand;

Join

TEMP_HIST:

LOAD ID,

    AddMonths([Date],1) as [Date],

    Year(AddMonths([Date],1)) as Hist.Year,

    Month(AddMonths([Date],1)) as Hist.Month,

    Style,

    Color,

    Size,

    Bucket,

    Units as OpeningStock,

    Dollars,

    Markdown,

    Deval,

    Style & '_' & Color & '_' & Size & '_' & Bucket as Hist.Key ;

SQL SELECT *

FROM StockOnHand;

If Oracle is the backend, lag function which is easier to maintain. It goes like this:

Select date, units as ClosingStock,lag(units,1,0) over (partition by style,Color,Size order by style,Color,Size,date) as OpeningStock from StockOnHand;

Kiran Rokkam.

View solution in original post

7 Replies
Not applicable
Author

Use above(Closing) for opening value.

Regards,

Kiran Rokkam.

Not applicable
Author

Hi Kiran,

This doesn't work when I create a table with sums. They Hist.Key not used as dimension. Essentially I'm trying to whats in the image.

G

Untitled.jpg

Not applicable
Author

Grifter,

If you have only one record every month, just add one more field (Opening_units) in the script for getting the opening balance as last month closing balance. This would give more flexibility for writing closing/ opening inventory expressions.

PS: I am not able see any image.

Kiran.

Not applicable
Author

Hi,

The problem is there are multiple records I tried to create a Hist.Key that would make the records uniq and I think it does. However I'm not sure how I can get the previous months value in the load script.

Untitled.jpg

Grif

Not applicable
Author

Hi try the below script:

TEMP_HIST:

LOAD ID,

    [Date],

    Year([Date]) as Hist.Year,

    Month([Date]) as Hist.Month,

    Style,

    Color,

    Size,

    Bucket,

    Units,

    Dollars,

    Markdown,

    Deval,

    Style & '_' & Color & '_' & Size & '_' & Bucket as Hist.Key ;

SQL SELECT *

FROM StockOnHand;

Join

TEMP_HIST:

LOAD ID,

    AddMonths([Date],1) as [Date],

    Year(AddMonths([Date],1)) as Hist.Year,

    Month(AddMonths([Date],1)) as Hist.Month,

    Style,

    Color,

    Size,

    Bucket,

    Units as OpeningStock,

    Dollars,

    Markdown,

    Deval,

    Style & '_' & Color & '_' & Size & '_' & Bucket as Hist.Key ;

SQL SELECT *

FROM StockOnHand;

If Oracle is the backend, lag function which is easier to maintain. It goes like this:

Select date, units as ClosingStock,lag(units,1,0) over (partition by style,Color,Size order by style,Color,Size,date) as OpeningStock from StockOnHand;

Kiran Rokkam.

Not applicable
Author

Thanks Kiran. The data is coming from an Access DB. I will try to load and let you know how it goes.

Best regards,

G

Not applicable
Author

That did the trick! Thanks!

G