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
OsmanEmi
Frequent Visitor

Using OR function within Text.replace function - Power Query

Hi,

I'm sort of new to power query and while adding a new column I used the below function which works, however I wanted to ask if their was a way to also replace more than one value.

= [#"DestinationType"] & " | " & Text.Replace([#Destination Name], "Spend", "Facts")

Would need to be

= [#"DestinationType"] & " | " & Text.Replace([#Destination Name], "Spend" OR "App" OR "Card" , "Facts")

Is this possible to do in one step?

1 ACCEPTED SOLUTION
v-rzhou-msft
Community Support
Community Support

Hi @OsmanEmi 

I suggest you to build a replacement table then try my M code. Currently, you will replace all words by the same word "Fact". This way will still work if you want to replace multiple words by different words.

My Sample:

1.png

Replacement Table:

2.png

Use M Code to add a custom column:

Text.Combine(
    {
  [Destination Type],
  List.Accumulate(
    List.Numbers(0,Table.RowCount(replacements)),
    [Destination Name],
    (state, current) =>
    Text.Replace(state, replacements[Find]{current},
    replacements[Replace]{current}))
    },
    " | " )

Result is as below.

2.png

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
v-rzhou-msft
Community Support
Community Support

Hi @OsmanEmi 

I suggest you to build a replacement table then try my M code. Currently, you will replace all words by the same word "Fact". This way will still work if you want to replace multiple words by different words.

My Sample:

1.png

Replacement Table:

2.png

Use M Code to add a custom column:

Text.Combine(
    {
  [Destination Type],
  List.Accumulate(
    List.Numbers(0,Table.RowCount(replacements)),
    [Destination Name],
    (state, current) =>
    Text.Replace(state, replacements[Find]{current},
    replacements[Replace]{current}))
    },
    " | " )

Result is as below.

2.png

Best Regards,
Rico Zhou

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

hnguy71
Memorable Member
Memorable Member

Here's an alternative solution:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUQrOz00tycjMS1cILkjNS1EwNDJWitWByDk6OcPZSfn5BQpJqakFCo4FBSiqnBOLULW5uLopxcYCAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [DestinationType = _t, #"Destination Name" = _t]),
    ConcatCol = Table.AddColumn(Source, "MyNewColumn", 
        each 
            [DestinationType] & " | " & 
            Text.Combine(
                List.ReplaceMatchingItems(Text.Split([Destination Name], " "), {{"APP", "Facts"}, {"SPEND", "Facts"}, {"CARD", "Facts"}}, Comparer.OrdinalIgnoreCase), 
                " ")
        )
in
    ConcatCol

 



Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!
goncalogeraldes
Super User
Super User

Hello there @OsmanEmi ! Try this and see if it works for your case:

 

 

= [#"DestinationType"] & " | " & Table.ReplaceValue([Your table name], each if Text.Contains([#Destination Name], {"Spend", "App", "Card"} then "Facts" else [Destination Name], Replacer.ReplaceValue, {"Destination Name"})

 

 

Alternatively, you can check this thread for a similar solution! Or this one!

 

Hope this answer solves your problem! If you need any additional help please tag me in your reply.
If my reply provided you with a solution, pleased mark it as a solution ✔️ or give it a kudoe 👍
Thanks!

Best regards,
Gonçalo Geraldes

 

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.