Using a record as a custom function library

You can store custom functions in a record. Here is an example.

[
    add = (a,b) => a+b,
    multiply = (a,b) => a*b
]

To use this library, copy the code above to a blank query named fnLibrary. Typing = fnLibrary[add](3,7) in the formula bar will now yield the result 10.

All this works because a function is a value and therefore may take the place of a value in the name-value pairs of a record. The built-in functions of PowerQuery are stored in a record – it can be returned by entering = #shared in the formula bar.

References

Understanding Power Query M functions by Microsoft.

How to create and use an R function library in Power BI by Imke Feldmann. In this post Imke extends the idea of a function library record by automating import of function code from GitHub.

Create a function library in Power BI using M-extensions by Imke Feldmann. Imke describes an alternative to the idea of a function library record. There are limitations which Imke lists – and it’s worthwhile reading the comments to the post as they provide further insight.

Leave a comment