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.
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.
Solved! Go to Solution.
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
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
Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City
Check out the April 2024 Power BI update to learn about new features.