Forum Discussion

WilkoVanA's avatar
WilkoVanA
Frequent Visitor
8 years ago

Passing a column as parameter to a function

Hi,

 

Something I have a workaround for but not happy with it is when I try to use custom function in Power BI and whant the name of the column as a parameter. In the example below:

fnParseColumn = (parTable as table, parNewColumnName as text) =>
    let
       AddParsedNameColumn = Table.AddColumn(parTable, "Name Trimmed", each if [Name To Parse] = null then "" else fnParseName([Name To Parse]), type text),
        RemoveOldColumn = Table.RenameColumns(Table.RemoveColumns(AddParsedNameColumn, {"Name To Parse"}),{{"Name Trimmed", parNewColumnName}})
    in
        RemoveOldColumn,

 

There is a column in the table (called "Name To Parse") in the original table that is created by renaming a column to ensure the function to pick the right one. How can I pass the name of that column as a parameter and then (more importantly) how can I use it in the statements above?

 

Any help is greatly appreciated.

Wilko

3 Replies

  • WilkoVanA's avatar
    WilkoVanA
    Frequent Visitor

    Hi,

     

    Something I have a workaround for but not happy with it is when I try to use custom function in Power BI and whant the name of the column as a parameter. In the example below:

    fnParseColumn = (parTable as table, parNewColumnName as text) =>
        let
           AddParsedNameColumn = Table.AddColumn(parTable, "Name Trimmed", each if [Name To Parse] = null then "" else fnParseName([Name To Parse]), type text),
            RemoveOldColumn = Table.RenameColumns(Table.RemoveColumns(AddParsedNameColumn, {"Name To Parse"}),{{"Name Trimmed", parNewColumnName}})
        in
            RemoveOldColumn,

     

    There is a column in the table (called "Name To Parse") in the original table that is created by renaming a column to ensure the function to pick the right one. How can I pass the name of that column as a parameter and then (more importantly) how can I use it in the statements above?

     

    Any help is greatly appreciated.

    Wilko

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    Why not just pass the entire column in versus the name using something like Table.SelectColumns? Or I may be missing something.

    • WilkoVanA's avatar
      WilkoVanA
      Frequent Visitor

      Thanks for the suggestion. I tried that but didn't succeed in getting the syntax right. What is the type that is passed to the function? How do I refer to it in the function?

       

      If you have some hints or code snippets as examples....

       

      Regards,

      Wilko