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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

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
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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