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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
09
Helper I
Helper I

Finding the last date a discrepancy of stock happens

Hello all,

I'm working on a dax formula that takes in account all the particular cases that might happen. the formula will calculate the last date a discrepancy of stock for each material occurs. Thanks to this community, the current formula works perfectly in this case  below for example and it gives me as a result 21 june.

DateMaterial Gap
23-juin2
22-juin2
21-juin0
20-juin0
19-juin1
18-juin0
17-juin0
16-juin1
15-juin1

 

PS: the DAX formula is  

=var currentValue = SELECTCOLUMNS( TOPN(0; 'Table'; 'Table'[Date]; DESC); "@val"; 'Table'[Date])

return IF( currentValue = 0;

var lastNonGap = CALCULATE( MIN('Table'[Date]); 'Table'[Material Gap] <> 0)

return CALCULATE( MAX('Table'[Date]); 'Table'[Material Gap] = 0; 'Table'[Date]] < lastNonGap)

)

 

 

But, only in this case below  (The material is inbounded but with a gap: 2 parts in a system and 0 parts in the other one), i get no result (No date generated):

DateMaterial Gap
23-juin2
22-juin2

Any suggetion to improve the formula so that it will take into account this particular case.

Thank you a lot for your help 🙂 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @09 ,

Please have a try. 

Create a measure.

 

Measure =
VAR _maxdate =
    CALCULATE (
        MAX ( 'Table'[Date] ),
        FILTER ( ALL ( 'Table' ), 'Table'[Material Gap] = 0 )
    )
RETURN
    IF ( _maxdate = SELECTEDVALUE ( 'Table'[Date] ), 1, BLANK () )

 

Or a column.

 

Column = 
VAR _maxdate =
    CALCULATE (
        MAX ( 'Table'[Date] ),
        FILTER (  ( 'Table' ), 'Table'[Material Gap] = 0 )
    )
RETURN
    IF ( _maxdate =  ( 'Table'[Date] ), 1, BLANK () )

 

Then filter the measure or the column.

vpollymsft_0-1655694238237.png

vpollymsft_1-1655694263514.pngvpollymsft_2-1655694274427.png

If I have misunderstood your meaning, please provide more details with your desired output.

How to Get Your Question Answered Quickly 

 

Best Regards

Community Support Team _ Polly

 

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

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi @09 ,

Please have a try. 

Create a measure.

 

Measure =
VAR _maxdate =
    CALCULATE (
        MAX ( 'Table'[Date] ),
        FILTER ( ALL ( 'Table' ), 'Table'[Material Gap] = 0 )
    )
RETURN
    IF ( _maxdate = SELECTEDVALUE ( 'Table'[Date] ), 1, BLANK () )

 

Or a column.

 

Column = 
VAR _maxdate =
    CALCULATE (
        MAX ( 'Table'[Date] ),
        FILTER (  ( 'Table' ), 'Table'[Material Gap] = 0 )
    )
RETURN
    IF ( _maxdate =  ( 'Table'[Date] ), 1, BLANK () )

 

Then filter the measure or the column.

vpollymsft_0-1655694238237.png

vpollymsft_1-1655694263514.pngvpollymsft_2-1655694274427.png

If I have misunderstood your meaning, please provide more details with your desired output.

How to Get Your Question Answered Quickly 

 

Best Regards

Community Support Team _ Polly

 

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

Hello @Anonymous 

Thank you for your help.

I'm actually working with power Query in excel.

I'm looking for the last time a stock gap between two systems has occurred for one material. The gap value is actually not a column but a dax measure. And the final result would be displayed in a pivot table. 

For now, this DAX formula works for me except for particular cases when i have older stock discrepancies so the formula returns the date of occurence of the first GAP that it has been regulated and what i really want is the last date the gap occured.

This is my Dax formula 

=CALCULATE(MIN('HISTORIQUE MB52'[Date]);FILTER(ALLEXCEPT('HISTORIQUE MB52';'HISTORIQUE MB52'[Material]);[Ecart 4400 UNR]<>0))

 

[Ecart 4400 UNR] this is a dax measure not a column that gives the value of stock GAP which is different from 0.

The two last rows, i have not a correct result: it gives me the date of the first GAP that  happened.

 

MaterialGAPLast GAP DATE
MATERIAL A-111/03/2022
MATERIAL B-120/01/2022
MATERIAL C131/01/2022
MATERIAL D-117/01/2022
MATERIAL E-117/01/2022

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 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.