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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Create New Column by Extracting Values from Multiple Columns

The goal is to extract 5 digit value from multiple columns and create a new column with the extracted values. For more context, below is sample data. I am familiar with accomplishing this task after loading data into PBIX and using SWITCH function to accomplish this. However this scenario requires me to accomplish this task while in Power Query prior to data load. Any advice will be greatly appreciated. 

 

Sample Data:

EntityAssignee

Steph Curry

MGR12345
Michael JordanMGR12346
Lebron JamesMGR12347
Kobe BryantMGR12348
12349_Ops9988073

Expected Results: Notice new column "Store_Number". The new column comprises of extracted values from both Entity + Assignee.

EntityAssigneeStore_Number
Steph CurryMGR1234512345
Michael JordanMGR1234612346
Lebron JamesMGR1234712347
Kobe BryantMGR1234812348
12349_Ops998807312349
2 ACCEPTED SOLUTIONS

No worries. 
Here is the code that will look for MGR first...

= Table.AddColumn(#"Changed Type", "Store", each if Text.Contains([Assignee], "MGR") then Text.AfterDelimiter([Assignee], "MGR") else if Text.PositionOfAny([Entity], {"0".."9"}) >= 0 then Text.Select([Entity], {"0".."9"}) else null, type text)




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





View solution in original post

I would use Text.Trim in a subsequent step in the query. 

If you select the Store_Number column and select Transform->Format->Trim in the ribbon you will end up with code that looks like...

 

= Table.TransformColumns(Custom1, {{"Store_Number", Text.Trim, type text}})

 

change that code to...

 

= Table.TransformColumns(Custom1, {{"Store_Number", each Text.TrimStart(_, "0"), type text}})

 

and it will trim any "0" from the start of the string.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





View solution in original post

6 REPLIES 6
jgeddes
Super User
Super User

There is not enough sample data provided to give a definitive solution but as starting point the following code will check the Entity column if it contains numbers and extracts them if they exist. If the Entity column does not contain numbers then numbers are extracted from the Assignee column.

= Table.AddColumn(#"Changed Type", "Custom", each if Text.PositionOfAny([Entity], {"0".."9"}) >= 0 then Text.Select([Entity], {"0".."9"}) else Text.Select([Assignee], {"0".."9"}), type number)

I would suggest you read up on the Text Functions of M to help you with your actual solution.
Text Functions in M - Microsoft Learn 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Anonymous
Not applicable

@jgeddes Thank you so much for your support. Lets look at it in a different angle. How would you frame this if you were to look at the ASSIGNEE column first. If "MGR" is present in the ASSIGNEE column, then extract the 5 digit values. If "MGR" is not present, are there numbers in the ENTITY column. Is there a way to update your sample code that would look at it in this angle?

 

Apologies if this seems very amature, M is very foreign to me. Your support is greatly appreciated. 

No worries. 
Here is the code that will look for MGR first...

= Table.AddColumn(#"Changed Type", "Store", each if Text.Contains([Assignee], "MGR") then Text.AfterDelimiter([Assignee], "MGR") else if Text.PositionOfAny([Entity], {"0".."9"}) >= 0 then Text.Select([Entity], {"0".."9"}) else null, type text)




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Anonymous
Not applicable

@jgeddes Thank you again for your support. Question for you...if the 5 digit value has a 0 in front of it (ex: 02495) and I want to get the 4 digit value (ex: 2495), how can I achieve this? Or even 3 digit (ex: 00912 >> expected output: 912). I am aware that this can be done if the type is switched to number but I have to keep the type as text. A slight tweak to what you provided and it is almost complete. Any thoughts? 

 

= Table.AddColumn(#"Changed Type", "Store_Number", each if Text.Contains([Assignee], "MGR") then Text.AfterDelimiter([Assignee], "MGR") else if Text.PositionOfAny([Entity], {"0".."9"}) >= 0 then Text.Middle([Entity], 0, 5) else null, type text)

 

 

I would use Text.Trim in a subsequent step in the query. 

If you select the Store_Number column and select Transform->Format->Trim in the ribbon you will end up with code that looks like...

 

= Table.TransformColumns(Custom1, {{"Store_Number", Text.Trim, type text}})

 

change that code to...

 

= Table.TransformColumns(Custom1, {{"Store_Number", each Text.TrimStart(_, "0"), type text}})

 

and it will trim any "0" from the start of the string.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!





Anonymous
Not applicable

@jgeddes Outstanding! Thank you!

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors