Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
spandiar
Frequent Visitor

Subtract Qty of previous day with qty of current day-dax

Hi,

I have 3 tables. 
1)

Stock Month

 which store monthly stock on hand qty for item

 

ItemAccDateQty
P131/01/2023300
P128/02/2023200

 

 

 

2) Stock Daily -Which store daily transaction of the item 

ItemAccDateQty
P102/02/2023-20
P103/02/20235
P104/02/20236
P105/02/20237
P106/02/20234
P107/02/20238
P108/02/2023-20
P109/02/20239
P110/02/20231
P111/02/20231
P112/02/2023-6
P113/02/202315
P114/02/20234
P115/02/2023-11
P116/02/202310
P117/02/2023-20
P118/02/20235
P119/02/20236
P120/02/20237
P121/02/20234
P122/02/20238
P123/02/2023-20
P124/02/20239
P125/02/20231
P126/02/20233
P127/02/2023-6
P128/02/20235
P101/03/202366
P102/03/2023-8
P103/03/2023-6
P104/03/2023-9
P105/03/20238
P106/03/20234
P107/03/20235
P108/03/20236
P109/03/20233
P110/03/20234
P111/03/20237

 

3) Date table

 

I want get daily stock balance.
where I need subtract qty of stock month for the item with qty of  Stock Daily for the item. So for 28/02/2023 stock balance will 195 (200-5). on 27/02/2023 it will StockBalance of 28th with stock qty of 27th. it will be 201 (195-(-6)) as stock qty on 27th is -6. and it will reverse calculation it 31/01/2023.
Required result

ItemAccDateQtyStockQty
P1

 

01/02/2023

10175
P102/02/2023-20185
P103/02/20235165
P104/02/20236170
P105/02/20237176
P106/02/20234183
P107/02/20238187
P108/02/2023-20195
P109/02/20239175
P110/02/20231184
P111/02/20231185
P112/02/2023-6186
P113/02/202315180
P114/02/20234195
P115/02/2023-11199
P116/02/202310188
P117/02/2023-20198
P118/02/20235178
P119/02/20236183
P120/02/20237189
P121/02/20234196
P122/02/20238200
P123/02/2023-20208
P124/02/20239188
P125/02/20231197
P126/02/20233198
P127/02/2023-6201
P128/02/20235195
P101/03/202366129
P102/03/2023-8137
P103/03/2023-6143
P104/03/2023-9152
P105/03/20238144
P106/03/20234140
P107/03/20235135
P108/03/20236129
P109/03/20233126
P110/03/20234122
P111/03/20237115

 

I am adding multiple calculated colum in Stock Daily table but its ging circular dependency error. I do have dates table and bot the table is join with date table.


1st Calculated column

StockBalance for Month =
Var DMnth = ENDOFMONTH('Stock Daily'[AccDate])
Var HStockdate =ENDOFMONTH('Stock Month'[AccDate])
Var OpeningBal =
LOOKUPVALUE('Stock Daily'[Qty],'Stock Month'[Item],'Stock Daily'[Item],'Stock Month'[AccDate],DMnth)
Return
IF (HStockdate =DMnth, OpeningBal,BLANK())

2nd Calculated Column
Closing Balance =
Var vAccEOM =ENDOFMONTH('Stock Daily'[AccDate])
Var vAccDTCurrent = CALCULATE(SELECTEDVALUE('Stock Daily'[AccDate]))
VAR vBal='Stock Daily'[StockBalance for Month]-Stock Daily'[Qty]
var ClosingBal =IF (vAccEOM=vAccDTCurrent,vBal,BLANK())
Return
ClosingBal

3rd Calcuted Column
PreviousDay=PREVIOUSDAY('Stock Daily'[AccDate])

4th Calculated Column
Opening Balance =
Var PQty = CALCULATE(SELECTEDVALUE('Stock Daily'[Qty]),
               FILTER('Stock Daily','Stock Daily'[AccountingDate]='Stock Daily'[PreviousDay]))
Return PQty


Can you please help me. 

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

Write these calculated column formulas

Stock = LOOKUPVALUE(Stock[Qty],Stock[AccDate],Data[AccDate],Stock[Item],Data[Item])
Date of stock availability = CALCULATE(MAX(Data[AccDate]),FILTER(Data,Data[Item]=EARLIER(Data[Item])&&Data[Stock]>0))
Cumulative qty = CALCULATE(SUM(Data[Qty]),FILTER(Data,Data[Item]=EARLIER(Data[Item])&&Data[AccDate]<=Data[Date of stock availability]&&Data[AccDate]>=EARLIER(Data[AccDate])))
Stock value = CALCULATE(sum(Data[Stock]),FILTER(Data,Data[Item]=EARLIER(Data[Item])&&Data[Stock]>0))
Bal = Data[Stock value]-Data[Cumulative qty]

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

7 REPLIES 7
Ashish_Mathur
Super User
Super User

Hi,

Write these calculated column formulas

Stock = LOOKUPVALUE(Stock[Qty],Stock[AccDate],Data[AccDate],Stock[Item],Data[Item])
Date of stock availability = CALCULATE(MAX(Data[AccDate]),FILTER(Data,Data[Item]=EARLIER(Data[Item])&&Data[Stock]>0))
Cumulative qty = CALCULATE(SUM(Data[Qty]),FILTER(Data,Data[Item]=EARLIER(Data[Item])&&Data[AccDate]<=Data[Date of stock availability]&&Data[AccDate]>=EARLIER(Data[AccDate])))
Stock value = CALCULATE(sum(Data[Stock]),FILTER(Data,Data[Item]=EARLIER(Data[Item])&&Data[Stock]>0))
Bal = Data[Stock value]-Data[Cumulative qty]

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Thanks a ton Ashish for the solution. Its working fine for Feb month. But its not working March and Jan month. 

If Bal is 210 on 28/02/2023, Qty =-10 on 01/03/2023 then Balance should be 220 ond so on

You are welcome.  I do not understand what you mean.  Share the PBI file, show the problem and the expected result.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Access denied message.  Show the problem there clearly.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi Ashish, I have given access to the report. Bal is calculating correct for Feb and Jan. But for March its not. on 28/02/2023 I have qty from Stock table is 103, to calulate back dated qty we have subtracted 103 with transacation of 28th i.e. 37. So opening bal for 27th 140.
for 1/03/2023 it should be 103-13 =90. 
Thanks in Advance.

Hi,

See if revising the Cumulative Qty formula to this reolves the issue

Cumulative qty = CALCULATE(SUM(Data[Qty]),FILTER(Data,Data[Item]=EARLIER(Data[Item])&&Data[AccDate]>Data[Date of stock availability]&&Data[AccDate]<=EARLIER(Data[AccDate])))

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.