El Cid
Member
- Joined
- Aug 22, 2016
- Messages
- 52
- Reaction score
- 0
- Points
- 6
- Location
- Greenville, SC
- Excel Version(s)
- Excel 2016
I tried to follow this - https://blog.crossjoin.co.uk/2014/06/25/using-list-generate-to-make-multiple-replacements-of-words-in-text-in-power-query/
Here's my M-Code:
let
//Get table of word replacements - Old Text and New Text - tblRenameNew is a CSV file that has columns OLDNAME and NEWNAME
Replacements = tblRenameNew,
//Get table containing BAD text to change - SALES_NEW01 contains two columns JOBID and SALES
TextToChange = SALES_NEW01,
//Get list of words to replace - OLDNAME from tblRenameNew
WordsToReplace = Table.Column(Replacements,"OLDNAME"),
//Get list of words to replace them with - NEWNAME from tblRenameNew
WordsToReplaceWith = Table.Column(Replacements, "NEWNAME"),
//A non-recursive function to do the replacements
ReplacementFunction = (InputText)=>
let
//Use List.Generate() to do the replacements
DoReplacement = List.Generate(
()=> [Counter=0, MyText=InputText],
each [Counter]<=List.Count(WordsToReplaceWith),
each [Counter=[Counter]+1,
MyText=Text.Replace(
[MyText],
WordsToReplace{[Counter]},
WordsToReplaceWith{[Counter]})],
each [MyText]),
//Return the last item in the list that List.Generate() returns
GetLastValue = List.Last(DoReplacement)
in
GetLastValue,
//Add a calculated column to call the function on every row in the table containing the text to change
Output = Table.AddColumn(TextToChange, "Changed Text", each ReplacementFunction([Text]))
in
Output
This is what I end up with...
Obviously, I'm not understanding how this function is working. Can someone point me in the right direction?
Here's my M-Code:
let
//Get table of word replacements - Old Text and New Text - tblRenameNew is a CSV file that has columns OLDNAME and NEWNAME
Replacements = tblRenameNew,
//Get table containing BAD text to change - SALES_NEW01 contains two columns JOBID and SALES
TextToChange = SALES_NEW01,
//Get list of words to replace - OLDNAME from tblRenameNew
WordsToReplace = Table.Column(Replacements,"OLDNAME"),
//Get list of words to replace them with - NEWNAME from tblRenameNew
WordsToReplaceWith = Table.Column(Replacements, "NEWNAME"),
//A non-recursive function to do the replacements
ReplacementFunction = (InputText)=>
let
//Use List.Generate() to do the replacements
DoReplacement = List.Generate(
()=> [Counter=0, MyText=InputText],
each [Counter]<=List.Count(WordsToReplaceWith),
each [Counter=[Counter]+1,
MyText=Text.Replace(
[MyText],
WordsToReplace{[Counter]},
WordsToReplaceWith{[Counter]})],
each [MyText]),
//Return the last item in the list that List.Generate() returns
GetLastValue = List.Last(DoReplacement)
in
GetLastValue,
//Add a calculated column to call the function on every row in the table containing the text to change
Output = Table.AddColumn(TextToChange, "Changed Text", each ReplacementFunction([Text]))
in
Output
This is what I end up with...
Obviously, I'm not understanding how this function is working. Can someone point me in the right direction?