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 (59)
- Merged Idea (1)
RE: Custom (Reusable) DAX Function
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.
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.
RE: Custom (Reusable) DAX Function
Custom functions would be so helpful for displaying number of seconds in mm:ss format.
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 ;-)
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.
RE: Custom (Reusable) DAX Function
one of my use case is that i have a custom format string that i would apply to multiple measuresif 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
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.
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.
RE: Custom (Reusable) DAX Function
This feature is a must. How can you write efficient code without user defined funtions?????
RE: Custom (Reusable) DAX Function
Yo MS Team - it's April 2023. Ping.