Forum Discussion

Kaitra's avatar
Kaitra
Frequent Visitor
4 years ago
Solved

Power Query needed formula

I have the following situation.   Column A Column B AAA   AAA   CCC   BBB   DDD   CCC   CCC deactive DDD deactive   In Column A are different values. In col...
  • BA_Pete's avatar
    BA_Pete
    4 years ago

    Kaitra ,

     

    Ah yes, I see.

     

    In Power Query, go to New Source>Blank Query then in Advanced Editor paste my code over the default code. You can then follow the steps I took to complete this:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnR0VNJRUlCK1UFlOzs7w9lOTk5wtouLC1Y1EHZKamJySWZZKpJShFAsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Column A" = _t, #"Column B" = _t]),
        chgTypes = Table.TransformColumnTypes(Source,{{"Column A", type text}, {"Column B", type text}}),
        groupColumnA = Table.Group(chgTypes, {"Column A"}, {{"data", each _, type table [Column A=nullable text, Column B=nullable text]}}),
        addMaxColumnB = Table.AddColumn(groupColumnA, "maxColumnB", each Table.Max([data], "Column B")),
        expandMaxColumnB = Table.ExpandRecordColumn(addMaxColumnB, "maxColumnB", {"Column B"}, {"Column B"}),
        expandData = Table.ExpandTableColumn(expandMaxColumnB, "data", {"Column B"}, {"Column B.1"})
    in
        expandData

     

    SUMMARY:

    1) Group table on [Column A] and keep All Rows as [data]

    2) Get max value of column B from nested table and expand record

    3) Expand [data] again to reinstate original data

     

    This gives me the following output:

     

    Pete

  • AlexisOlson's avatar
    4 years ago

    I had something written before I saw BA_Pete's answer. Please accept that one if it works for you but I thought I might as well post a slightly different alternative since it's already written.

     

    You could group by Column A and define the max over Column B as Column C and then merge this back to the original table.

     

    Group:

     

    Merge and Expand:

     

    Full sample M code you can paste into your Advanced Editor:

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcnR0VNJRUlCK1UFlOzs7w9lOTk5wtouLC1Y1EHZKamJySWZZKpJShFAsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Column A" = _t, #"Column B" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column A", type text}, {"Column B", type text}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Column A"}, {{"Column C", each List.Max([Column B]), type nullable text}}),
        #"Merged Queries" = Table.NestedJoin(#"Changed Type", {"Column A"}, #"Grouped Rows", {"Column A"}, "Grouped Rows", JoinKind.LeftOuter),
        #"Expanded Grouped Rows" = Table.ExpandTableColumn(#"Merged Queries", "Grouped Rows", {"Column C"}, {"Column C"})
    in
        #"Expanded Grouped Rows"