Skip to main content

Power BI

Needs Votes

Custom (Reusable) DAX Function

Vote (303) Share
Omaer Khan's profile image

Omaer Khan on 01 Mar 2017 02:06:50

I'd like to be able to create reusable functions in DAX, instead of using the same DAX code in multiple measures.

Example: I have some complicated DAX code, which calculates the no of working seconds between two date-times. This excludes holidays, weekends, and only counts seconds from the start of the work-day, to the end of the work-day.

I use this DAX code a lot, for many measures which need to be in "Working Time" (working hours/minutes).

I would love to not have to copy-paste a bunch of code into all of my measures, but to be able to create a reusable function instead.

Comments (30)
Omaer Khan's profile image Profile Picture

1e1d7fb3 d119-4a31-bc1b-fe610fff81c9 on 06 Jul 2020 00:18:17

RE: Custom (Reusable) DAX Function

I would love to have this function, too. Especially around passing arguments to these functions would be "very" helpful and would declutter a lot. Recent example, I had to do the same non-standard comparison calculation for 5 different product brands --> 5 new measures. one functions and 5 ways to define arguments would help.

Omaer Khan's profile image Profile Picture

ff238841 3458-45b7-879e-063ae9abbe4c on 06 Jul 2020 00:14:05

RE: Custom (Reusable) DAX Function

..tumbleweed... I genuinely wonder what the PowerBi team is working on sometimes when features like this get ignored for years.

Omaer Khan's profile image Profile Picture

95e61163 b4f8-4636-b1fd-e25b2fa144d8 on 06 Jul 2020 00:13:53

RE: Custom (Reusable) DAX Function

Must have feature. Would be amazing to have the ability to define reusable functions in DAX.

Omaer Khan's profile image Profile Picture

b8104d4d 36e3-483c-93ae-60c348f6c83a on 06 Jul 2020 00:13:45

RE: Custom (Reusable) DAX Function

Yeap..DAX seems very limited without this simple programming feature.

I want to cut the typing and make my code more readable

Omaer Khan's profile image Profile Picture

0fc61e10 dc2f-4ec0-9487-a7545551da3e on 06 Jul 2020 00:13:36

RE: Custom (Reusable) DAX Function

I have the exact same problem. I added several columns that calculate hours between 2 dates in DAX. Then was told, after the fact, it needed to exclude weekends and holidays. Now, I can either write a calculated formula in DAX and copy and paste the code everywhere, or I rewrite the whole thing in powerquery(M) and create a custom function and move ALL my columns, and obviously change my formulas from DAX to M. VERY FRUSTATING.

Omaer Khan's profile image Profile Picture

ade0d665 95b8-4c4d-abb6-a7144c21d9f9 on 06 Jul 2020 00:10:59

RE: Custom (Reusable) DAX Function

Agree, much time can be saved from creating your own functions with parameter passing.

DAX is great, make it greater, thanks.

Omaer Khan's profile image Profile Picture

65c88a40 2a5b-4b65-9906-a3d11145690d on 06 Jul 2020 00:09:38

RE: Custom (Reusable) DAX Function

Is this something that Calculation Groups would solve (if introduced into Power BI?) https://www.sqlbi.com/articles/introducing-calculation-groups/

Omaer Khan's profile image Profile Picture

3be26031 1c6c-407b-9c95-4594603a3051 on 06 Jul 2020 00:09:35

RE: Custom (Reusable) DAX Function

Would be a great add. Currently having to use the exact same massive formula to generate a custom column for a dozen tables, and if I improve the formula in one of them, I have to re-copy and paste and localize it a dozen times.

Omaer Khan's profile image Profile Picture

fab9e5fa 2e39-4d31-8852-f6b9e5079ce9 on 06 Jul 2020 00:05:43

RE: Custom (Reusable) DAX Function

Yes please. I currently have dozens of measures that are basically the same thing but pointed at different time windows (since they need to be directly compared). This would be a great add.

Omaer Khan's profile image Profile Picture

3abd52e9 835b-43cd-9dc8-1b40e4f0cdd4 on 06 Jul 2020 00:05:29

RE: Custom (Reusable) DAX Function

Please add User defined functions to Power BI