Forum Discussion
How to remove text strings stored in a list from a column
- 6 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
Thanks @carlpaul153 for the question, Anonymous for comment and a solution, and Jimmy801 for the code above.
I do something very similar. I am processing e-mails to count topics defined by the subject line. I want to remove duplicates - e-mails that contain either "FW: " or "RE:" ("Fw: " and "Re:"), place the trimmed subject in a new column, and then use 'Table.Distinct' on the new column to remove duplicates.
I have adapted Jimmy801 code to a 'Table.Addcolumn'. Here is the relevant code - [Subject] is the column with the subject line from the e-mails. I found that 'List.Accumulate' returned a list, the first element was the processed code, the second was from the substring list. Consequence of using Jimmy801's code as his does a few extra things.
SubstringList = {"FW: ", "Fw: ", "RE: ", "Re: "},
AddTitle = Table.AddColumn(SetTypeCompactTable, "Title", each
List.Accumulate( SubstringList, {[Subject], ""}, (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
}
) {0}, Text.Type ),
This works.