Skip to main content
Microsoft Idea

Power BI

Under Review

Custom (Reusable) DAX Function

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

German Velez on 03 Apr 2024 20:56:02

RE: Custom (Reusable) DAX Function

I am fairly new to Power BI and DAX (less than 3 years) but over 40 years programming in C, C#, C++, etc. I am used to having UDFs to reduce code duplication but in my latest DAX endeavor, I was faced with having to generate a dynamic DAX table (based on a user specified Fiscal Year) and returning 2 or 2 columns from the resulting table. The problem is that I cannot return a record from a measure and thereby forced to replicate the measure for each of the desired columns. If that's the case, having a UDF one could encapsulate the creation of the dynamic table and having as an argument the column to be returned. If an update is necessary, the developer will only need to modify the UDF rather than every measure that has the same logic.

Omaer Khan's profile image Profile Picture

Samantha Merrill on 22 Mar 2024 16:56:23

RE: Custom (Reusable) DAX Function

Custom functions would be so helpful for displaying number of seconds in mm:ss format.

Omaer Khan's profile image Profile Picture

Katharina Schwarzer on 30 Jan 2024 09:45:06

RE: Custom (Reusable) DAX Function

Copy-pasting code is so 1990ies ... LOVE the feature in Power Query and Excel, would SO love to have a similar option in DAX. Thanks in advance ;-)

Omaer Khan's profile image Profile Picture

Kaushal Vyas on 13 Oct 2023 09:58:22

RE: Custom (Reusable) DAX Function

We create similar data structures using different filters for each customer group. Having the ability to create a user defined function would save time to copy and paste the same code again, and make the code easier to manage over time.

Omaer Khan's profile image Profile Picture

Sascha Müller on 28 Jul 2023 09:12:01

RE: Custom (Reusable) DAX Function

one of my use case is that i have a custom format string that i would apply to multiple measures


if i change this i have to edit all affected measures...


e.g.


var formatstr =

    switch(MIN('Format'[Fmt])

        ,0,"#,##0"

        ,1,"#,##0.00"

        )


return formatstr

Omaer Khan's profile image Profile Picture

Michael Rudzinski on 15 Jun 2023 17:12:19

RE: Custom (Reusable) DAX Function

I'd like to use a function to control number formatting. In Excel, the "Accounting" format will treat 0 and 0.01 differently when displaying with no decimal places. If it's a "true" zero, it will be displayed as a dash, but if it rounds to zero, it will be displayed as a 0. I need to replicate this in Power BI, but it applies to ALL THE NUMBERS, so I really need a function if I have any chance of doing this. Or you could just make Power BI formatting like Excel formatting.

Omaer Khan's profile image Profile Picture

Asger Møller on 30 May 2023 15:21:42

RE: Custom (Reusable) DAX Function

Defining custom functions and sharing the function with other analysts across the organization would be invaluable for aligning KPI's across the enterprise.

Omaer Khan's profile image Profile Picture

juliet Simmons on 24 May 2023 09:46:06

RE: Custom (Reusable) DAX Function

This feature is a must. How can you write efficient code without user defined funtions?????

Omaer Khan's profile image Profile Picture

James Howell on 20 Apr 2023 18:39:25

RE: Custom (Reusable) DAX Function

Yo MS Team - it's April 2023. Ping.

Omaer Khan's profile image Profile Picture

Kayden Kiat Hao LIM on 30 Jan 2023 16:59:12

RE: Custom (Reusable) DAX Function

Unforgiveable that this has yet to be implemented, no wonder powerBI is not even more popular than it should be

Merged Idea (1)