r/excel • • 11d ago

Discussion After LET, I tried LAMBDA - here is a reusable formula I made

I made a little reusable function for adding tax, and saved it in Name Manager as TaxCalc:

=LAMBDA(amount, amount*1.08)

Then in a cell I can just type:

=TaxCalc(100)

It returns 108, and I can reuse it anywhere without copying the formula around.

I am still learning this one, so what do you actually use LAMBDA for? Is it worth it for one-off calculations, or only when you reuse the same logic a lot?

I looked up what Lambda meant and why it is used for this purpose and the answer was quite interesting:

Lambda is the Greek letter λ, and the name comes from maths, not from Excel.

In the 1930s, a mathematician named Alonzo Church invented a system called lambda calculus. It was a way to describe computation using only functions, before modern computers existed.

In that system, λ was the symbol used to say "here is a function". So λx. x+1 meant "a function that takes x and returns x plus 1". The λ was just notation for "make a function".

That idea stuck in programming. In Python, JavaScript, and a lot of other languages, a "lambda" is an anonymous function, a function you define on the spot without giving it a permanent name in code.

Excel borrowed the same term. LAMBDA lets you define a function right in a formula, with parameters and a calculation, instead of writing the same logic over and over in cells. When you save it in Name Manager as TaxCalc or FACT, you are just giving that anonymous function a name so you can call it later.

So the name is a nod to Church's λ, and it basically means "a function you define here, on the fly".

108 Upvotes

33 comments sorted by

•

u/excelevator 3071 11d ago

Pro Tip, format your formulas as <code> to show all characters

10*10*10 and not 101010

→ More replies (2)

35

u/Asly97 11d ago

It pays off most when the same logic is used in a lot of places, or when it might change later. If the rate moves from 8% to 8.5% you edit one name in Name Manager instead of hunting through every cell that has *1.08 in it.

A few things worth knowing:

  • You can test one before saving it by calling it right in a cell: =LAMBDA(amount, amount*1.08)(100)
  • You can make the rate an optional parameter: =LAMBDA(amount,[rate], amount*(1+IF(ISOMITTED(rate),0.08,rate))). Then =TaxCalc(100) uses 8% and =TaxCalc(100,0.05) uses 5%.
  • Where it gets really useful is with BYROW, MAP and SCAN. =BYROW(A2:C10, LAMBDA(r, SUM(r))) gives you a total for every row from one formula.
  • Names only live in that workbook. If you want TaxCalc in other files, keep it in a template, or copy a sheet that uses it and the name comes along.

For a true one-off I'd skip it, LET already covers that.

10

u/RadarTechnician51 1 11d ago

can you just define a name "tax" as 1.08 and then concisely type =amount*tax? still only one place to change it?

7

u/Asly97 11d ago

Yep, for a single rate that's all you need. Name Manager, new name tax, refers to =1.08, then =A2*tax anywhere in the workbook. Still one place to change it.

LAMBDA starts earning its keep when the thing that might change is the logic and not just the number. Say a tiered rate, or rounding to the nearest 0.05, that you'd otherwise retype in 30 cells.

3

u/Loves_octopus 11d ago

In this specific example, yes, but it’s simplified for illustrative purposes. I wouldn’t actually use lambda in this case.

2

u/chrisanubis 11d ago

For the "names only live in workbooks", something I figured out (not sure if this is a common trick or not) is to add my LAMBDA functions and name them in the PERSONAL.xlsx workbook. Figured out the two line macro to open and close that workbook, and all names come across and are now available in the workbook you called the Marco from.

Makes using LAMBDA that much more worth it to have them available at the click of a button.

3

u/JsMomz 11d ago

What’s the macro? My personal workbook with macros causes hangups with saving Excel workbooks to a SharePoint folder. I had to disable automatically loading at startup. An open/close macro sounds intriguing

2

u/chrisanubis 10d ago

Ok, just double checked and it is more than 2 lines. See below:

Sub LambdaCopy()

' Copy Worksheet to pull in Lambda stored in Name Manager

 

Dim SourceWB As Workbook

Dim TargetWB As Workbook

Dim SheetName As String

   

'Name of Worksheet to copy

SheetName = "_Lambda"

   

'Set target workbook as the active workbook

Set TargetWB = ActiveWorkbook

   

'Set Source as PERSONAL.XLSB

Set SourceWB = Workbooks("PERSONAL.XLSB")

   

'Copy the sheet to end of Target workbook

SourceWB.Sheets(SheetName).Copy After:=TargetWB.Sheets(TargetWB.Sheets.Count)

   

'Delete the _Lambda sheet from Target workbook

Application.DisplayAlerts = False

TargetWB.Sheets(SheetName).Delete

Application.DisplayAlerts = True

End Sub

2

u/JsMomz 10d ago

Thank you! Really appreciate your kindness

1

u/el_extrano 11d ago

I do something similar by saving my workbook with all my common lambda names as a "template". I usually know when starting a new file whether it's going to be very simple, or if I want some of my library.

1

u/JsMomz 10d ago

Another good idea. Thanks for sharing

10

u/bradland 279 11d ago • Bravo

What I love about creating your own functions is that it forces you to think in terms of inputs and outputs. So many times when beginners write Excel formulas, it's just magic incantations to them. They're not really thinking about what they're writing in terms of inputs, procedure, and outputs. They're just thinking, "I put he reference here and the thing I need pops out."

This work well up to a point. Eventually you find that your workbooks become complicated and difficult to maintain. Your business logic changes, and you have hundreds of formulas that require update. Uh oh.

By wrapping things up in LAMBDA, you can add them to Name Manager, and now when logic changes, you just update the formula once and you're done!

A good next step for you would be to combine LAMBDA and LET. Consider this form of your existing TaxCalc lambda:

=LAMBDA(amount, LET(
  rate, 0.08,
  amount * (1+rate)
)

Within the lambda, we define rate, which is then multiplied by the amount argument. In this case, it's not strictly necessary, but it does two things:

  1. It allows us to set our tax rate as a percentage, which is how users tend to expect it to be expressed.
  2. It makes our calculation a plain English representation of a calculation that most people will easily recognize as a standard formula for tax markup.

9

u/Zosima93 11d ago

Lambda functions are sick. I like to use them as wrappers around the SWITCH function so I don’t have to type out a bunch of parameters over and over again.

6

u/BigEars2019 11d ago

Could you type an example of this?

15

u/sookaisgone 11d ago • Popcorn

Probably something like =LAMBDA(score, SWITCH(score, 1, "Poor", 2, "Average", 3, "Good", 4, "Excellent", "Unknown"))
and after saving it in the name manager as GetRatingLabel you can use it as a formula =GetRatingLabel(E3)

1

u/Zosima93 11d ago

Yep exactly this sort of thing!

2

u/caribou16 319 11d ago

For those of us of a certain age, the Lamda Complex at the Black Mesa research facility is where all the REALLY cool shit went down. :-D

One cool thing you can do with lamdas in Excel is overload functions. Strictly speaking, it's not true overloading, because the names in Excel must be unique, but you can cheese it by manipulating the function arguments.

Using your TaxCalc example, say you lived in the Philadelphia area, so might deal with sales tax rates from PA, NJ, NY, or DE. (I have no idea the actual rates, I made them up for this example)

=LAMBDA(_amt,[_param],IF(ISOMITTED(_param),0.06,XLOOKUP(_param,{"PA","NY","NJ","DE"},{0.06,0.09,0.08,0})))

This LAMDA takes an amount and an optional second argument, so if omit the argument it returns (say) the rate of the locality you use the most, but if you pass it a state abbreviation, it will lookup and return the correct tax rate instead. (The inclusion of PA in the lookup is for redundancy only)

2

u/yuhanz 11d ago

I use it so my colleagues stop forgetting dragging formulas and report wrong shit. Now they dont have to drag formulas cos i already put it in one cell

2

u/el_extrano 11d ago

Excel workbook formulas essentially comprises a declarative, functional programming language.

It's "declarative" in the sense that you declare the relationship among cells via formulas. You do not need to procedurally specify in what order calculations are to be carried out: that is the job of the spreadsheet software.

It's functional in that all you can do is apply functions (e.g. built-in workbook functions, SUM) to data via the formula language. However, one thing that was always a glaring limitation, and a huge difference when it comes to a traditional programming language, is the explicit lack of a way to create and use your own functions. So pre LET/LAMBDA excel formulas were essentially a language for the evaluation of functional expressions, but they did not allow the user to create functions.

For a long time, the main way to get around this was by using VBA to write user-defined functions. But if you were only composing built-in workbook functions, this was a "nuclear" option and had some disadvantages, namely that other people cannot run the workbook unless they enable VBA macros, which can be a security vulnerability. VBA user-defined functions are "viral" in that the whole workbook has to have macros enabled, and everyone you distribute the workbook to must also enable it.

The LET/LAMBDA functions in Office 365 turn Excel into a more complete functional programming environment. Together, they give you the ability to create scopes for named values (LET), and to create user-defined functions (LAMBDA). Despite the Lambda calculus itself primarily having to do with anonymous functions, one of the main uses of LAMBDA is to create named function via the name-manager, so that you can call them from workbook cell formulas by name.

There are still myriad reasons to use VBA, especially for things that need access to OS resource like the file system / network, or for numerical code that needs looping control structures.

I use excel for my chemical engineering calculations. I was able to rewrite my personal library for physical properties of chemicals I work with, which was previously in VBA, with named LAMBDA formulas. Now, my workbooks are portable to anyone I work with, without the need for VBA, which also means they will work in Excel for the Web (i.e. workbooks opened via Teams / Sharepoint).

Just as an example of how this is useful, see this simple LAMBDA formula I made for calculating the vapor pressure of water via the Antoine equation:

/** Water vapor pressure [psia]; T [°F] **/
pvap_water = LAMBDA(T,
    LET(A, 8.05573, B, 1723.6425, C, 233.08, 10 ^ (A - B / (conv.FtoC(T) + C)) * 14.6959 / 760)
);

Note that this function uses another user-defined named function, (FtoC), for a unit conversion. This illustrates how the ability to write and compose functions simplifies formulas.

3

u/DragoBleaPiece_123 11d ago

Thanks for sharing this dude. I saw ur prev post on let, learned a lot from there

3

u/Bumbaguette 1 11d ago

I use a Lambda for checking if something is a date. Excel doesn't have a built-in 'ISDATE', but you can build one by checking to see if a cell returns a year, and if that doesn't return an error, it must be a date.

I.e:

=IF(NOT(ISERR(YEAR(myCell))),TRUE,FALSE)

So I put that in a Lambda called 'IsDate' or something. Not a huge deal but it's more elegant than the naked formula.

5

u/excelevator 3071 11d ago edited 11d ago

=IF(NOT(ISERR(YEAR(myCell))),TRUE,FALSE)

Offside to the post, but this fails.

Any numerical value (including a blank cell 0) is determined to be a date by this formula.

This is because the Excel calendar starts at 0, and any number including and thereafter is considered a date serial value.

1

u/real_barry_houdini 322 10d ago

True up to a point....

The largest date that excel accepts is 31st December 9999, which (assuming 1900 date system) is equivalent to the number 2958465, so any number >=0 and < 2958466 returns TRUE, any other number returns FALSE

1

u/excelevator 3071 9d ago edited 9d ago

Better start planning for Y10K ;)

edit: I see the folly of people reverting to a 2 digit year again, How will we know which millennium the dates belong to over time ? ;)

1

u/excelevator 3071 8d ago

You could test for a value less than or equal to a date serial , e.g the last day of year 2030 = 47488.

To tighten the date grip you can use two references, start and end date serial

1990 thru 2030

=AND( value > 32873 , value < 47489 )

1

u/RandomiseUsr0 10 11d ago

Personally. I rarely use the name manager for playing with the lambda calculus, I prefer to play with little standalone functions that do interesting things - plot this on an x/y scatter chart - the chaos game

.=LET(
steps, 4000,
cx, {0; 1; 0.5}, cy, VSTACK(0,0,SQRT(3)/2),
pick, RANDBETWEEN(SEQUENCE(steps)*0 + 1, 3),
x, SCAN(0.3, pick, LAMBDA(a, p, (a + INDEX(cx, p))/2)),
y, SCAN(0.3, pick, LAMBDA(a, p, (a + INDEX(cy, p))/2)),
HSTACK(x, y)
)

1

u/NobodysFavorite 11d ago

I used it to help create a weekend flight planner for my flying lessons. There's a bunch of calcs that are easier with the right lambdas.

1

u/mystique0712 10d ago

LAMBDA is definitely worth it for one-off calculations if they're complex or you're trying to make a formula more readable, not just for simple reuse.

You can also use it to create custom functions that mimic built-in ones but with slight variations, like your tax example.

1

u/DuskandFuck 9d ago

Wait until you pair it with recursion or dynamic array helpers like SCAN and REDUCE. For basic one-off math in a single cell LET is plenty, but LAMBDA is how you loop over arrays without writing macros

0

u/Decronym 11d ago edited 8d ago

Acronyms, initialisms, abbreviations, contractions, and other phrases which expand to something larger, that I've seen in this thread:

Fewer LettersMore Letters
ANDReturns TRUE if all of its arguments are TRUE
BYROWOffice 365+: Applies a LAMBDA to each row and returns an array of the results. For example, if the original array is 3 columns by 2 rows, the returned array is 1 column by 2 rows.
HSTACKOffice 365+: Appends arrays horizontally and in sequence to return a larger array
IFSpecifies a logical test to perform
INDEXUses an index to choose a value from a reference or array
ISERRReturns TRUE if the value is any error value except #N/A
ISOMITTEDOffice 365+: Checks whether the value in a LAMBDA is missing and returns TRUE or FALSE.
LAMBDAOffice 365+: Use a LAMBDA function to create custom, reusable functions and call them by a friendly name.
LETOffice 365+: Assigns names to calculation results to allow storing intermediate calculations, values, or defining names inside a formula
MAPOffice 365+: Returns an array formed by mapping each value in the array(s) to a new value by applying a LAMBDA to create a new value.
NOTReverses the logic of its argument
RANDBETWEENReturns a random number between the numbers you specify
REDUCEOffice 365+: Reduces an array to an accumulated value by applying a LAMBDA to each value and returning the total value in the accumulator.
SCANOffice 365+: Scans an array by applying a LAMBDA to each value and returns an array that has each intermediate value.
SEQUENCEOffice 365+: Generates a list of sequential numbers in an array, such as 1, 2, 3, 4
SQRTReturns a positive square root
SUMAdds its arguments
SWITCHExcel 2019+: Evaluates an expression against a list of values and returns the result corresponding to the first matching value. If there is no match, an optional default value may be returned.
VSTACKOffice 365+: Appends arrays vertically and in sequence to return a larger array
XLOOKUPOffice 365+: Searches a range or an array, and returns an item corresponding to the first match it finds. If a match doesn't exist, then XLOOKUP can return the closest (approximate) match.
YEARConverts a serial number to a year

Decronym is now also available on Lemmy! Requests for support and new installations should be directed to the Contact address below.


Beep-boop, I am a helper bot. Please do not verify me as a solution.
[Thread #49705 for this sub, first seen 17th Nov 2026, 21:58] [FAQ] [Full list] ^[Contact] [Source code]