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
r_fioretti
Helper I
Helper I

Invoke Custom Function

Hello,

I have different versions for xlsx files, and I’ve created custom function for each version in order to group them.

Before I had more than one version for those xlsx, I had just one function to transform each xlsx and it was working very well. I could invoke it as figure 02 shows.

Now I have one function that indicates with function name power query should invoke to transform the file xlsx, but I can figure out how to invoke it. Figure 03.

Is there any way to call the custom function using a columns value as a parameter?

 

Figure 01 - Column that indicates which function to invoke.Figure 01 - Column that indicates which function to invoke.

 

Figure 02 – Previous query. Works fine for same xlsx versions.Figure 02 – Previous query. Works fine for same xlsx versions.

 

 

Figure 03 – Current query. Can’t figure out how to invoke the function from column [Função].Figure 03 – Current query. Can’t figure out how to invoke the function from column [Função].

2 ACCEPTED SOLUTIONS
m_dekorte
Super User
Super User

Hi @r_fioretti,

 

A couple of things I want to call out:

Table.TransformColumns doesn't allow field access beyond the the column you're transforming, instead you can use Table.ReplaceValue.

Inside the [Funcao] I see a list with what appears to be a textual string, not a function. You'll need to access the list and if it is a string pass Expression.Evaluate as well.

 

Here's an example that illustrates both scenario's:

let
    fxEven = (n) => n+1,
    fxOdd  = (n) => n+1,
    Source = Table.FromColumns( 
        {{1..9}},
        type table [ n =number]
    ),
    AddListWithFunction = Table.AddColumn(Source, "Fx", each if Number.IsEven([n]) then {fxEven} else {fxOdd}),
    ReplaceWithFunction = Table.ReplaceValue(AddListWithFunction,each [n],each Function.Invoke( [Fx]{0}, {[n]} ),Replacer.ReplaceValue,{"n"}),
    AddListWithText = Table.AddColumn(ReplaceWithFunction, "Txt", each if Number.IsEven([n]) then {"fxEven2"} else {"fxOdd2"}),
    ReplaceWithText = Table.ReplaceValue(AddListWithText,each [n],each Function.Invoke( Expression.Evaluate( [Txt]{0}, [fxEven2 = (n) => n+1, fxOdd2  = (n) => n+1] ), {[n]} ),Replacer.ReplaceValue,{"n"})
in
    ReplaceWithText

 

Ps. If this helps solve your query please mark this post as Solution, thanks!

 

View solution in original post

@m_dekorte 


As you said Table.TransformColumns doesn't allow field access, so I added a new column instead of transforming the [Content] column.
04.PNG

View solution in original post

2 REPLIES 2
m_dekorte
Super User
Super User

Hi @r_fioretti,

 

A couple of things I want to call out:

Table.TransformColumns doesn't allow field access beyond the the column you're transforming, instead you can use Table.ReplaceValue.

Inside the [Funcao] I see a list with what appears to be a textual string, not a function. You'll need to access the list and if it is a string pass Expression.Evaluate as well.

 

Here's an example that illustrates both scenario's:

let
    fxEven = (n) => n+1,
    fxOdd  = (n) => n+1,
    Source = Table.FromColumns( 
        {{1..9}},
        type table [ n =number]
    ),
    AddListWithFunction = Table.AddColumn(Source, "Fx", each if Number.IsEven([n]) then {fxEven} else {fxOdd}),
    ReplaceWithFunction = Table.ReplaceValue(AddListWithFunction,each [n],each Function.Invoke( [Fx]{0}, {[n]} ),Replacer.ReplaceValue,{"n"}),
    AddListWithText = Table.AddColumn(ReplaceWithFunction, "Txt", each if Number.IsEven([n]) then {"fxEven2"} else {"fxOdd2"}),
    ReplaceWithText = Table.ReplaceValue(AddListWithText,each [n],each Function.Invoke( Expression.Evaluate( [Txt]{0}, [fxEven2 = (n) => n+1, fxOdd2  = (n) => n+1] ), {[n]} ),Replacer.ReplaceValue,{"n"})
in
    ReplaceWithText

 

Ps. If this helps solve your query please mark this post as Solution, thanks!

 

@m_dekorte 


As you said Table.TransformColumns doesn't allow field access, so I added a new column instead of transforming the [Content] column.
04.PNG

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