Creating LAMBDA functions in Excel is a timely and complex topic, since for the first time we can create our own custom functions from scratch in Excel. In the past, this could only be done with so-called UDFs (User-Defined Functions) using VBA, but the level of complexity and skill required was considerable.

The concept of LAMBDA in Excel involves “creating a custom function that I can reuse in any worksheets or files I want, one that’s easily editable and can also be used by others.” For those who work extensively in Excel, this is an incredible opportunity to streamline their workflow, since we often find ourselves repeating the same operations multiple times when they could be completed in a single step.
In this guide, we’ll create a LAMBDA function, walking through the various logical steps to better understand the concept and apply it to our own files. Once created, the function can be used on all sheets in the file and can also be easily exported to other files (simply copy and paste a blank sheet from the file containing the LAMBDA to the one without the LAMBDA).
We’ll use a table called TBL_NBA that lists the NBA championship-winning teams, along with other information such as the opponents they defeated in the finals, their conference, and the final score.
Note: This entire guide was created before the introduction of the GROUPBY function and is based on Excel for Microsoft 365, since many of these operations would be much more complicated, or nearly impossible, with earlier versions
The Purpose of Creating the LAMBDA Function in Excel
In this case, we want to create a function LAMBDA that returns the following in a single step:
- The list of unique values in a range
- Counting the unique values within the same interval
and then sort these values in descending order based on their count. So we want to see the most frequent values in the database first, followed by the others.
In our case, we’ll use this in the database to count NBA samples, but it would work just as well in other databases, as long as the goal (to count unique values and then sort them) remains the same.
How to Get the Result Without Using the LAMBDA Function in Excel
There are three steps we need to follow to solve this problem:
- Extract unique values from the database
- Then, for each unique value, count the number of times it appears in the database
- Sort the total according to the count
We can then proceed to extract the unique values:

=UNIQUE(TBL_NBA[CHAMPIONS])
Using the UNIQUE function, which returns the unique values in a range, we find all the team names starting from cell G5. We can then count how many times each unique value appears in the original database:

=COUNTIF(TBL_NBA[CHAMPIONS],G5#)
We now have both the unique values and their counts. We simply need to sort them to get the result we want:

=SORT(G5#:H5#,2,-1)
SORT in this case retrieves both the unique values (G5#) and their counts (H5#), and then sorts by the second column (which is why the 2 is in the formula) in descending order (the -1 in the formula).
The result is correct, but we had to go through several steps to achieve it. By using a LAMBDA, we could automate this by creating our own function, which we’ll call UNIQUE.COUNT.SORT: this function retrieves the unique values, counts them, and then sorts them based on the count, returning both the column with the unique values and their count.
How to Use the HSTACK Function to Reduce Steps in Excel
The functions HSTACK and VSTACK have revolutionized how arrays are handled in Excel, since they allow us to stack values either horizontally or vertically in a single step. In our case, we want to avoid having to first extract the unique values and then count them; we can therefore reduce the number of steps as follows:

=HSTACK(
UNIQUE(TBL_NBA[CHAMPIONS]),
COUNTIF(TBL_NBA[CHAMPIONS],UNIQUE(TBL_NBA[CHAMPIONS])))
The function first extracts the unique values from the NBA samples and then adds, on the right, the count for each team.
At this point, all we have to do is sort them again, using the function shown above:

=SORT(
HSTACK(
UNIQUE(TBL_NBA[CHAMPIONS]),
COUNTIF(TBL_NBA[CHAMPIONS],UNIQUE(TBL_NBA[CHAMPIONS]))),2,-1)
In this case, we sort by the second value in the array, that is, by the count, thus obtaining the correct final result.
How to Use the LET Function to Simplify Calculations in Excel
The LET function allows us to reduce “redundant” calculations in formulas, and in this case it’s very helpful, since, as we can see, both the range in question and the unique values are repeated in various parts of the formula.
For example, if we were to create a function like this:
=LET(range,B3,range+1)
We would simply be adding 1 to the value in cell B3.
We can therefore rewrite the formula by first declaring the range we’re interested in, to avoid repetition:

=LET(range,TBL_NBA[CHAMPIONS],
SORT(
HSTACK(
UNIQUE(range),
COUNTIF(range,UNIQUE(range))),2,-1))
The function LET allows us to refer to our reference column as “range.” This greatly simplifies writing the subsequent formula, since we’ll now only see “range” instead of “TBL_NBA[CHAMPIONS]” after having declared it at the beginning of the formula.
We can also take it a step further by simplifying the formula even more with a second parameter that contains the unique values:

=LET(range,TBL_NBA[CHAMPIONS],
unique_int,UNIQUE(range),
SORT(
HSTACK(
unique_int,
COUNTIF(range,unique_int)),2,-1))
As we can see, to obtain the unique values, we declare that unique_int is equal to UNIQUE(range), which we have just declared as “range” equal to TBL_NBA[CHAMPIONS]. We can then reference our definitions immediately after creating them, simplifying the process.
At this point, we have adapted the formula as much as possible to easily create our LAMBDA.
How to Create a LAMBDA Function in Excel
To create a LAMBDA function, we need to write the formula as clearly as possible for Excel. By using LET, we can declare what we need just once and then reuse the individual parameters in subsequent formulas. The goal is always to create a unique function that does something very specific, as in this case (unique values are counted, then sorted by count, and placed in a two-column array).
However, we must decide what information the user will need to enter to help them use the function, in this case, the parameter we’ve called “range,” from which all subsequent calculations are based. For this reason, it’s useful to set `unique_int` to `UNIQUE(range)`, since this allows us to enter only one parameter during the input phase, and Excel will handle the rest.
So let’s see how to do this, let’s rewrite the previous function from LAMBDA, testing it by entering our “TBL_NBA[CHAMPIONS]” range at the end:

=LAMBDA(range,
LET(unique_int,UNIQUE(range),
SORT(
HSTACK(unique_int,
COUNTIF(range,unique_int)),2,-1)))
(TBL_NBA[CHAMPIONS])
In this case, the function LAMBDA alerts us that:
- "Interval" is the parameter that must be entered by the user
- The formula sorts the array consisting of two columns: the unique values (calculated based on the parameter) and their respective counts
- TBL_NBA[CHAMPIONS] is, in this specific case, the range we are considering for the analysis
The unusual aspect of LAMBDA is that the range to be considered is placed at the end of the function, after all the parentheses have been closed. This is because it serves as a validity check for the function, rather than the final LAMBDA function itself.
In fact, the latter will be written as follows:
=LAMBDA(range,
LET(unique_int,UNIQUE(range),
SORT(
HSTACK(unique_int,
COUNTIF(range,unique_int)),2,-1)))
since we remove our test interval "TBL_NBA[CHAMPIONS]".
In summary, to use our LAMBDA, simply enter the desired value for “range,” and the function will perform all the operations we mentioned earlier, namely:
- Get the unique values
- Count how many times they appear within the defined range
- Sort them from most frequent to least frequent
- Insert two columns, first with the unique values (1) and then with the count (2), in sorted order (3)
How to Use the LAMBDA Function in Excel
We’ve written the function LAMBDA but haven’t created a new function yet. To do this, we’ll need to create a new name in Excel that corresponds to the function we just saw. We can choose a name for the new function and add a brief explanation.
To access the option for creating new names, click on the Formulas menu > Define Name:

Let’s take a closer look:

- Name of the new function: UNIQUE.COUNT.SORT
- Definition: Creates a two-column matrix (sorted by the count from Z to A) containing the unique values within a range and their respective counts
- Refers to: the previous formula
At this point, we’ve created our function LAMBDA!
We can then simplify the entire previous calculation to find the NBA champions as follows:

=UNIQUE.COUNT.SORT(TBL_NBA[CHAMPIONS])
We’ve already defined all our calculations when creating the LAMBDA function, so all we need to do is enter the range, and we’re done!
The advantage of using LAMBDA functions in Excel
Using LAMBDA functions in Excel incredibly simplifies the process if we want to replicate the same calculations but with different ranges or parameters.
In this case, we’ve created a single function that performs several operations at once; all it needs is the range to use for all the calculations.
Let’s take, for example, the case where we want to calculate:
- The winners’ most frequent opponents during the finals
- Which conference is more successful, when compared to the other (there are only two)
- What are the most common scores in the finals
At this point, having created our UNIQUE.COUNT.SORT function, all we have to do is change our reference range, and the formula will do all the work for us.
For example, in the case of opponents:

=UNIQUE.COUNT.SORT(TBL_NBA[OPPONENTS])
If we define the table headers correctly, we’ll have no problem letting other people use this new function as well.
Or in the case of scores:

=UNIQUE.COUNT.SORT(TBL_NBA[SERIES])
Here, too, the calculation is immediate, fortunately for us. So we won’t have to repeat all the logical steps from the beginning of the guide; once the function is created, we can reuse it anywhere on the worksheet.
In the case of LAMBDA, we must therefore clearly define its use cases, since it’s suitable when we need to perform the same operations multiple times on the same file.
How to Reuse a LAMBDA Function in Excel
To reuse an LAMBDA function that we’ve created in Excel, the absolute easiest way is:
- Copy a blank sheet from the file containing our function LAMBDA (FILE A)
- Paste the blank sheet from FILE A into a file that does not contain the function LAMBDA (FILE B)
At this point, the blank worksheet will carry over all the names created in the previous file (FILE A) and allow us to use this function on the new file as well.
Ideally, we could have all our custom LAMBDA functions in a single file that we then use, for example:
- Copy a blank sheet from the file containing all our functions LAMBDA (FILE MASTER)
- Paste the blank sheet from the MASTER FILE into a new file (FILE B)
This way, FILE B would contain all our LAMBDA entries and allow us to speed up numerous calculations all at once.
If, on the other hand, we wanted to transfer only a LAMBDA, we could simply copy a cell used by the LAMBDA to the new sheet, thereby avoiding the need to carry over all the names from the previous file, thanks to Jan Karel Pieterse for pointing this out!
Another possible method is to create a Gist on GitHub and then retrieve our LAMBDA values into the file, but, in this case, we’ll need to use the Excel Labs add-in to quickly apply them to our file. Keep in mind that Excel Labs stores the LAMBDAs with English function names, so stay consistent if you share them with people who use Excel in another language.
Practical Example for the LAMBDA Function in Excel

Another interesting use case for creating an LAMBDA is deriving the final standings from a list of league matches. I’ve gathered all the matches from the 2024–2025 Premier League season and want to calculate the final standings in one go.

I can confirm that the formula works, but it’s not exactly quick to set up, a typical scenario where LAMBDA can be useful for better understanding how to reuse the logic.
Here’s the formula:
=LET(team1,TBL[HOME],team2,TBL[AWAY],score,TBL[SCORE],pwin,3,ploss,0,pdraw,1,separator," - ",
score1,TEXTBEFORE(score,separator),
score2,TEXTAFTER(score,separator),
check,IF(score1=score2,HSTACK(pdraw,pdraw),
IF(score1<score2,HSTACK(ploss,pwin),
IF(score1>score2,HSTACK(pwin,ploss)))),
teams,VSTACK(team1,team2),
points,VSTACK(INDEX(check,0,1),INDEX(check,0,2)),
gf,VSTACK(score1,score2),gs,VSTACK(score2,score1),
gfinal,gf-gs,
final,DROP(SORT(GROUPBY(teams,HSTACK(points,gfinal),SUM,,0),
{2,3},{-1,-1}),
0,-1),
final)Let’s break down the formula into individual steps:
=LET(team1,TBL[HOME],team2,TBL[AWAY],score,TBL[SCORE],pwin,3,ploss,0,pdraw,1,separator," - ",
Let’s specify that we’ll enter two teams: the home team (HOME) and the away team (AWAY). We’ll also enter the final match result (SCORE), the points for a win (3), the points for a loss (0), and the points for a tie (1), along with the score separator (“ - ”).
score1,TEXTBEFORE(score," - "),
score2,TEXTAFTER(score," - "),
We must distinguish between the home team’s goals (score1) and the away team’s goals (score2). It might be tempting to use TEXTSPLIT to separate them, but it doesn’t handle these cases well because it results in an “array of arrays” error. The return value of the TEXTSPLIT function spans multiple cells, but if we process one cell at a time, the return arrays might have different sizes, and Excel doesn’t particularly like that. It’s much simpler to use TEXTBEFORE to retrieve home goals and TEXTAFTER for away goals.
check,IF(score1=score2,HSTACK(pdraw,pdraw),
IF(score1<score2,HSTACK(ploss,pwin),
IF(score1>score2,HSTACK(pwin,ploss)))),
Who won the game? Let’s run a check comparing the goals:
- In the first case, score1 = score2, so it’s a tie. Let’s create an array with the points scored for the tie: {1, 1}
- In the second case, score2 prevailed. The array will therefore be {0\3}, where we have 0 points for the home team and 3 for the away team
- The last case is a home win, so the array will be {3\0}
We need this to determine who won the championship, since the first criterion is the total number of points earned.
teams,VSTACK(team1,team2),
points,VSTACK(INDEX(check,0,1),INDEX(check,0,2))
Here, we’ll go over various transformations that might be useful. By placing the values for the home team (Team1) and the away team (Team2) into a single array, we simplify the calculations in our formula. VSTACK works like HSTACK, but instead of stacking horizontally, it stacks vertically.
Points scored, on the other hand, are stacked by first taking the home team’s points with INDEX(check,0,1), which takes the first column, and then the away team’s points with INDEX(check,0,2).
Essentially, we’re creating two “long” arrays, each in a single column, in technical terms, these would be vectors.
gf,VSTACK(score1,score2),gs,VSTACK(score2,score1),
We use the same strategy to calculate goals. Essentially, we’re creating `gf`, which represents goals scored, by first stacking the home goals and then the away goals. Next, we do the same for goals conceded, entering the goals conceded first and then the goals scored. This calculation is necessary because, when teams are tied on points, goal difference is what matters.
Let’s pause for a moment to better understand the logic so far. I’ll just highlight a few matches:
| HOME_TEAM | AWAY_TEAM | RESULT |
| Leicester | Tottenham Hotspur | 1-1 |
| Brighton | Manchester United | 2-1 |
| Crystal Palace | West Ham | 0-2 |
| Fulham | Leicester | 2-1 |
| Manchester City | Ipswich | 4-1 |
If I calculate teams using this data, the result will be:
Leicester
Brighton
Crystal Palace
Fulham
Manchester City
Tottenham Hotspur
Manchester United
West Ham
Leicester
Ipswich
This is because the home teams come first (up to Manchester City) and then the away teams (starting with Tottenham).
If I calculate the points instead, the result will be:
1
3
0
3
3
1
0
3
0
0
This is because we’re discussing the home teams first, then the away teams. Leicester tied with Tottenham (1 point), Brighton beat Manchester United (3 points), and so on. Here too, City won (3 points), Tottenham was the first away team and tied with Leicester (1 point).
Goals scored:
1
2
0
2
4
1
1
2
1
1
City scored 4 goals in their 4-1 win over Ipswich, while Tottenham scored just 1 in their 1-1 draw with Leicester.
Goals conceded:
1
1
2
1
1
1
2
0
2
4
City conceded a goal in their 4-1 win, and Tottenham did the same despite drawing 1-1.
Now let’s put it all together:
| TEAMS | POINTS | GOALS SCORED | GOALS_ALLOWED |
| Leicester | 1 | 1 | 1 |
| Brighton | 3 | 2 | 1 |
| Crystal Palace | 0 | 0 | 2 |
| Fulham | 3 | 2 | 1 |
| Manchester City | 3 | 4 | 1 |
| Tottenham Hotspur | 1 | 1 | 1 |
| Manchester United | 0 | 1 | 2 |
| West Ham | 3 | 2 | 0 |
| Leicester | 0 | 1 | 2 |
| Ipswich | 0 | 1 | 4 |
From here, it should be clearer why we’re going through all these steps, because at this point we can easily calculate the goal difference as the difference between goals scored and goals conceded, and later on, we’ll group the teams.
gfinal,gf-gs,
In fact, goal difference is simply goals scored minus goals allowed.
final,DROP(SORT(GROUPBY(teams,HSTACK(points,gfinal),SUM,,0),
{2,3},{-1,-1}),
0,-1),
Here lies the heart of the formula. Basically, we’re following these steps:
- Let’s group the data by individual teams, obtaining both the points and the goal difference, and summing the values. This is necessary because, as mentioned, points alone aren’t enough, when teams are tied on points, goal difference is what counts
- We then sort the data using SORT in two different ways: first by the second column (points) in descending order (2, then -1), then by goal difference (3, then -1 again)
- Next, we remove the last column because it’s the goal difference and we don’t want to see it; using DROP, we select the last column from the right, and it is then removed
At this point, we have our formula!
Now let’s see how to convert it to LAMBDA:
=LAMBDA(team1,team2,score,[pwin],[ploss],[pdraw],
LET(pw, IF(ISOMITTED(pwin), 3, pwin),
pl, IF(ISOMITTED(ploss), 0, ploss),
pd, IF(ISOMITTED(pdraw), 1, pdraw),
score1, TEXTBEFORE(score," - "),
score2, TEXTAFTER(score," - "),
check,
IF(score1=score2, HSTACK(pd,pd),
IF(score1<score2, HSTACK(pl,pw),
IF(score1>score2, HSTACK(pw,pl)))),
teams, VSTACK(team1,team2),
points, VSTACK(INDEX(check,0,1), INDEX(check,0,2)),
gf, VSTACK(score1,score2),
gs, VSTACK(score2,score1),
gfinal, gf-gs,
final,
DROP(
SORT(
GROUPBY(teams, HSTACK(points,gfinal), SUM,,0),
{2,3},
{-1,-1}
),
0,-1
),
final
)
)
The optional arguments in square brackets, [pwin], [ploss] and [pdraw], use 3, 0 and 1 points when you leave them out: ISOMITTED checks whether each one was entered and falls back to the default value.
To turn it into a reusable function, go to Formulas > Name Manager > New, enter a name such as LEAGUE_TABLE and paste the whole LAMBDA in the Refers to box. From then on, the final standings of any league take a single formula:
=LEAGUE_TABLE(TBL[HOME],TBL[AWAY],TBL[SCORE])
If a competition gives 2 points for a win, just add the fourth argument: =LEAGUE_TABLE(TBL[HOME],TBL[AWAY],TBL[SCORE],2). Keep the scores in the SCORE column as text (for example 2 - 1): typed as they are, Excel may turn some of them into dates.
Conclusion on the LAMBDA function in Excel
The same procedure outlined in the guide should help you create your own LAMBDA functions in Excel from scratch. There are certainly many steps involved, but they help you better understand why these functions represent a true revolution for Excel.
Specifically, the best logical process for creating them (at least for me) is:
- Define a clear goal based on combinations of functions you frequently use (such as SORT, UNIQUE, and COUNTIF, in this specific guide)
- Create, without LAMBDA, the entire underlying creation process, with various steps
- Minimize complexity by using support functions such as HSTACK and VSTACK
- Reduce the number of intervals to be entered by declaring them with LET
- Apply the formula to a LAMBDA by testing it with a known interval
- Create the new LAMBDA as a function
At this point, all you have to do is try it out on your own file and start creating your own LAMBDA functions!
If you’ve already created some, feel free to share them in the comments so everyone can use them, just like UNIQUE.COUNT.SORT, which we covered in this guide and which you can already use as you see fit!
I’d like to thank Owen Price for giving me the push I needed to create this guide, if you want to experiment with LAMBDA, I highly recommend following him on LinkedIn!
