Return the top N items ranked by scores
=IF(
OR(items="", scores="", n=""),
"",
IFERROR(
INDEX(
SORTN(
SORT(
FILTER(
{items, scores},
(items<>"") * (scores<>"") * ISNUMBER(scores)
),
2,
FALSE
),
VALUE(n),
0,
2,
FALSE
),
,
1
),
""
)
)TOP_N returns the highest-ranked items from a list based on a corresponding list of numeric scores. It's useful for leaderboards, top-performing products, prioritizing tasks, or summarizing the best results without manually sorting. For example, if A2:A10 contains item names and B2:B10 contains their scores, then =TOP_N(A2:A10, B2:B10, 3) returns the 3 items with the highest scores.
Use the inputs below to create TOP_N as a reusable custom function in Google Sheets.
Learn how to add custom functions to Google SheetsTOP_N
Return the top N items ranked by scores
=IF( OR(items="", scores="", n=""), "", IFERROR( INDEX( SORTN( SORT( FILTER( {items, scores}, (items<>"") * (scores<>"") * ISNUMBER(scores) ), 2, FALSE ), VALUE(n), 0, 2, FALSE ), , 1 ), "" ) )
items
scores
n
Range of items to return in ranked order
A2:A100
Numeric scores used to rank items (same shape as items)
B2:B100
Number of top results to return
5
Other functions in the same category: List & Array Operations
Return the botttom N items ranked by scores
Returns the first non-blank value in a range
Extract a list of unique values from a range
Compare two ranges and list the elements that are in the first but not in the second
Concatenates multiple ranges or arrays and removes duplicates
Named functions enable the creation of custom, reusable formulas that mimic built-in functions, streamlining calculations and data manipulations. These functions simplify complex formulas, making spreadsheets more readable and less prone to errors. By encapsulating intricate logic within a single function call, they enhance consistency across your data. Utilizing named functions reduces the need to write lengthy formulas repeatedly, thereby improving workflow efficiency and productivity in data analysis and management.
Download and import — fastest way to add the function but does not include argument descriptions and examples
Follow these simple steps to download and import a function into your spreadsheet. This method is the quickest and easiest way to add the named function to your Google Sheets document, but will not include the argument description and examples.
Once added, the function will be ready to use in your document like any other built-in function. Simply type the function name and provide the required inputs to use it in your calculations.
Copy and paste — copy and paste each property one at a time
Follow these simple steps to integrate a custom named function into your spreadsheet:
Once added, the function will be ready to use in your document like any other built-in function. Simply type the function name and provide the required inputs to use it in your calculations.