Forum Discussion
How to remove text strings stored in a list from a column
- 5 years ago
Hello carlpaul153
check out this solution. this code replaces all substrings defined in the step SubstringList transforms the column1 into a list with two items. first is new value, second is the removed part. Then duplicated the column and transform them again. Extracting in the column1 the first item, in the changes-column the 2nd item
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WckxKVorViVZKSU0D0+kZmWC6AgTArCoQUIqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}), SubstringList = {"bc", "xx", "zzz"}, TranformColumn1 = Table.TransformColumns ( #"Changed Type", { { "Column1", (cell)=> List.Accumulate(SubstringList, {cell, ""}, (old, current)=> let CreateNew = Text.Replace(old{0},current,""), CreateSubstringsRemoved = try Text.Combine(List.Difference(Text.ToList(old{0}), Text.ToList(CreateNew))) otherwise "" in {CreateNew, if CreateSubstringsRemoved <> "" and old{1}<> "" then old{1} & ", " & CreateSubstringsRemoved else if old{1} <>"" and CreateSubstringsRemoved = "" then old{1} else CreateSubstringsRemoved } ) } } ), Duplicated = Table.DuplicateColumn(TranformColumn1, "Column1", "changes"), Final = Table.TransformColumns ( Duplicated, { { "Column1", each _{0} }, { "changes", each _{1} } } ) in FinalCopy paste this code to the advanced editor in a new blank query to see how the solution works.
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
Hi carlpaul153
the error you got with the following expression
List.Accumulate(substring, [Column1],(state, current) => Text.Replace(state, substring{current}, ""))
is most likely due to the following fact.
Mine, for lack of complete information, is an attempt to reconstruct what you got based on what I know to be how the List.Accumulate function works.
the third parameter of the function, which is itself a function, takes the value of the avriable "current" from the list which is the first parameter of the function, in our case "substring".
If, as it likely is but we are waiting for your confirmation, the substring list is a list of lists, then you get the error you reported.
The syntactically correct form would be the following:
List.Accumulate(substring, [Column1],(state, current) => Text.Replace(state, current, ""))
in this way, in each step, you replace the i-th substring with "" inside the string contained in the current row of column1.
But this, if my supposition is correct, is not enough.
You probably need to change the structure of the list as well:
substring, which must be a list of string, like this:
{"ad", "b","hg"}
not a list of lists,
like this
{{"ad"}, {"b"},"hg"}
But all this is my guess.