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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
smann
Helper I
Helper I

Can I refer to Table using String?

Here's what I tried to do that didn't work.

 

Replace this:

 

NewTable=#"MyTable"

 

With this:

 

TableName = "MyTable"

 

NewTable = #TableName

 

Is there something I can do along these lines? Thanks!

1 ACCEPTED SOLUTION
MarcelBeug
Community Champion
Community Champion

What I had in mind is to create a record for all tables you want to reference.

 

Query MyTables:

 

= [My Table = #"My Table", table1 = table1, table2 = table2]

The function that returns the table based on the name of the table:

 

 

= (table as text) as table => Expression.Evaluate(table,MyTables)

 

But why don't you just reference the tables as table? It's quit rare to reference a table by the name of the table.,

Specializing in Power Query Formula Language (M)

View solution in original post

5 REPLIES 5
MarcelBeug
Community Champion
Community Champion

As far as I know this can't be done directly.

 

A workaround is to create a record with the table(s) you want to reference.

This record can then be used as so called "Environment" wit function Expression.Evaluate.

 

Query MyTables creates a record:

 

= [My Table = #"My Table"]

 

Query in which the table is referenced:

 

let
    Source = "#""My Table""",
    NewTable = Expression.Evaluate(Source, MyTables)
in
    NewTable
Specializing in Power Query Formula Language (M)

thanks for the reply @MarcelBeug

 

I didn't quite follow what you said (very new to power query and rusty on this stuff in general), but I think I got the gist. I made a function that takes a string and returns a table based on a bunch of if statements (e.g. if string="table1" then #"table1" else if string="table2" then #"table2" etc).

 

let me know if that's what you had in mind. it's working so far as a workaround

MarcelBeug
Community Champion
Community Champion

What I had in mind is to create a record for all tables you want to reference.

 

Query MyTables:

 

= [My Table = #"My Table", table1 = table1, table2 = table2]

The function that returns the table based on the name of the table:

 

 

= (table as text) as table => Expression.Evaluate(table,MyTables)

 

But why don't you just reference the tables as table? It's quit rare to reference a table by the name of the table.,

Specializing in Power Query Formula Language (M)

@MarcelBeug got it, thanks. It's a very specific situation: I want to pull in a table from a csv and use that to check values in various tables. So one of the columns from the csv is the table name. To automate it, I want to use that column to refer to my tables rather than hard-coding for each table

MarcelBeug
Community Champion
Community Champion

Thanks for clarifying the context. Well appreciated

Specializing in Power Query Formula Language (M)

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.