Forum Discussion
HenryJS
4 years agoPost Prodigy
Power Query: Filter to Latest Date Row
Hi all, How can I filter to only show the row with the latest date for each externalid below? So only the row with 07/05/2022 for 3929 would show as it is the latest date.
- 4 years ago
Hi HenryJS ,
Base data:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjazNFDSUXLNLcjJr0wtUvDJTEzKzMksqVTwzCsuLUrMS04FShsZGBnpm+obGivF6gD1WBpZAgUDivLTUouLM/PzEnOAqlNSc/Nw6DNH1obXBuJUGkJUxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [externalid = _t, doctype = _t, Expiry = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"externalid", Int64.Type}, {"doctype", type text}, {"Expiry", type date}}), #"Grouped Rows" = Table.Group(#"Changed Type", {"externalid"}, {{"allrows",each Table.AddIndexColumn( Table.Sort(_,{{"Expiry", Order.Ascending}}), "Row Rank",1,1), each _, type table [externalid=nullable number, doctype=nullable text, Expiry=nullable date]}}), #"Expanded allrows" = Table.ExpandTableColumn(#"Grouped Rows", "allrows", {"externalid", "doctype", "Expiry", "Row Rank"}, {"allrows.externalid", "allrows.doctype", "allrows.Expiry", "allrows.Row Rank"}), #"Filtered Rows" = Table.SelectRows(#"Expanded allrows", each ([allrows.Row Rank] = 1)) in #"Filtered Rows"Final output:
refer:
https://data-witches.com/2020/10/21/adding-a-row-rank-based-on-a-different-column-with-power-query/
Best Regards
Lucien
AdHuikeshoven
2 years agoNew Member
The currently accepted solution depends on modifyin M code in the advanced editor. This is not necessary. The way to do it is to group on doctype and aggregate on the Max of Expiry, add an aggregate level called expansion and select all rows. Next step is to expand and add a conditional column with name "Latest?". Compare column Expiry with column Max of Expiry, and if equal, enter "latest", otherwise "null". Filter column "Latest?" on "latest". If necessary you can delete columns "Max of Expiry" and "Latest?", and you are done.