Search code examples
excelpowerbipowerquerypowerbi-desktopm

Power Query Add Column with Sorted Text for each cell


Given the following M code:

let Customer_Names = Table.FromRecords(
{  
    [Customer_Name = "Roger Kevan"],
    [Customer_Name = "Quincy Tod"],
    [Customer_Name = "Legend Vere"],
    [Customer_Name = "Hendrix Triston"],
    [Customer_Name = "Brycen Hallam"]
})
 

in  
    Customer_Names

Which yields a table with a few random names as in the image:

enter image description here

Could you please show me how to add a new column with the sorted name?

for example in the first cell in the Customer_Name column, for the name "Roger Kevan" - the resulting text in the new column should be: "aeegnorrrv"

My purpose is to detect names that contain misspellings or same names with different order of words.


Solution

  • enter image description here

    let Customer_Names = Table.FromRecords(
    {  
        [Customer_Name = "Roger Kevan"],
        [Customer_Name = "Quincy Tod"],
        [Customer_Name = "Legend Vere"],
        [Customer_Name = "Hendrix Triston"],
        [Customer_Name = "Brycen Hallam"]
    }),
        #"Added Custom" = Table.AddColumn(Customer_Names, "Custom",  each  
    let a = Text.Length([Customer_Name]),
    b = {0..a-1},
    c = Splitter.SplitTextByPositions(b)([Customer_Name]),
    d = List.Difference(c, {" "}),
    e = List.Sort(List.Transform(d, each Text.Lower(_))),  
    f = Text.Combine(e, "")
    in f
    
    )
    in
        #"Added Custom"