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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
RettT
Regular Visitor

DAX measure to calculate the number of discrete times an item was stocked out over a period of time

Hi,

I'm looking for help with writing a measure that will calculate the number of times an item went out of stock during a time period. Table example is below. The expected result for this would be 3 discrete stock outs. 

Thanks!

DateStock outComment
1/1/20230In stock
1/2/20231Out of stock
1/3/20230 In stock
1/4/20231Out of stock
1/5/20231Out of stock
1/6/20230In stock
1/7/20231Out of stock
1/8/20231Out of stock
1/9/20231Out of stock
1/10/20230In stock
1/11/20230In stock
2 ACCEPTED SOLUTIONS
barritown
Super User
Super User

Hi @RettT,

 

I'd add an auxiliary column with a flag showing that the status of the column "Stock out" changed from 0 to 1. Then you could sum this column to obtain the necessary value.

 

Below you can find this solution packed into a measure. I used a slightly updated dataset as you can see.

 

barritown_0-1694701189125.png

 

The very same measure in plain text:

Measure = 
VAR _ExtData = 
    ADDCOLUMNS ( Data,
    "@Trigger",
    VAR CurrentItem = [Item]
    VAR CurrentDate = [Date]
    VAR PreviousDate = MAXX ( FILTER ( Data, [Item] = CurrentItem && [Date] < CurrentDate ), [Date] )
    VAR PreviousValue = MINX ( FILTER ( Data, [Item] = CurrentItem && [Date] = PreviousDate ), [Stock out] ) + 0
    RETURN IF ( [Stock out] - PreviousValue = 1, 1, 0 ) )
RETURN SUMX ( _ExtData, [@Trigger] )

 

Best Regards,

Alexander

My YouTube vlog in English

My YouTube vlog in Russian

View solution in original post

This worked great and will be very helpful. I very much appreciate your time and efforts!

View solution in original post

2 REPLIES 2
barritown
Super User
Super User

Hi @RettT,

 

I'd add an auxiliary column with a flag showing that the status of the column "Stock out" changed from 0 to 1. Then you could sum this column to obtain the necessary value.

 

Below you can find this solution packed into a measure. I used a slightly updated dataset as you can see.

 

barritown_0-1694701189125.png

 

The very same measure in plain text:

Measure = 
VAR _ExtData = 
    ADDCOLUMNS ( Data,
    "@Trigger",
    VAR CurrentItem = [Item]
    VAR CurrentDate = [Date]
    VAR PreviousDate = MAXX ( FILTER ( Data, [Item] = CurrentItem && [Date] < CurrentDate ), [Date] )
    VAR PreviousValue = MINX ( FILTER ( Data, [Item] = CurrentItem && [Date] = PreviousDate ), [Stock out] ) + 0
    RETURN IF ( [Stock out] - PreviousValue = 1, 1, 0 ) )
RETURN SUMX ( _ExtData, [@Trigger] )

 

Best Regards,

Alexander

My YouTube vlog in English

My YouTube vlog in Russian

This worked great and will be very helpful. I very much appreciate your time and efforts!

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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