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! Learn more

Reply
Rinosh
Frequent Visitor

Need to find the second last value

Hi all,

Need your valuable help.

 

Need  to find out the second last value ( date & value) from the  data table

 

Example : from the below table ,sould find out the below values

1. Latest date  & Kms/miles , if maintanace type is PMS  ( solved using Maxx & filter)

2. Second last date & Kms/miles , if maintanance type is PMS ( Need help to find out)

3.  Diffrence between both dates & Kms/Miles. ( Solved using diffrence)

 

The result expected is  , for the vehicle KTU 920 ER ( Should vary based on vehicles)

1. latest service date  & KMS =10/12/2022 & 266000 Kms

2. Second last service date & Kms = 04/12/2022 & 265432 Kms

 

DateVehicle  CategoryKms/MilesMaintanance type
3/12/2022KTU 920 ER  LMV265432RR
4/12/2022KTU 920 ER  LMV265432PMS
5/12/2022KTU 920 ER  LMV265432RR
10/12/2022KTU 920 ER  LMV266000PMS
1 ACCEPTED SOLUTION

@Rinosh If you need it to consider vehicle number then it is just:

 

Second To Last Date Measure PMS = 
  VAR __Vehicle = MAX('Table'[Registration or Serial No.])
  VAR __Last = 
    MAXX(
      FILTER(
        ALL('Table'), 
        [Registration or Serial No.] = __Vehicle && [Maintanance type] = "PMS"
      ),
      [Date]
    )
  VAR __SecondToLast = 
    MAXX(
      FILTER(
        ALL('Table'),
        [Registration or Serial No.] = __Vehicle && 
          [Date]<__Last && [Maintanance type] = "PMS"
      ),
      [Date]
    )
RETURN
  __SecondToLast



Second To Last Value Measure PMS = 
  VAR __Vehicle = MAX('Table'[Registration or Serial No.])
  VAR __Result = 
    MAXX(
      FILTER(
        ALL('Table'),
        [Registration or Serial No.] = __Vehicle && 
          [Date] = [Second To Last Date Measure PMS] && [Maintanance type] = "PMS"
      ),
      [Kms/Miles]
    )
RETURN
  __Result

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

10 REPLIES 10
Greg_Deckler
Community Champion
Community Champion

@Rinosh Try:

Second To Last Date Measure =
  VAR __Last = MAXX(ALL('Table'),[Date])
  VAR __SecondToLast = MAXX(FILTER(ALL('Table'),[Date]<__Last),[Date])
RETURN
  __SecondToLast


Second To Last Value Measure =
  MAXX(FILTER(ALL('Table'),[Date] = [Second To Lst Date Measure]),[Kms/Miles])


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Hi, thank you for your quick responce.

 

I am getting error on the above dax function.  Also what i was expecting , Second last date & Kms  if vehicle maintance type is PMS

For "PMS" , the dates in dEscending order are 15/12/22,13/12/22,04/12/22 and 03/12/22

The corresponding second last dates are : 13/12/22,04/12/22,03/12/122,No date

I hope the following measure helps:

 

Second_Last_Date =
VAR Last_date= calculate(LASTDATE('Table'[Date]),'Table'[Job] = "PMS")
VAR SecondLastDate = MAXX(FILTER(ALL('Table'),'Table'[Date]<Last_date && 'Table'[Job] = "PMS"),[Date])
RETURN
SecondLastDate
Here is the result
Padycosmos_0-1673802040798.png

 

@Rinosh Had a minor typo in the name of the measure. Attached a PBIX below signature, here are the updated measures:

Second To Last Date Measure PMS = 
  VAR __Last = MAXX(FILTER(ALL('Table'), [Maintanance type] = "PMS"),[Date])
  VAR __SecondToLast = MAXX(FILTER(ALL('Table'),[Date]<__Last && [Maintanance type] = "PMS"),[Date])
RETURN
  __SecondToLast



Second To Last Value Measure PMS = 
  MAXX(FILTER(ALL('Table'),[Date] = [Second To Last Date Measure PMS] && [Maintanance type] = "PMS"),[Kms/Miles])


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler 

 

Really thank you for your effort.

The function is working partially for my requirment.

if the data sets are with multiple vehicle number , its getting considered  overall second last date & value

 

DateRegistration or Serial No.Nature of JobKms/Miles
12/4/2022KTU 920 ERPMS265432
12/5/2022KTU 920 ERRR265432
12/12/2022KTU 920 ERRR265432
12/15/2022KTU 920 ERPMS265666
12/29/2022KTU 920 ERRR266376
1/6/2023KTU 920 ERRR266882
12/3/2022EPE 572 ERPMS256359
12/13/2022EPE 572 ERPMS257245
12/28/2022EPE 572 ERRR258091
1/5/2022EPE 572 ERRR258752

 

For above data set, 

 

 i am getting the result of 

Vehicle numberSecond To Last DateSecond To Last Value Measure
EPE 572 ER12/13/2022257245
KTU 920 ER12/13/2022257245

 

Expected result is 

Vehicle numberSecond To Last DateSecond To Last Value Measure
EPE 572 ER12/3/2022256359
KTU 920 ER12/4/2022265432

 

Can you please help to adress this issue also.

Really thank you for the help.

 

I think you are confused, the result should be
or something I don't understand

Screen Capture #221.png

Rinosh
Frequent Visitor

Appolagise the mistake,

 

Resuld should be,

 

Second last date & value of maintanance type "PMS" 

The under lined  line  will be the second last value of maintanace type "PMS"

DateRegistration or Serial No.Nature of JobKms/Miles
12/4/2022KTU 920 ERPMS265432
12/5/2022KTU 920 ERRR265432
12/12/2022KTU 920 ERRR265432
12/15/2022KTU 920 ERPMS265666
12/29/2022KTU 920 ERRR266376
1/6/2023KTU 920 ERRR266882
12/3/2022EPE 572 ERPMS256359
12/13/2022EPE 572 ERPMS257245
12/28/2022EPE 572 ERRR258091
1/5/2022EPE 572 ERRR258752

 

 

 

@Rinosh If you need it to consider vehicle number then it is just:

 

Second To Last Date Measure PMS = 
  VAR __Vehicle = MAX('Table'[Registration or Serial No.])
  VAR __Last = 
    MAXX(
      FILTER(
        ALL('Table'), 
        [Registration or Serial No.] = __Vehicle && [Maintanance type] = "PMS"
      ),
      [Date]
    )
  VAR __SecondToLast = 
    MAXX(
      FILTER(
        ALL('Table'),
        [Registration or Serial No.] = __Vehicle && 
          [Date]<__Last && [Maintanance type] = "PMS"
      ),
      [Date]
    )
RETURN
  __SecondToLast



Second To Last Value Measure PMS = 
  VAR __Vehicle = MAX('Table'[Registration or Serial No.])
  VAR __Result = 
    MAXX(
      FILTER(
        ALL('Table'),
        [Registration or Serial No.] = __Vehicle && 
          [Date] = [Second To Last Date Measure PMS] && [Maintanance type] = "PMS"
      ),
      [Kms/Miles]
    )
RETURN
  __Result

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

look is this what you need?

https://dropmefiles.com/Nv1Mo

 

Second Max Date = 
VAR _tbl =
    CALCULATETABLE ( TOPN ( 2, 'Table', 'Table'[Date], DESC ) )
VAR _RESULT =
    CALCULATE ( MIN ( 'Table'[Date] ), _tbl )
RETURN
    IF ( ISINSCOPE ( 'Table'[Registration or Serial No.] ), _RESULT )
------
total = 
SUM ( 'Table'[Kms/Miles] )
----
Second Max KM = 
SUMX (
    VALUES ( 'Table'[Registration or Serial No.] ),
    VAR _second = [Second Max Date]
    RETURN
        CALCULATE ( [total], 'Table'[Date] = _second )
)

 

Rinosh
Frequent Visitor

@Ahmedx 

 

Thank you for the support.

Not getting right  result  as expected due to  vehicle maintance type = PMS  not considered in the function.

Result recieved is 

Vehicle numberSecond max dateSecond max Kms
KTU 920 ER12/29/2022266376
EPE 572 ER12/13/2022257245

 

Expected result is 

 

Vehicle numberSecond To Last DateSecond To Last Value Measure
EPE 572 ER12/3/2022256359
KTU 920 ER12/4/2022265432

 

Helpful resources

Announcements
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!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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