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
jmdh
Advocate IV
Advocate IV

How to use Funcction.Invoke with a variable instead of the function name?

Hi,

 

I am trying to use the Invoke.Function like this

functionToExecute="List.First",

result = Function.Invoke(functionToExecute, {parameter list})

 

instead of 

Function.Invoke(List.First , {parameter list})

 

Obviously, this won't work, but i wonder how to achieve it...

 

Many thanks

1 ACCEPTED SOLUTION
MarcelBeug
Community Champion
Community Champion

You need Expression.Evaluate.

 

As an example I created Query1 with a list 1-10 and Query2 with functions, parameters and the invoked function:

 

Query1:

 

{1..10}

Query2 (edited version: you need to add &[Quer1=Query1] to #shared in order to have the results loaded into the data model):

 

let
    Source = #table(type table[Function = text, Parameters = {any}],{{"List.Sum",{"Query1"}},{"List.FirstN",{"Query1",5}}}),
    #"Added Custom" = Table.AddColumn(Source, "Invoked Function", each Expression.Evaluate("Function.Invoke(" & [Function] & ",{" & Text.Combine(List.Transform([Parameters], Text.From),", ") & "})",#shared&[Query1=Query1]))
in
    #"Added Custom"

 

It looks like (before the edit):

 

Dynamic Invoke Function.png

Specializing in Power Query Formula Language (M)

View solution in original post

4 REPLIES 4
MarcelBeug
Community Champion
Community Champion

You need Expression.Evaluate.

 

As an example I created Query1 with a list 1-10 and Query2 with functions, parameters and the invoked function:

 

Query1:

 

{1..10}

Query2 (edited version: you need to add &[Quer1=Query1] to #shared in order to have the results loaded into the data model):

 

let
    Source = #table(type table[Function = text, Parameters = {any}],{{"List.Sum",{"Query1"}},{"List.FirstN",{"Query1",5}}}),
    #"Added Custom" = Table.AddColumn(Source, "Invoked Function", each Expression.Evaluate("Function.Invoke(" & [Function] & ",{" & Text.Combine(List.Transform([Parameters], Text.From),", ") & "})",#shared&[Query1=Query1]))
in
    #"Added Custom"

 

It looks like (before the edit):

 

Dynamic Invoke Function.png

Specializing in Power Query Formula Language (M)

Thank you Marcel, (again)

This is hvery helpful since it shows a good example of how to write Expression.Evaluate (which i had tried without successs thanks to a previous post of yours)  and also the use of #shared.

 

Do you know where i could find documentation about #shared?

 

 

regards

jmdh

PS funny i have an error on the first function, not the second: good for my brain

MarcelBeug
Community Champion
Community Champion

Simply search for Power Query #shared and you get lots of information, e,g, from:

Reza Rad

Ken Puls

 

Specializing in Power Query Formula Language (M)

Stupid of me, the List 1 to 10 was not numeric. now it works;

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!

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.