Forum Discussion

joooffice's avatar
joooffice
Helper I
5 years ago
Solved

Sort names alphabetically but keeping groups with same ID together

I have a list of members of families attending an event that I want to sort alphabetically but some of the people have different surnames within the same family. I still want them to appear together ...
  • Jimmy801's avatar
    5 years ago

    Hello joooffice 

     

    check out this solution. Used Table.Group to group by ID with all rows and the first name found. Then sort for this name and expand afterwards

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZLNCsIwEIRfpeSsYBt/j1pQ0Yuo4KH0UEjQ4NpI2qK+vYm22rgJeArst7OT2SRJSNgPSYfsLqI86XPKKiiDKCBpp0GHTDF9xCcBLKBeEnqJa9rb56Pp6dJWFtxNZkrKc9G1GpqhdGwaKjyTDnVpLoArrMGoUUXGb8lFCSI//sXtjD5u6deZuv5eamRyxJW68fzX1IUalVnpRmozJMKk1gxN+lgqxgH+YvVESgdmbZDd3WQlc16gwL2JLi0k4C+EiV8Ttck+e4BU3brBftq21H6510KW4pXLtmsTexrWuMjXJ30C", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Name = _t, Title = _t]),
        #"Changed Type" = Table.Buffer(Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Name", type text}, {"Title", type text}})),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"AllRows", each _, type table [ID=number, Name=text, Title=text]},{"NameToSort", each _[Name]{0}}}),
        #"Sorted Rows" = Table.Sort(#"Grouped Rows",{{"NameToSort", Order.Ascending}}),
        #"Removed Other Columns" = Table.SelectColumns(#"Sorted Rows",{"AllRows"}),
        #"Expanded AllRows" = Table.ExpandTableColumn(#"Removed Other Columns", "AllRows", {"ID", "Name", "Title"}, {"ID", "Name", "Title"})
    in
        #"Expanded AllRows"

     

    Copy paste this code to the advanced editor in a new blank query to see how the solution works.

    If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
    Kudoes are nice too

    Have fun

    Jimmy