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

Fabric Ideas just got better! New features, better search, and direct team engagement. Learn more

Custom (Reusable) DAX Function

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.
Status: Under Review
Comments
juliet_simmons
New Member

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

asvm1
New Member

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

fbcideas_migusr
New Member

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.

ProdwareCube
Advocate I

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

Kaushal_Vyas1
New Member

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.

katharina_schwa
New Member

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 😉

smerrill
New Member

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

fbcideas_migusr
New Member

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.

ian_metzger
New Member

This would be super helpful - spend way too much time copying and pasting code, feels a bit backwards for this not to be a feature in 2024.

rodrigo_morais
New Member

This suggestion has been open for over 7 years.

Is there a reason it is still under review?

Custom, user-defined functions would improve our workflow, and greatly facilitate code maintainability and reproducibility.