Forum Discussion
Power Query: Filter to Latest Date Row
- 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
on your document table make a new colum.
where you write True/false pr line if this is the latest date for this External ID.
New colum:
Max_date_by_esternal_ID =
IF(
calculate(Max(Document[Date], filter(document, Document[ExternalID], Earlier([Document[ExternalID])))=Document[Date] ,
True(),
False()
)
the EARLIER will take the External ID from the Line it is currently on and use that as a filter to get the max date
NOW you have a TRUE / False Column you can use as a filter
Mine look like this and work
so you would get 3 True lines
since external id 3929 has 2 Expiry dates that are both the latest date avaliable for that Id