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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Reddyp
Helper I
Helper I

Inventory_stock

I need help in inventory calculation,
Example: I have received stock on 1st, 2nd, and 3rd and the stock is 10 shirts per day and total stock is 30 now 
and on 4th 15 shirts sold 

desired result: the sold count should deduct fromn first in date(the count sold is 15 on 4th so 10 should be decuted from 1st date and 5 shoud be deducted on 2nd)
and in the same way when 7 shirts sold on 5th date then 5 deducted from 2nd date (remaining in 2nd date) and 2 should deduct from 3rd date
 

DateNew StockStock outResult
01-01-202210 0
02-01-202210 0
03-01-202210 8
04-01-2022 15 
05-01-2022 7 

 

Simply, first out should be deducted from first in number 

1 ACCEPTED SOLUTION

@Reddyp Sorry about that, syntax issues:

Measure = 
  VAR __Date = MAX('Table'[Date])
  VAR __InTable = FILTER(ALL('Table'),[New Stock] <> BLANK())
  VAR __OutTable = FILTER(ALL('Table'),[Stock out] <> BLANK()) 
  VAR __Out = SUMX(__OutTable,[Stock out])
  VAR __ResultTable = 
    ADDCOLUMNS(
      ADDCOLUMNS(
        __InTable,
        "__RunningTotal",SUMX(FILTER(__InTable,[Date]<=__Date),[New Stock])
      ),
      "__WhileLoop",[__RunningTotal] - __Out
    )
  VAR __Result = MAXX(FILTER(__ResultTable,[Date] = __Date),[__WhileLoop])
RETURN
  IF(__Result <= 0,0,__Result)


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

4 REPLIES 4
Greg_Deckler
Community Champion
Community Champion

@Reddyp Try this:

Measure = 
  VAR __Date = MAX('Table'[Date])
  VAR __InTable = FILTER(ALL('Table',[New Stock] <> BLANK())
  VAR __OutTable = FILTER(ALL('Table',[Stock out] <> BLANK()) 
  VAR __Out = SUMX(__OutTable,[Stock out])
  VAR __ResultTable = 
    ADDCOLUMNS(
      ADDCOLUMNS(
        __InTable,
        "__RunningTotal",SUMX(FILTER(__InTable,[Date]<=__Date),[New Stock])
      ),
      "__WhileLoop",[__RunningTotal] - __Out
    )
  VAR __Result = MAXX(FILTER(__ResultTable,[Date] = __Date),[__WhileLoop])
RETURN
  IF(__Result <= 0,0,__Result)


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler Thank you for the reply, but I am facing issue while exicuting the measure 
Can you please recheck or can you please guide me to know my mistake in adding measure 

Reddyp_4-1648818195865.png

 

@Reddyp Sorry about that, syntax issues:

Measure = 
  VAR __Date = MAX('Table'[Date])
  VAR __InTable = FILTER(ALL('Table'),[New Stock] <> BLANK())
  VAR __OutTable = FILTER(ALL('Table'),[Stock out] <> BLANK()) 
  VAR __Out = SUMX(__OutTable,[Stock out])
  VAR __ResultTable = 
    ADDCOLUMNS(
      ADDCOLUMNS(
        __InTable,
        "__RunningTotal",SUMX(FILTER(__InTable,[Date]<=__Date),[New Stock])
      ),
      "__WhileLoop",[__RunningTotal] - __Out
    )
  VAR __Result = MAXX(FILTER(__ResultTable,[Date] = __Date),[__WhileLoop])
RETURN
  IF(__Result <= 0,0,__Result)


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler Thanks for the solution, need one more help 
I need to do this inventory calculation for individual branch.

Example: 

DateBranchNew StockStock outResult
01-01-2022Branch 110 0
02-01-2022Branch 110 0
03-01-2022Branch 110 8
04-01-2022Branch 1 15 
05-01-2022Branch 1 7 
06-01-2022Branch 210 0
07-01-2022Branch 210 5
08-01-2022Branch 210 10
09-01-2022Branch 2 15 
10-01-2022Branch 310 0
11-01-2022Branch 3 10 

 

and please share best source (website/books) to learn DAX 

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors