Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
andres_montanag
Frequent Visitor

pass a column name as variable

Hi guys, I need your knowledge 😄

 

I am trying to pass the name of a column as a variable to create a table from a custom function.

this M code work:

 

(tabla as table, fecha as date, name as text) =>
    let
        nameColumn = "Full Name",
        Origen = tabla,  
        #"Filtro" = Table.SelectRows(Origen, each (  [Full Name] = name) and ( [Created Date] = fecha))    
    in
        #"Filtro"

 

but I would like something like this:

 

(tabla as table, fecha as date, name as text,nameColumn) =>
    let
        Origen = tabla,
        #"Filtro" = Table.SelectRows(Origen, each (  [nameColumn] = name) and ( [Created Date] = fecha))    
    in
        #"Filtro"

 

 

but I get the following error; "Expression.Error: The field 'nameColumn' of the record wasn't found"

 

Please any idea why the variable is not recognised in the filter context?

1 ACCEPTED SOLUTION
andres_montanag
Frequent Visitor

Hi, I found the solution in this post.

My code is as follows:

 

(tabla as table,nameColumn as text, fecha as date, name as text) =>
    let
        Origen = tabla,
        #"Filtro" = Table.SelectRows(Origen, each (  Record.Field(_,nameColumn) = name) and ( [Created Date] = fecha))    
    in
        #"Filtro"

 

the solution is the function:

Record.Field(record as record, field as text) as any

 

View solution in original post

2 REPLIES 2
andres_montanag
Frequent Visitor

Hi, I found the solution in this post.

My code is as follows:

 

(tabla as table,nameColumn as text, fecha as date, name as text) =>
    let
        Origen = tabla,
        #"Filtro" = Table.SelectRows(Origen, each (  Record.Field(_,nameColumn) = name) and ( [Created Date] = fecha))    
    in
        #"Filtro"

 

the solution is the function:

Record.Field(record as record, field as text) as any

 

Yep. And the function Table.Column is table equivalent of Record.Field (it returns a list rather than a single value).

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.