Forum Discussion

HenryJS's avatar
HenryJS
Post Prodigy
4 years ago
Solved

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.      
  • v-luwang-msft's avatar
    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