How to use the REDUCE function
What is the REDUCE function?
The REDUCE function shrinks an array to an accumulated value, a LAMBDA function is needed to properly accumulate each value in order to return a total.
Table of Contents
1. Introduction
What is an array?
An array in Excel is a group of values. The values may come from a worksheet but sometimes the are hard-coded into formulas using curly brackets like this: {5,2,6,1}
What is a LAMBDA function?
The LAMBDA function lets you build custom functions using only regular Excel functions, no VBA is needed. These custom functions based on the LAMBDA function are available only in your workbook.
You can also create recursive functions that before was only possible in User defined Functions and macros created in visual basic for applications (VBA).
Can the REDUCE function create more values than the original array?
Yes. You can actually create more values than the original size using the REDUCE function, this article shows how:
Workaround for the TEXTSPLIT function – LAMBDA function
2. Syntax
REDUCE([initial_value], array, lambda(accumulator, value, calculation))
3. Arguments
[initial_value] | Optional. Accumulator start value. |
array | Required. An array or cell range to be processed. |
lambda( accumulator, value, calculation) |
Required. A lambda function with three parameters: - accumulator - value - calculation |
accumulator | Required. A value that changes for each value in the array or cell range. |
value | Required. Iterates through each value in the array or cell range. |
calculation | Required. A formula that uses parameters accumulator and value. |
4. Example 1
This example shows how to count values in a cell range that meet a specific condition. If a number begins with 1 the accumulator argument is increased by 1.
This is a simple demonstration of the REDUCE function, I know that the formula can be made a lot smaller using only the SUM function and the LEFT function. This an easy example so you can understand the basics of the REDUCE function.
Formula in cell D3:
The accumulator argument is used as a variable to keep track of numbers that begin with 1. For example, the first number in cell range B3:B12 is 745. It doesn't begin with 1 so nothing is added to the acc. The initial value of acc is 0 (zero) and is still 0 (zero).
However, the second value is 13 and it begins with 1. Acc is 0 (zero) and 1 is added to 0 (zero). The acc value is now 1. This repeats until all values in B3:B12 are processed.
Explaining the formula
Step 1 - Get first character from left
The LEFT function extracts a specific number of characters always starting from the left.
Function syntax: LEFT(text, [num_chars])
LEFT(val)
becomes
LEFT(745)
and returns "7".
Step 2 - Compare character with "1"
The equal sign lets you compare values in an Excel formula.
LEFT(val)="1"
becomes
"7"="1"
and returns FALSE.
The equal sign is a logical operator, the result is TRUE or FALSE.
Step 3 - Add acc by 1 if logical expression evaluates to TRUE
The IF function returns one value if the logical test is TRUE and another value if the logical test is FALSE.
Function syntax: IF(logical_test, [value_if_true], [value_if_false])
IF(LEFT(val)="1",acc+1,acc)
becomes
IF(FALSE,acc+1,acc)
and returns acc which is 0 (zero) to begin with.
Step 4 - Pass two parameters to function
The LAMBDA function build custom functions without VBA, macros or javascript.
Function syntax: LAMBDA([parameter1, parameter2, …,] calculation)
LAMBDA(acc,val, IF(LEFT(val)="1",acc+1,acc))
Step 5 - Pass all values in cell range B3:B12
REDUCE(0,B3:B12,LAMBDA(acc,val, IF(LEFT(val)="1",acc+1,acc)))
returns 4.
4 numbers begin "1" in cell range B3:B12.
5. Example 2
This formula lists unique distinct "Regions" from column C and populates the first column of the returning array, the remaining columns are populated by values from the "Text" column based on the adjacent "Region" value.
In other words, the formula groups values by items in a given column based on a condition that they are on the same row.
For example, item "East" is found in cells C3 and C6. The corresponding values on the same rows are "EVA LFO" and "THR DQY", these values are now grouped together in the output array. This is done with each unique distinct value in cell range C3:C12.
Formula in cell E3:
Explaining the formula
Step 1 - Compare Region to values in cell range C3:C12
The equal sign is a logical operator, the result is TRUE or FALSE. It lets you compare values in an Excel formula.
val=C3:C12
Step 2 - Filter values in cell range B3:B12 based on condition
The FILTER function extracts values/rows based on a condition or criteria.
Function syntax: FILTER(array, include, [if_empty])
FILTER(B3:B12,val=C3:C12)
Step 3 - Transpose values vertically to horizontally
The TRANSPOSE function converts a vertical range to a horizontal range, or vice versa.
Function syntax: TRANSPOSE(array)
TRANSPOSE(FILTER(B3:B12,val=C3:C12))
Step 4 - Add array to acc
The VSTACK function combines cell ranges or arrays. Joins data to the first blank cell at the bottom of a cell range or array (vertical stacking)
Function syntax: VSTACK(array1,[array2],...)
VSTACK(acc,TRANSPOSE(FILTER(B3:B12,val=C3:C12)))
Step 5 - Create a LAMBDA function in order to use the REDUCE function
The LAMBDA function build custom functions without VBA, macros or javascript.
Function syntax: LAMBDA([parameter1, parameter2, …,] calculation)
LAMBDA(acc,val,VSTACK(acc,TRANSPOSE(FILTER(B3:B12,val=C3:C12))))
Step 6 - List unique distinct values from cell range C3:C12
The UNIQUE function returns a unique or unique distinct list.
Function syntax: UNIQUE(array,[by_col],[exactly_once])
UNIQUE(C3:C12)
becomes
UNIQUE({"East";"South";"South";"East";"North";"West";"South";"West";"South";"North"})
and returns
{"East";"South";"North";"West"}
Step 7 - Pass each unique distinct value to the LAMBDA function
REDUCE("",UNIQUE(C3:C12),LAMBDA(acc,val,VSTACK(acc,TRANSPOSE(FILTER(B3:B12,val=C3:C12)))))
First iteration: {"",#N/A,#N/A,#N/A}
All iterations stacked vertically:
{"",#N/A,#N/A,#N/A;"EVA LFO","THR DQY",#N/A,#N/A;"RQW WPQ","HNL RYN","UNW NUM","BHZ ONF";"HYJ WXA","JZM UXJ",#N/A,#N/A;"WEA HGU","NXE OGF",#N/A,#N/A}
The first row in the resulting array is a blank value and the remaining values are error values. This row is not needed.
Step 8 - Remove first row in the array
The DROP function removes a given number of rows or columns from a 2D cell range or array.
Function syntax: DROP(array, rows, [columns])
DROP(REDUCE("",UNIQUE(C3:C12),LAMBDA(acc,val,VSTACK(acc,TRANSPOSE(FILTER(B3:B12,val=C3:C12))))),1)
becomes
DROP({"",#N/A,#N/A,#N/A;"EVA LFO","THR DQY",#N/A,#N/A;"RQW WPQ","HNL RYN","UNW NUM","BHZ ONF";"HYJ WXA","JZM UXJ",#N/A,#N/A;"WEA HGU","NXE OGF",#N/A,#N/A},1)
and returns
{"EVA LFO","THR DQY",#N/A,#N/A;"RQW WPQ","HNL RYN","UNW NUM","BHZ ONF";"HYJ WXA","JZM UXJ",#N/A,#N/A;"WEA HGU","NXE OGF",#N/A,#N/A}
Step 9 - Stack unique distinct values and the resulting array horizontally
The HSTACK function combines cell ranges or arrays. Joins data to the first blank cell to the right of a cell range or array (horizontal stacking)
Function syntax: HSTACK(array1,[array2],...)
HSTACK(UNIQUE(C3:C12),DROP(REDUCE("",UNIQUE(C3:C12),LAMBDA(acc,val,VSTACK(acc,TRANSPOSE(FILTER(B3:B12,val=C3:C12))))),1))
returns
{"East", "EVA LFO", "THR DQY", #N/A, #N/A;"South", "RQW WPQ", "HNL RYN", "UNW NUM", "BHZ ONF";"North", "HYJ WXA", "JZM UXJ", #N/A, #N/A;"West", "WEA HGU", "NXE OGF", #N/A, #N/A}
Step 10 - Remove error values
The IFERROR function if the value argument returns an error, the value_if_error argument is used. If the value argument does NOT return an error, the IFERROR function returns the value argument.
Function syntax: IFERROR(value, value_if_error)
IFERROR(HSTACK(UNIQUE(C3:C12),DROP(REDUCE("",UNIQUE(C3:C12),LAMBDA(acc,val,VSTACK(acc,TRANSPOSE(FILTER(B3:B12,val=C3:C12))))),1)),"")
becomes
IFERROR({"East", "EVA LFO", "THR DQY", #N/A, #N/A;"South", "RQW WPQ", "HNL RYN", "UNW NUM", "BHZ ONF";"North", "HYJ WXA", "JZM UXJ", #N/A, #N/A;"West", "WEA HGU", "NXE OGF", #N/A, #N/A}, "")
and returns
{"East", "EVA LFO", "THR DQY", "", "";"South", "RQW WPQ", "HNL RYN", "UNW NUM", "BHZ ONF";"North", "HYJ WXA", "JZM UXJ", "", "";"West", "WEA HGU", "NXE OGF", "", ""}
6. Function not working
The REDUCE function returns
- #NAME? error if you misspell the function name.
- propagates errors, meaning that if the input contains an error (e.g., #VALUE!, #REF!), the function will return the same error.
6.1 Troubleshooting the error value
When you encounter an error value in a cell a warning symbol appears, displayed in the image above. Press with mouse on it to see a pop-up menu that lets you get more information about the error.
- The first line describes the error if you press with left mouse button on it.
- The second line opens a pane that explains the error in greater detail.
- The third line takes you to the "Evaluate Formula" tool, a dialog box appears allowing you to examine the formula in greater detail.
- This line lets you ignore the error value meaning the warning icon disappears, however, the error is still in the cell.
- The fifth line lets you edit the formula in the Formula bar.
- The sixth line opens the Excel settings so you can adjust the Error Checking Options.
Here are a few of the most common Excel errors you may encounter.
#NULL error - This error occurs most often if you by mistake use a space character in a formula where it shouldn't be. Excel interprets a space character as an intersection operator. If the ranges don't intersect an #NULL error is returned. The #NULL! error occurs when a formula attempts to calculate the intersection of two ranges that do not actually intersect. This can happen when the wrong range operator is used in the formula, or when the intersection operator (represented by a space character) is used between two ranges that do not overlap. To fix this error double check that the ranges referenced in the formula that use the intersection operator actually have cells in common.
#SPILL error - The #SPILL! error occurs only in version Excel 365 and is caused by a dynamic array being to large, meaning there are cells below and/or to the right that are not empty. This prevents the dynamic array formula expanding into new empty cells.
#DIV/0 error - This error happens if you try to divide a number by 0 (zero) or a value that equates to zero which is not possible mathematically.
#VALUE error - The #VALUE error occurs when a formula has a value that is of the wrong data type. Such as text where a number is expected or when dates are evaluated as text.
#REF error - The #REF error happens when a cell reference is invalid. This can happen if a cell is deleted that is referenced by a formula.
#NAME error - The #NAME error happens if you misspelled a function or a named range.
#NUM error - The #NUM error shows up when you try to use invalid numeric values in formulas, like square root of a negative number.
#N/A error - The #N/A error happens when a value is not available for a formula or found in a given cell range, for example in the VLOOKUP or MATCH functions.
#GETTING_DATA error - The #GETTING_DATA error shows while external sources are loading, this can indicate a delay in fetching the data or that the external source is unavailable right now.
6.2 The formula returns an unexpected value
To understand why a formula returns an unexpected value we need to examine the calculations steps in detail. Luckily, Excel has a tool that is really handy in these situations. Here is how to troubleshoot a formula:
- Select the cell containing the formula you want to examine in detail.
- Go to tab “Formulas” on the ribbon.
- Press with left mouse button on "Evaluate Formula" button. A dialog box appears.
The formula appears in a white field inside the dialog box. Underlined expressions are calculations being processed in the next step. The italicized expression is the most recent result. The buttons at the bottom of the dialog box allows you to evaluate the formula in smaller calculations which you control. - Press with left mouse button on the "Evaluate" button located at the bottom of the dialog box to process the underlined expression.
- Repeat pressing the "Evaluate" button until you have seen all calculations step by step. This allows you to examine the formula in greater detail and hopefully find the culprit.
- Press "Close" button to dismiss the dialog box.
There is also another way to debug formulas using the function key F9. F9 is especially useful if you have a feeling that a specific part of the formula is the issue, this makes it faster than the "Evaluate Formula" tool since you don't need to go through all calculations to find the issue..
- Enter Edit mode: Double-press with left mouse button on the cell or press F2 to enter Edit mode for the formula.
- Select part of the formula: Highlight the specific part of the formula you want to evaluate. You can select and evaluate any part of the formula that could work as a standalone formula.
- Press F9: This will calculate and display the result of just that selected portion.
- Evaluate step-by-step: You can select and evaluate different parts of the formula to see intermediate results.
- Check for errors: This allows you to pinpoint which part of a complex formula may be causing an error.
The image above shows cell reference B3:B12 converted to hard-coded value using the F9 key. The REDUCE function requires non-error values which is not the case in this example. We have found what is wrong with the formula.
Tips!
- View actual values: Selecting a cell reference and pressing F9 will show the actual values in those cells.
- Exit safely: Press Esc to exit Edit mode without changing the formula. Don't press Enter, as that would replace the formula part with the calculated value.
- Full recalculation: Pressing F9 outside of Edit mode will recalculate all formulas in the workbook.
Remember to be careful not to accidentally overwrite parts of your formula when using F9. Always exit with Esc rather than Enter to preserve the original formula. However, if you make a mistake overwriting the formula it is not the end of the world. You can “undo” the action by pressing keyboard shortcut keys CTRL + z or pressing the “Undo” button
6.3 Other errors
Floating-point arithmetic may give inaccurate results in Excel - Article
Floating-point errors are usually very small, often beyond the 15th decimal place, and in most cases don't affect calculations significantly.
'REDUCE' function examples
Question: I am trying to create an excel spreadsheet that has a date range. Example: Cell A1 1/4/2009-1/10/2009 Cell B1 […]
The above image demonstrates a formula that adds values in three different columns into one column. Table of Contents Merge […]
The picture above shows data presented in only one column (column B), this happens sometimes when you get an undesired […]
Functions in 'Logical' category
The REDUCE function function is one of 16 functions in the 'Logical' category.
How to comment
How to add a formula to your comment
<code>Insert your formula here.</code>
Convert less than and larger than signs
Use html character entities instead of less than and larger than signs.
< becomes < and > becomes >
How to add VBA code to your comment
[vb 1="vbnet" language=","]
Put your VBA code here.
[/vb]
How to add a picture to your comment:
Upload picture to postimage.org or imgur
Paste image link to your comment.
Contact Oscar
You can contact me through this contact form