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

Measure to estimate loss of sales because of stock out

Hello all,

 

I have a complex problem that i have been struggling with to find a solution, so i hope someone here can help me out with this.

 

I have a date table and 2 fact tables, one with sales and one with inventory.

We have multiple stores and each store get one entry per day in the inventory table for each product they have in stock, if they dont have a product in stock there is no entry for that, so no "0 stock" entry.

Likewise the sales table only get transactons in it, so there is no "0 sales" entrys there either, just missing rows for the product if it didnt sold that day.

 

What i would like to do is

1:  check when a store have a missing entry for a product in both of those tables, to confirm a stockout that day

2: estimate missing sales due to that stockout based on the sales before and after the stockout occured.

 

Sample of sales table:

sale_dateproduct_idquantitystore_id
2019-01-011101
2019-01-011112
2019-01-011103
2019-01-01271
2019-01-012132
2019-01-013112
2019-01-013103
2019-01-02111
2019-01-02122
2019-01-02183
2019-01-022101
2019-01-022112
2019-01-02382

 

Sample of inventory table:

dateproduct_idquantitystore_id
2019-01-011301
2019-01-011202
2019-01-011353
2019-01-012201
2019-01-012252
2019-01-013221
2019-01-013232
2019-01-013223
2019-01-021201
2019-01-02192
2019-01-021253
2019-01-022131
2019-01-022122
2019-01-023122

 

So the following products and dates should be marked as stockout, could be a measure, a column or a table:

dateproduct_idstore_idStockout
2019-01-0123TRUE
2019-01-0223TRUE
2019-01-0231TRUE
2019-01-0233TRUE

 

Is this possible to achieve?

9 REPLIES 9
v-lionel-msft
Community Support
Community Support

Hi @Trolleri ,

 

Maybe you can try to do like this:

Number_store = 
CALCULATE(
    DISTINCTCOUNT(Inventory[store_id]),
    ALLEXCEPT(
        Inventory,
        Inventory[date], Inventory[product_id]
    )
)
Sum_store = 
CALCULATE(
    SUM(Inventory[store_id]),
    ALLEXCEPT(
        Inventory,
        Inventory[date], Inventory[product_id]
    )
)
Store_id(measure) = 
VAR x = 
CALCULATE(
    MAX(Inventory[store_id]),
    ALL(Inventory)
)

RETURN
SWITCH(
    [Number_store],
    x, BLANK(),
    x-1, 6-[Sum_store],
    x-2, 6-[Sum_store]
)
Stockout = 
IF(
    [Store_id(measure)] <> BLANK(),
    "True",
    BLANK()
)

ii4.PNG

 

Best regards,
Lionel Chen

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

Mariusz
Community Champion
Community Champion

Hi @Trolleri 


Sure, Please see the attached solution with the below Measure that later is used to filter the table 

check = 
VAR __date = MAX( 'calendar'[Date] ) <= CALCULATE( MAX( sales[sale_date] ), ALL( sales ) )
RETURN 
    INT( ISEMPTY( sales ) && ISEMPTY( inventory ) &&  __date )

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
LinkedIn

 

Hi @Mariusz ,

 

The formula works, but it always returns 0.

Im not sure what it is supposed to return?

Mariusz
Community Champion
Community Champion

Hi @Trolleri 

 

Have you checked the attached file?

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
LinkedIn

 

Hi @Mariusz ,

 

Now i found out why it didnt worked for me, you had a +0 in your sums so now its displaying correctly for me in that type of visual, thank you.

 

But if i want to count those rows where the filter is 1, how do i do that?

I want to figure out how many days in % that had stockouts for each product, i have tried to count rows with that filter but it doesnt return anything if i set the filter to 1.

Mariusz
Community Champion
Community Champion

Hi @Trolleri 

 

Please see the attached file with adjusted code to return Total Count 

 

Best Regards,
Mariusz

If this post helps, then please consider Accepting it as the solution.

Please feel free to connect with me.
LinkedIn

 

@Trolleri , if @Mariusz 's solution does not work, the mark both of us @. We will check again.

amitchandak
Super User
Super User

On 2019-01-01, how do we know sale is lost, when these no record in sales. Because there is no inventory record for that day ??

@Trolleri 

Something like


except(
crossjoin(distinct(sales[date]),distinct(sales[product_id]),distinct(sales[store_id])),
selectcolumns(inventory,"date",inventory[date],"product_id",inventory[product_id],"store_id",inventory[store_id]
))

Hi @amitchandak, ,

 

Thank you for your swift reply!

 

Yes, we assume sale is lost because there is no inventory record that day.

 

Unfortunately that formula doesnt seem to work, i get this error:

"The expression refers to multiple columnsMultiple columns cannot be converted to a scalar value"

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.