User-defined functions are valuable resources for DAX developers. By creating functions, developers can compartmentalize model code into smaller, manageable segments that facilitate independent testing and debugging. After thorough validation and optimization, each function becomes a building block that contributes to the overall robustness of the project.
Functions can be used to share a common business logic within a semantic model, as well as between different models. You can get libraries of DAX user-defined functions at https://daxlib.org/,
To avoid confusion, we always use Pascal case for user-defined functions.
When defining parameters, we have the option to choose the parameter type, subtype, and parameter-passing mode. The most crucial detail is the parameter-passing mode; we dedicate a specific section to this topic later on in the chapter. There are two parameter-passing modes, and the choice of parameter-passing mode significantly impacts the function’s behavior. In contrast, the parameter type and subtype are less relevant.
The parameter-passing modes are:
VAL: Short for Value. Indicates a parameter that is evaluated before the function call, in the evaluation context of the caller. A VAL parameter has a single and well-defined value during the execution of the function body. Multiple evaluations of the same parameter always produce the same result.
EXPR: short for Expression. Indicates a parameter that is an expression, evaluated in the evaluation context where it is being used in the function body. Multiple evaluations of an EXPR parameter may (and oftentimes do) lead to different results.