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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
andres_montanag
Regular 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
Regular 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
Regular 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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors