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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
MizzH_UK
New Member

Replacing values within column with if statement and text.startswith

Hi 

Newbie here so please be gentle!

 

I'm using power query (M language). I am trying to modify a simple replace text command so that it is conditional using text.startswith. 

 

Ultimately I want to amend any items in a specified column which start with "X" so that the X is removed (replaced with a space).

 

e.g. 

X1001

X1002

XA200

 

becomes

1001

1002

A200

 

I do not want to add another conditional column.

 

My code (not working)  is as follows:

= Table.ReplaceValue(#"Replaced Value 2") each if Text.StartsWith([COLUMN],"X") then Replacer.ReplaceText([COLUMN],"X","") else [COLUMN])

 

Please can you help me get this working?

 

TIA 

Miriam

1 ACCEPTED SOLUTION
Vijay_A_Verma
Super User
Super User

Use this (Replace Column1 and #"Added Custom" appropriately.

Table.ReplaceValue(#"Added Custom",each [Column1],each if Text.Start([Column1],1)="X" then Text.ReplaceRange([Column1],0,1,"") else [Column1], Replacer.ReplaceValue,{"Column1"})

 

View solution in original post

5 REPLIES 5
Flippadood
New Member

I have a similar problem set.  I am trying to use Text.StartsWith to replace names in the table of a .xml file.  In the column Organization.Subordinate, I want to replace all text that starts with 32539A with 32539AA.  When I right click on the cell and select replace value, I received the output below.  Being new to Power Query I don't understand the context of the answer provided in order to modify for my purposes.  Thanks.

 

= Table.ReplaceValue(#"Expanded Force_Organization","Text.StartWith 32539A","32539AA",Replacer.ReplaceText,{"Organization.Subordinate"})

Anonymous
Not applicable

Hi can this be used for Direct Query or only applicable for Import?

It is applicable for both.

MizzH_UK
New Member

worked well, thank you @Vijay_A_Verma 

Vijay_A_Verma
Super User
Super User

Use this (Replace Column1 and #"Added Custom" appropriately.

Table.ReplaceValue(#"Added Custom",each [Column1],each if Text.Start([Column1],1)="X" then Text.ReplaceRange([Column1],0,1,"") else [Column1], Replacer.ReplaceValue,{"Column1"})

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors