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:

 

RecordFiscalYearMaxOfFiscalYearSelect
GE20162021FAUX
GE20172021FAUX
GE20182021FAUX
GE20192021FAUX
GE20202021FAUX
GE20212021VRAI
ADE20132020FAUX
ADE20142020FAUX
ADE20152020FAUX
ADE20162020FAUX
ADE20172020FAUX
ADE20182020FAUX
ADE20192020FAUX
ADE20202020VRAI

 

Many thanks in advance for a solution

 

Bernard

 

  • 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: 

2 Replies

  • m_alireza's avatar
    m_alireza
    Icon for Solution Specialist rankSolution Specialist

    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
    NanardPet
    Regular Visitor

    Thanks a lot m_alireza : it works fine!