Delete middle initial from name

ClearScreen 21 Reputation points
2021-06-29T13:29:02.15+00:00

I have a Power Query/M question. I have a list of full names, some of which have middle initials (without a period) and some of which do not. Names (and the possible initial) are delimited by spaces. Some of the names have multiple first or last names which I would want to keep. So you might have a mix of names like

John Smith
John Q Smith
Maggie Q Smith
Anne Margaret Smith
Ann Margaret Q Smith
Anne Margaret Nixon Smith

I would like to return all the names but not the initials. In other languages I would use regex but I cannot do so in PQ, and I'm just beginning to learn M. Has anyone come across something similar?

Community Center | Not monitored
{count} votes

Accepted answer
  1. Lz._ 9,016 Reputation points
    2021-07-01T05:50:34.187+00:00

    @ClearScreen . You're welcome & Thanks for posting back
    If that did it, the following does the same with List.Accumulate

    let  
        Source = Table.FromList(  
            {"John Smith","John Q Smith","Maggie Q Smith","Anne Margaret Smith",  
             "Anne Margaret Q Smith","Anne Margaret Y Nixon Smith"}, null,  
            type table [Full Name = text]  
        ),  
        RemovedInitial = Table.AddColumn(Source, "Clean Full Name", each  
            List.Accumulate(Text.Split([Full Name], " "), null,  
                (state,current)=> if Text.Length(current) > 1  
                    then Text.Combine({state, current}, " ") else state  
            ),  
            type text  
        ),  
        RemovedColumn = Table.SelectColumns(RemovedInitial,{"Clean Full Name"}),  
        RenamedColumn = Table.RenameColumns(RemovedColumn,{<!-- -->{"Clean Full Name", "Full Name"}})  
    in  
        RenamedColumn  
    

    A couple of articles re. List.Accumulate:

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Lz._ 9,016 Reputation points
    2021-06-30T05:39:00.027+00:00

    @ClearScreen

    OK, so the following does it assuming you don't have a Full Name like "A John Smith" where A isn't in the middle but will be remove anyway. If you need to keep it as "A John Smith" in this case let me know.

    As you begin with PQ I decomposed the steps with (hopefully) clear step names inside Table.AddColumn

    let  
    // Table for demo.  
        Source = Table.FromList(  
            {"John Smith","John Q Smith","Maggie Q Smith","Anne Margaret Smith",  
             "Anne Margaret Q Smith","Anne Margaret Y Nixon Smith"}, null,  
            type table [Full Name = text]  
        ),  
    //  
        RemovedInitial = Table.AddColumn(Source, "Clean Full Name", each  
            let  
                SplittedByDelim = Text.Split([Full Name], " "),  
                SelectedByLength = List.Select(SplittedByDelim, each Text.Length(_) > 1),  
                CombinedItems = Text.Combine(SelectedByLength, " ")  
            in  
                CombinedItems,  
                type text  
        ),  
        RemovedColumn = Table.SelectColumns(RemovedInitial,{"Clean Full Name"}),  
        RenamedColumns = Table.RenameColumns(RemovedColumn,{{"Clean Full Name", "Full Name"}})  
    in  
        RenamedColumns  
    

    Any question let me know

    1 person found this answer helpful.

  2. Lz._ 9,016 Reputation points
    2021-07-02T05:16:01.59+00:00

    @ClearScreen . To shorten your 1st function

    (FullName as text) as text =>  
    let  
        SplittedByDelim = Text.Split(FullName, " ")  
    in  
        List.Accumulate(SplittedByDelim, null,  
            (state, current) => if Text.Length(current) > 1  
                then Text.Combine({state, current}, " ")  
                else state  
        )  
    

    OR, even shorter

    (FullName as text) as text =>  
        List.Accumulate(Text.Split(FullName, " "), null,  
            (state, current) => if Text.Length(current) > 1  
                then Text.Combine({state, current}, " ")  
                else state  
        )  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.