Skip to main content
Announcements
Global Transformation Awards! Applications are now open. Submit Entry
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Calculation using previous row value

Dear Experts

I need to calculate the column “Risky stock” as shown in the attached excel file.

The other columns I have them loaded from SQL.

Any help?

11 Replies
Not applicable
Author

have you tried making use of the above() function?

Not applicable
Author

The above function does mot reset when the item changes.

Not applicable
Author

If the above function can reset when the item changes please tell me how it will helps

Not applicable
Author

Could you include a sample qvw to show what your problem is?

preminqlik
Specialist II
Specialist II

ORGINAL_DATA:

LOAD *,RowNo() as Rowno INLINE [

    Item,Average Sales, Month, Stock

    Item 1, 200, 2, 150

    Item 1, 200, 3, 250

    Item 1, 200, 4, 1000

    Item 1, 200, 6, 100

    Item 1, 200, 7, 500

    Item 1, 200, 10, 750

    Item 1, 200, 12, 150

    Item 2, 100, 2, 10

    Item 2, 100, 10, 1200

    Item 2, 100, 2, 100

];

STAGE1:

Load *,

if(rowno()=1,if([P_Average Sales]=[Average Sales],1,Stock-(Month*[Average Sales])),

IF([Average Sales]=[P_Average Sales],IF(Peek('RISK_STOCK')<0,(Stock+Peek('RISK_STOCK'))-((Month-P_Month)*[Average Sales]),Stock-((Month-P_Month)*[Average Sales])),Stock-(Month*[Average Sales]))) as RISK_STOCK;

Load *,

Peek([Average Sales]) as [P_Average Sales],

peek(Month) as P_Month,

Peek(Stock) as P_Stock

Resident ORGINAL_DATA;

DROP table ORGINAL_DATA; !

Not applicable
Author

Hi

I used the syntax you proposed Prem Kumar Thangallapally .

I got strange numbers.

Prem Kumar Thangallapally  and Robert Daitzman please find attached the qvw file

Please if anyone can help.

Thank you.

preminqlik
Specialist II
Specialist II

try with this ..you have took Expdate in expression instead of Exp_month

Stock_New:

LOAD *,RowNo() as Rowno

Resident Stock ;

Drop Tables Stock;

STAGE1:

Load *,

rangesum(if(rowno()=1,if([P_AvrgSales]=[AvrgSales],1,Stock_Qty-(Exp_Month*[AvrgSales])),

IF([AvrgSales]=[P_AvrgSales],IF(Peek('RISK_Stock_Qty')<0,(Stock_Qty+Peek('RISK_Stock_Qty'))-((Exp_Month-P_Monthe)*[AvrgSales]),Stock_Qty-((Exp_Month-P_Exp_Month)*[AvrgSales])),Stock_Qty-(Exp_Month*[AvrgSales]))),0) as RISK_Stock_Qty;

Load *,

Peek([AvrgSales]) as [P_AvrgSales],

peek(Exp_Month) as P_Exp_Month,

rangesum(Peek(Stock_Qty),0) as P_Stock_Qty

Resident Stock_New;

DROP table Stock_New;

Not applicable
Author

Dear PREMinQLIK

Thank you for your help but I am still getting wrong numbers.

I used the below script:

Stock_New:

LOAD *,RowNo() as Rowno

Resident Stock ;

Drop Tables Stock;

STAGE1:

Load *,

rangesum(if(rowno()=1,if([P_AvrgSales]=[AvrgSales],1,Stock_Qty-(Exp_Month*[AvrgSales])),

IF([AvrgSales]=[P_AvrgSales],IF(Peek('RISK_Stock_Qty')<0,(Stock_Qty+Peek('RISK_Stock_Qty'))

-((Exp_Month-[P_Exp_Month])*[AvrgSales]),Stock_Qty-((Exp_Month-[P_Exp_Month])*[AvrgSales])),Stock_Qty-(Exp_Month*[AvrgSales]))),0) as RISK_Stock_Qty;

Load *,

Peek([AvrgSales]) as [P_AvrgSales],

peek([Exp_Month]) as [P_Exp_Month],

Peek(Stock_Qty) as P_Stock_Qty

Resident Stock_New;

DROP table Stock_New;

and got the below as result (it is only a small part of the data)

SKUExDateExp_MonthRisky StTotal Stk
ST00004601-07-157167127
ST00004618-07-15744
ST00004601-08-158154180
ST00004701-06-15610666
ST00004706-06-1563434
ST00004701-08-158100180
ST00004801-08-158-327153
ST00005401-07-15733
ST00005712-06-156-7282,872
ST00005801-06-156-2550150
ST00005811-06-156-7261,824
ST00005901-06-156-5092,791
ST00005902-06-156-48524
STFRSWTDB0250G012ASS04-07-157-2601
STLBTSTDB0017G024CHC08-01-151-2491
STNGTSTBG0100G012GLF28-12-1412-9520
STSTBSDAF0190G012GRB02-03-153-17693
STSTBSDSHGFPRO002ASS21-08-15877
STSTBSDSHGFPRO002ASS01-09-15901
STSTCDTDB0080G006COC01-03-153-2271
STSTCOKAF0085G012ORG01-03-1537472
STSTJAMJR0325G012APR01-09-159-57159
STSTOBRAF0250G012PLN14-07-157-13010
STSTRCKBG0130G012CHC20-04-154-1591
STSTRCKBG0130G012CHC17-06-156-2390
STSTRCKBG0130G012CHC21-07-157-18593
STSTRCKPK0120G012CHC22-07-15708
STSTSYDDB0001L012SOY20-06-156-530130
STSTTSTBG0090G012SOY01-07-157-10659
STSTTSTBG0090G012SOY10-07-157-820246
STSTTSTBG0100G012ASS28-02-152-3510
STSTTSTBG0130G012CRL01-07-157-1047246
STSTTSTBG0130G012CRL07-07-157-900147
STSTTSTBG0130G012NST01-07-157-102658
STSTTSTBG0130G012RCE01-07-157-1584297
STSTTSTCR0100G012MDT15-07-157-87166
STSTTSTDC0250G012BRT01-01-151-441
STSTWFRAF0160G012VNL01-06-156-21525
STWFWFBAF0034G024CCN02-06-156936936
STWFWFBAF0034G024CCN01-10-1610106106
STWFWFBAF0034G024COC14-04-154-1361839

Not applicable
Author

I think the problem is that the value is not resetting for new item.