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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Anonymous
Not applicable

Reverse name and last name but with a constraint

I want to reverse the first and last name in a column but there are sentences in that same column. I don't want to touch the sentence but recover them as they are. (If possible in M) Thank you 

 

charlyNeo_0-1645714668454.png

 

1 ACCEPTED SOLUTION
PijushRoy
Super User
Super User

Hi @Anonymous 

You can try these power query steps
#"Duplicated Column" = Table.DuplicateColumn(#"Changed Type1", "Agents", "Agents - Copy"),
#"Split Column by Delimiter" = Table.SplitColumn(#"Duplicated Column", "Agents - Copy", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"Agents - Copy.1", "Agents - Copy.2", "Agents - Copy.3", "Agents - Copy.4"}),
#"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Agents - Copy.1", type text}, {"Agents - Copy.2", type text}, {"Agents - Copy.3", type text}, {"Agents - Copy.4", type text}}),
#"Added Conditional Column" = Table.AddColumn(#"Changed Type2", "Custom", each if [#"Agents - Copy.4"] = null then 1 else 0),
#"Added Custom" = Table.AddColumn(#"Added Conditional Column", "Final Column", each if [Custom] = 1 then ([#"Agents - Copy.2"]&" "&[#"Agents - Copy.1"]) else [Agents]),
#"Trimmed Text" = Table.TransformColumns(#"Added Custom",{{"Agents - Copy.2", Text.Trim, type text}}),
#"Removed Columns" = Table.RemoveColumns(#"Trimmed Text",{"Agents - Copy.1", "Agents - Copy.2", "Agents - Copy.3", "Agents - Copy.4", "Custom"})
in
#"Removed Columns"

0pb1.JPG

 

If you have any question, please let me know.
If solve your requirement, please mark this answer as SOLUTION
If this comment helps you, please LIKE this comment/Kudos




Did I answer your question? Mark my post as a solution!
Appreciate your Like/Kudos

Proud to be a Super User!





View solution in original post

3 REPLIES 3
PijushRoy
Super User
Super User

Hi @Anonymous 

You can try these power query steps
#"Duplicated Column" = Table.DuplicateColumn(#"Changed Type1", "Agents", "Agents - Copy"),
#"Split Column by Delimiter" = Table.SplitColumn(#"Duplicated Column", "Agents - Copy", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"Agents - Copy.1", "Agents - Copy.2", "Agents - Copy.3", "Agents - Copy.4"}),
#"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Agents - Copy.1", type text}, {"Agents - Copy.2", type text}, {"Agents - Copy.3", type text}, {"Agents - Copy.4", type text}}),
#"Added Conditional Column" = Table.AddColumn(#"Changed Type2", "Custom", each if [#"Agents - Copy.4"] = null then 1 else 0),
#"Added Custom" = Table.AddColumn(#"Added Conditional Column", "Final Column", each if [Custom] = 1 then ([#"Agents - Copy.2"]&" "&[#"Agents - Copy.1"]) else [Agents]),
#"Trimmed Text" = Table.TransformColumns(#"Added Custom",{{"Agents - Copy.2", Text.Trim, type text}}),
#"Removed Columns" = Table.RemoveColumns(#"Trimmed Text",{"Agents - Copy.1", "Agents - Copy.2", "Agents - Copy.3", "Agents - Copy.4", "Custom"})
in
#"Removed Columns"

0pb1.JPG

 

If you have any question, please let me know.
If solve your requirement, please mark this answer as SOLUTION
If this comment helps you, please LIKE this comment/Kudos




Did I answer your question? Mark my post as a solution!
Appreciate your Like/Kudos

Proud to be a Super User!





Hi Piyush,

 

If I need to swap the first and last names in the cell and also while swapping if I need to have the final format like last name ,First name in caps. (Lastname space comma first letter of first name in caps and end with a period) do we have the option to make the transform query changed in the same way. Now this is part of the requirement where i will have manys excels and then I need to load them and change few columns according to the required values like extracting first two chars of a column, for one of the columns after extraction converting them into upper case. and the above one for first and last name in the same sheet

Anonymous
Not applicable

Thank you, this answers a lot. There is one more point. How to manage people with several first and last names but I think we can't really calculate it. Except with an additional indicator in a pre-existing column that would indicate that in a specific case it is a compound name.

Helpful resources

Announcements
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 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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