Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
paul_cranberry
Frequent Visitor

Splitting a column at a variable position

My data looks like this...

 

Column A
Dr. Sam Karta Jr., CEO, Tallis Partners
Fred Sampson, III, Director, Aspen Institute
Ruth Kaufmann, PhD, Executive Director
Marci Cheng, CHRO

 

I need to split this column so that job titles — but not suffixes (e.g., PhD, Jr., III, etc.) — transfer to a new column. Not everyone has a suffix, and each row entry has a different number of commas, so I cannot reliably split using a delimiter.

 

Instead, I want PQ to look for the existence of a suffix (e.g., PhD, Jr., III, etc.) then split any remaining text (i.e., text after the suffix) into a new column. If a suffix does not exist (see Marci Cheng), PQ splits at the comma.

 

So, the data would look like this...

 

Column A Column B
Dr. Sam Karta Jr.CEO, Tallis Partners
Fred Sampson, IIIDirector, Aspen Institute
Ruth Kaufmann, PhDExecutive Director
Marci ChengCHRO

 

Can you help? Thank you so much!

2 ACCEPTED SOLUTIONS
ronrsnfld
Super User
Super User

Create a blank query which IS a List of Suffixes.

{"CEO","III","PhD"}

 

Then try this code (which refers to the Suffix query)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("PY5BCsIwEEWvMmQ99A7SVIwiLdVd6CLE0QTatEwm4vGNXbj9vPf41irNDdzcAhfH4uDMDULb9Qh3N88xw1DnRJzVhFYdmR4/eMtrQjDGIOjI5GVlhEPeKIFJWaIUoV0Yi4RaLs/FpWoMQSN0H/JF4pv+7o5eHfsIbaD0qg9OY6+m6Qs=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Column A" = _t]),
    #"Add Name" = Table.AddColumn(Source,"Name", (r)=>
        let 
            split = Text.SplitAny(r[Column A]," ,"),
            s = List.Accumulate(
                    Suffix,
                    {},
                    (s,c)=>s & List.Combine({List.FindText(split,c)})),
            Name = if s={} then Text.BeforeDelimiter(r[Column A],",") else Text.BeforeDelimiter(r[Column A], s{0}) & s{0},
            p = if s = {} then Text.AfterDelimiter(r[Column A],",") else Text.AfterDelimiter(r[Column A], s{0}),
            Position = Text.Trim(p,{" ",","})
        in 
            [Name = Name, Position = Position], type [Name=text, Position=text]),
    #"Removed Columns" = Table.RemoveColumns(#"Add Name",{"Column A"}),
    #"Expanded Name" = Table.ExpandRecordColumn(#"Removed Columns", "Name", {"Name", "Position"})
in
    #"Expanded Name"

 

I suspect there is a more efficient method, but this should work according to your specifications.

 

Data

 

ronrsnfld_0-1731721786325.png

Results

 

ronrsnfld_1-1731721819920.png

 

 

 

View solution in original post

Omid_Motamedise
Super User
Super User

@paul_cranberry Thanks for sharing such a lovely problem. just copy the following code and past it into Advance editor.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("PY5BCsIwEEWvMmQ99A7SVIwiLdVd6CLE0QTatEwm4vGNXbj9vPf41irNDdzcAhfH4uDMDULb9Qh3N88xw1DnRJzVhFYdmR4/eMtrQjDGIOjI5GVlhEPeKIFJWaIUoV0Yi4RaLs/FpWoMQSN0H/JF4pv+7o5eHfsIbaD0qg9OY6+m6Qs=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Column A" = _t]),
    List = {{"PhD,","PhD"},{"CEO,","CEO"},{", III"," III"},{"Director,","Director"}},
    #"Replaced Value1" = List.Accumulate(List,Source,(a,b)=>Table.ReplaceValue(a,b{0},b{1},Replacer.ReplaceText,{"Column A"})),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Replaced Value1", "Column A", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Column A.1", "Column A.2"})
in
    #"Split Column by Delimiter"

If my answer helped solve your issue, please consider marking it as the accepted solution.

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

Hi @paul_cranberry ,

Thanks for all the replies!

I see that you have posted the same thread in both the Power BI Desktop and Power Query forums and there are many users who have given their solutions. Please check to see if there is a solution for you, and if there is, please remember to accept their reply as a solution so that other users can find a solution faster, and forward that reply to the bottom of the other threads you have posted with the same problem, thank you!


Best Regards,
Dino Tao

Omid_Motamedise
Super User
Super User

@paul_cranberry Thanks for sharing such a lovely problem. just copy the following code and past it into Advance editor.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("PY5BCsIwEEWvMmQ99A7SVIwiLdVd6CLE0QTatEwm4vGNXbj9vPf41irNDdzcAhfH4uDMDULb9Qh3N88xw1DnRJzVhFYdmR4/eMtrQjDGIOjI5GVlhEPeKIFJWaIUoV0Yi4RaLs/FpWoMQSN0H/JF4pv+7o5eHfsIbaD0qg9OY6+m6Qs=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Column A" = _t]),
    List = {{"PhD,","PhD"},{"CEO,","CEO"},{", III"," III"},{"Director,","Director"}},
    #"Replaced Value1" = List.Accumulate(List,Source,(a,b)=>Table.ReplaceValue(a,b{0},b{1},Replacer.ReplaceText,{"Column A"})),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Replaced Value1", "Column A", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Column A.1", "Column A.2"})
in
    #"Split Column by Delimiter"

If my answer helped solve your issue, please consider marking it as the accepted solution.
ronrsnfld
Super User
Super User

Create a blank query which IS a List of Suffixes.

{"CEO","III","PhD"}

 

Then try this code (which refers to the Suffix query)

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("PY5BCsIwEEWvMmQ99A7SVIwiLdVd6CLE0QTatEwm4vGNXbj9vPf41irNDdzcAhfH4uDMDULb9Qh3N88xw1DnRJzVhFYdmR4/eMtrQjDGIOjI5GVlhEPeKIFJWaIUoV0Yi4RaLs/FpWoMQSN0H/JF4pv+7o5eHfsIbaD0qg9OY6+m6Qs=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Column A" = _t]),
    #"Add Name" = Table.AddColumn(Source,"Name", (r)=>
        let 
            split = Text.SplitAny(r[Column A]," ,"),
            s = List.Accumulate(
                    Suffix,
                    {},
                    (s,c)=>s & List.Combine({List.FindText(split,c)})),
            Name = if s={} then Text.BeforeDelimiter(r[Column A],",") else Text.BeforeDelimiter(r[Column A], s{0}) & s{0},
            p = if s = {} then Text.AfterDelimiter(r[Column A],",") else Text.AfterDelimiter(r[Column A], s{0}),
            Position = Text.Trim(p,{" ",","})
        in 
            [Name = Name, Position = Position], type [Name=text, Position=text]),
    #"Removed Columns" = Table.RemoveColumns(#"Add Name",{"Column A"}),
    #"Expanded Name" = Table.ExpandRecordColumn(#"Removed Columns", "Name", {"Name", "Position"})
in
    #"Expanded Name"

 

I suspect there is a more efficient method, but this should work according to your specifications.

 

Data

 

ronrsnfld_0-1731721786325.png

Results

 

ronrsnfld_1-1731721819920.png

 

 

 

lbendlin
Super User
Super User

How many gazillions of suffixes and their variations do you have?

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Top Solution Authors