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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

Maximum/Average consecutive number of days that a value drops below zero

Hi guys, 

 

currently I'm working on a detailled inventory report. 

Basically, my data consists of two tables:

- Table A: Information about the stock level s of product p at company c at date d

DateProductCompanyStock
01-01-2020xAmazon100
02-01-2020xAmazon40
03-01-2020xAmazon60
01-01-2020yWalmart70

 

- Table B: Information about the demand r of product at companyat date d.

 

DateProductCompanyDemandIdQuantity
01-01-2020xAmazon150
01-01-2020xAmazon240
02-01-2020yAmazon360

 

Of course I could add the 'Demand' as additional column for table A, however I want to have all the calculations done dynamically so I can filter out certain types of demand. 

 

First of all, I created the net-stock per date, product and company. Basically, this is just an easy measure: 

NetStock := SUM(Stock) - SUM(Quantity)

 

Next to that, I want to create a KPI that measures the number of occassions that the stock drops below 0 (and we are basically out of stock). As aggregating all the products and companies into one sum does not work, I decided to determine this on the lowest level:

 

 

 

StockOutOccasions = 
var JoinedTable =
SUMMARIZE(
    'Table_A';
    'Table_A'[Company];
    'Table_A'[Product];
    'Table_A'[Date];
    "OutOfStock"; IF([NetStock] < 0; 1; 0)
)
return SUMX(JoinedTable; [OutOfStock])

 

 

 

 

Similair measures have been created to measure the stock deficit, probability of stock-out etc.

 

My final KPI is to represent the duration of a stock-out, both the maximum and average duration. 

However, I'm quite struggling how to do this, as I use a measured table in previous calculations.

My expectations are for the following measured table:

DateProductCompanyNetStock
01-01xy-10
02-01xy-15
03-01xy-10
04-01xy5
05-01xy-20
06-01xy10

Maximum duration: 3 (=> 01-01 untill 03-01)

Average duration: 2 (=> (3+1)/2 )

 

I had the idea to create an additional 'column' which indicates the number of consecutive days that the netstock < 0, and 0 if the netstock >= . This would result in the following:

Date01-0102-0103-0104-0105-0106-01
x123010

 

Then the maxium duration would just be the maximum value of this column.

The average can be determined as: COUNT(x>0)/COUNT(x=1)

 

So, the idea is there, I tried a couple of things, but I'm mainly stuck on the restrictions of the measured table. 

Can anybody help me how to create the column x? (See the last matrix)

I am aware that the measure is probably going to be somewhat slow, however I prefer this since this allows me to use filters.

 

Thanks in advance!

4 REPLIES 4
TomMartens
Super User
Super User

Hey @Anonymous ,

 

maybe this post will provide what you are looking for or at least provides some new ideas: 

https://community.powerbi.com/t5/Desktop/DAX-formula-for-consecutive-events-with-some-criteria/m-p/861009#M413119

 

If not, please create a pbix file that contains some sample data but still reflects your data model, upload the file to onedrive or dropbox and share the link.

Make sure that the pbix has a proper data model (star schema), meaning that there are dedicated tables for company, product, date, (not sure about the demandid).

 

Regards,

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany
Anonymous
Not applicable

HI @Anonymous,

Measure formula is calculated based on its row contents, so it can be dynamically affected by filter/slicers.
When you use it in a calculated column/table, its row contents level will be fixed as whole table and it not able to interact with the slicer/filter to achieve a dynamic calculated column/table.

BTW, measure formulas not able to use as a category to group and aggregate records.
Regards,

Xiaoxin Sheng

amitchandak
Super User
Super User

@ImkeF , can you help

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Hi Amit,

I wish I could help here, but that's beyond my DAX skills today.

A nested / conditional index in DAX needs some DAX superpowers from @Greg_Deckler@TomMartens  or @OwenAuger .

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors