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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

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. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h

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. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h
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
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.