Forum Discussion
How to dynamically set the data range for the LARGE function (or any function really)?
Consider formatting your data as a Table.
Place your cursor in your current range and use Ctrl+T to convert it to a table.
Use the Table Design Tab to select or create a Table Style that is to your liking.
Give your table a name in the Table Name box in the Table Design tab of the ribbon.
As you add new columns to the right of this sheet, it will automatically expand the table to include the new columns.
Suppose your table is called product_sales:
You can now refer to the table and columns by name and as such always retrieve the right-most column with this formula:
=INDEX(product_sales,,COLUMNS(product_sales))
Further, if you want to get the top 3 largest values and their products from the latest month, you can use this:
=LET(
t,product_sales[#All],
header,INDEX(t,1,),
latest,FILTER(t,(header="Product name")+(COLUMN(header)=MAX(COLUMN(header)))),
best_latest,FILTER(latest,INDEX(latest,,2)>=LARGE(INDEX(latest,,2),3)),
best_latest
)
To break this down:
1. We assign the shorter name "t" to the table containing your data.
2. We take the first row from "t" and give it the name "header".
3. We FILTER "t" for those columns where either the first row contains "Product name" or (indicated by the +) where the column number of the header is equal to the maximum column number in the whole header. This is equivalent to saying it is the right-most column. We call the result of this first filter "latest". It is a two-column array.
4. We apply a second FILTER to filter "latest" for those rows where the value in the second column is greater than or equal to the 3rd largest value in that column. The result is a two-column, four-row array where the first column contains the product name, the second column contains the value for that month and the first row contains the original column header.
It looks like this:
This formula will always get the 3 rows with the largest values in the right-most column of the table.