How to use the BYCOL function
What is the BYCOL function?
The BYCOL function passes all values in a column based on an array to a LAMBDA function, the LAMBDA function calculates new values based on a formula you specify. It then continues with the next column until all columns in the array have been processed.
Table of Contents
1. Introduction
What is an array in Excel?
An array in Excel is a collection of values arranged in rows and columns. It can be thought of as a table or a grid of data.
There are two types of arrays. One-dimensional arrays: A single row or column of data. Two-dimensional arrays: Data organized in both rows and columns.
There are two types of array formulas: The first type returns a single value and the second type returns multiple values. An array formula is a formula that can perform multiple calculations on one or more sets of values.
Excel 365 subscribers have access to dynamic array formulas, a powerful feature that automatically adjusts its output range. These formulas populate the initial target cell and expand into neighboring cells as needed, adapting their size based on the formula's result. This automatic expansion and contraction of the output range is the key characteristic that gives them the name "dynamic" array formulas. The process of extending results into adjacent cells is known as "spilling.
Excel processes arrays in RAM allowing for rapid computations. However, when array sizes exceed available memory, Windows may resort to using virtual memory on the hard drive or SSD. This fallback to disk storage significantly slows down calculations, as accessing data from these devices is much slower than from RAM.
What is the LAMBDA function?
The BYCOL function uses the LAMBDA function as its second argument to define how each cell in the array should be calculated. This combination allows you to perform cumulative calculations across a range of values.
The LAMBDA function is required in the BYCOL function, you can't leave it out. Read more: LAMBDA function
2. Syntax
BYCOL(array, lambda(array, calculation))
Argument | Description |
array | Required. A cell reference to a cell range or an array. |
3. How to count the top 10 largest numbers in a cell range per column
Cell range B4:F13 contains some random numbers. The top ten largest numbers out of all numbers in cells B4:F13 are highlighted in blue. Cell H4 contains a formula that counts the top 10 largest numbers per column. In other words, how many top 10 numbers are in each column in B4:F13.
Excel 365 formula in cell B15:
The formula in cell B15 returns a dynamic array that spills values horizontally. It returns the following array: {5, 1, 1, 2, 1} which corresponds to each column in B4:F13.
Here is how the formula works:
- LARGE(B4:F13,10): This function finds the 10th largest value in the range B4:F13.
- B4:F13 >= LARGE(B4:F13,10): This compares each cell in the range B4:F13 to the 10th largest value. It creates a boolean array where each cell is TRUE if it's greater than or equal to the 10th largest value, and FALSE otherwise.
- IF(B4:F13>=LARGE(B4:F13,10),1,0): This part is the condition being checked for each cell in the range B4:F13
IF(condition, value_if_true, value_if_false): This is the IF function, which takes three arguments:a. The condition to check (B4:F13 >= LARGE(B4:F13,10))
b. The value to return if the condition is true (1)
c. The value to return if the condition is false (0)
The IF statement checks if each cell is greater than or equal to this 10th largest value. If true, it returns 1; if false, it returns 0. For each cell in B4:F13, if the value is greater than or equal to the 10th largest value in the range, it returns 1. Otherwise, it returns 0.
This effectively creates a new array of the same size as B4:F13, but filled with only 1s and 0s. - LAMBDA(a,SUM(a)): This is the calculation applied to each column. It sums up the 1s and 0s in each column.
- BYCOL(): This function applies a calculation to each column of a range and returns the results.
Explaining the formula
Step 1 - Calculate the 10th largest number
The LARGE function calculates the k-th largest value from an array of numbers.
Function syntax: LARGE(array, k)
LARGE(B4:F13,10)
becomes
LARGE({527,79,121,76,128;219, ... ,241},10)
and returns 701.
Step 2 - Compare the 10th largest number to all numbers in the cell range
The greater than sign lets you compare values in an Excel formula, this example shows the greater than sign and an equal sign combined.
These characters are logical operators, they return TRUE or FALSE.
B4:F13>=LARGE(B4:F13,10)
becomes
B4:F13>=701
becomes
{527,79,121,76,128;219, ... ,241}>=701
and returns
{FALSE, FALSE, FALSE, FALSE, FALSE; FALSE, ... , FALSE}
Step 3 - Replace boolean values with numerical values
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(B4:F13>=LARGE(B4:F13,10),1,0)
becomes
IF({FALSE, FALSE, FALSE, FALSE, FALSE; FALSE, ... , FALSE},1,0)
and returns
{0, 0, 0, 0, 0; 0, ... , 0}.
Step 4 - Build the LAMBDA calculation
The SUM function allows you to add numerical values, the function returns the sum in the cell it is entered in. The SUM function is cleverly designed to ignore text and boolean values, adding only numbers.
Function syntax: SUM(number1, [number2], ...)
SUM(a)
Step 5 - Build the LAMBDA function
The LAMBDA function build custom functions without VBA, macros or javascript.
Function syntax: LAMBDA([parameter1, parameter2, …,] calculation)
LAMBDA(a,SUM(a))
Step 6 - Pass values to the LAMBDA function
BYCOL(IF(B4:F13>=LARGE(B4:F13,10),1,0),LAMBDA(a,SUM(a)))
becomes
BYCOL({0, 0, 0, 0, 0; 0, ... , 0},LAMBDA(a,SUM(a)))
and returns
{5, 1, 1, 2, 1}.
4. How to count the bottom 10 numbers in a cell range per column
Cell range B4:F13 contains some random numbers. The bottom ten smallest numbers are highlighted yellow.
Cell H4 contains a formula that counts the bottom 10 smallest numbers per column.
Excel 365 formula in cell H4:
Explaining the formula
Step 1 - Calculate the 10th smallest number
The SMALL function returns the k-th smallest value from a group of numbers.
Function syntax: SMALL(array, k)
SMALL(B4:F13,10)
becomes
SMALL({527,79,121,76,128;219, ... ,241},10)
and returns 145.
Step 2 - Compare the 10th smallest number to all numbers in the cell range
The greater than sign lets you compare values in an Excel formula, this example shows the greater than sign and an equal sign combined.
These characters are logical operators, they return TRUE or FALSE.
B4:F13>=SMALL(B4:F13,10)
becomes
B4:F13<=701
becomes
{527,79,121,76,128;219, ... ,241}<=145
and returns
{FALSE, TRUE, TRUE, TRUE, TRUE; FALSE, ... , FALSE}
Step 3 - Replace boolean values with numerical values
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(B4:F13<=SMALL(B4:F13,10),1,0)
becomes
IF({FALSE, TRUE, TRUE, TRUE, TRUE; FALSE, ... , FALSE},1,0)
and returns
{0, 1, 1, 1, 1;0, ... , 0}.
Step 4 - Build the LAMBDA calculation
The SUM function allows you to add numerical values, the function returns the sum in the cell it is entered in. The SUM function is cleverly designed to ignore text and boolean values, adding only numbers.
Function syntax: SUM(number1, [number2], ...)
SUM(a)
Step 5 - Build the LAMBDA function
The LAMBDA function build custom functions without VBA, macros or javascript.
Function syntax: LAMBDA([parameter1, parameter2, …,] calculation)
LAMBDA(a,SUM(a))
Step 6 - Pass values to the LAMBDA function
BYROW(IF(B4:F13<=SMALL(B4:F13,10),1,0),LAMBDA(a,SUM(a)))
becomes
BYROW({0, 1, 1, 1, 1;0, ... , 0},LAMBDA(a,SUM(a)))
and returns
{0, 2, 2, 3, 3}.
Functions in 'Logical' category
The BYCOL 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