Transcript
Page 1: Excel > Excel Stuff > Excel Tips and Tricks > Array Formula Examples

This workbook demonstrates numerous examples of one of the most powerful, but underused, features of Excel: array formulas.

An array formula works on whole ranges of the worksheet at once. Suppose that we have two ranges of numbers:

1 52 63 74 8

Now, suppose that as an input to a formula, we need to know the product of the corresponding values in each array (e.g., 1*5, 2*6, etc). We can create an array of these products with A1:A4*B1:B4 (assuming the original values are in those ranges). The resulting array would be {5,12,21,32}. The example to the right shows how this works.

Suppose that we need to know the results of dividing each number in K4:K13 by L4:L13. We could create in M4 the formula: =K4/L4 and then copy this down. That would give us the result that we need. Alternatively, we could create an array. First select M4:M13. Now, type in the formula: =K4:K13/L4:L13 and then hold down Ctrl and Shift while pressing the Enter key. If you look at one of the cells in the array, you will see that the formula is: {=K4:K13/L4:L13}. Notice that Excel has added the curly brackets indicating that this is an array formula. If you were to just press the Enter key (neglecting to hold down Ctrl and Shift) you would get only the value in M4.

So, what's the advantage? Well, in this case nothing really. However, as we'll see, array formulas are much more efficient than regular formulas in two ways:1) They save space in the worksheet. Using array formulas we can eliminate columns showing intermediate results. This also makes the worksheet easier to read because it isn't cluttered up by unnecessary information.2) They calculate more quickly than the equivalent series of individual formulas.

Follow along with the examples, and your spreadsheet skills will improve markedly.

Page 2: Excel > Excel Stuff > Excel Tips and Tricks > Array Formula Examples

Number Divisor10 10 120 10 230 10 340 10 450 10 560 10 670 10 780 10 890 10 9

100 10 10

Resulting Array

This workbook demonstrates numerous examples of one of the most powerful, but underused, features of Excel: array formulas.

An array formula works on whole ranges of the worksheet at once. Suppose that we have two ranges of numbers:

1 52 63 74 8

Now, suppose that as an input to a formula, we need to know the product of the corresponding values in each array (e.g., 1*5, 2*6, etc). We can create an array of these products with A1:A4*B1:B4 (assuming the original values are in those ranges). The resulting array would be {5,12,21,32}. The example to the right shows how this works.

Suppose that we need to know the results of dividing each number in K4:K13 by L4:L13. We could create in M4 the formula: =K4/L4 and then copy this down. That would give us the result that we need. Alternatively, we could create an array. First select M4:M13. Now, type in the formula: =K4:K13/L4:L13 and then hold down Ctrl and Shift while pressing the Enter key. If you look at one of the cells in the array, you will see that the formula is: {=K4:K13/L4:L13}. Notice that Excel has added the curly brackets indicating that this is an array formula. If you were to just press the Enter key (neglecting to hold down Ctrl and Shift) you would get only the value in M4.

So, what's the advantage? Well, in this case nothing really. However, as we'll see, array formulas are much more efficient than regular formulas in two ways:1) They save space in the worksheet. Using array formulas we can eliminate columns showing intermediate results. This also makes the worksheet easier to read because it isn't cluttered up by unnecessary information.2) They calculate more quickly than the equivalent series of individual formulas.

Follow along with the examples, and your spreadsheet skills will improve markedly.

Page 3: Excel > Excel Stuff > Excel Tips and Tricks > Array Formula Examples

Weight Score0.1 950.2 850.4 750.2 65

0.1 55Weighted Average 75Variance 120Standard Deviation 10.95

What this spreadsheet does:

This sheet demonstrates how to calculate a weighted average, variance, and standard deviation using array formulas. Note that I could have used the SumProduct function to calculate the weighted average, but Excel has no function to calculate standard deviation when the weights are unequal.

Had I used normal formulas, I would have had to add a column or two to do intermediate calculations. The array functions are much more efficient in terms of speed of calculation and in the amount of spreadsheet space they consume.

How the array functions work:

Weighted Average:Array functions work on whole ranges at a time, rather than individual cells. The weighted average is the most simple. The formula is =SUM(A2:A6*B2:B6). First, the formula says "take each value in A2:A6 and multiply it by the corresponding value in B2:B6. This creates an array (or vector), and the Sum function simply calculates the total of all of the elements of the array.

Variance:This is a bit more complicated, but the logic is similar. The formula is =SUM((B2:B6-AVERAGE(B2:B6))^2*A2:A6). To calculate the variance, we subtract the average of all possible outcomes from each possible outcome, square the difference, multiply by the probablility of occurance, and finally sum the results. Let's break this formula down to its parts in order of calculation:

The first part takes each value in B2:B6 and subtracts the average of these values. The result is an array of deviations from the mean. Next, each value in this array is squared resulting in an array of squared deviations from the mean. Now, we calculate a weighted average of these squared deviations by multiplying each squared deviation by its probability of occurance and summing the results.

Standard Deviation:The standard deviation is calculated by finding the square root of the variance. The easiest way to do this would be to use =sqrt(B8), but we don't have to use a cell for the variance calculation. Normally, we only care about the standard deviation, so we would use the formula: =SQRT(SUM((B2:B6-AVERAGE(B2:B6))^2*A2:A6)). This is the same formula as before, but we have added the SQRT() function around the whole thing.

Page 4: Excel > Excel Stuff > Excel Tips and Tricks > Array Formula Examples

What this spreadsheet does:

This sheet demonstrates how to calculate a weighted average, variance, and standard deviation using array formulas. Note that I could have used the SumProduct function to calculate the weighted average, but Excel has no function to calculate standard deviation when the weights are unequal.

Had I used normal formulas, I would have had to add a column or two to do intermediate calculations. The array functions are much more efficient in terms of speed of calculation and in the amount of spreadsheet space they consume.

How the array functions work:

Weighted Average:Array functions work on whole ranges at a time, rather than individual cells. The weighted average is the most simple. The formula is =SUM(A2:A6*B2:B6). First, the formula says "take each value in A2:A6 and multiply it by the corresponding value in B2:B6. This creates an array (or vector), and the Sum function simply calculates the total of all of the elements of the array.

Variance:This is a bit more complicated, but the logic is similar. The formula is =SUM((B2:B6-AVERAGE(B2:B6))^2*A2:A6). To calculate the variance, we subtract the average of all possible outcomes from each possible outcome, square the difference, multiply by the probablility of occurance, and finally sum the results. Let's break this formula down to its parts in order of calculation:

The first part takes each value in B2:B6 and subtracts the average of these values. The result is an array of deviations from the mean. Next, each value in this array is squared resulting in an array of squared deviations from the mean. Now, we calculate a weighted average of these squared deviations by multiplying each squared deviation by its probability of occurance and summing the results.

Standard Deviation:The standard deviation is calculated by finding the square root of the variance. The easiest way to do this would be to use =sqrt(B8), but we don't have to use a cell for the variance calculation. Normally, we only care about the standard deviation, so we would use the formula: =SQRT(SUM((B2:B6-AVERAGE(B2:B6))^2*A2:A6)). This is the same formula as before, but we have added the SQRT() function around the whole thing.

Page 5: Excel > Excel Stuff > Excel Tips and Tricks > Array Formula Examples

Product Month Unit SalesA January 25B January 50C January 17A February 20B February 42C February 26A March 19B March 48C March 21A April 16B April 35C April 19A May 24B May 52C May 23A June 21B June 47

C June 22

Product Total UnitsA 125B 274

C 128

Month Total UnitsJanuary 92February 88March 88April 70May 99

June 90

What this spreadsheet does:We have a list of products and their unit sales by month. We wish to determine how many of each product were sold over the entire six-month period. Additionally, we wish to know how many units were sold during each of the six months.

How the array functions work:To calculate the total sales of Product A, the formula is: =SUM(($A$2:$A$19=A22)*($C$2:$C$19)). This is less complicated if we break it down into parts:

The first part, ($A$2:$A$19=A22), creates an array of True/False (boolean) values. Keep in mind that True = 1 and False = 0, so we have an array of 0's and 1's. Excel looks at each cell in A2:A19 and determines if the value is the same as that in A22, in this case "A". If it is, it puts a 1 in the array, if not it puts in a 0. So, we get an array that looks like this: {1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0}.

Next, we multiply this array by the array of unit sales in C2:C19. Obviously, multiplying anything by 0 returns a 0, so the new array will be populated by sales for January only. It will look like this: {25,0,0,20,0,0,19,0,0,16,0,0,24,0,0,21,0,0}.

Finally, the Sum function simply calculates the total of each of the values in this array. For Product A it is 125 units. Each of the other two array formulas work in exacly the same way, except that the product name is different.

Calculating monthly sales works in exactly the same way, except that the criteria is the name of the month rather than the name of the product.

Note that we could sort the list in any way (by product, by month, or by units) and our formulas will still work perfectly.

Page 6: Excel > Excel Stuff > Excel Tips and Tricks > Array Formula Examples

What this spreadsheet does:We have a list of products and their unit sales by month. We wish to determine how many of each product were sold over the entire six-month period. Additionally, we wish to know how many units were sold during each of the six months.

How the array functions work:To calculate the total sales of Product A, the formula is: =SUM(($A$2:$A$19=A22)*($C$2:$C$19)). This is less complicated if we break it down into parts:

The first part, ($A$2:$A$19=A22), creates an array of True/False (boolean) values. Keep in mind that True = 1 and False = 0, so we have an array of 0's and 1's. Excel looks at each cell in A2:A19 and determines if the value is the same as that in A22, in this case "A". If it is, it puts a 1 in the array, if not it puts in a 0. So, we get an array that looks like this: {1,0,0,1,0,0,1,0,0,1,0,0,1,0,0,1,0,0}.

Next, we multiply this array by the array of unit sales in C2:C19. Obviously, multiplying anything by 0 returns a 0, so the new array will be populated by sales for January only. It will look like this: {25,0,0,20,0,0,19,0,0,16,0,0,24,0,0,21,0,0}.

Finally, the Sum function simply calculates the total of each of the values in this array. For Product A it is 125 units. Each of the other two array formulas work in exacly the same way, except that the product name is different.

Calculating monthly sales works in exactly the same way, except that the criteria is the name of the month rather than the name of the product.

Note that we could sort the list in any way (by product, by month, or by units) and our formulas will still work perfectly.

Page 7: Excel > Excel Stuff > Excel Tips and Tricks > Array Formula Examples

Year Sales1990 12051991 12751992 13461993 13951994 15001995 14521996 15201997 15921998 16751999 17342000 1793

2001 1846

Using Built-in Functions and ArraysArithmetic Average Growth Rate 3.99%Compound Average Growth Rate 3.95%Standard Deviation of Growth Rate 2.73%

Alternative Array FormulasArithmetic Average Growth Rate 3.99%Compound Average Growth Rate 3.95%Standard Deviation of Growth Rate 2.73%

What this spreadsheet does:We have a listing of sales year by year over the past 12 years and wish to know what the compound average annual rate of sales growth has been over this period. It also shows how to calculate the standard deviation of sales growth.

Note that we need to deal with growth rates, not dollar growth of sales. For analysis and forecasting purposes, the growth rates are much closer to the ideals of stationarity in the mean and variance than are the dollar amounts. Also, we really should use the geometric (compound) rate of growth, not the arithmetic (typical) rate of growth.

How the array functions work:There's really nothing too complicated (or new) in these functions. In the first set of formulas, we use a built-in function on the array of growth rates. To calculate the array of growth rates, we use the formula: B3:B13/B2:B12-1. This takes each cell in B3:B13 and divides it by the corresponding value in B2:B12. We then subtract 1 to get the percentage change.

The alternative array formulas are a little more complicated, but the idea is the same. There's no advantage to using them, I'm merely showing how it could be done. At this point, you should be able to figure them out.

Page 8: Excel > Excel Stuff > Excel Tips and Tricks > Array Formula Examples

What this spreadsheet does:We have a listing of sales year by year over the past 12 years and wish to know what the compound average annual rate of sales growth has been over this period. It also shows how to calculate the standard deviation of sales growth.

Note that we need to deal with growth rates, not dollar growth of sales. For analysis and forecasting purposes, the growth rates are much closer to the ideals of stationarity in the mean and variance than are the dollar amounts. Also, we really should use the geometric (compound) rate of growth, not the arithmetic (typical) rate of growth.

How the array functions work:There's really nothing too complicated (or new) in these functions. In the first set of formulas, we use a built-in function on the array of growth rates. To calculate the array of growth rates, we use the formula: B3:B13/B2:B12-1. This takes each cell in B3:B13 and divides it by the corresponding value in B2:B12. We then subtract 1 to get the percentage change.

The alternative array formulas are a little more complicated, but the idea is the same. There's no advantage to using them, I'm merely showing how it could be done. At this point, you should be able to figure them out.

Page 9: Excel > Excel Stuff > Excel Tips and Tricks > Array Formula Examples

DATE S&P 500 IBM GM

Dec-96 740.74 37.88 55.75 S&P 500 IBMJan-97 786.16 39.22 59.00 Total Return 55.19% 227.10%Feb-97 790.82 35.94 57.88 Monthly Return 0.74% 1.99%Mar-97 757.12 34.31 55.38 Standard Deviation 5.16% 10.36%Apr-97 801.34 40.13 57.88 Coef. of Variation 7.02 5.19

May-97 848.28 43.25 57.38 Beta 1.00 1.24 Jun-97 885.14 45.13 55.75Jul-97 954.31 52.88 61.88

Aug-97 899.47 50.69 62.75Sep-97 947.28 53.00 66.94Oct-97 914.62 49.25 64.19

Nov-97 955.40 54.75 60.94Dec-97 970.43 52.31 60.75Jan-98 980.28 49.38 57.94Feb-98 1049.34 52.22 68.94Mar-98 1101.75 51.94 67.75Apr-98 1111.75 57.94 67.38

May-98 1090.82 58.75 71.88Jun-98 1133.84 57.41 66.81Jul-98 1120.67 66.25 72.31

Aug-98 957.28 56.31 58.13Sep-98 1017.01 64.25 54.88Oct-98 1098.67 74.25 63.19

Nov-98 1163.63 82.56 69.88Dec-98 1229.23 92.19 71.56Jan-99 1279.64 91.63 89.75Feb-99 1238.33 84.88 82.56Mar-99 1286.37 88.63 87.00Apr-99 1335.18 104.59 89.06

May-99 1301.84 116.00 69.00Jun-99 1372.71 129.25 66.00Jul-99 1328.72 125.69 61.13

Aug-99 1320.41 124.56 66.25Sep-99 1282.71 121.00 62.94Oct-99 1362.93 98.25 70.44

Nov-99 1388.91 103.06 73.80Dec-99 1469.25 107.88 72.69Jan-00 1394.46 112.25 80.56Feb-00 1366.42 102.75 76.06Mar-00 1498.58 118.38 82.81Apr-00 1452.43 111.50 93.63

May-00 1420.60 107.31 70.63Jun-00 1454.60 109.56 58.06Jul-00 1430.83 112.25 56.94

Aug-00 1517.68 132.02 70.00

What this spreadsheet does:We wish to compare two stocks (IBM and GM) to each other and a broad market index (S&P 500). The comparison consists of total return over the five-year period, compound average monthly return, standard deviation of monthly returns, coefficient of variation of monthly returns, and betas.

This worksheet demonstrates the efficiency of array functions. If we used non-array functions, we would have to add three columns to calculate the monthly returns. This would not only take more space in the worksheet, but it would require an additional 180 calculations and would needlessly clutter the worksheet.

There are no new techniques here, the calculations all use the same technique as shown in the "Sales Growth" worksheet.

Page 10: Excel > Excel Stuff > Excel Tips and Tricks > Array Formula Examples

Sep-00 1436.51 112.63 65.00Oct-00 1429.40 98.50 62.13

Nov-00 1314.95 93.50 49.50Dec-00 1320.28 85.00 50.94Jan-01 1366.01 112.00 53.70Feb-01 1239.94 99.90 53.32Mar-01 1160.33 96.18 51.85Apr-01 1249.46 115.14 54.81

May-01 1255.82 111.80 56.90Jun-01 1224.38 113.50 64.35Jul-01 1211.23 105.21 63.60

Aug-01 1133.58 99.95 54.75Sep-01 1040.94 91.72 42.90Oct-01 1059.78 108.07 41.32

Nov-01 1139.45 115.59 49.70Dec-01 1149.56 123.89 48.10

Historical stock prices provided by CSI, Inc. Historical mutual fund and industry prices provided by Media General Financial Services.

Page 11: Excel > Excel Stuff > Excel Tips and Tricks > Array Formula Examples

GM-13.72%

-0.25%10.80%

(43.96)

1.14

What this spreadsheet does:We wish to compare two stocks (IBM and GM) to each other and a broad market index (S&P 500). The comparison consists of total return over the five-year period, compound average monthly return, standard deviation of monthly returns, coefficient of variation of monthly returns, and betas.

This worksheet demonstrates the efficiency of array functions. If we used non-array functions, we would have to add three columns to calculate the monthly returns. This would not only take more space in the worksheet, but it would require an additional 180 calculations and would needlessly clutter the worksheet.

There are no new techniques here, the calculations all use the same technique as shown in the "Sales Growth" worksheet.

Page 12: Excel > Excel Stuff > Excel Tips and Tricks > Array Formula Examples

Historical stock prices provided by CSI, Inc. Historical mutual fund and industry prices provided by Media General Financial Services.


Recommended