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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
mattramirez2020
Helper II
Helper II

MAX Date and 2nd Max Date Help!

Hello, I am trying to make two calculated columns in a query for max date and 2nd max date. Both of the dates come from another query and have the criteria of having been "completed". I am really struggling to figure out how to do this in a calculated column and would really appreciate any help. Any help would be immensely appreciated!!!!

 

Query: AllPIDs

PeopleID
1
2
3

 

Query: Appointments

PeopleIDDateStatus
11/1/20Completed
12/1/20Completed
14/1/20Completed
16/20/20Scheduled
212/1/18Completed
212/5/18Completed
312/31/19Completed

 

Ideal Query: AllPIDs with two new calculated columns referencing Appointments Table

PeopleIDMax Completed Date2nd Max Completed Date
14/1/202/1/20
212/5/1812/1/18
312/31/19 

 

1 ACCEPTED SOLUTION

This is more easily solved with DAX than in the query editor (M).  The expressions I provided are for DAX calculated columns, not custom columns in the query editor.  Unless you need to do data transformations involving these new columns, it is the way to go.  Load your query, and then click on New Column for each of them.

 

Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

4 REPLIES 4
mahoneypat
Employee
Employee

Please try these expressions in your columns.  This assumes you have a 1:many relationship between your tables.

 

Max Date = CALCULATE(Max(Appointments[Date]), Appointments[Status] = "Completed")

 

2nd Max Date = var __maxdate = [Max Date]

return CALCULATE(Max(Appointments[Date]), Appointments[Status] = "Completed", Appointments[Date] <> __maxdate)

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


 

Hey @mahoneypat ,

 

It tells me that this name is not recognized? Limitation of Custom Column possibly?

 

Error.PNG

This is more easily solved with DAX than in the query editor (M).  The expressions I provided are for DAX calculated columns, not custom columns in the query editor.  Unless you need to do data transformations involving these new columns, it is the way to go.  Load your query, and then click on New Column for each of them.

 

Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Worked perfectly! Thanks!

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.