Use the DSUM and DAVERAGE functions to total and average values,
respectively, that meet a specified condition;. Use the Function Arguments dialog
box to ...
Lesson 7 - Database Functions
Lesson 7
Da ta ba s e F unctions Les s on Topics The DSUM Function The DAVERAGE Function Exercise: Calculating the Total Earnings Exercise: Rate Summary
Les s on Objectives At the end of the lesson, you will be able to: Define a database function; Use the DSUM and DAVERAGE functions to total and average values, respectively, that meet a specified condition; Use the Function Arguments dialog box to enter the conditions; Use multiple conditions with Database functions.
Student Files Us ed You will use the following file from your student folder: Billable Hours
Copyright © 1985-2007, Finney Learning Systems, Inc. All rights reserved.
67
68
The DSUM Function
Microsoft Excel 2003 - Advanced
Database functions let you “sift” through large amounts of data quickly and easily. They work similarly to standard functions. The only major difference is that they impose conditions. For example, you know that the SUM function adds values in a range of cells. The DSUM function (the “D” is for “Database”) does the same thing, but it might perform this function only if the values are in a specified range or greater than a given amount. As you know from previous lessons, conditions such as these are called criteria. You are going to work with two database functions — DSUM and DAVERAGE. 1.
Open Billable Hours. This is a table for a consulting firm showing the hours worked for three clients. In addition to the client names, there are columns for the dates worked, and the number of hours, rate of pay, and total compensation for each date. A formula in the Total column multiplies the number of hours by the hourly rate.
2.
Click in E2 and notice the formula on the Formula bar.
3.
You are going to use the DSUM function to enter the total hours for each client. Ordinarily, this would take several formulas to arrive at such a result, especially since the companies are not in order. You are going to create a second sheet in the workbook to display this data. Double-click the Sheet2 tab and name it Summary of Hours.
4.
Change the Column Standard Width to 12.
5.
Starting in A1, type the following in the new sheet. The information in D2:F3 is merely the criteria you use to calculate the total hours for each company.
6.
You are going to enter a DSUM function in B2.
Lesson 7 - Database Functions
69
Click in B2. 7.
You are going to use the Insert Function dialog box to enter the DSUM function and its arguments. Click the Insert Function button on the Formula bar. The Insert Function dialog box appears.
8.
In the Or select a category box, click the down arrow and choose Database. In the Select a function box, notice that all the functions begin with “D”, for “Database.”
9.
Scroll down and choose DSUM. Beneath the list box, notice the arguments for this function. They are the same for all the database
functions: database, field, criteria. The database is the location of the data being used. In this case it is Sheet1, A1:E7. The field is the location where the calculations will occur. In this case, it is the Hours column, and the criteria refers to the conditions that are imposed. In this case the criteria will be the data for Company A only. 10.
Click OK.
70
Microsoft Excel 2003 - Advanced
The Function Arguments dialog box appears. Notice on the spreadsheet behind it, that =DSUM() has been entered in B2 and, of course, it is displayed on the Formula bar. Within the parentheses of this incomplete formula will be the arguments — database, field, criteria. Arguments are the inputs that the function uses to calculate its results.
Notice that the names of the three arguments are listed in a form. Also notice the information below the form, explaining the purpose of this dialog box and a definition of the first item, Database. 11.
Click in the Database text box.
12.
You could type the location of the database manually, but it is easier to point. Click the Company Hours sheet tab.
13.
When indicating the database, the header row has to be included.
14.
Drag through A1:E7, which is the database you will be using. When you drag, the dialog box collapses momentarily. Notice the following in the Function Arguments dialog box: ‘Company Hours’!A1:E7. The apostrophes are needed because there is a space between Company and Hours. The exclamation point is used to separate the two parts of the address.
15.
Click in the Field text box.
16.
Once again, you are going to enter the information by pointing. Click the Company Hours sheet tab.
Lesson 7 - Database Functions
17.
71
Click C1 to indicate the Hours column. Notice that C1 has been added to the formula both in the dialog box and on the Formula bar. Notice =”Hours” to the right of the text box. Quotation marks are needed to indicate that this is text.
18.
The Criteria field is different than the two fields you have just typed. For Database and Field, you indicated existing data in the database. For the Criteria field, you must provide at least one pair of cells: one to indicate a field in the database, and another to indicate its value. The Criteria you specify act as a filter: the database function, DSUM, will be applied only to rows for which the Criteria are true. Click in the Criteria text box. In the dialog box, notice the description of the Criteria field: Criteria is the range of cells that contains the conditions you specify. The range includes a column label and one cell below the label for the condition. In other words, Excel is looking for two cells, one above the other: the "column label" is the name of any column in the database (such as "Hours"), and the "condition" cell below it can contain any logical expression. These will be explained shortly.
19.
If necessary, drag the dialog box out of the way and select D2:D3. This tells Excel that the criteria is Company A in the Client column. Excel will find every row for which the Client is Company A and add it to the DSUM. Notice =”Client” at the right. Also notice the number of hours, 114, displayed in two places on the dialog box.
20.
Click OK. The formula is complete and the result, 114, is in B2. On the Formula bar, notice the complete formula. The three parts are separated by commas.
21.
You are going to do the same thing for Company B. Go to B3 and repeat steps 7-20. For step 19, drag through E2:E3 rather than D2:D3.
22.
When finished, go to B4 and do the same thing for Company C. In step 19, drag though F2:F3.
72
Microsoft Excel 2003 - Advanced
Notice the total hours for all three clients. By using the DSUM function, you have quickly created three separate sums from one table, without sorting or rearranging the data.
The DAVERAGE Function
You are going to use the DAVERAGE function to average the hours per days worked for Company A. For example if Company A received work for three days, the formula will show the average number of hours for those three days. The number of employees that were working for the client each day is not a concern of this spreadsheet. 1.
Billable Hours should still be open.
2.
Go to the Summary of Hours sheet and insert the following, beginning in A6.
Cell B6 is where the DAVERAGE function will go. Cells D6:F7 are the criteria that you will use in the Function Arguments dialog box. Notice that the Date criteria have logical operators (e.g., =) before them, whereas the Client criterion (Company A) does not. If a criteria cell contains no logical operator, Excel assumes you mean "equal to" (=). 3.
You are going to insert a DAVERAGE function in B6. Go to B6.
4.
Click the Insert Function button on the Formula bar. The Insert Function dialog box appears.
5.
Verify that DAVERAGE is chosen in the Select a function text box and click OK. The Function Arguments dialog box appears.
6.
For the Database field, click the Company Hours tab and select A1:E7.
7.
Tap the TAB key to go to the Field text box.
Lesson 7 - Database Functions
8.
73
This time, for practice, you are going to type directly into the text box rather than use the pointing method. In the Field text box, type “Hours” (be sure to include the quotation marks) and tap the TAB key to go to the Criteria field. This is where you are going to use the criteria you have typed in the spreadsheet. As before, it will consist of a column title and a criterion below it.
9.
If necessary, move the dialog box aside and select D6:D7. This will tell Excel to look for hours that pertain only to “clients” that are “Company A”. Notice that the result, 38, already appears on the dialog box.
10.
Click OK. The result, 38, appears in B6.
You can specify more than one criterion. You are going to show the hours for Company A on or before 3/31/07. 1.
With B6 the active cell, click the Insert Function button on the Formula bar. You are taken directly to the Function Arguments dialog box. The Insert Function command takes you there when the active cell contains a function.
2.
The information in the Database and Field text boxes will remain the same, but you have to change the criteria. Delete the contents of the Criteria box.
3.
In the worksheet, select the range D6:E7. (Move the dialog box, if necessary.) You have specified two criteria.
4.
Click OK. Notice the new result: 40. Because some of the data was in April, those rows were filtered out by the DAVERAGE function when you included the Date criterion which stated that you wanted results only on or before 3/31/07.
74
Microsoft Excel 2003 - Advanced
You can also have more than one criterion for a particular column. For example, you are going to get the Company A average only for the month of March. To do this, you need another Date criterion, which you have already typed in F6:F7. 1.
With B6 the active cell, click the Insert Function button on the Formula bar.
2.
Once again, delete the contents of the Criteria field.
3.
In the worksheet, select the range D6:F7. You have selected three criteria. The first will return only the hours for Company A, the second for the days on or before 3/31/07, and the third for the days on or after 3/1/07.
4.
Click OK. Notice the new result, 76. It is higher for two reasons: this is an average and you have restricted the hours to only the month of March.
Exercise: Calculating the Total Earnings
You are going to use a DSUM function to calculate the total amount earned by a company. 1.
First, you are going to type headers to indicate what information you are entering. In A9, type: Client
2.
In B9, type: Total Earnings
3.
Select A9:B9 and click the Bold button on the Formatting toolbar.
4.
Instead of entering the criteria to the right, this time you are going to enter the criteria on the left of the DSUM. In A10, type: Company A
5.
In B10, type: =DSUM(
6.
Click the Company Hours sheet tab and select A1:E7.
7.
Type a comma.
8.
The Company Hours sheet tab should still be active.
Lesson 7 - Database Functions
75
Click in E1 to indicate that the Total column will contain the data to add. 9. 10.
Type a comma. Finally, indicate the criteria fields. These are on the Summary of Hours sheet. Click the Summary of Hours sheet tab.
11.
Drag through A9:A10 to indicate these as the criteria cells.
12.
Type: )
13.
Tap the ENTER key to complete the formula. Notice the total amount, 4470.
14.
Exercise: Rate Summary
Change the contents of A10 to Company B and to Company C and watch the DSUM value change.
You are going to use the DSUM function to calculate the number of hours worked for each billing rate. 1.
Billable Hours should still be open.
2.
Make Summary of Hours the active sheet, if necessary.
3.
In A13, type: Rate
4.
In B13, type: Hours
5.
Select A13:B13 and click the Bold button on the Formatting toolbar.
6.
Copy the contents of A13 to D13:G13. Rate should appear five times in row 13.
7.
In A14:A17, type the four rates that appear in the Rate column of the database: 25, 40, 45, 50
8.
In D14:G14, type the same rates: 25, 40, 45, 50
9.
In B14, type: =DSUM(
10.
Click the Company Hours sheet tab and select A1:E7 to select the database.
11.
Type a comma and click in C1 (the Hours column header).
12.
Type another comma.
76
Microsoft Excel 2003 - Advanced
13.
Click the Summary of Hours sheet tab and select D13:D14.
14.
Type a right parenthesis and tap the ENTER key. The total hours worked at the hourly rate of 25 appears in B14.
15.
To illustrate a point, you are going to do something incorrectly. Make B14 the active cell, and use the Fill Handle to copy the formula down to B17.
16.
If you click in the cells and observe the formulas, you will see that they have been copied incorrectly. This is because none of the ranges were entered with absolute cell references. You are going to correct these references and copy the formula again. Delete B15:B17.
17.
Click in B14.
18.
The first two arguments of the DSUM function refer to the database on the Company Hours sheet. These references will not change, because you will always be referring to the same database, and to the same field within the database. The third argument will change, however. Therefore, you are going to change the first two parameters to absolute cell references. On the Formula Bar, highlight the following part of the formula in B14: A1:E7,'Company Hours'!C1 The three cell names will be converted into absolute references.
19.
Tap the ABSOLUTE REFERENCE key (F4). Notice the dollar signs that were added to the cell references.
20.
Use CTRL/ENTER. The result of the formula remains the same.
21.
Use the Fill Handle to copy the formula in B14 down to B15:B17.
22.
Now, all that is left is to adjust the Criteria references in B15:B17.
Lesson 7 - Database Functions
77
Click in B15. 23.
Click anywhere in the formula on the Formula bar. In the worksheet, notice the highlighted range for the Criteria range.
24.
You can change this reference in two ways. Either one is equally valid, so choose the one you find easiest: !
On the Formula bar, delete 'Summary of Hours'!D14:D15 and replace it with E13:E14.
!
Point to any border of the blue outline around D14:D15 (the pointer will turn into a four-headed arrow) and drag the blue outline until it surrounds E13:E14.
25.
Whichever way you chose to correct the reference, you should now see a blue outline around E13:E14.
26.
Tap the ENTER key. The formula should display the correct result: 15.
27.
Repeat steps 22 - 26 to change the references in B16 and B17 to F13:F14 and G13:G14, respectively. Your worksheet should look like the following:
End of Lesson 7