Advanced Functions
How to create custom functions using LET and LAMBDA
Last updated
How to create custom functions using LET and LAMBDA
Last updated
Microsoft has added two powerful features in recent years which effectively allow users to create custom functions.
There are many benefits to using these functions, including:
Improved readability of formulas
Minimising the number of references to Calculation inputs
Making complex functions reusable
To understand how LET and LAMBDA functions work, we recommend reading the Microsoft documentation. Links are included in the relevant sections below.
LET and LAMBDA functions are available in Office 365 versions of Excel. If you are using older versions of Excel, you can still use these functions within Models, but they will not produce working Excel spreadsheets.
If you are new to LET functions, first read the Microsoft documentation here
To create a LET function, you can type the function into the Formula Entry Bar, as you would for any other function, with two key differences: Arguments (called "variables" in the Microsoft documentation) and References to those arguments must be entered via the AutoComplete dropdown. This is demonstrated in the example below.
Let's say we want to create a LET function to find one of the roots of a quadratic equation, as shown above. This is useful as the arguments a
and b
are both used twice in the formula, so using a LET function means we only have to enter the input values for those arguments (which could be references to Variables) once.
If you are new to LAMBDA functions, first read the Microsoft documentation here
To create a LAMBDA function, go to the Lambda applet, which is accessible from the Navigation Bar, or create one on the fly from the Formula Entry autocomplete menu.
Lambdas are stored separately from Models so can be used in any Model that has access to the Lambda's Namespace.
When you create a new Lambda you will need to perform the following steps, as per the screenshots below:
Select a Namespace
Choose a Name (1). The name must obey Excel naming rules for Named Ranges, and not include spaces or certain special characters. The full rules are complex so we will not list them here, but the Lambda dialog will not let you proceed if the name does not meet the rules.
Create the Arguments for the function (2)
Enter the formula (3)
The Calculation entry step for a Lambda uses the same Formula Entry Bar as Variables, but with reduced capabilities (e.g. as Lambdas exist outside Models, you cannot reference Variables or Periods in a Lambda formula).
Lambdas can reference other Lambdas - use this to greatly simplify complex calculations
Once you have created a Lambda function, it can be used in Models just like any other Function, as long as the Lambda's Namespace is available in your Model.
For instance, to use the QuadraticRoot
Lambda from the above example, we would simply type the name of the Lambda into the Formula Entry Bar, select the function from the dropdown, and enter arguments as desired, as shown below.
When we enter each of the arguments (a
, b
, and c
), we type in the name of the argument and then select the Argument Calculation Part with the plus in braces symbolfrom the AutoComplete dropdown, as shown below.
To reference these arguments, we type in the name of the argument, and select the Argument Reference Calculation Part with the comma in braces symbol , as shown below.