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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply

undefined2nd max date based on ID and status

Hello All,

 

I am trying to get 2nd max date for each ID month wise .

for eg for ID 1 in month Feb i should get 16-02-2022 for status closed and for march month it should be null as i don't have 2nd max date for closed status.

Can someone please help me on this.

 

 

IddateStatus
115-02-2022ready
116-02-2022closed
117-02-2022ready
118-02-2022closed
222-03-2023ready
223-03-2023closed

 

 

Expected Output:

 

IddateStatusoutputdate
115-02-2022ready16-02-2022
116-02-2022closed16-02-2022
117-02-2022ready16-02-2022
118-02-2022closed16-02-2022
222-03-2023readynull
223-03-2023closednull

Thanks & Regards,

Poojashri

1 ACCEPTED SOLUTION

@poojashribanger Oh, I was assuming that you would have the ID and Status in context only. You could do the following as a calculated column:

 

Column = 
  VAR __ID = [Id]
  VAR __Status = "Closed"
  VAR __MaxDate = MAXX(FILTER('Table', [Id] = __ID && [Status] = __Status), [date])
  VAR __MaxDate2 = MAXX(FILTER('Table', [Id] = __ID && [Status] = __Status && [date] < __MaxDate), [date])
RETURN
  __MaxDate2

 

as a measure:

 

Measure = 
  VAR __ID = MAX([Id])
  VAR __Status = "Closed"
  VAR __MaxDate = MAXX(FILTER('Table', [Id] = __ID && [Status] = __Status), [date])
  VAR __MaxDate2 = MAXX(FILTER('Table', [Id] = __ID && [Status] = __Status && [date] < __MaxDate), [date])
RETURN
  __MaxDate2

 



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

8 REPLIES 8
ThxAlot
Super User
Super User

For fun only, a showcase of powerful worksheet formulas.

=INDEX(FILTER([Date],([Id]=[@Id])*(EOMONTH([Date]&"",0)=EOMONTH([@Date],0))*([Status]="closed")),2)

ThxAlot_0-1696860201487.png



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LearnAndPractise(Everyday)


)



Thanks , this is the output i am expecting but it should be 16thFeb(i.e 2nd max date of closed status) i want it to be done on Power BI .

Is it possible?

DOLEARY85
Resident Rockstar
Resident Rockstar

Hi,

 

You can do this in a couple of steps:

 

1. create a calculated column to rank the dates by month:

 

Rank =
RANKX(
   FILTER('Table', 'Table'[date].[Month] = EARLIER('Table'[date].[Month])),
   'Table'[date],
   ,
   ASC,
   Dense
)
 
2. create a measure to calculate on the ID and use the rank = 2:
 
SecondMaxDate =
CALCULATE(
   MAX('Table'[date]),
   FILTER(
      ALLEXCEPT('Table','Table'[Id]),
      'Table'[Rank] = 2
   )
)
 
DOLEARY85_0-1696859439312.png

 

If I answered your question, please mark my post as solution, Appreciate your Kudos 👍

thanks for your response.

But i need 2nd max date for only status =closed also is there any posibilities that this can be done in modelling tab as a column in fact table rather than measure

Greg_Deckler
Community Champion
Community Champion

@poojashribanger Perhaps:

Measure = 
  VAR __MaxDate = MAX('Table'[date])
  VAR __MaxDate2 = MAXX(FILTER('Table', [date] < __MaxDate), [date])
RETURN
  __MaxDate2


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...

Thanks for the response Greg ,I tried this but this is not giving me the expected output.

i want the expected output like this

IddateStatusoutputdate
115-02-2022ready16-02-2022
116-02-2022closed16-02-2022
117-02-2022ready16-02-2022
118-02-2022closed16-02-2022
222-03-2023readynull
223-03-2023closednull

@poojashribanger Oh, I was assuming that you would have the ID and Status in context only. You could do the following as a calculated column:

 

Column = 
  VAR __ID = [Id]
  VAR __Status = "Closed"
  VAR __MaxDate = MAXX(FILTER('Table', [Id] = __ID && [Status] = __Status), [date])
  VAR __MaxDate2 = MAXX(FILTER('Table', [Id] = __ID && [Status] = __Status && [date] < __MaxDate), [date])
RETURN
  __MaxDate2

 

as a measure:

 

Measure = 
  VAR __ID = MAX([Id])
  VAR __Status = "Closed"
  VAR __MaxDate = MAXX(FILTER('Table', [Id] = __ID && [Status] = __Status), [date])
  VAR __MaxDate2 = MAXX(FILTER('Table', [Id] = __ID && [Status] = __Status && [date] < __MaxDate), [date])
RETURN
  __MaxDate2

 



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...

Thanks Greg this seems to work for me.

 

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

Top Solution Authors