• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member
  • Login

Recursive LAMBDA Functions

You are here: Home / Excel Formulas / Recursive LAMBDA Functions
Excel LAMBDA Recursion
March 4, 2021 by Mynda Treacy

The new Excel LAMBDA function that we looked at last week also enables us to write custom recursive LAMBDA functions. Recursion is simply when a routine calls itself. That said, it can be a bit mind bending, so in this tutorial we’ll look at an example that’s easy to follow. Once you understand the structure of the function you can apply it to more complex tasks.

Note: If you aren’t familiar with LAMBDA yet, be sure to see my first tutorial on LAMBDA.

Watch the Video

Subscribe YouTube

Download Workbook

Enter your email address below to download the sample workbook.

By submitting your email address you agree that we can email you our Excel newsletter.
Please enter a valid email address.

Download the Excel Workbook and follow along. Note: This is a .xlsx file please ensure your browser doesn't change the file extension on download.

Recursive LAMBDA Function Example

A common example used to explain recursion is the factorial function. A factorial function multiplies all whole numbers from our chosen number down to 1. For example, if our chosen number is 5 the formula would be:

= 5 x 4 x 3 x 2 x 1

Which equals 120.

Note: there is already a factorial function called FACT in Excel, so you would never write your own lambda for this calculation, but it’s a good way to demonstrate recursion, so go with me.

A factorial function can be written as:

n * factorial(n-1)

In other words, the result of factorial(n) can be calculated as n multiplied by the result of factorial(n-1). And the call for n-1 can recursively descend lower, and lower, till 1.

We can write this function which I’ll call FACTORIAL, with LAMBDA like so:

FACTORIAL: =LAMBDA(n, IF(n<2, 1, n*FACTORIAL(n-1)))

It’s that reference to itself that can be super confusing and prevents us being able to evaluate the function in a cell like we can with non-recursive lambdas.

A key component of writing recursive lambdas is to provide it with an opportunity to bail out of the recursive loop. In the formula above I’ve done this with IF. That is, when n gets to less than 2, n is simply 1 and the formula can stop evaluating there without moving on to the recursive part.

IMPORTANT: the bailout is always written at the start of the function.

Note: If n = 0 then factorial will return 1. It might seem odd that multiplying no numbers together results in 1, but if you follow it backwards from 3 you can see the pattern:

factorial example

Source: https://www.mathsisfun.com/numbers/factorial.html

Testing Recursive LAMBDAS

With non-recursive lambdas we can simply enter the variables in parentheses after the function, and we saw this in the LAMBDA Function example I gave last week like this:

=LAMBDA(x,y, x+y)(2,3),

But doing this with a recursive lambda like so:

=LAMBDA(n, IF(n<2, 1, n*FACTORIAL(n-1)))(5),

Will return a #NAME! error because FACTORIAL isn’t defined as a name yet.

One option is to define the name for the recursive lambda and then call it in the cell. However, toing and froing between the name manager and the worksheet is tedious when troubleshooting.

Hopefully, the Excel team will come up with a better tool for authoring lambdas soon. In the meantime, the suggestion from Microsoft is to use LET and a ME parameter. Using the FACTORIAL example, we can replace the recursive call to FACTORIAL with ME (passing in ME as the first parameter) like so:

=LET(

FACTORIAL, LAMBDA(ME, n, IF(n<2,1,n*ME(ME,n-1))),

FACTORIAL(FACTORIAL,5)

)

The ME parameter makes this possible by passing FACTORIAL as a parameter to itself, allowing it to then use that parameter to call itself.

Once you’re happy your lambda is returning the correct result you can remove the first ME parameter and replace the ME with the name you want to use, so

=LAMBDA(ME n, IF(n<2,1,n*ME(ME,n-1)))

Becomes:

=LAMBDA(n, IF(n<2,1,n*FACTORIAL(n-1)))

And you can define it as a name in the name manager.

Excel LAMBDA Recursion
Mynda Treacy

Microsoft MVP logo

AUTHOR Mynda Treacy Co-Founder / Owner at My Online Training Hub

CIMA qualified Accountant with over 25 years experience in roles such as Global IT Financial Controller for investment banking firms Barclays Capital and NatWest Markets.

Mynda has been awarded Microsoft MVP status every year since 2014 for her expertise and contributions to educating people about Microsoft Excel.

Mynda teaches several courses here at MOTH including Excel Expert, Excel Dashboards, Power BI, Power Query and Power Pivot.

More Excel Formulas Posts

Summarize Months to Quarters

Excel Formulas to Summarise Monthly Data into Quarters

3 ways (good, better, best) to summarize monthly data into quarters using formulas. Lots of examples and sample file to download.
Excel BYROW and BYCOL Functions

Excel BYCOL and BYROW Functions

Excel BYCOL and BYROW functions fundamentally change the way we write formulas that calculate across columns and down rows.
python in excel natively

How to Use Python in Excel Natively

How to use Python in Excel natively using libraries like Pandas, NumPy, Matplotlib, Seaborn and more for analysis and spectacular charts!
excel dynamic named ranges

Excel Dynamic Named Ranges

Excel Dynamic Named Ranges update automatically to include new data in the ranges referenced in your formulas and PivotTables etc.
functions for financial modelling

Excel Functions for Financial Modeling

Top 23 must know Excel functions for Financial Modeling. Includes example Excel file and step by step instructions.
excel formula by example

Excel Formula by Example

Excel can now write a formula by example. Simply give it an example or two of the result and Excel will write the formula.
ai-aided excel formula editor

AI Aided Excel Formula Editor

Save time with this free AI Excel formula editor add-in that writes, edits, improves and interprets formulas for you!
top excel functions for data analysts

Top Excel Functions for Data Analysts

Must know Excel Functions for Data Analysts and what functions you don’t have to waste time learning and why.
excel advanced formula environment

Excel Labs (Formerly, Advanced Formula Environment)

Excel Labs is a long awaited, new improved way to write, name and store Excel formulas, including LAMBDAS with the help of AI.
Pro Excel Formula Writing Tips

Pro Excel Formula Writing Tips

Must know Excel formula writing tips, tricks and tools to make you an Excel formula ninja, including a new formula editor.


Category: Excel Formulas
Previous Post:Excel LAMBDA FunctionExcel LAMBDA Function
Next Post:Converting Decimal Time to Days, Hours, Minutes, Seconds in Power BI

Reader Interactions

Comments

  1. Jon Peltier

    September 21, 2021 at 12:32 am

    I had trouble getting your formula to work properly.

    =LET(
    FACTORIAL, LAMBDA(ME, n, IF(x<2,1,n*ME(ME,n-1))),
    FACTORIAL(5)
    )

    It contains an undefined variable x, which should be replaced by n. It also needs to pass FORMULA into the internal Lambda as another parameter:

    =LET(
    FACTORIAL, LAMBDA(ME,n, IF(n<2,1,n*ME(ME,n-1))),
    FACTORIAL(FACTORIAL,5)
    )

    Reply
    • Mynda Treacy

      September 21, 2021 at 9:53 am

      Oops, thanks for spotting, Jon! I did not copy that from my file to my blog post very well at all. I’ve fixed it now.

      Reply
  2. Rajan Ghadi

    March 22, 2021 at 6:24 pm

    Hello Mynda,

    Can you please help me with lambda wherein I require average of highest n values from given row?

    I tried, but not getting answer

    =LAMBDA(range,n,
    IF(n=1,LARGE(range,n),
    AVERAGE(LARGE(range,rajan(n,n-1)))))(B2:F2,3)

    Reply
    • Mynda Treacy

      March 22, 2021 at 7:01 pm

      As I said before, please post your question and sample Excel file on our forum where we can help you further and you can clearly illustrate why you think you need a LAMBDA for this: https://www.myonlinetraininghub.com/excel-forum

      Reply
  3. Mike Glennon

    March 11, 2021 at 7:35 pm

    Excellent post Mynda. I think LAMBDA and LET are two incredible improvements in Excel but will take a while to understand fully, and this post helps towards that. Recursion is difficult to visualise and this post goes a long way towards helping in the understanding of recursion in LAMBDA.

    Reply
    • Mynda Treacy

      March 11, 2021 at 9:08 pm

      So pleased you found it helpful, Mike! Sure is a lot to learn with the new functions. Have fun with them 🙂

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Current ye@r *

Leave this field empty

Sidebar

More results...

Popular Content

  • 10 Common Excel Mistakes to Avoid
  • Top Excel Functions for Data Analysts
  • Secrets to Building Excel Dashboards in Less Than 15 Minutes
  • Pro Excel Formula Writing Tips
  • Hidden Excel Double-Click Shortcuts
  • Top 10 Intermediate Excel Functions
  • 5 Pro Excel Dashboard Design Tips
  • 5 Excel SUM Function Tricks
  • 239 Excel Keyboard Shortcuts

100 Excel Tips and Tricks eBook

Download Free Tips & Tricks

239 Excel Keyboard Shortcuts

Download Free PDF

mynda treacy microsoft mvpHi, I'm Mynda Treacy and I run MOTH with my husband, Phil. Through our blog, webinars, YouTube channel and courses we hope we can help you learn Excel, Power Pivot and DAX, Power Query, Power BI, and Excel Dashboards.

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel Office Scripts
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook

Sign up to our newsletter and join over 400,000
others who learn Excel and Power BI with us.

 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate
  • Sponsor Our Newsletter

Support

  • Contact
  • Forum
  • Helpdesk – For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.