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 a word in every list of a single column

I have a column in my table where every cell consists of one list of words. I want to replace certain word with another one from each of these lists but I am not sure how to do that. I have tried similar M language scripts as I do in the case where a column consists of text, but now I get only error messages. 

 

Example:

Column 1
{"Cat", "Dog", "Mouse"}
{"Cat, Dog"}
{"Dog"}
{"Dog", "Tiny Mouse"}

 

So the "elements" in this column are lists. Say I want to change all "Mouse" words to "Rat". The desired result would be:

 

Column 1
{"Cat", "Dog", "Rat"}
{"Cat, "Dog"}
{"Dog"}
{"Dog", "Tiny Rat"}
1 ACCEPTED SOLUTION

Use this

= Table.ReplaceValue(Custom1, each [Column 1], each List.Transform([Column 1],(x)=>Text.Replace(x,"Mouse","Rat")), Replacer.ReplaceValue,{"Column 1"})

View solution in original post

5 REPLIES 5
Vijay_A_Verma
Super User
Super User

Can you please post some sample data from table and also list of replacement words?

Anonymous
Not applicable

Hi, I added an example to my original message. 

See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test

Important for you is the last Table.ReplaceValue.

I have used first Table.ReplaceValue for converting your example values into a list.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wqo5Rck4siVHSUYhRcslPhzB880uLU2OUapVidbCogAtj4aHqjgUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Column 1" = _t]),
    Custom1 = Table.ReplaceValue(Source, each [Column 1], each Expression.Evaluate([Column 1]),Replacer.ReplaceValue,{"Column 1"}),
    Custom2 = Table.ReplaceValue(Custom1, each [Column 1], each List.ReplaceMatchingItems([Column 1],{{"Mouse","Rat"}}), Replacer.ReplaceValue,{"Column 1"})
in
    Custom2

 

Anonymous
Not applicable

Thank you! I used the Table.ReplaceValue line and got it to work. Further, would it be possible to replace some part of a word as well?

 

For example, if one of those lists included a word "Tiny Mouse" and I wanted it to be replaced with "Tiny Rat" as well? 

Use this

= Table.ReplaceValue(Custom1, each [Column 1], each List.Transform([Column 1],(x)=>Text.Replace(x,"Mouse","Rat")), Replacer.ReplaceValue,{"Column 1"})

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