(Excel examples for Beginners)
In this end-to-end excel example, you will learn – Excel formula for Beginners – How to Sum top n values in Excel.
Excel formula for Beginners – How to Sum top n values in Excel
Generic formula
=SUMPRODUCT(LARGE(rng,{1,2,N}))
Explanation
To sum the top values in a range, you can use a formula based on the LARGE function, wrapped inside the SUMPRODUCT function. In the generic form of the formula (above), rng represents a range of cells that contain numeric values and N represents the idea of Nth value.
In the example, the active cell contains this formula:
=SUMPRODUCT(LARGE(B4:B13,{1,2,3}))
How this formula works
In its simplest form, LARGE will return the “Nth largest” value in a range. For example, the formula:
=LARGE(B4:B13, 2)
will return the 2nd largest value in the range B4:B13 which, in the example above, is the number 9.
However, if you supply an “array constant” (e.g. a constant in the form {1,2,3}) to LARGE as the second argument, LARGE will return an array of results instead of a single result. So, the formula:
=LARGE(B4:B13,{1,2,3})
will return the 1st, 2nd, and 3rd largest value in the range B4:B13. In the example above, where B4:B13 contains the numbers 1-10, the result from LARGE will be the array {8,9,10}. SUMPRODUCT then sums the numbers in this array and returns a total, which is 27.
SUM instead of SUMPRODUCT
SUMPRODUCT is a flexible function that allows you to uses cell references for k inside the LARGE function.
However, if you are using a simple hard-coded array constant like {1,2,3} you can just use the SUM function:
=SUM(LARGE(B4:B13,{1,2,3}))
Note you must enter this formula as an array formula if you use cell references and not an array constant for k inside LARGE.
When N becomes large
When N becomes large it becomes tedious to create the array constant by hand – If you want to sum to the top 20 or 30 values in a large list, typing out an array constant with 20 or 30 items will take a long time. In this case, you can use a shortcut for building the array constant that uses the ROW and INDIRECT functions.
For example, if you want to SUM the top 20 values in a range called “rng” you can write a formula like this:
=SUMPRODUCT(LARGE(rng,ROW(INDIRECT("1:20"))))
Variable N
With insufficient data, a fixed N can cause errors. In this case, you can try a formula like this:
=SUMPRODUCT(LARGE(rng,ROW(INDIRECT("1:"&MIN(3,COUNT(rng))))))
Here, we use MIN with COUNT to sum the top 3 values, or the count of values, if less than 3.
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.
Excel formula for Beginners – How to Count cells that contain odd numbers in Excel
Excel formula for Beginners – How to Count cells that contain negative numbers in Excel
Excel formula for Beginners – How to Count cells less than something in Excel