Using custom Lambda functions within Excel GROUPBY and PIVOTBY formulas
Using custom Lambda functions within Excel GROUPBY and PIVOTBY formulas
Explore the hidden potential of Excel's GROUPBY and PIVOTBY functions by integrating custom lambdas for
dynamic and flexible data aggregation.
Among the latest additions to the Excel family of dynamic array functions are two aggregation tools:
GROUPBY and PIVOTBY. While useful in their basic form, they truly shine when paired with custom
Lambda functions, enabling users to summarize data in ways beyond what prede�ned Eta lambdas
o�er. This guide will walk you through using these advanced features e�ectively.
In simple terms, LAMBDA is an anonymous function that allows you to build your own functions using
other functions. Think of it as a shortcut for creating a custom function for a speci�c task without
needing to formally de�ne and name it.
=LAMBDA(x, x + 1)
1 of 9 2/15/2025, 3:28 PM
Firefox https://www.ablebits.com/office-addins-blog/custom-lambda-excel-gro...
Lambda functions are useful because they let you encapsulate complex logic in a simple, reusable
way. This can make your formulas more readable and your calculations more �exible.
Microsoft Excel has its own LAMBDA function, enabling users to create custom functions that can be
called by user-friendly names and reused throughout a workbook.
Simple example
Imagine you have two functions that do the same thing but look di�erent. Eta reduction helps you
determine that these functions are essentially the same.
�(x) = x + 1
g(x) = �(x)
Using eta conversion, you can see that g is really just � in disguise because g does exactly what � does.
So, the equation can be simpli�ed to:
g = �
In other words, Eta reduction shows that two functions are essentially the same as they give the same
result for any input.
For example, to group data in B2:B30 and sum the values in C2:C30, the full syntax would be:
2 of 9 2/15/2025, 3:28 PM
Firefox https://www.ablebits.com/office-addins-blog/custom-lambda-excel-gro...
In this formula, x is just a dummy variable, or a placeholder, for the values in each row of C2:C30. The
GROUPBY function iterates through each row in that range and applies the LAMBDA function, which in
turn calls SUM.
While the full syntax works �ne, it's unnecessarily complex for such a simple operation. With an eta
lambda, you can remove the LAMBDA wrapper and call the SUM function directly by its name:
This not only improves formula readability but makes it super easy and intuitive to use. In fact, you
don't need to know anything about lambdas – simply select the required function from the list:
3 of 9 2/15/2025, 3:28 PM
Firefox https://www.ablebits.com/office-addins-blog/custom-lambda-excel-gro...
Note. The eta-reduced lambda syntax is only possible for functions that accept a single argument
like SUM, MIN, MAX, COUNT, etc. It cannot be used for a function that requires two or more
arguments.
Understanding how the inbuilt eta-lambdas operate within Excel aggregation functions, let's look at
how you can construct your custom lambdas to perform calculations that extend beyond the
capabilities of prede�ned eta-reduced lambdas.
4 of 9 2/15/2025, 3:28 PM
Firefox https://www.ablebits.com/office-addins-blog/custom-lambda-excel-gro...
To achieve this, you can use the following lambda function that calculates 10% of the sum of a given
range. More precisely, the function takes a range x as input and returns the sum of the range
multiplied by 10 percent:
LAMBDA(x, 10%*SUM(x))
After entering the formula in, say E3, Excel will display the grouped data with a new column showing
10% of the revenue for each project type, exactly as needed for your analysis:
5 of 9 2/15/2025, 3:28 PM
Firefox https://www.ablebits.com/office-addins-blog/custom-lambda-excel-gro...
Among the list of functions available with GROUPBY is a new ARRAYTOTEXT function that converts an
array of values into text strings. By default, the values are separated with a comma and a space, as
shown in this example that demonstrates the standard usage of the function.
However, if you wish to use a di�erent delimiter, you can create your own Lambda function using
TEXTJOIN and specify the desired character in its delimiter argument. For instance, to group data by
the manager names in column A and return all projects associated with each manager as strings
separated with a vertical bar surrounded by spaces on both sides, you would use the following
formula:
As a result, you will receive a neatly organized list where each manager's projects are grouped
together and separated by the delimiter of your choosing, providing a clear and readable summary.
Additionally, you can arrange the joined values alphabetically, by nesting the SORT function within
TEXTJOIN like this:
6 of 9 2/15/2025, 3:28 PM
Firefox https://www.ablebits.com/office-addins-blog/custom-lambda-excel-gro...
By doing so, you ensure that the projects for each manager are not only grouped together but also
sorted from A to Z, enhancing the clarity and organization of your data.
By using this formula, you will get each manager's projects presented in the form of a string, where
values are separated by the speci�ed delimiter, listed only once, and sorted alphabetically:
7 of 9 2/15/2025, 3:28 PM
Firefox https://www.ablebits.com/office-addins-blog/custom-lambda-excel-gro...
Let's consider a dataset below with separate columns for regions (A), product names(C), and annual
sales (D).
To get an average of annual sales for each product-region category, you can use a built-in eta lambda
AVERAGE – this will work nicely as column C lists annual sales:
In case you want the quarterly sales average for each group, you need to divide the annual sales
�gures by 4. This can be accomplished using a custom lambda function:
8 of 9 2/15/2025, 3:28 PM
Firefox https://www.ablebits.com/office-addins-blog/custom-lambda-excel-gro...
Hopefully, these examples have given you insights into how powerful and �exible Excel's new dynamic
array functions can be when combined with custom lambda functions. In a similar manner, you can
aggregate data in various ways using other functions that accept lambdas, such as BYROW, BYCOL,
etc.
9 of 9 2/15/2025, 3:28 PM