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

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
Anonymous
Not applicable

Put all M functions in a single query and call from elsewhere - is it possible

Dear all,

 

In terms of structuring my power query analysis I was wondering is it possible to put all your functions in a single query and then call them as required. At the moment each of my functions is its own query. Is it possible to consolidate these all together. In a similar way one as you may have a file/module in another language that contains all related functions.

 

For example I would like to a have a single query/file that contains the functions

 

/* my functions

 

add = (x, y) =>

    let

       return = x + y

    in

       return

 

subtract = (x, y) =>

    let

       return = x - y

    in

       return

 

*/ end of my functions

 

/* Then a new query

let

   myTable = someTableOfData

   _1 = Table.SelectColumns(myTable, {"numberColumn"}),

   _2 = Table.AddColumn(_1, "numberAdd1", each add([numberColumn],1))
in
   _2

2 ACCEPTED SOLUTIONS
MarcelBeug
Community Champion
Community Champion

No, it is not possible.

You can combine functions in 1 query, but only call them from within the same query.

 

The way to structure your queries is to group them: in the edit queries pane you can select your functions, rightclick and choose Move to Group and move your functions to an existing group or to a new group.

Specializing in Power Query Formula Language (M)

View solution in original post

Actually, you could return your functions as properties of a record. As far as I know, this would be the closest you would get to modules.

 

Module
= [add = (x,y) => x+y, multiply = (x,y)=>x*y]

 

 

Calling a function:

= Module[multiply](12,14)

View solution in original post

6 REPLIES 6
MarcelBeug
Community Champion
Community Champion

No, it is not possible.

You can combine functions in 1 query, but only call them from within the same query.

 

The way to structure your queries is to group them: in the edit queries pane you can select your functions, rightclick and choose Move to Group and move your functions to an existing group or to a new group.

Specializing in Power Query Formula Language (M)
Anonymous
Not applicable

Thanks for that. That is what I suspected. I have grouped my functions as you suggest, but was thinking it might get messy if I start to make use of user created functions to any extent.

 

I was playing around with the sections command in M, but I think that while it exists in the Language Specification it might not have been implemented.

Actually, you could return your functions as properties of a record. As far as I know, this would be the closest you would get to modules.

 

Module
= [add = (x,y) => x+y, multiply = (x,y)=>x*y]

 

 

Calling a function:

= Module[multiply](12,14)
Anonymous
Not applicable

I have tried that out and it works great. I can have all my functions as members of a record in one query and then call them from wherever I need to.

 

Thanks!

Anonymous
Not applicable

That is a really great idea! I think I will do that where I have functions that make sense to group together.

During my investigations I came to the similar conclusion that the Power Query UI's (in Excel and Power BI) are a kind of layers on top of the M language, and that these layers don't provide access to the entire core M language.

 

Note: that is my own conclusion, it may not be entirely true, but apparently we have similar thoughts.

 

Specializing in Power Query Formula Language (M)

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

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