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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
necmik
Microsoft Employee
Microsoft Employee

Text to Function definition

Is there a way to transform a text to function definition in M language? What I need is something like Function.From(text). For instance this text contains a function definition with empty body:
TextValue = "(InputColumn1 as text) as any => ..."

This one has 2 parameters:

TextValue = "(InputColumn1 as text, InputColumn2 as number) as any => ..."

 

I need to transfor this to function like adding a new column with function type:

FunctionAddedTable = Table.AddColumn(Source, "FunctionColumn", each [TextValue ])
1 ACCEPTED SOLUTION

I was hoping for something I could paste into the Advanced Editor but I'll take a guess at what I think you mean.

 

Is this close to what you're after?

let
    Source = #table({"ListCol"},{{{[name="InputColumn1", type="text"],[name="InputColumn2", type="number"]}},{{[name="InputColumn1", type="text"]}}}),
    #"Added Custom" = Table.AddColumn(Source, "ToText", each "(" & Text.Combine(List.Transform([ListCol], each [name] & " as " & [type]), ", ") & ") as any => ""..."""),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "ToFunction", each Expression.Evaluate([ToText]))
in
    #"Added Custom1"

 

AlexisOlson_0-1680792975726.png

View solution in original post

7 REPLIES 7
Anonymous
Not applicable

The BIccountant has a great blog post on this with a custom fn for you to use.

 

Dynamically create types from text with Type.FromText in Power Query and Power BI (thebiccountant.co...

Yep. It looks like Expression.Evaluate is key here.

How can I use Expression.Evaluate here? I read the BIccountant blog. It gave me some idea but I couldn't figure out how to create a function from text. To be clear what I am trying to do is:

I have a table and this table has a column contains input schema as list of records:

{

  [name="InputColumn1", type="text"],

  [name="InputColumn2", type="number"]

}

 

My aim is to generate a function like this:

(InputColumn1 as text, InputColumn2 as number) as any => ...

Can you give an explicit example of input and expected output? I can't quite figure out exactly what part you're stuck on.

Actually I had given in my previous answer. My input is:

{

  [name="InputColumn1", type="text"],

  [name="InputColumn2", type="number"]

}

 

What I want as output is:

(InputColumn1 as text, InputColumn2 as number) as any => ...

(A function type)

I was hoping for something I could paste into the Advanced Editor but I'll take a guess at what I think you mean.

 

Is this close to what you're after?

let
    Source = #table({"ListCol"},{{{[name="InputColumn1", type="text"],[name="InputColumn2", type="number"]}},{{[name="InputColumn1", type="text"]}}}),
    #"Added Custom" = Table.AddColumn(Source, "ToText", each "(" & Text.Combine(List.Transform([ListCol], each [name] & " as " & [type]), ", ") & ") as any => ""..."""),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "ToFunction", each Expression.Evaluate([ToText]))
in
    #"Added Custom1"

 

AlexisOlson_0-1680792975726.png

That is what I exactly want. I didn't expect Expression.Evaluate would be enough because of the word "Evaluate". Very thanks

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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