Forum Discussion

NanardPet's avatar
NanardPet
Regular Visitor
3 years ago
Solved

Max value from a column in Power Query Editor for each value in another column

Hi there,   I would like to select all the rows of a table for which, for each different record, the fiscal year corresponds to the last year of this record. Here is the expected result in Excel: ...
  • m_alireza's avatar
    3 years ago

    Hi NanardPet 

    You can achieve this through the group by function in Power Query. Copy and paste this in your advanced editor and amend as needed: 

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcndV0lEyMjA0U4rVQfDMUXgWKDxLZJ6RAQrPEMxzdIEqNUblmqByTVG5Zqhcc1SuBSrXEoULckUsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Record = _t, FiscalYear = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Record", type text}, {"FiscalYear", Int64.Type}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Record"}, {{"MaxOfFiscalYear", each List.Max([FiscalYear]), type nullable number}, {"AllRows", each _, type table [Record=nullable text, FiscalYear=nullable number]}}),
        #"Expanded AllRows" = Table.ExpandTableColumn(#"Grouped Rows", "AllRows", {"FiscalYear"}, {"FiscalYear"}),
        #"Added Conditional Column" = Table.AddColumn(#"Expanded AllRows", "Select", each if [FiscalYear] = [MaxOfFiscalYear] then true else false)
    in
        #"Added Conditional Column"


    Sample output: 

  • NanardPet's avatar
    3 years ago

    Thanks a lot m_alireza : it works fine!