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

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
bi_quest18
Regular Visitor

DAX to find last date where sum went negative?

Hi All,

 

New to DAX and struggling to find a calculation to perform the following:

 

Data is like this:

FiscalDate CurrentInventoryAmt SalesAmt RemainderAmt 

20180714    1000                        100              900
20180707     1000                        200              700
20180701    1000                         1000           -300
 20180624   1000                          0                -300  

Couple of notes:

1. The Fiscal date needs to look backwards (Descending order), fiscal dates are the week start dates.

2. RemainderAmt is a running total calculated as taking the remainderAmt of the last week and subtracting SalesAmt.

 

I need to find:

For each fiscal date, i need to find the number of days where the RemainderAmt went negative

For e.g.

For 20180714, RemainderAmt went negative in 20180701, so the measure needs to return 14 (days between 20180714 and 20180701)

For 20180707, the measure will return 7 days 

For 20180701, the measure will return 0 days since the remainder is already negative.

 

Any guidance will be appreciated!

Thanks

 

2 REPLIES 2
PattemManohar
Community Champion
Community Champion

@bi_quest18 Please try add to add below three fields as "New Column"

 

MaxDate (Negative amount)

 

MaxDate = 
VAR _MaxDate = CALCULATE(MAX(Test37[FiscalDate]),FILTER(Test37,SIGN(Test37[RemainingAmt])=-1))
VAR _MaxDateFinal = DATE(LEFT(_MaxDate,4),MID(_MaxDate,5,2),RIGHT(_MaxDate,2))
RETURN _MaxDateFinal

FiscalDate conversion to proper Date datatype

 

FiscalDateNew = DATE(LEFT(Test37[FiscalDate],4),MID(Test37[FiscalDate],5,2),RIGHT(Test37[FiscalDate],2))

Then, here is the Days logic

 

Days = IF(Test37[MaxDate]>Test37[FiscalDateNew],0,DATEDIFF(Test37[MaxDate],Test37[FiscalDateNew],DAY))

image.png





Did I answer your question? Mark my post as a solution!

Proud to be a PBI Community Champion




Hi Pattermmanohar, This is great. thanks!

Since the data set is huge = 2 billion+ rows, the current cube size is 127GB.

 

Is it possible to do this in measures? I am trying to minimize calculated columns to conserve the footprint of the deployed cube. 

Thanks again

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

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