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
Reddyp
Helper I
Helper I

Inventory Stock_Balance and cost calculation_DAX

Hi, 

I have some store data and need DAX to calculate remaining stock in iventory along with purchase cost

Example: 

Reddyp_0-1651735798312.jpeg

In this table I have two Branches and each has two procucts, On Jan the total purchase of I10 product in Branch1 is 10 and cost per unit is $21. when there us no sale of I20 on Jan in the Branch1 then this count should come under "Last month remaining quantity" on Feb
Note: Out of the 10 "I10" products if 2 sold on Jan then the remaining count "8" should come under "Last month remaining quantity" on Feb along with purchased cost

DAX should get result into "Last month remaining quantity" after filtering Branch and Products

ProductBranchMonthTypeQuantity AmountLast Month remaining quantityPrice per Unit
I10Branch1JanPurchase1021  
I10Branch1FebPurchase10491021
I10Branch1MarPurchase10291049
I10Branch1AprPurchase10461029
I20Branch1MayPurchase1047  
I20Branch1JunePurchase10221047
I20Branch1JulyPurchase10171022
I20Branch1AugPurchase10141017
I10Branch2JanPurchase1030  
I10Branch2FebPurchase10401030
I10Branch2MarPurchase10231040
I10Branch2AprPurchase10401023
I20Branch2MayPurchase1029  
I20Branch2JunPurchase10311029
I20Branch2JulPurchase10281031
I20Branch2AugPurchase10311028


Thank you!

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi Reddy

Not sure how the real data looks like and some info is missing such as the the quantity sold. However, based on the available information and data:

1st you need to have a month number column. Create a new column 

 

Month Number =
VAR CurrentMonth = Stores[Month]
RETURN
    SWITCH (
        TRUE (),
        CurrentMonth = "Jan", 1,
        CurrentMonth = "Feb", 2,
        CurrentMonth = "Mar", 3,
        CurrentMonth = "Apr", 4,
        CurrentMonth = "May", 5,
        CurrentMonth = "June", 6,
        CurrentMonth = "July", 7,
        CurrentMonth = "Aug", 8,
        CurrentMonth = "sep", 9,
        CurrentMonth = "Oct", 10,
        CurrentMonth = "Nov", 11,
        CurrentMonth = "Dec", 12
    )

 

For last month quantity create a new column

 

Last Month remaining quantity =
VAR StoreBranchTable =
    CALCULATETABLE ( Stores, ALLEXCEPT ( Stores, Stores[Product], Sores[Branch] ) )
VAR CurrentMonthNum = Stores[Month Number]
RETURN
    MINX (
        FILTER ( StoreBranchTable, Stores[Month Number] = CurrentMonthNum - 1 ),
        Stores[Quantity]
    )

 

For last month amont create a new column

 

Price per Unit =
VAR StoreBranchTable =
    CALCULATETABLE ( Stores, ALLEXCEPT ( Stores, Stores[Product], Sores[Branch] ) )
VAR CurrentMonthNum = Stores[Month Number]
RETURN
    MINX (
        FILTER ( StoreBranchTable, Stores[Month Number] = CurrentMonthNum - 1 ),
        Stores[Amount]
    )

 

 

View solution in original post

1 REPLY 1
tamerj1
Super User
Super User

Hi Reddy

Not sure how the real data looks like and some info is missing such as the the quantity sold. However, based on the available information and data:

1st you need to have a month number column. Create a new column 

 

Month Number =
VAR CurrentMonth = Stores[Month]
RETURN
    SWITCH (
        TRUE (),
        CurrentMonth = "Jan", 1,
        CurrentMonth = "Feb", 2,
        CurrentMonth = "Mar", 3,
        CurrentMonth = "Apr", 4,
        CurrentMonth = "May", 5,
        CurrentMonth = "June", 6,
        CurrentMonth = "July", 7,
        CurrentMonth = "Aug", 8,
        CurrentMonth = "sep", 9,
        CurrentMonth = "Oct", 10,
        CurrentMonth = "Nov", 11,
        CurrentMonth = "Dec", 12
    )

 

For last month quantity create a new column

 

Last Month remaining quantity =
VAR StoreBranchTable =
    CALCULATETABLE ( Stores, ALLEXCEPT ( Stores, Stores[Product], Sores[Branch] ) )
VAR CurrentMonthNum = Stores[Month Number]
RETURN
    MINX (
        FILTER ( StoreBranchTable, Stores[Month Number] = CurrentMonthNum - 1 ),
        Stores[Quantity]
    )

 

For last month amont create a new column

 

Price per Unit =
VAR StoreBranchTable =
    CALCULATETABLE ( Stores, ALLEXCEPT ( Stores, Stores[Product], Sores[Branch] ) )
VAR CurrentMonthNum = Stores[Month Number]
RETURN
    MINX (
        FILTER ( StoreBranchTable, Stores[Month Number] = CurrentMonthNum - 1 ),
        Stores[Amount]
    )

 

 

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.

Top Solution Authors