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

Xavier ARNOUX on 18 Jan 2023 09:19:56

RE: Custom (Reusable) DAX Function

Hi,I have dax code with 35 lines for each kpi and I don't understand that "personal function" is not yes implemented in Power BI ! So I have to duplicate this code for more than 50 KPI !!!When could you answer at this lack ?

Omaer Khan's profile image Profile Picture

Rodolfo Vladimir Carmona Noguera on 13 Jan 2023 17:10:20

RE: Custom (Reusable) DAX Function

Please implement this feature.This is a must! for a serious programing language.Thanks.

Omaer Khan's profile image Profile Picture

William Bond on 12 Oct 2022 00:06:19

RE: Custom (Reusable) DAX Function

Please implement this!!! I waste so much time copy/pasting similar code, which could be fixed by creating a custom reusable function with passthrough variables...

Omaer Khan's profile image Profile Picture

on 06 Oct 2022 18:46:40

RE: Custom (Reusable) DAX Function

Would be useful if you had the ability to create your own DAX functions that allows for variables to be inserted into the function.

I find that quite often I require a Base Measure that only subtly needs changing. Eg, Groups by a different dimension in a SUMMARIZE formula, or alternately uses a different SUM measure to produce a different result with the same manipulation of the rest of the DAX Formula.

This will allow for the Base Measure to be adjusted if a more effective solution emerges, rather than requiring a change for every single measure that the function has been applied to.

Omaer Khan's profile image Profile Picture

Andrew Russell on 02 Oct 2022 01:47:34

RE: Custom (Reusable) DAX Function

All we need are UDF to return results. I get they will need context but it will save ALOT of coding hrs is to be able to do something like:IF(A, Function(param),IF(B,Function(param),etc....

Omaer Khan's profile image Profile Picture

Rafal Monka on 08 Sep 2022 08:54:31

RE: Custom (Reusable) DAX Function

Dear Microsoft Power BI Team, every serious developer working with DAX code will say that this feature is a must be.The question is when Custom functions will be available?

Omaer Khan's profile image Profile Picture

sajjad mousavi on 17 Jul 2022 10:28:14

RE: Custom (Reusable) DAX Function

More that 5 years and still UNDER REVIEW?! Seriously?! Hurry up Microsoft! You should act before even people think about it! This is a VERY BASIC feature. QBasic had it 20 years ago!

Omaer Khan's profile image Profile Picture

Kurt Petteloot on 12 Feb 2022 08:56:26

RE: Custom (Reusable) DAX Function

Unbelievable that this is still not being picked up. How many more votes would the PBI dev team need before this is addressed? Opened since 5 years and still no sign of anything that is remotely going to support this basic request? Not sure if this will make any change, but I voted anyway.

Omaer Khan's profile image Profile Picture

Nelson Yu on 20 Oct 2021 19:01:38

RE: Custom (Reusable) DAX Function

That will be very helpful, if combined with:
https://ideas.powerbi.com/ideas/idea/?ideaid=15bd4fbe-829a-4ae2-b093-937b64cf790c
(I would even want to have a choice to write visual-level function that applies to any measures in a table/matrix so I don't need to write them column by column)

I have dataset that return data where aggregation is already done for all hierarchical levels by the data source, because the aggregation cannot be done by just summing or weighted averaging constituents in a matrix. To have any visual to pick value from the right hierarchy level, I have to write measures that does something like:

var level8Value = CALCULATE(Database[values], [Level]=8)
...

return
SWITCH(
TRUE,
ISINSCOPE(Database[LEVEL8]), level8Value,
...
ISINSCOPE(Database[LEVEL1]), level1Value,
BLANK()
)

For 50 to 60 measures.
If any modification to calculation is needed that would be a maintenance nightmare. Really wish we can have a way to abstract out these logic into a reusable user defined function that supports columns as valid inputs.

Omaer Khan's profile image Profile Picture

James Lees on 14 Oct 2021 10:01:34

RE: Custom (Reusable) DAX Function

Please ensure UDFs to allow all sorts of logic, on a par with built-in functions where possible.

For example, my client has a custom calendar driven by a finance periods table in a business system, which may have periods as 4-4-5 week periods rather than calendar months. All my "previous month" measures have to use ugly, repeated code which is (a) repetitive, (b) hard to write, (c) hard to read.

I would love to have the power to be able to create a UDF, say PreviousFinancePeriod, on an equal status with PREVIOUSMONTH() which means I could then create measures like
WIP B/F = calculate ([WIP], PreviousFinancePeriod (Dates[Date]))

Merged Idea (1)