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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

Seek help in recursive inventory dax

Hi,

 

I would like to seek for help in creating recursive inventory stock logic.

Currently this is the example table we have.

ProductCountryYear/MonthInitial StockStock on handintransitdemandBalance
AABC2022/1001005030120
AABC2022/11120305030170
AABC2022/12170305030220


We would like to create 2 recursive dax
1. Balance = initial stock + stock on hand + intransit - demand
2. Initial stock = (last month's) Balance + stock on hand + intransit - demand

The known data in the table are
1. Initial stock of 2022/10 : 0 (as the starting point)
2. Stock on hand, intransit, demand, product, country in all periods

Thanks for your help.

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

please check the below picture and the attached pbix file.

 

Jihwan_Kim_0-1666766658179.png

 

Balance measure: = 
CALCULATE (
    SUMX ( Data, Data[Stock on hand] + Data[Intransit] - Data[Demand] ),
    FILTER ( ALL ( Data ), Data[Date] <= MAX ( Data[Date] ) ),
    VALUES ( Data[Country] ),
    VALUES ( Data[Product] )
)

 

Initial stock measure: =
VAR _previousdate =
    MAXX (
        FILTER (
            ALL ( Data ),
            Data[Product] = MAX ( Data[Product] )
                && Data[Country] = MAX ( Data[Country] )
                && Data[Date] < MAX ( Data[Date] )
        ),
        Data[Date]
    )
RETURN
    IF (
        SELECTEDVALUE ( Data[Date] ) = DATE ( 2022, 10, 1 ),
        0,
        CALCULATE (
            [Balance measure:],
            FILTER ( ALL ( Data ), Data[Date] = _previousdate ),
            VALUES ( Data[Country] ),
            VALUES ( Data[Product] )
        )
    )

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Thanks. It is very helpful.

Jihwan_Kim
Super User
Super User

Hi,

please check the below picture and the attached pbix file.

 

Jihwan_Kim_0-1666766658179.png

 

Balance measure: = 
CALCULATE (
    SUMX ( Data, Data[Stock on hand] + Data[Intransit] - Data[Demand] ),
    FILTER ( ALL ( Data ), Data[Date] <= MAX ( Data[Date] ) ),
    VALUES ( Data[Country] ),
    VALUES ( Data[Product] )
)

 

Initial stock measure: =
VAR _previousdate =
    MAXX (
        FILTER (
            ALL ( Data ),
            Data[Product] = MAX ( Data[Product] )
                && Data[Country] = MAX ( Data[Country] )
                && Data[Date] < MAX ( Data[Date] )
        ),
        Data[Date]
    )
RETURN
    IF (
        SELECTEDVALUE ( Data[Date] ) = DATE ( 2022, 10, 1 ),
        0,
        CALCULATE (
            [Balance measure:],
            FILTER ( ALL ( Data ), Data[Date] = _previousdate ),
            VALUES ( Data[Country] ),
            VALUES ( Data[Product] )
        )
    )

 


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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