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.
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"} |
Solved! Go to Solution.
Use this
= Table.ReplaceValue(Custom1, each [Column 1], each List.Transform([Column 1],(x)=>Text.Replace(x,"Mouse","Rat")), Replacer.ReplaceValue,{"Column 1"})
Can you please post some sample data from table and also list of replacement words?
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
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"})
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.