Forum Discussion

arnabmit's avatar
arnabmit
Icon for Helper I rankHelper I
4 years ago
Solved

Flag rows based on highest percentage

I want to create a new column "Flag" which will flag the highest percentage of the same Case ID. I am an M amateur and would really appreciate some help.                       The f...
  • v-yalanwu-msft's avatar
    4 years ago

    Hi, arnabmit ;

    You could group by the Case ID column then add a condition column.

    1. group by Case ID column.

    2.expand the table.

    3. add a condition column.

    4.delete the Max column.and The final output is shown below:

    In additonal , M language like below:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyVtJRcgRiQwNVpVgdmIgTEFtCRUyMDKFqzFBEnIHYBCpiZA7TZYwi4gLE5lARY0szqC5zU2QRkBojkEgsAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Case ID" = _t, Name = _t, Percent = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Case ID", Int64.Type}, {"Name", type text}, {"Percent", Percentage.Type}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Case ID"}, {{"Max", each List.Max([Percent]), type nullable number}, {"a", each _, type table [Case ID=nullable number, Name=nullable text, Percent=nullable number]}}),
        #"Expanded a" = Table.ExpandTableColumn(#"Grouped Rows", "a", {"Name", "Percent"}, {"a.Name", "a.Percent"}),
        #"Added Conditional Column" = Table.AddColumn(#"Expanded a", "Flag", each if [a.Percent] = [Max] then 1 else 0),
        #"Removed Columns" = Table.RemoveColumns(#"Added Conditional Column",{"Max"}),
        #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"a.Percent", "Percent"}, {"a.Name", "Name"}}),
        #"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"Percent", Percentage.Type}})
    in
        #"Changed Type1"

     
    Best Regards,
    Community Support Team _ Yalan Wu
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.