Understanding custom functions
In many cases, we may face a situation where we repeatedly need to calculate something. In such cases, it makes absolute sense to create a custom function that takes care of all the calculation logic needed. After defining a custom function, we can invoke this function many times. As stated in the Introduction to Power Query M formula language in Power BI section, under Function value, we can create a custom function by putting the list of parameters (if any) in parentheses, along with the output data type and the goes-to symbol =>
, followed by a definition of the function.
The following example shows a straightforward form of a custom function that gets a date input and adds one day to it:
SimpleFunction = (DateValue as date) as date => Date.AddDays(DateValue, 1)
We can simply invoke the preceding function as follows:
SimpleFunction(#date(2020,1,1))
The result of invoking the function is 2/01/2020
.
We can define a custom function...