In How to Use Excelâ€™s LET Function, I showed several examples of a powerful function that Microsoft added to Excel 365 in the summer of 2020. In this article, Iâ€™ll show you how to use the LET function with Excelâ€™s relatively new feature, *dynamic arrays*.

If you want to follow along with the explanation, you can download the example file here.

Specifically, weâ€™ll use the LET function to report any number of top or bottom results, dynamically.

### What’s a Dynamic Array?

Microsoft introduced dynamic arrays for Excel 365 in 2018 and 2019â€”depending on your 365 subscription. This feature fundamentally changed the way that Excel works with arrays. Until then, with Control-Shift-Enter (CSE) arrays, we selected the cell or cells into which an array would be displayed. And then we entered the array using CSE.

But now, with dynamic arrays, we can enter array formulas normally, and they will â€śspillâ€ť into as many empty cells as necessary to display the complete array.

These tables illustrate the SORTBY function in action, where the Sorted Table shows the Table1 data sorted by Sales. I didnâ€™t set up the Sorted Table in an Excel Table because dynamic arraysâ€”returned by SORTBY in this caseâ€”donâ€™t work inside Excel Tables.

### Introducing the SORTBY Function

Only two formulas were needed to return the entire Sorted Table. I entered these formulas in the cells directly below the two labels shown:

**Prod:**Â Â =SORTBY( Table1[Prod], Table1[Sales], -1 )

**Sales:**Â Â =SORTBY( Table1[Sales], Table1[Sales], -1 )

The SORTBY function also could return both columns and ten rows with this single formula:

=SORTBY( Table1, Table1[Sales], -1 )

And hereâ€™s the syntax for the SORTBY functionâ€¦

**=SORTBY( Array, Â ByArray1, SortOrder1, Â [ByArray2], [SortOrder2], â€¦ , â€¦ )**

**Array**: The range or array to sort.

**ByArray1**: The first array to sort on.

**SortOrder1**: The sort order, where 1 = ascending order (the default), and -1 = descending order.

Optionally, you can enter additional pairs of ByArray and SortOrder arguments.

### How to Return Sections of a Column of Values

Suppose you now want to return only the top and bottom number of results from Table1, a number specified in the RowsDisp cell shown here.

Thatâ€™s where the fun comes in!

To begin, letâ€™s think about the challenge we face. The easiest way to use formulas to sort our data is to use the SORTBY functionâ€”a function that returns an array. And therefore, we must wrap the SORTBY function with some other function that can return different chunks from the array, as shown in the preceding figure.

If the numbers were in one column of your worksheet, we could choose between two functions to return, say, the first three cells in the column:

**=INDEX( reference, row_num, column_num )**

**=OFFSET( reference, rows, cols, height, width )**

Specifically, we could write formulas like this:

=INDEX( MyRange, 1 ) : INDEX( MyRange, 3 )

=OFFSET( MyRange, 0, 0, 3 )

But these formulas donâ€™t work with an array. Specificallyâ€¦

=INDEX( {1;2;3;4;5}, 1) : INDEX( {1;2;3;4;5}, 3 )

â€¦ returns a #VALUE! error. That is, the reference operator â€ś:â€ť doesnâ€™t work with arrays.

And Excel doesnâ€™t even allow us to enter:

=OFFSET( {1;2;3;4;5}, 0, 0, 3 )

### How to Return Sections of an Array

Letâ€™s expand our possible solutions to this problem by thinking even more about arrays. Hereâ€™s one **very useful** but somewhat obscure Excel feature when we use arrays in formulas:

*When a worksheet function expects a single value, but we give it an array instead, the function returns a result for each value in that array.*

For example, this formula returns the value 2, as expected:

=INDEX( {2;4;6;8;10}, 1 )

But this formula returns the value 12:

=SUM( INDEX( {2;4;6;9;10}, {1;2;3} )Â )

That is, because we used the array {1;2;3} for the INDEX functionâ€™s row_num argument, the INDEX function returned the array {2;4;6}, and then the SUM function returned the sum of those numbers, which is 12.

So then, going back to the Sorted Table, shown again here, all we need to do to return arrays of the first three Product and Sales columns is to use formulas like this:

=INDEX( Prod, {1;2;3} )

=INDEX( Sales, {1;2;3} )

But how do we generate those array constants dynamically?

### Introducing the SEQUENCE Function

This is where another dynamic array function comes into play: SEQUENCE. Hereâ€™s its syntaxâ€¦

**=SEQUENCE( rows, columns, start, step )**

â€¦and here are a few examples from the example workbook you can download:

=SEQUENCE(3)Â = Â {1;2;3}

=SEQUENCE( 3, 1, 10, -1 )Â =Â {10;9;8}

=SEQUENCE( 3, 1, 5, 1) Â = Â {5;6;7}

### Using the LET Function to Return Product Names

So now, FINALLY, letâ€™s get to the LET function. Hereâ€™s the Table we want to sort, some named cells, and the tables we want to create:

Here are the contents of the named cells:

**RowsAll:**Â =ROWS( Table1 )

**RowsDisp:** 5

Now, letâ€™s begin the LET functionâ€¦

For the top products, we know weâ€™re going to need the SEQUENCE array, so letâ€™s start with it:

=LET(

Seq, SEQUENCE( RowsDisp ),

ArrayTop, 99,

ArrayTop )

Now we can add the SORTBY array to display the product names:

=LET(

Seq, SEQUENCE( RowsDisp ),

ArrayAll, SORTBY( Table1[Prod], Table1[Sales], -1 ),

ArrayTop, 99,

ArrayTop )

And finally, all we need to do is to use INDEX to return the number of top Product names that we specified in the RowsDisp cell:

=LET(

Seq, SEQUENCE( RowsDisp ),

ArrayAll, SORTBY( Table1[Prod], Table1[Sales], -1 ),

ArrayTop, INDEX( ArrayAll, Seq ),

ArrayTop )

### Using the LET Function to Return the Sales Values

To return the top of the Sales values, we can copy the Prod array to the Sales cell and then change only the highlighted value:

=LET(

Seq, SEQUENCE( RowsDisp ),

ArrayAll, SORTBY( Table1[Sales], Table1[Sales], -1),

ArrayTop, INDEX( ArrayAll,Seq ),

ArrayTop )

If we wanted to comment this formula, we could use this variation:

=LET(

Seq, SEQUENCE( RowsDisp ),Â Â Note1, “An array like: {1;2;3}”,

ArrayAll, SORTBY( Table1[Sales], Table1[Sales], -1),Â Â Note2, “The full sorted Sales”,

ArrayTop, INDEX( ArrayAll,Seq ),Â Â Note3, “The top rows of sorted Sales”,

ArrayTop )

Here’s the syntax for the SEQUENCE function again:

**=SEQUENCE( rows, columns, start, step )**

And hereâ€™s the commented formula for the bottom of our sorted array for Sales:

=LET(

Seq, SEQUENCE( RowsDisp, 1, RowsAll – RowsDisp + 1 ), Note1, “An array like: {8;9;10}”,

ArrayAll, SORTBY( Table1[Sales], Table1[Sales], -1),Â Â Note2, “The full sorted Sales array”,

ArrayTop, INDEX( ArrayAll, Seq ),Â Â Â Â Â Â Â Â Â Â Â Note3, “The bottom rows of sorted Sales”,

ArrayTop )

And then, if we enter a RowsDisp value of 3, hereâ€™s our result:

And the formulas that return the labels for the two tables are:

=”Top “&CHOOSE(RowsDisp,”One”, “Two”, “Three”, “Four”, “Five”) &” Items”

=”Bottom “&CHOOSE(RowsDisp,”One”, “Two”, “Three”, “Four”, “Five”) &” Items”

Again, you can download the workbook at this link.

Also, How to Use Excel’s LET Function provides an introduction to the LET function.