Using a Variable as a Column Name in Power Query

Todd Bocik 141 Reputation points
2021-02-01T13:19:31.56+00:00

I have a table that contains many columns of data. Based on certain criteria, I would like to get a specific columns worth of data. This means that I need to use a variable for the selected column name. I have other parts of the logic working so I will simplify my issue down to this example.

let
Source = Table1,
TestColumnName = "HC-2 Score",
#"Added Conditional Column" = Table.AddColumn(Source, "Test Score", each if [Job Title] = "DIRECTOR" then [TestColumnName] else null)
in
#"Added Conditional Column"

The line below works but is not dynamic. I need to choose the column to retrieve the scores.

"Added Conditional Column" = Table.AddColumn(Source, "Test Score", each if [Job Title] = "DIRECTOR" then [#"HC-2 Score"] else null)

What is the solution to this problem?

Community Center | Not monitored
0 comments No comments
{count} votes

Answer accepted by question author
  1. Lz._ 38,106 Reputation points Volunteer Moderator
    2021-02-01T13:47:01.853+00:00

    @Todd Bocik

    If problem understood you're looking for something like:

    let  
        Source = Table1,  
        VarColumnName = "HC-2 Score",  
        #"Added Conditional Column" = Table.AddColumn(Source, "Test Score", each  
            if [Job Title] = "DIRECTOR" then Record.Field(_, VarColumnName) else null  
        )  
    in  
        #"Added Conditional Column"  
    
    5 people found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Todd Bocik 141 Reputation points
    2021-02-01T13:51:34.1+00:00

    Thank you so much. It worked perfectly!


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.