Skip to main content
Microsoft Idea

Power BI

Under Review

Custom (Reusable) DAX Function

Vote (667) 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 (57)
Omaer Khan's profile image Profile Picture

Nelson Yu on 09 Oct 2021 05:06:31

RE: Custom (Reusable) DAX Function

We have metrics that couldn't be aggregated with sum, weighted average, etc when fed into a matrix with more than 1 level of hierarchy, while upstream data source has done the calculation and fed all hierarchy of data into a single table. Have been relying on Measures to apply logic to a column so that aggregation at each hierarchical level will pick the right level directly from data source, but we have to create these measures for each column one by one. Would be nice if we can create a custom DAX function that takes a column or even a table as parameter, so the logic can apply to the column or all columns in a table, that will reduce a lot of boilerplate DAX code.

Omaer Khan's profile image Profile Picture

Dmitri Kouminov on 24 Sep 2021 14:21:58

RE: Custom (Reusable) DAX Function

Must have feature.

Omaer Khan's profile image Profile Picture

Raghunandan Balasubramaniam on 10 Jul 2021 04:48:11

RE: Custom (Reusable) DAX Function

I faced the same problem. I really need to be able to keep common code in a function, parameterize it and use it across all my measures.

Omaer Khan's profile image Profile Picture

Kevin McAloon on 11 May 2021 02:19:56

RE: Custom (Reusable) DAX Function

I am calculating a time period based on current date and a product attribute of lead time. Without a custom DAX function that can accept parameters, I guess the next best solution is adding a calculated column. Would love the option of a custom DAX function, for such a case.

Omaer Khan's profile image Profile Picture

Power BI User on 27 Apr 2021 19:42:57

RE: Custom (Reusable) DAX Function

Please add this! It would save so much time.

Omaer Khan's profile image Profile Picture

Johannes 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

Neil MacMullen 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

Kosta 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

Kimmo 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

Power BI User 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.

Merged Idea (1)