cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Faith_Data
Regular Visitor

Calculate Difference Between the last non blank value and the second last nonblank value with filter

Reg noMileage
CCN 342D3342
CCN 455A880
VWT 403B10935
CCN 342D4000
CCN 455A1000

 Please help with the dax function to calculate difference between the last non blank row and the second last non blank row.The reg number is repeated.

1 ACCEPTED SOLUTION
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

 

Untitled.png

 

Mileage diff measure: =
VAR lastmileage =
    MAX ( Data[Mileage] )
VAR previousofthelastmileage =
    MAXX (
        FILTER (
            ALL ( Data ),
            Data[Reg no] = MAX ( Data[Reg no] )
                && Data[Mileage] < lastmileage
        ),
        Data[Mileage]
    )
RETURN
    IF ( HASONEVALUE ( Data[Reg no] ), lastmileage - previousofthelastmileage )

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


View solution in original post

6 REPLIES 6
tamerj1
Community Champion
Community Champion

Hi @Faith_Data 
Here you go https://we.tl/t-Mrey2dTPxa

First of all make sure the have the mileage column as intiger data type. 

 

 

Mileage diff measure - Tamer = 
SUMX ( 
    VALUES ( 'PM Data'[REG NO] ),
    CALCULATE (
        VAR LastTwoDatesTable = TOPN ( 2,'PM Data', 'PM Data'[LAST SERVICE], DESC )
        VAR Last_Date = MAXX ( LastTwoDatesTable, 'PM Data'[LAST SERVICE] )
        VAR Last_2ndDate = MINX ( LastTwoDatesTable, 'PM Data'[LAST SERVICE] )
        VAR Last_DateMileage = MAXX ( FILTER ( LastTwoDatesTable, 'PM Data'[LAST SERVICE] = Last_Date ), 'PM Data'[LAST MILEAGE] )
        VAR Last_2ndDateMileage = MAXX ( FILTER ( LastTwoDatesTable, 'PM Data'[LAST SERVICE] = Last_2ndDate ), 'PM Data'[LAST MILEAGE] )
        RETURN
            IF ( 
                COUNTROWS ( LastTwoDatesTable ) >= 2,
                Last_DateMileage - Last_2ndDateMileage
            )
    )
)

 

 

1.png

Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

 

Untitled.png

 

Mileage diff measure: =
VAR lastmileage =
    MAX ( Data[Mileage] )
VAR previousofthelastmileage =
    MAXX (
        FILTER (
            ALL ( Data ),
            Data[Reg no] = MAX ( Data[Reg no] )
                && Data[Mileage] < lastmileage
        ),
        Data[Mileage]
    )
RETURN
    IF ( HASONEVALUE ( Data[Reg no] ), lastmileage - previousofthelastmileage )

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


Thank you @Jihwan_Kim for faster response ,however the measure on my end is bringing all blanks

Hi,

Please share your sample pbix file's link, and then I can try to look into it to come up with a more accurate solution.

Thanks.

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Go to My LinkedIn Page


tamerj1
Community Champion
Community Champion

Hi @Faith_Data 
Please provide the expected results as per the sample data provided.

Helpful resources

Announcements
June 2022 update 768X460.jpg

Check it out!

Click here to read more about the June 2022 updates!

Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Power BI Dev Camp Session 23 768x460.jpg

Check it Out!

Mark your calendars and join us on Thursday, June 30 at 11a PDT for a great session with Ted Pattison!

June 20 episode 7 with aka link 768x460.jpg

The Power BI Community Show

Join us on June 20 at 11 am PDT when Kim Manis shares the latest on Azure Synapse analytics, the Microsoft Intelligent Data Platform, and notable Power BI Updates from Microsoft Build 2022.

Top Solution Authors
Top Kudoed Authors