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
Anonymous
Not applicable

Replacing inside the column if the text starts with a certain value

Hi,

 

I'm trying to write a one-line code in Power Query without creating additional columns. 

 

I have a column that looks like this:

 

 

ColumnName
abc
def
1230009
1231099
ghi
1230101

 

I need to add a 0 in front of each "123". 

 

My code looks like this:

 

let

Source = ...,

my_table = Source{...},

Replace_ColumnName = Table.ReplaceValue(my_table, each [ColumnName], each if Text.StartsWith([ColumnName], "301") then Text.Combine("0", [ColumnName]) else [ColumnName], Replacer.ReplaceValue, {"ColumnName"})

in

Replace_ColumnName 

 

Unfortunately, this doesn't work. 

Please let me know if there is a way to fix it. 

1 ACCEPTED SOLUTION
Jimmy801
Community Champion
Community Champion

Hello @Anonymous 

 

You have to use the Table.TransformColumns function. Here the codeexample

let
	Source = #table
	(
		{"ColumnName"},
		{
			{"abc"},	{"def"},	{"1230009"},	{"1231099"},	{"ghi"},	{"1230101"}
		}
	),
    Transfrom = Table.TransformColumns
    (
        Source,
        {
            {
                "ColumnName",
                each if Text.Start(Text.From(_),3)="123" then "0"&Text.From(_) else _
            }
        }
    )
in
	Transfrom

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works. If this solution fits your need, copy and past a part of it and implement it in your query, or I could create a custom function what makes it easier to apply if you are not used that much to power query.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

View solution in original post

1 REPLY 1
Jimmy801
Community Champion
Community Champion

Hello @Anonymous 

 

You have to use the Table.TransformColumns function. Here the codeexample

let
	Source = #table
	(
		{"ColumnName"},
		{
			{"abc"},	{"def"},	{"1230009"},	{"1231099"},	{"ghi"},	{"1230101"}
		}
	),
    Transfrom = Table.TransformColumns
    (
        Source,
        {
            {
                "ColumnName",
                each if Text.Start(Text.From(_),3)="123" then "0"&Text.From(_) else _
            }
        }
    )
in
	Transfrom

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works. If this solution fits your need, copy and past a part of it and implement it in your query, or I could create a custom function what makes it easier to apply if you are not used that much to power query.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

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