excelexcel-formulaexcel-2019

Extract Certain Text from excel


Need help in extracting person names with similar First name and different surname and extract to another cell.

Cell A will contain - Aaron Hank; Abagnale Frank; Abbey Edward; Adams Abigail; Adams Henry; Adams Scott

Excepted Data in Cell B - Adams Abigail; Adams Henry; Adams Scott


Solution

  • You can use Power Query which is a part of Excel 2019. Below is a rather long winded way of doing it, step by step. The query returns the most common first name with all matching surnames:

    let
        // Load data from the table
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        // Split the single cell into multiple rows using ";" delimiter
        Rows = Table.ExpandListColumn(
                  Table.TransformColumns(Source, {{"Column1", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv)}}),
                  "Column1"),
        // Remove extra spaces
        Trimmed = Table.TransformColumns(Rows, {{"Column1", Text.Trim, type text}}),
        // Split each row into FirstName and Surname (using the first space as delimiter)
        SplitNames = Table.SplitColumn(Trimmed, "Column1", Splitter.SplitTextByDelimiter(" "), {"FirstName", "Surname"}),
        // Group by FirstName to count occurrences
        Grouped = Table.Group(SplitNames, {"FirstName"}, {{"Count", each Table.RowCount(_), type number}}),
        // Determine the maximum count
        MaxCount = List.Max(Grouped[Count]),
        // Get the most common first name(s)
        CommonNames = Table.SelectRows(Grouped, each [Count] = MaxCount)[FirstName],
        // Filter original rows to keep only those with the most common first name
        Filtered = Table.SelectRows(SplitNames, each List.Contains(CommonNames, [FirstName])),
        // Combine FirstName and Surname into a full name
        CombinedNames = Table.AddColumn(Filtered, "FullName", each [FirstName] & " " & [Surname], type text),
        // Combine all full names into a single text string using "; " as delimiter
        Result = Text.Combine(CombinedNames[FullName], "; ")
    in
        Result