Vous êtes sur la page 1sur 5

Chapter 9: Tables and Worksheet Databases 265

h A home that has been listed for no more than two months, with a list price greater than
$300,000
h A condominium with square footage between 1,000 and 1,500
h A single-family home listed in the month of March

To join criteria with an AND operator, use multiple columns in the criteria range. Figure 9-20
shows a criteria range that selects records with a list price of less than $250,000 and square
footage of at least 2,000.

Figure 9-20: This criteria range uses multiple columns that select records using a logical AND operation.

Figure 9-21 shows another example. This criteria range displays listings from the month of March.
Notice that the field name (Date Listed) appears twice in the criteria range. The criteria selects
the records in which the Date Listed date is greater than or equal to March 1, and the Date Listed
date is less than or equal to March 31.

Figure 9-21: This criteria range selects records that describe properties that were listed in the month of
March.

The date selection criteria may not work properly for systems that don’t use the U.S.
date formats. To ensure compatibility with different date systems, use the DATE func-
tion to define such criteria, as in the following formulas:

=”>=”&DATE(2009,3,1)
=”<=”&DATE(2009,3,31)

14_475362-ch09.indd 265 4/14/10 10:09 PM


266 Part II: Using Functions in Your Formulas

To join criteria with a logical OR operator, use more than one row in the criteria range. A criteria
range can have any number of rows, each of which joins with the others via an OR operator.
Figure 9-22 shows a criteria range (A1:C3) with two rows of criteria.

Figure 9-22: This criteria range has two sets of criteria, each of which is in a separate row.

In this example, the filtered table shows the rows that meet either of the following conditions:

h A condo with a square footage of at least 1,800


or
h A single-family home priced less than $250,000

This is an example of the type of filtering that you cannot perform by using standard
(non-advanced) filtering.

You can repeat a value on multiple rows to include the same criteria in two or more AND criteria.
Suppose you want a condo in the Central area, but you would be willing to consider a condo in
another area as long as it has a pool and at least three bedrooms. Figure 9-23 shows how you
use the OR operator between the Area, and the Pool and Bedrooms criteria, but still limit your
search to only one Type.

Figure 9-23: Repeating values in the criteria range applies the OR operator to only those criteria that aren’t
repeated.

14_475362-ch09.indd 266 4/14/10 10:09 PM


Chapter 9: Tables and Worksheet Databases 267

Specifying computed criteria


Using computed criteria can make filtering even more powerful. Computed criteria filter the table
based on one or more calculations. For example, you can specify computed criteria that display
only the rows in which the List Price (column D) is greater than average.

=D9>AVERAGE(D:D)

Notice that this formula uses a reference to the first data cell in the List Price column. Also, when
you use computed criteria, the cell above it must not contain a field name. You can leave the top
row blank or provide a descriptive label, such as Above Average. The formula will return a value,
but that value is meaningless.
By the way, you can also use a standard filter to display data that’s above (or below) average.
The next computed criteria example displays the rows in which the price per square foot is less
that $100. Cell D9 is the first data cell in the List Price column, and cell G9 is the first data cell in
the SqFt column. As shown in Figure 9-24, the computed criteria formula is

=(D9/G9)<100

Figure 9-24: Using computed criteria with advanced filtering.

Following is another example of a computed criteria formula. This formula displays the records
listed within the past 60 days:

=B9>TODAY()–60

14_475362-ch09.indd 267 4/14/10 10:09 PM


268 Part II: Using Functions in Your Formulas

Keep these following points in mind when using computed criteria:

h Computed criteria formulas are always logical formulas: They must return either TRUE or
FALSE. However, the value that’s returned is irrelevant.
h When referring to columns, use a reference to the cell in the first data row in the field of
interest (not a reference to the cell that contains the field name).
h When you use computed criteria, do not use an existing field label in your criteria range.
A computed criterion essentially computes a new field for the table. Therefore, you must
supply a new field name in the first row of the criteria range. Or, if you prefer, you can
simply leave the field name cell blank.
h You can use any number of computed criteria and mix and match them with noncom-
puted criteria.
h If your computed formula refers to a value outside the table, use an absolute reference
rather than a relative reference. For example, use $C$1 rather than C1.
h In many cases, you may find it easier to add a new calculated column to your worksheet
database or table and avoid using computed criteria.

Using Database Functions


To create formulas that return results based on a criteria range, use Excel’s database worksheet
functions. These functions all begin with the letter D, and they are listed in the Database category
of the Insert Function dialog box.
Table 9-4 lists Excel’s database functions. Each of these functions operates on a single field in the
database.

Table 9-4: Excel’s Database Worksheet Functions


Function Description
DAVERAGE Returns the average of database entries that match the criteria
DCOUNT Counts the cells containing numbers from the specified database and criteria
DCOUNTA Counts nonblank cells from the specified database and criteria
DGET Extracts from a database a single field from a single record that matches the specified
criteria
DMAX Returns the maximum value from selected database entries
DMIN Returns the minimum value from selected database entries
DPRODUCT Multiplies the values in a particular field of records that match the criteria in a database
DSTDEV Estimates the standard deviation of the selected database entries (assumes that the data
is a sample from a population)

14_475362-ch09.indd 268 4/14/10 10:09 PM


Chapter 9: Tables and Worksheet Databases 269

Function Description
DSTDEVP Calculates the standard deviation of the selected database entries, based on the entire
population of selected database entries
DSUM Adds the numbers in the field column of records in the database that match the criteria
DVAR Estimates the variance from selected database entries (assumes that the data is a sample
from a population)
DVARP Calculates the variance, based on the entire population of selected database entries

The database functions all require a separate criteria range, which is specified as the last argu-
ment for the function. The database functions use exactly the same type of criteria range as dis-
cussed earlier in the “Specifying Advanced Filter Criteria” section (see Figure 9-25).
The formula in cell B24, which follows, uses the DSUM function to calculate the sum of values in a
table that meet certain criteria. Specifically, the formula returns the sum of the Sales column for
records in which the Month is Feb and the Region is North.

=DSUM(B6:G21,F6,Criteria)

In this case, B6:G21 is the entire table, F6 is the column heading for Sales, and Criteria is the name
for B1:C2 (the criteria range).
Following is an alternative version of this formula that uses structured table references:

=DSUM(Table1[#All],Table1[[#Headers],[Sales]],Criteria)

This workbook is available on the companion CD-ROM. The filename is database


formulas.xlsx.

You may find it cumbersome to set up a criteria range every time you need to use a
database function. Fortunately, Excel provides some alternative ways to perform condi-
tional sums and counts. Refer to Chapter 7 for examples that use SUMIF, COUNTIF, and
various other techniques.

If you’re an array formula aficionado, you might be tempted to use a literal array in place of the
criteria range. In theory, the following array formula should work (and would eliminate the need
for a separate criteria range). Unfortunately, the database functions do not support arrays, and
this formula simply returns a #VALUE! error.

=DSUM(B6:G21,F6, {“Month”,”Region”;”Feb”,”North”})

14_475362-ch09.indd 269 4/14/10 10:09 PM