<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic using a dynamic column name in Table.AddColumn columnGenerator in Power Query</title>
    <link>https://community.fabric.microsoft.com/t5/Power-Query/using-a-dynamic-column-name-in-Table-AddColumn-columnGenerator/m-p/2380835#M71283</link>
    <description>&lt;P&gt;Hi, all,&lt;/P&gt;&lt;P&gt;I would like to build on a very useful query Chriss Webb published on his blog (&lt;A href="https://blog.crossjoin.co.uk/2014/06/25/using-list-generate-to-make-multiple-replacements-of-words-in-text-in-power-query/" target="_blank" rel="noopener"&gt;Using List.Generate() To Make Multiple Replacements Of Words In Text In Power Query&lt;/A&gt;). I would like to turn it into a function so that I could easily reuse it in different projects.&lt;/P&gt;&lt;P&gt;This will be a signature of the function:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;(TableToChange as table, ColumnToMakeReplacements as text, ReplacementsTable as table, optional SearchForColumnName as text, optional ReplaceWithColumnName as text) as table =&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, I cannot figure out how I can provide a variable to a columnGenerator in the last step called &lt;STRONG&gt;Output&lt;/STRONG&gt;. See this function-like query:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;let
    // Create a table with text to be replaced
    Source = Json.Document("[{""Text"":""the cat sat on the mat""},{""Text"":""the cat sat next to the dog""},{""Text"":""the dog chased the cat""},{""Text"":""the dog sat on the mat""},{""Text"":""the catamaran sails through the air""}]"),
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), {"Text"}, null, ExtraValues.Error),
    #"Expanded Source Text" = Table.ExpandRecordColumn(#"Converted to Table", "Text", {"Text"}, {"Text"}),

    // Create a table of replacement pairs
    Source2 = Json.Document("[{""Word To Replace"":""cat"",""Replace With"":""bear""},{""Word To Replace"":""mat"",""Replace With"":""chair""},{""Word To Replace"":""dog"",""Replace With"":""dragon""},{""Word To Replace"":""the"",""Replace With"":""THE""},{""Word To Replace"":""air"",""Replace With"":""water""}]"),
    #"Converted to Table2" = Table.FromList(Source2, Splitter.SplitByNothing(), {"ReplacementsTable"}, null, ExtraValues.Error),
    #"Expanded ReplacementsTable" = Table.ExpandRecordColumn(#"Converted to Table2", "ReplacementsTable", {"Word To Replace", "Replace With"}, {"Word To Replace", "Replace With"}),

    // Initialize variables
    TableToChange = #"Expanded Source Text", 
    ColumnToMakeReplacements = "Text",
    ReplacementsTable = #"Expanded ReplacementsTable",
    SearchForColumnName = "Word To Replace",
    ReplaceWithColumnName = null,

    //Get table of word replacements
    Replacements = ReplacementsTable,
    //Get list of words to replace
    WordsToReplace = 
        if SearchForColumnName = null then 
            List.Buffer(Replacements[Search For])
        else 
            List.Buffer(Table.Column(Replacements, SearchForColumnName)),
    //Get list of words to replace them with
    WordsToReplaceWith = 
        if ReplaceWithColumnName = null then 
            List.Buffer(Replacements[Replace With])
        else 
            List.Buffer(Table.Column(Replacements, ReplaceWithColumnName)),
    //A non-recursive function to do the replacements
    ReplacementFunction = (InputText)=&amp;gt; 
     let
       //Use List.Generate() to do the replacements
       DoReplacement = List.Generate(
                          ()=&amp;gt; [Counter=0, MyText=InputText], 
                          each [Counter]&amp;lt;=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(TableToChange, ColumnToMakeReplacements &amp;amp; " Replaced Text", each ReplacementFunction(Table.Column(TableToChange, ColumnToMakeReplacements)))
in
    Output&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The last step works if the column name is hard-coded:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Output = Table.AddColumn(TableToChange, ColumnToMakeReplacements &amp;amp; " Replaced Text", each ReplacementFunction([Text]))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I know that with this syntax, the columnGenerator always passes just one row (or one value) on each iteration of DoReplacement function. It seems that when I provide the column using Table.Column function, it passes the whole column instead of a row.&lt;/P&gt;&lt;P&gt;I do not know how should I adjust it to make of the column I provide to the columnGenerator dynamic. I am sure that it is a no-brainer for experienced Power Query users, but I got stuck on this. Appreciate your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edit: I realized that even though the Chris Webb's blog was one of the sources used in this solution, I have actually modified a solution from &lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="1522" data-lia-user-login="ImkeF" class="lia-mention lia-mention-user"&gt;ImkeF&lt;/a&gt; that can be found here: &lt;A href="https://www.thebiccountant.com/2016/05/22/multiple-replacements-in-power-bi-and-power-query/" target="_self"&gt;Multiple replacements or translations in Power BI and Power Query&lt;/A&gt;. Sorry for attributing incomplete credits.&lt;/P&gt;</description>
    <pubDate>Wed, 09 Mar 2022 10:56:40 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2022-03-09T10:56:40Z</dc:date>
    <item>
      <title>using a dynamic column name in Table.AddColumn columnGenerator</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/using-a-dynamic-column-name-in-Table-AddColumn-columnGenerator/m-p/2380835#M71283</link>
      <description>&lt;P&gt;Hi, all,&lt;/P&gt;&lt;P&gt;I would like to build on a very useful query Chriss Webb published on his blog (&lt;A href="https://blog.crossjoin.co.uk/2014/06/25/using-list-generate-to-make-multiple-replacements-of-words-in-text-in-power-query/" target="_blank" rel="noopener"&gt;Using List.Generate() To Make Multiple Replacements Of Words In Text In Power Query&lt;/A&gt;). I would like to turn it into a function so that I could easily reuse it in different projects.&lt;/P&gt;&lt;P&gt;This will be a signature of the function:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;(TableToChange as table, ColumnToMakeReplacements as text, ReplacementsTable as table, optional SearchForColumnName as text, optional ReplaceWithColumnName as text) as table =&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, I cannot figure out how I can provide a variable to a columnGenerator in the last step called &lt;STRONG&gt;Output&lt;/STRONG&gt;. See this function-like query:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;let
    // Create a table with text to be replaced
    Source = Json.Document("[{""Text"":""the cat sat on the mat""},{""Text"":""the cat sat next to the dog""},{""Text"":""the dog chased the cat""},{""Text"":""the dog sat on the mat""},{""Text"":""the catamaran sails through the air""}]"),
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), {"Text"}, null, ExtraValues.Error),
    #"Expanded Source Text" = Table.ExpandRecordColumn(#"Converted to Table", "Text", {"Text"}, {"Text"}),

    // Create a table of replacement pairs
    Source2 = Json.Document("[{""Word To Replace"":""cat"",""Replace With"":""bear""},{""Word To Replace"":""mat"",""Replace With"":""chair""},{""Word To Replace"":""dog"",""Replace With"":""dragon""},{""Word To Replace"":""the"",""Replace With"":""THE""},{""Word To Replace"":""air"",""Replace With"":""water""}]"),
    #"Converted to Table2" = Table.FromList(Source2, Splitter.SplitByNothing(), {"ReplacementsTable"}, null, ExtraValues.Error),
    #"Expanded ReplacementsTable" = Table.ExpandRecordColumn(#"Converted to Table2", "ReplacementsTable", {"Word To Replace", "Replace With"}, {"Word To Replace", "Replace With"}),

    // Initialize variables
    TableToChange = #"Expanded Source Text", 
    ColumnToMakeReplacements = "Text",
    ReplacementsTable = #"Expanded ReplacementsTable",
    SearchForColumnName = "Word To Replace",
    ReplaceWithColumnName = null,

    //Get table of word replacements
    Replacements = ReplacementsTable,
    //Get list of words to replace
    WordsToReplace = 
        if SearchForColumnName = null then 
            List.Buffer(Replacements[Search For])
        else 
            List.Buffer(Table.Column(Replacements, SearchForColumnName)),
    //Get list of words to replace them with
    WordsToReplaceWith = 
        if ReplaceWithColumnName = null then 
            List.Buffer(Replacements[Replace With])
        else 
            List.Buffer(Table.Column(Replacements, ReplaceWithColumnName)),
    //A non-recursive function to do the replacements
    ReplacementFunction = (InputText)=&amp;gt; 
     let
       //Use List.Generate() to do the replacements
       DoReplacement = List.Generate(
                          ()=&amp;gt; [Counter=0, MyText=InputText], 
                          each [Counter]&amp;lt;=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(TableToChange, ColumnToMakeReplacements &amp;amp; " Replaced Text", each ReplacementFunction(Table.Column(TableToChange, ColumnToMakeReplacements)))
in
    Output&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The last step works if the column name is hard-coded:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Output = Table.AddColumn(TableToChange, ColumnToMakeReplacements &amp;amp; " Replaced Text", each ReplacementFunction([Text]))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I know that with this syntax, the columnGenerator always passes just one row (or one value) on each iteration of DoReplacement function. It seems that when I provide the column using Table.Column function, it passes the whole column instead of a row.&lt;/P&gt;&lt;P&gt;I do not know how should I adjust it to make of the column I provide to the columnGenerator dynamic. I am sure that it is a no-brainer for experienced Power Query users, but I got stuck on this. Appreciate your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Edit: I realized that even though the Chris Webb's blog was one of the sources used in this solution, I have actually modified a solution from &lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="1522" data-lia-user-login="ImkeF" class="lia-mention lia-mention-user"&gt;ImkeF&lt;/a&gt; that can be found here: &lt;A href="https://www.thebiccountant.com/2016/05/22/multiple-replacements-in-power-bi-and-power-query/" target="_self"&gt;Multiple replacements or translations in Power BI and Power Query&lt;/A&gt;. Sorry for attributing incomplete credits.&lt;/P&gt;</description>
      <pubDate>Wed, 09 Mar 2022 10:56:40 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/using-a-dynamic-column-name-in-Table-AddColumn-columnGenerator/m-p/2380835#M71283</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2022-03-09T10:56:40Z</dc:date>
    </item>
    <item>
      <title>Re: using a dynamic column name in Table.AddColumn columnGenerator</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/using-a-dynamic-column-name-in-Table-AddColumn-columnGenerator/m-p/2387930#M71546</link>
      <description>&lt;P&gt;Hi&amp;nbsp;Anonymous&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think it seems a bit difficult, because the&amp;nbsp;ReplacementFunction provided in the blog also refer to the columns in the data source.&amp;nbsp;So it's a bit difficult to say dynamic.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best Regards,&lt;/P&gt;
&lt;P&gt;Stephen Tao&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this post &lt;STRONG&gt;helps&lt;/STRONG&gt;, then please consider&lt;STRONG&gt;&lt;EM&gt; Accept it as the solution &lt;/EM&gt;&lt;/STRONG&gt;to help the other members find it more quickly.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Mar 2022 07:16:43 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/using-a-dynamic-column-name-in-Table-AddColumn-columnGenerator/m-p/2387930#M71546</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2022-03-11T07:16:43Z</dc:date>
    </item>
    <item>
      <title>Re: using a dynamic column name in Table.AddColumn columnGenerator</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/using-a-dynamic-column-name-in-Table-AddColumn-columnGenerator/m-p/2388113#M71552</link>
      <description>&lt;P&gt;Okay, so it is not so straightforward as I originally thought. Sad to hear that.&lt;/P&gt;&lt;P&gt;Anyway, thank you for taking time to answer.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Mar 2022 08:27:08 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/using-a-dynamic-column-name-in-Table-AddColumn-columnGenerator/m-p/2388113#M71552</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2022-03-11T08:27:08Z</dc:date>
    </item>
    <item>
      <title>Re: using a dynamic column name in Table.AddColumn columnGenerator</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/using-a-dynamic-column-name-in-Table-AddColumn-columnGenerator/m-p/2388155#M71556</link>
      <description>&lt;P&gt;Replace your output line with this&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Output = Table.AddColumn(TableToChange, ColumnToMakeReplacements &amp;amp; " Replaced Text", each ReplacementFunction(Record.Field(_,ColumnToMakeReplacements)))&lt;/P&gt;</description>
      <pubDate>Fri, 11 Mar 2022 08:43:41 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/using-a-dynamic-column-name-in-Table-AddColumn-columnGenerator/m-p/2388155#M71556</guid>
      <dc:creator>Vijay_A_Verma</dc:creator>
      <dc:date>2022-03-11T08:43:41Z</dc:date>
    </item>
    <item>
      <title>Re: using a dynamic column name in Table.AddColumn columnGenerator</title>
      <link>https://community.fabric.microsoft.com/t5/Power-Query/using-a-dynamic-column-name-in-Table-AddColumn-columnGenerator/m-p/2388207#M71561</link>
      <description>&lt;P&gt;Hi &lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="317743" data-lia-user-login="Vijay_A_Verma" class="lia-mention lia-mention-user"&gt;Vijay_A_Verma&lt;/a&gt;, that was it. I hoped that some slight adjustment should do the trick and here we are! It works like a charm. Thanks a lot for your help.&lt;/P&gt;</description>
      <pubDate>Fri, 11 Mar 2022 09:06:22 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Power-Query/using-a-dynamic-column-name-in-Table-AddColumn-columnGenerator/m-p/2388207#M71561</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2022-03-11T09:06:22Z</dc:date>
    </item>
  </channel>
</rss>

