Reply
Matthew_Theis
Advocate II
Advocate II
Partially syndicated - Outbound

Power Query - Finding most recent Date for a group

Rebuild_DT.PNG

Hello Everyone,

 

My data contains historical updates made to part numbers at a given location.  I would like to filter using Power Query to only the most recent record for each location's part number.  I have many values in GrpParent_Loc_KPN - I want to find the most recent Rebuild_DT.

I have a Rebuild_DT column which is a datetime field.  I also have a GrpParent_Loc_KPN which is a concatenation of three fields.  I would like to identify with a 1 or 0 whether or not the Rebuild_DT is the most recent for each GrpParent_Loc_KPN.

 
 

 

 

 

1 ACCEPTED SOLUTION
Jimmy801
Community Champion
Community Champion

Syndicated - Outbound

Hello @Matthew_Theis 

 

this solution uses the same approach as the one from @Vera_33, but it's more compact

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDUByIjAyNDJR2lxESlWB2ImBEWMbi6pCRMdUhixjCx5GSImBGSXpBYLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Rebuild_DT = _t, GrpParent_Loc_K = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Rebuild_DT", type date, "en-US"}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"GrpParent_Loc_K"}, {{"MaxDate", each List.Max([Rebuild_DT]), type date}})
in
    #"Grouped Rows"

Jimmy801_0-1613456264554.png

Jimmy801_1-1613456272630.png

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

View solution in original post

2 REPLIES 2
Jimmy801
Community Champion
Community Champion

Syndicated - Outbound

Hello @Matthew_Theis 

 

this solution uses the same approach as the one from @Vera_33, but it's more compact

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDUByIjAyNDJR2lxESlWB2ImBEWMbi6pCRMdUhixjCx5GSImBGSXpBYLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Rebuild_DT = _t, GrpParent_Loc_K = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Rebuild_DT", type date, "en-US"}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"GrpParent_Loc_K"}, {{"MaxDate", each List.Max([Rebuild_DT]), type date}})
in
    #"Grouped Rows"

Jimmy801_0-1613456264554.png

Jimmy801_1-1613456272630.png

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

Vera_33
Resident Rockstar
Resident Rockstar

Syndicated - Outbound

Hi @Matthew_Theis 

 

Paste it in Advanced editor with a blank query, next time please provide some data in a format we can copy:

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCkpNKs3MSYl3CVHSUXIvKghILErNK4n3yU+O91aK1YlWMjDUByIjAyNDoILizNRcuKgRVlG42vScFEylyILGGIJG6NpjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
#"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Rebuild_DT", type datetime}, {"GrpParent_Loc_K", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"GrpParent_Loc_K"}, {{"allrows", each _, type table}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each [a=Table.AddIndexColumn([allrows],"Index",0,1),
b=Table.AddColumn(a,"new",each if [Rebuild_DT]=List.Max(a[Rebuild_DT]) then 1 else 0)][b]),
#"Removed Other Columns" = Table.SelectColumns(#"Added Custom",{"Custom"}),
#"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"Rebuild_DT", "GrpParent_Loc_K", "Index", "new"}, {"Rebuild_DT", "GrpParent_Loc_K", "Index", "new"})
in
#"Expanded Custom"

avatar user

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

Check out the February 2025 Power BI update to learn about new features.

Top Solution Authors (Last Month)
Top Kudoed Authors (Last Month)