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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
EaglesTony
Post Prodigy
Post Prodigy

How do I get the max value of an datetime field

Hi,

 

  I have this table:

 

  ID               StartDate

  1                 3/2/2024  10:05:00 PM

  1                 4/5/2024    9:32:00 AM

  1                 1/1/2024    8:00:00 AM

  2                 5/2/2024  10:05:00 PM

  2                 4/5/2024    9:32:00 AM

  2                 1/1/2024    8:00:00 AM

 

What I need is this:

  ID               StartDate

  1                 4/5/2024    9:32:00 AM

  2                 5/2/2024  10:05:00 PM

 

I want to do it in a PowerM Query.

1 ACCEPTED SOLUTION

@EaglesTony,

 

In the Group By, add an additional aggregation for all rows:

 

DataInsights_0-1728571675011.png

 

Click the double arrow and select the additional columns to include:

 

DataInsights_1-1728571712219.png

 

Add a filter "StartDate = Max Start Date" (see below query).

 

Result:

 

DataInsights_2-1728571731667.png

 

Query (replace Source step with your actual source):

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bY7RCQAhDENXOfottE3bO3UVcf81FORAxM/0JU1aI6VEysoQ+JOryNTOJgynnhY3xuJAlZiHYNPd4BzLUKph6nd7gEvBt+VxyeeDxzmg/AV9AA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, StartDate = _t, EndDate = _t]),
    ChangeType = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"StartDate", type datetime}, {"EndDate", type date}}),
    GroupRows = Table.Group(ChangeType, {"ID"}, {{"Max Start Date", each List.Max([StartDate]), type nullable datetime}, {"All Rows", each _, type table [ID=nullable number, StartDate=nullable datetime, EndDate=nullable date]}}),
    ExpandRows = Table.ExpandTableColumn(GroupRows, "All Rows", {"StartDate", "EndDate"}, {"StartDate", "EndDate"}),
    FilterRows = Table.SelectRows(ExpandRows, each [StartDate] = [Max Start Date]),
    RemoveColumn = Table.RemoveColumns(FilterRows,{"StartDate"})
in
    RemoveColumn

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

3 REPLIES 3
DataInsights
Super User
Super User

@EaglesTony,

 

Use the Group By function in Power Query:

 

DataInsights_0-1728508277362.png

 

DataInsights_1-1728508289537.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




I forgot to mention I have other columns including End Date and I need that in the final result.

 

When I do the group by you mentioned, I only get 2 columns as shown above, but need that 3rd column as well(End Date).

@EaglesTony,

 

In the Group By, add an additional aggregation for all rows:

 

DataInsights_0-1728571675011.png

 

Click the double arrow and select the additional columns to include:

 

DataInsights_1-1728571712219.png

 

Add a filter "StartDate = Max Start Date" (see below query).

 

Result:

 

DataInsights_2-1728571731667.png

 

Query (replace Source step with your actual source):

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bY7RCQAhDENXOfottE3bO3UVcf81FORAxM/0JU1aI6VEysoQ+JOryNTOJgynnhY3xuJAlZiHYNPd4BzLUKph6nd7gEvBt+VxyeeDxzmg/AV9AA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, StartDate = _t, EndDate = _t]),
    ChangeType = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"StartDate", type datetime}, {"EndDate", type date}}),
    GroupRows = Table.Group(ChangeType, {"ID"}, {{"Max Start Date", each List.Max([StartDate]), type nullable datetime}, {"All Rows", each _, type table [ID=nullable number, StartDate=nullable datetime, EndDate=nullable date]}}),
    ExpandRows = Table.ExpandTableColumn(GroupRows, "All Rows", {"StartDate", "EndDate"}, {"StartDate", "EndDate"}),
    FilterRows = Table.SelectRows(ExpandRows, each [StartDate] = [Max Start Date]),
    RemoveColumn = Table.RemoveColumns(FilterRows,{"StartDate"})
in
    RemoveColumn

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

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.