Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

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. 

  • 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

1 Reply

  • Jimmy801's avatar
    Jimmy801
    Icon for Community Champion rankCommunity 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