Jump to content
Business Intelligence & Analytics for Digital Transformation

Announcing LAMBDA: Turn Excel formulas into custom functions

Saurabh Jain

Recommended Posts

Today we are releasing to our Beta customers a new capability that will revolutionize how you build formulas in Excel. Excel formulas are the world’s most widely used programming language, yet one of the more basic principles in programming has been missing, and that is the ability to use the formula language to define your own re-usable functions.



Simply put, LAMBDA allows you to define your own custom functions using Excel’s formula language. Excel already allows you to define custom functions, but only by writing them in an entirely different language such as JavaScript.  In contrast, LAMBDA allows you to define a custom function in Excel’s own formula language.   Moreover, one function can call another, so there is no limit to the power you can deploy with a single function call. For folks with a computer science background, you’re probably already familiar with the concept of lambdas, and the introduction of LAMBDA makes the Excel formula language Turing Complete...


Reusable Custom Functions

With LAMBDA, you can take any formula you’ve built in Excel and wrap it up in a LAMBDA function and give it a name (like “MYFUNCTION”). Then anywhere in your sheet, you can refer to MYFUNCTION, re-using that custom function throughout your sheet. I’ll show a couple examples below.



Reusable functions is reason enough to start taking advantage of LAMBDA, but there’s one more thing… you can do recursion. If you create a LAMBDA called MYFUNCTION for example, you can call MYFUNCTION within the definition of MYFUNCTION. This is something that before, was only possible in Excel through script (like VBA/JavaScript). I’ll also show an example below of how you can leverage this to build things that were not possible before without writing script.



Reusable Custom Functions

One of the more challenging parts of working with formulas in Excel is that you often get fairly complex formulas that are re-used numerous times through the sheet (often by just copy/pasting). This can make it hard for others to read and understand what’s going on, put you more at risk of errors, and make it hard to find and fix the errors. With LAMBDA, you have re-use and composability. Create libraries for any pieces of logic you plan to use multiple times. It offers convenience and reduces the risk of errors.

Station IDs

For example, imagine I have a list of station IDs, where the state is encoded in the ID, and I want to pull that value out:




There are many ways to do this with Excel functions, here’s how I did it (I’m sure many of you have much more efficient ways to doing this… forgive me… I know for example the team would remind me I should take advantage of the LET function)


If I take that formula and copy it down the column, I can get the results shown in the table above.

Notice I specify the arguments my function will take (in this case stationID) and the logic for my function. Now in my spreadsheet, I can simply write GETLOCATION as a formula and reference the cell that has the stationID, just like any other Excel function. If I notice I have an error, I fix it in one place, and everywhere that uses that function is fixed.




Another added benefit, is that I can now compose that function with additional logic. For example, if I had a table of tax rates for each location, I could write this simple formula to return the rate based on the stationID.


=XLOOKUP(GETLOCATION(B18), table1[locations], table1[tax]).

OK, lot’s more to drill into here around how you can use this capability to build up a rich set of function libraries, make your sheets easier to understand and less error prone, etc. These functions can even take data types as arguments. We’ll post an example later of a custom function that takes two cities as input and calculates the distance between them using the geo coordinates and the radius of the earth to perform the calculation.

Let’s shift into the other big impact this will have on what you can build in Excel. This one is a bit more complex, but is pretty revolutionary for us… recursion.


One of the big missing pieces in Excel formulas has been the ability to loop… to repeat over a set of logic at a dynamically defined interval. There are ways that you can manually configure the interval at which Excel recalculates to mimic this to an extent, but it’s not inherent to the formula language. That changes with LAMBDA.


Let’s take an example, albeit a bit contrived, but it’s a simple way to get the point across.

Imagine I have a set of strings, and I want to specify which characters should be removed from those strings dynamically:




Because the set of characters you’re specifying are not static, there really isn’t any good way of doing this. If you knew it was always a fixed set of characters, you could do a ton of nested logic, but that would be pretty complex and error prone to author. Not to mention, if the number of characters to be removed was larger than what you’d accounted for, it would fail.


With LAMBDA, we can create a function called REPLACECHARS that references itself allowing you to iterate over the list of characters to be removed:



=LAMBDA(textString, illegalChars,      IF(illegalChars="", textstring,        REPLACECHARS(        SUBSTITUTE(textString, LEFT(illegalChars, 1), ""),        RIGHT(illegalChars, LEN(illegalChars)-1) )))


Notice that in the definition of REPLACECHARS, there is a reference to REPLACECHARS. The IF statement says if there are no more illegal characters, return the input textString, and otherwise remove each occurrence of the leftmost character in illegalChars. Recursion kicks in with the request to call REPLACECHARS again with the updated string, and the rest of illegalChars. This means it will keep calling itself until it has parsed over every character to be removed, giving the desired result.




Not just numbers & strings

If you’ve been following the Excel improvements over the past couple years, you probably noticed two significant improvements with the type of data you can work with in Excel:

  • Dynamic arrays - Rather than passing a single value into a function, you can pass an array of values, and functions can also return arrays of values. 
  • Data Types – The value stored in a cell is no longer just a string or a number. A single cell can contain a rich data type, with a large set of properties. 

Functions can take data types and arrays as arguments, and they can also return results as data types and arrays. The same is true with the lambdas you build.


Let’s take an example... I have a list of cities, and I want to calculate the total distance I’d travel if I were to go to each city in order.


We’ll post the code for this one in a later post, but it’s pretty basic:

  • We have an array of City data types. The city data type has the latitude & longitude properties.
  • With latitude & longitude, we can do some basic math using the radius of the earth to approximate the distance between two points (that’s the first Lambda we call DistanceBetweenCities)
  • We create a recursive lambda, DistanceBetweenMultipleCities, to iterate over the cities in the array. In addition to calling itself, to iterate over the list of cities, it also calls the DistanceBetweenCities function to get a running total of the distance traveled.







Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...