Using Spreadsheets to Solve Problems 1.9 - REFERENCE FUNCTIONS In a previous chapter it was shown how IF functions can be nested to decide between a set of possible outcomes. IF functions, however, are limited to seven levels of nesting, allowing for at most eight different outcomes. Furthermore, it quickly becomes apparent after two or three levels of nesting this method can be cumbersome and time consuming to implement. Excel provides another category of functions designed to look up values from a given list and return a corresponding value. In this section, we will use one of the simplest Reference functions, the LOOKUP function. THE LOOKUP FUNCTION THE LOOKUP FUNCTION SYNTAX The LOOKUP function is a function designed to look up values from a given list and return a corresponding value. The LOOKUP function takes a specified criterion (either a label or a value) and searches for a matching entry in a one-column or one-row range. When a match is found, it returns the cell contents (either a value or label) from the same position of a corresponding onecolumn or one-row range. The LOOKUP function has two syntax forms: vector and array. This chapter will discuss with the vector form of the LOOKUP function: LOOKUP(lookup_value,lookup_vector,result_vector) The arguments of the vector form of the LOOKUP Function syntax are as follows: Lookup_value is a value that the LOOKUP function searches for in the first vector. This value can be a number, text, logical value, or a name or reference that refers to a value. It can be thought of as the criteria you wish to match. Lookup_vector is a range of cells used to search for the lookup_value. This range must be one dimensional, containing either cells in a single row or a single column. The values in lookup_vector must be placed in ascending order. Result_vector is a range of cells that contain the values you wish to return. It too contains either one row or one column, and must also be the same size as lookup_vector. The results of a LOOKUP function can be one of the following: If the value you are looking for exactly matches a value in the lookup_vector then it will return the corresponding entry on the result_vector. If the lookup_value does not match an entry in the lookup_vector exactly, it is matched to the largest value in lookup_vector that is less than or equal to the lookup_value. If the lookup_value is smaller than the smallest value in lookup_vector, LOOKUP returns the #N/A error value. Reference Functions 1.9 CSE1111 Page 111 Using Spreadsheets to Solve Problems To illustrate how the LOOKUP function works, consider the following formula based on the worksheet in Figure 1: =LOOKUP(“Jane”, A2:A5, B2:B5). A B 1 Name Age 2 Alice 15 3 Jane 17 4 Mike 9 This formula will match Jane to the entries in the column range A2:A5. It 5 Sam 22 will return the corresponding value in the column range C2:C5. If Jane does Figure 1 not appear it will find the “closest” match alphabetically. In this case the formula finds Jane in the second row of the lookup_vector range, so it will return the second value in the result_vector range, which corresponds to the value 17. THE LOOKUP FUNCTION USING A HORIZONTAL ORIENTATION Here is an example of a lookup table in a horizontal format. In Figure 2 cells A1:E2 contain a tax schedule. Taxes are calculated on a progressive scale: incomes under $25,000 have no tax (0%), incomes of at least $25,000 but less than $50,000 are taxed at 10% of income, incomes of at least $50,000 but less than $100,000 are taxed at 15% of income, and incomes of $100,00 or more are taxed at the rate of 23%. In cell A5:C10 is a list of employees and their taxable income. In cell C6, write a formula to determine the income tax for the employee Jonas, such that it can be copied down the column. A 1 2 3 4 5 6 7 8 9 10 Income: Rate: Employee: Jonas Maria Zev Marta Alex B $ $ $ $ $ $ C Income Tax Rates: $ 25,000 $ 0% 10% Income 73,250 27,350 1,094 92,125 48,999 $ $ $ $ $ D 50,000 $ 15% E 100,000 23% Tax Due 10,987.50 2,735.00 13,818.75 4,899.90 Figure 2 For just Jonas the solution =B6*D3 can be used – Jonas’s income multiplied by the corresponding tax rate. However this approach will not work when the formula is copied down the column: income varies relatively (B6) but the tax rate (D3) neither stays the same nor changes relatively, as it is dependent on the income value. Another way to approach this would be to use a nested IF function: = B6*IF(B6<C2, B3, IF(B6<D2,C3,IF(B6<E2, D3, E3))) While this function works, it’s somewhat tedious to write and if more tax levels are added, the formula is difficult to maintain. Furthermore, if there are more than 8 different tax levels, this approach will no longer work. A more efficient method of solving this problem is to use a LOOKUP function: =B6*LOOKUP(B6,B$2:E$2,B$3:E$3) Reference Functions 1.9 CSE1111 Page 112 Using Spreadsheets to Solve Problems This formula will multiply the value $73,250, the income in cell B6, by corresponding tax rate. To find the corresponding tax rate the LOOKUP function will match the value in cell B6, the first argument of the function, to the values in the range B2:E2, as specified in the second argument of the function. Once a match is found, the corresponding value in the result vector (the third argument specified as B3:E3) will be returned. Note several features of this formula: The lookup value is a cell reference. This reference will copy relatively to match each successive employee’s income. The lookup and result vectors contain absolute row references so that when the formula is copied down these ranges will not be affected. The values found are NOT exact matches. i.e., $73,250 does not appear in the lookup vector range B2:E2. Rather the greatest value in the lookup vector range that does not exceed the lookup value is selected and the corresponding value in the result vector is returned. Both the lookup_vector and result_vector ranges do not include titles. These ranges start with the first value and end with the last value – no additional cells should be included in the range. How is the corresponding value found? The exact algorithm (step-by-step method) is not specified but one possible logical method might be as follows: The first category $0 is less than the lookup value $73,250 so the next value is considered. The second category $25,000 is again less than the lookup value $73,250 so again the next value is considered. The third category $50,000 is less than the lookup value $73,250 so the next value is considered. The fourth category $100,000 is more than the lookup value $73,250 so the previous value is matched – that of $50,000. $50,000 is the third value of the lookup vector, so the third value of the result vector, 15% in cell D3, is returned. Again this is not the “exact” method necessarily programmed into Excel. What is known is that the user must list the lookup values in ascending order or the function is not guaranteed to return the correct result. Reference Functions 1.9 CSE1111 Page 113 Using Spreadsheets to Solve Problems A MORE COMPLEX EXAMPLE: A B C 1 Price List: 2 Shipping $/lb. $ 0.50 3 4 Item No. $/each Weight (lbs) 5 124 $ 332 19 6 125 $ 55 5 7 126 $ 224 14 8 127 $ 483 16 9 128 $ 41 2 10 129 $ 83 20 11 130 $ 9 14 12 131 $ 268 2 13 132 $ 139 17 14 133 $ 52 11 15 134 $ 474 18 16 135 $ 294 20 17 136 $ 411 3 A 1 2 3 4 5 6 7 B C Customer Orders Customer Item# Qty Zael 134 3 Orwich 127 4 Wagma 104 2 PT 134 10 PT 128 1 D Total Price $ 1,449.00 $ 1,964.00 #N/A $ 4,830.00 $ 42.00 Orders! Figure 3 Pricing! A more complex example is given in Figure 3. This workbook example contains two worksheets: Sheet Pricing! contains a list of items numbers for various items you have for sale on E-Bay, their corresponding cost per unit, and weight in pounds (lbs) per unit. Also given on this worksheet is the shipping cost per pound. Worksheet Orders! contains a list of recent orders, including the item number being ordered and quantity. To complete the Orders! worksheet column D requires a formula to total the cost of this customer’s order, including the cost of all items and shipping. Step 1 - Understand the Problem. Mathematically the customer’s order total is Quantity * (Price/Item + Shipping Cost/Item). Since just the shipping weight in pounds (lbs) and cost per pound ($/lb) are given, this formula can be further expanded to the following: Quantity * (Price/Item + Weight/Item * $/lb to ship) Step 2 – Translate to Excel Syntax. If this formula is written directly, just substituting the appropriate values for Zael’s order, then the cost per unit and weight would not necessary copy down correctly. Both of these values are variable, but change by item number and not by relative position. Thus a LOOKUP will be required. Can a single LOOKUP function return both the cost per item and shipping weight? No, the LOOKUP function can return only one value at a time. So each variable, price/item, and weight/item, will require a separate LOOKUP. Reference Functions 1.9 CSE1111 Page 114 Using Spreadsheets to Solve Problems In pseudo code this formula will be: Quantity * (Lookup(item number, range of item numbers, range of prices) + Lookup(item number, range of item numbers, range of weights) * $/lb to ship). The cell references are as follows: o The quantity is on the same worksheet as the formula (Orders!) in cell C3. o The item number to be looked up is on the same worksheet as the formula in cell B3. o The range containing the item numbers to lookup is on sheet Pricing! in cells A5 through A17. o The range containing the corresponding prices is on sheet Pricing! in cells B5 through B17. o The range containing the corresponding weight is on sheet Pricing! in cells B5 through B17. o The cost per pound to ship is on sheet Pricing in cell C2. Applying these cell references the formula is: =C3*(LOOKUP(B3,Pricing!A5:A17,Pricing!B5:B17)+LOOKUP(B3,Pricing!A5:A17, Pricing!C5:C17)*Pricing!C2) Step 3 – Copying. In addition to knowing that a LOOKUP function is needed so that the formula can be copied, some of the arguments of the LOOKUP and the other operands must be considered. The Quantity and Item Number will change relatively as the formula is copied down the column so thus C3 and B3 are relative references. However, the lookup_vector and result vector ranges do not change when copied and therefore must contain an absolute row reference. Lastly, the $/lb for shipping in cell Pricing!C2 needs to be considered. Again this value does not change when the formula is copied and thus requires an absolute row reference. The final formula will be: =C3*(LOOKUP(B3,Pricing!A$5:A$17,Pricing!B$5:B$17)+ LOOKUP(B3,Pricing!A$5:A$17,Pricing!C$5:C$17)*Pricing!C$2) One point worth noting is the value that results in cell Orders!D5 is #N/A. This is an error message indicating that there is no corresponding answer. The reason for this is the lookup_value 104 is below the smallest value on the lookup_vector list (124). This error can be corrected either by changing the item number on the order to an existing item number from the list, or by adding item 104 to the top of the price list. A more serious error would be using an item number that is between the values in the lookup range or greater than those values. For example if the value 138 is used as an item number, Excel will return the greatest value on the list that does not exceed 138. Hence the pricing information for item 136 will be returned and not an error message. This is one of the drawbacks of the simple LOOKUP function. Other reference functions can be used to find only an exact match, but those will not be covered in this course. You may read about these functions using the Help feature in Excel. Reference Functions 1.9 CSE1111 Page 115 Using Spreadsheets to Solve Problems EXERCISE 1.9-1 REFERENCE FUNCTIONS: PRICING COPIES A 1 2 3 4 5 6 7 8 B The Lookup Table for Sliding-Scale Prices: Number of copies Price per copy 1 10 100 200 500 1000 Pricing! 0.07 0.06 0.05 0.04 0.03 0.02 9 10 11 Copies Made: Total Cost: 12 3 10 200 1001 13 14 15 16 A 1 B C D Delivery charges Delivery! 2 3 4 5 1. Write a formula in cell B13 that can be copied down the column to determine the total cost of making copies. The number of copies to be made is listed in Column. A. 2. The company also offers delivery. Their delivery charges are as follows: For orders under $5, there is a $5 delivery fee. Orders at least $5 but smaller than $20 there is a $7 delivery fee. Orders of $20 or more are delivered free of charge. Set up a reference table on the Delivery! worksheet so that you can write a reference function to calculate the delivery cost for each order. Organize the table in a horizontal format. 3. Write a formula that can be copied down to calculate Delivery Cost in cell C13. Use the reference table you created in question 2. Reference Functions 1.9 CSE1111 Page 116 Using Spreadsheets to Solve Problems EXERCISE 1.9-2 REFERENCE FUNCTIONS: GRADEBOOK Sheetname: Grades1! Sheetname: Grades2! A 1 2 A B C D E 700 A 550 B 400 C 300 D E 0 3 5 4 6 total pts 4 3 6 0 5 F 300 D 400 C A 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 550 B B last name Mouse Duck Bird Runner Cat Dog Coyote Mouse Pig Panther Doright Bunny Boop Duck first name Mickey Donald Tweety Road Felix Goofy Wiley Minnie Porky Pink Dudley Buggs Betty Daffy 700 A C B 1 2 7 8 D E F Midterm 1 midterm 2 final total 125 180 340 645 85 120 200 405 99 199 299 597 148 235 388 771 15 25 39 79 75 150 200 425 106 217 190 513 4 150 150 304 140 240 390 770 66 177 288 531 135 200 322 657 90 182 317 589 142 210 122 474 51 222 333 606 0 300 400 550 700 grade F D C B A G H % of final total grade 81% B 51% C 75% B 96% A 10% F 53% C 64% C 38% D 96% A 66% C 82% B 74% B 59% C 76% B 16 17 max possible points 150 250 400 800 Sheetname: scores! Use cell references whenever possible in your formulas: 1. Write a formula in cell scores!G2 that can be copied down the column to determine the percent of total possible points Mickey Mouse earned. 2. Write a formula in cell scores!H2 that can be copied down the column to determine Mr. Mouse’s final letter grade. Use a reference function and the grading scale on sheet Grades1!. 3. Write another formula in cell scores!H2 that can be copied down the column to determine Mr. Mouse’s final letter grade. Use a reference function and this time use the grade listing on sheet Grades2!. 4. How could you solve this problem without using a reference function? Reference Functions 1.9 CSE1111 Page 117 Using Spreadsheets to Solve Problems EXERCISE 1.9-3 REFERENCE FUNCTIONS: SALES ORDER PRICING A Disc! 1 2 3 4 5 6 7 8 9 B Company ABC Inc. DEF Inc. GHI Corp. LLL Ind. NYPD Inc. PQ Ltd. RFG Inc. XYZ Inc. C City Columbus Philadelphia Scranton Long Island Brooklyn Warren Columbus Cleveland D E Discount Discount A B State OH 5% 8% PA 5% 10% PA 7% 10% NY 7% 15% NY 5% 12% OH 5% 10% OH 7% 8% OH 6% 8% A D Total cost Customer Sales w/o Amount Ship Cost Discount 1 Name 3 1000 5% 4 100 10% 5 1 20% C E F Total Cost Shipping Cost with Discount >$25 2 ABC Inc. 345 $ 34.50 $ 379.50 $ 349.14 TRUE 3 DEF Inc. 92 $ 18.40 $ 110.40 $ 104.88 FALSE 4 ABC Inc. 2000 $ 100.00 $ 2,100.00 $ 1,932.00 TRUE 32 $ 6.40 $ 38.40 $ 36.48 FALSE 6 RFG Inc. 99 $ 19.80 $ 118.80 $ 110.48 FALSE 7 DEF Inc. 101 $ 10.10 $ 111.10 $ 105.55 FALSE $ 3,360.00 $ 3,091.20 TRUE 5 NYPD Inc. 8 ABC Inc. A B 1 Shipping Table #1 sale percent shipping 2 amount B 3200 $ 160.00 discount! sales! A 1 Shipping Table #2 2 sales amount 3 percent shipping ship1! B C D 1 100 1000 20% 10% 5% ship2! Above are some worksheets you have prepared to keep track of customer sales. The worksheet sales! lists each sale made by your company. It will be your job to calculate the shipping costs and factor in the appropriate discounted sales price for each sale. Shipping charges from $1 to $99.99 are 20% of the total sale, from $100 to $999.99 shipping charges are 10% of the total sale, and over $1000 are 5% of the total sale. Each company has negotiated different discount rates for discount types A and B as given on sheet discount!. Discount A is applied to sales under $200. Discount B is applied to sales of $200 and above. Discounts are calculated based total cost w/o discount (sales plus shipping) that you will calculate in column D. 1. If you were to use a reference function in cell sales!C2 to calculate the shipping cost of the $345 sale (cell sales!B2), would you want to use the data on sheet ship1! as the reference table or the data on sheet ship2! as the reference table? Why? 2. Using a LOOKUP function, write a formula in cell sales!C2 that can be copied down the column to calculate the shipping cost of the corresponding sale in that row. 3. What other method can be used to solve question #2? Is this method better or worse? Why? 4. Write a formula in cell sales!D2 that can be copied down the column to calculate the total cost before discounts (list price plus shipping). 5. Write a formula in cell sales!E2 that can be copied down the column to determine the discounted price of the order. Remember that discounts are based on a company’s negotiated discount rate and the total cost of the order (discount A for <$200 or discount B for >=$200). 6. Your competitor charges a $25 flat shipping rate per order. Write a formula (True/False) in cell sales!F2 to determine if your customer paid more than $25 for his/her shipping. Reference Functions 1.9 CSE1111 Page 118 Using Spreadsheets to Solve Problems EXERCISE 1.9-4 LOOKUP FUNCTIONS: – CHAPTER REVIEW – The three spreadsheets on the following page are all worksheets in a single workbook. The worksheet named Prod! lists product information (shipping time, cost, and shipping cost) by product number. The worksheet named Ship! is a table of shipping costs for the corresponding product costs. The worksheet named Order! is a list of customer orders (one item type per order) and the order quantity. 1. Write a formula in cell Prod!D3 using a reference function to calculate the shipping cost for product 0001. Write the formula so that it can be copied down the column. 2. Write a formula in cell Order!D3 to calculate the total cost of this order (product costs and shipping costs). Write the formula so that it can be copied down the column. 3. Write a formula in cell order!G3 to determine (true/false) if the shipment was late. A shipment is late if it took more that the required number of shipping days to be received. Write the formula so that it can be copied down the column. 4. Is it possible to write a LOOKUP function in cell Order!G20 to determine the customer name of the customer with the lowest value order? Why or why not? 5. Write a formula in cell Prod!D3 using a nested if function to calculate the shipping cost for product 0001. Write the formula so that it can be copied down the column. A 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 B Product Information Shipping Product Days ID Required 0001 0010 0011 0012 0013 0023 0032 0034 0043 0045 0067 0078 0089 C 1 2 7 4 3 7 3 3 4 5 1 6 7 D A E Shipping Product Cost per Costs item $ 25 D3 $ 15 $ 97 $ 65 $ 89 $ 85 $ 76 $ 48 $ 58 $ 32 $ 64 $ 64 $ 189 1 Product 2 Customer Number 3 Fraiser 0045 4 Johnson 0067 5 Jordon 0001 6 Chamberlain 0012 7 Reed 0023 8 Monroe 0034 9 Bird 0089 10 Barry 0032 11 Lucas 0043 12 Bradley 0010 13 DeBusher 0011 14 Rodman 0078 15 Malone 0013 Product ID 0001 0010 0011 0012 0013 0023 0032 0034 0043 0045 0067 0078 0089 Prod! A 1 Product Cost 3 Shipping Cost 2 B C D E F B Total Order QTY Cost 1 D3 1 1 1 2 3 2 3 1 1 4 1 1 H C Shipping Costs 0 $ 50 $ $ 3 $ 5 $ D Receipt Ship Date Date Late? 2/8/98 2/12/98 G3 2/8/98 2/16/98 2/8/98 2/9/98 2/8/98 2/15/98 2/8/98 2/9/98 2/8/98 2/11/98 2/8/98 3/1/98 2/8/98 2/14/98 2/8/98 2/13/98 2/8/98 2/15/98 2/8/98 2/10/98 2/8/98 2/9/98 2/8/98 2/21/98 E 75 $ 7 $ Customer Fraiser Johnson Jordon Chamberlain Reed Monroe Bird Barry Lucas Bradley DeBusher Rodman Malone Order! 100 9 Ship! Reference Functions 1.9 G Customer Orders Page 119