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
CNENFRNL I appreciate your effort. The problem with your solution is that for each row the function returns a list, when the expected result is a text.
Jimmy801 thank you very much. Honestly, I've found a way to deal with the problem in another way that works for me, so I haven't been able to test your algorithm. Either way I mark it as a solution to serve others. If anyone finds an error in it, please report it.
Anonymous I appreciate your effort to understand the problem with List.Accumulate. As I mentioned to Jimmy I already managed to solve the problem, but answering your question, substring is not a list of lists, just a list of texts.
hi carlpaul153
about your answer:
"@Rocco_sprmnt21 I appreciate your effort to understand the problem with List.Accumulate. As I mentioned to Jimmy I already managed to solve the problem, but answering your question, substring is not a list of lists, just a list of texts."
let me have doubts about what you say.
While waiting for you to document that type of error that you mentioned in correspondence with the code you posted in the first message, I explain the reasons for my doubts, hoping that they will be useful.
the variable substring=list of string as you claim:
the error message you got with your code and string list is different from the one you mentioned.
If you had modified your code as suggested by me (it was enough to delete only a small part of the text), you would have obtained this result:
if instead the variable substring contains lists inside it:
you get exactly the error you mentioned:
"List.Accumulate(substring, [Column1],(state, current) => Text.Replace(state, substring{current}, ""))
but i get the follow error:
Expression.Error: We cannot apply indexing to the type List."
PS
Apart from all these details, the solution with list.accumulate is very beautiful and elegant.