Sorting Sectors or Countries by Weights using Formulas
A portfolio of stocks is often grouped by industry sectors or countries. You can see how much of the portfolio is allocated to each sector or country in the table on the left below, which is sorted alphabetically. However, how do you sort this table on the left according to the portfolio weight using formulas?
First, you would sort the weights in ascending order. In the weight column, you would use the formula below:
Conceptual Formula:
=LARGE(RangeContainingWeights, RunningNumber)
where RangeContainingWeights is the range that contains the weights
RunningNumber is the numbers 1, 2, 3, 4, …
Example:
=LARGE($E$5:$E$15,1) gives you the largest number in the range
=LARGE($E$5:$E$15,2) gives you the 2nd largest number in the range, and so on.
Next, you would find the sector that matches the corresponding weight, using Index / Match.
Conceptual Formula:
=INDEX(RangeContainingSectors, MATCH(SortedWeight, RangeContainingWeights, 0))
where RangeContainingSectors is the range that contains the sectors
SortedWeight is the individual weights
Example:
=INDEX($D$5:$D$15,MATCH($I5,$E$5:$E$15,
Drag down the formulas and you will get the desired result:
Of course, you can do a Data -> Sort from the menu bar on a one-off basis, but using formulas has the advantage of working automatically, without you needing to do the sorting every time the data changes.
Caveats
The sharper ones of you would have noted that the formula above does not work in some cases. The first case is if any two or more of the portfolio weights have identical values. Some additional handling (e.g. a helper column which sorts by alphabetical order) is required, if that is the case.
Another case is if any of the range that contains the weights has error or non-numerical values. Some scrubbing of the data beforehand or some error handling in the formula would be needed in this case.
Happy Sorting!


