vlookup and sumif formulas to assist summarizing … and sumif formulas to assist summarizing...

13
Vlookup and Sumif Formulas to assist summarizing queried data When accessing data from Foundation through the MS Query tool, at times it is necessary to join multiple tables together to retrieve the required information. At times this is very easy, other instances are rather frustrating. The following demonstration shows examples of how data may be joined via formulas in Microsoft Excel. If we want to create a simple list of job records with their associated project managers, we would start by querying the JOBS table. From the Jobs table, we would pull the job_id, description and project_manager_id fields. Everything looks great so far, but I would also like to include the description of the project manager field in this list as well. I will add the PROJECT_MANAGERS table to the query.

Upload: lekien

Post on 27-May-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

Vlookup and Sumif Formulas to assist summarizing queried data

When accessing data from Foundation through the MS Query tool, at times it is necessary to join multiple tables together to retrieve the required information.

At times this is very easy, other instances are rather frustrating. The following demonstration shows examples of how data may be joined via formulas in Microsoft Excel.

If we want to create a simple list of job records with their associated project managers, we would start by querying the JOBS table. From the Jobs table, we would pull the job_id, description and project_manager_id fields.

Everything looks great so far, but I would also like to include the description of the project manager field in this list as well.

I will add the PROJECT_MANAGERS table to the query.

- MS Query makes some “assumptions” and creates joins between the two tables.

You will notice that the MS Query tool “joins” the two tables based on a common field or fields. The Company_no field will always be joined between existing tables. You will notice that one or more fields within a table will be highlighted. These fields are known as a “key identifier”. These are the fields that cannot be changed once entered in Foundation. Basically any maintenance ID (or number) once entered in Foundation is now “known” to all the other tables in the database by that key identifier. These key identifiers could potentially be referenced on hundreds of tables throughout the database, which is why – once they are entered, they cannot be altered.

Once the join is created, you must review your data for integrity.

This is the Query that contained ONLY the JOB table. Notice, there are MANY more jobs returned in this query as opposed to the query that contains two tables. On the right is the query with both tables present. Notice that Jobs 160 – 170 are not on the list on the right.

The “joins” in the MS query tool are rather particular. By default they want to satisfy a simple condition. If there is a project_manager_no on the JOBS table, then match it up with the project_manager_no on the PROJECT_MANAGERS table.

When there is no project_manager_no entered on the JOBS table, the query tool looks at the fields as a “blank”. Since there is no such animal as a “blank” project_manager_no, the link is not satisfied, and the record is dropped from the queried data.

NOTE: There are some advanced programmatic / coding solutions to these issues, but for the use of simple queries, we offer these solutions.

First, start by removing the project_managers table from the query. Single click on the table and press the delete key on your keyboard. You will notice that all of the data has returned.

Return the data to Excel.

?

Access Sheet2 in the Excel workbook and start a new query. In this query, add the project_managers table. From this table, choose project_manager_id and description.

Return this data to Excel – Sheet2

The Vlookup function will allow us to “join” the information from these two sheets together.

VLOOKUP function in Excel.

We have two worksheets with data. We have the JOB table information on Sheet1 and the PROJECT_MANAGERS information on Sheet2. We will use the VLOOKUP (vertical lookup) function in Excel to “find” the match between the value(s) on Sheet1 and Sheet2.

Sheet 1 Sheet 2

When I “find” a value in Column C of Sheet1, I want to return the value from Column B in Sheet2 when the project_manager_id values are the same.

The formula is written as follows:

=VLOOKUP(C2,Sheet2!A:B,2,FALSE)

The Vlookup function is broken down into 4 parts.

1. What Value do you want to find ? (lookup_value) 2. Where do you want to look for the value ? (table_array) 3. How many rows from the leftmost column in the table_array does the requested data

reside ? (col_index_num) 4. Do you want an exact match, or a “close” match (range_lookup)

These 4 Items are broken down as follows:

The Lookup_value is the cell on the spreadsheet that you want to pull information from. Although the help dialog state that it must be a value from the “leftmost column of a table”, this is not true, you may create a vlookup anywhere in a spreadsheet. In this example, we are starting the lookup in column “C”. I like to refer to this as “what are we looking FOR” ?

The table_array is the selected columns from which to look for the associated value defined in the first step. In this example, our array is on Sheet2, Columns A:B. Even though we are only looking to an array with 2 columns, these arrays can be much larger.

The Col_index_num is the number of columns from the first column in the array to locate the required data. In this case, the array is A:B. We are looking to return the data from column B. Column B is the SECOND column from column A, so we enter a 2 in the Col_index_num field.

The Range_Lookup is the most illogical “logical”’ value Excel offers. To find an exact match between the fields on the two sheets, you will select “false”. To find the first, or closest match, enter “true”. 99.99% of the time, you will want to enter “false”.

When the formula is written properly, you will see that Sheet1 has the associated Project Manager description pulled from the table on Sheet2.

Everything looks great, except for the #N/A errors in the rows where employees have no project_manager_id identified.

Excel 2007 has a nice formula to address any and all errors in calculated cells. This new

formula is called =IfError

If we append the original equation with the following:

=IfError(VLOOKUP(C2,Sheet2!A:B,2,FALSE),””) This formula states that if there is an error with the specified formula, return “” (two sets of double quotes = blank). For calculated numbers, such as a #DIV/0 error, you may want to use a 0 in place of the “”.

Resulting data:

Resulting data:

Use the filters, and this is a nice list to show who is the PM on a particular project. (Use filters and criteria in the MS query to show Active jobs only).

This theory may be applied with multiple fields within a worksheet.

SUMIF statement

The Sumif statement is loosely related to the Vlookup statement. The Vlookup statement has to be used when text based fields are involved. Vlookup may be used on numeric fields, but what if we need to return more than one value ? Imagine we need to sum the numbers in a particular column if the criterion in an adjacent column is met.

Here is an example. In columns D and E, we have a month and an associated $ amount for sales within that month. If we need to total the amounts from the array based on the month, we can use the Sumif statement.

The sumif statement is comprised of 3 items:

1. What range of Data do want evaluated ? (Range)

2. What / Where is the “KEY” you are looking for ? (Criteria)

3. What / Where is the range of cells where the data resides that needs to be summed up ? (sum_range)

The 3 items in the formula are broken down as follows:

RANGE : define the range of cells that you want to look for a particular value defined by the….

CRITERIA : The criteria in this formula is a cell reference. In some cases it can be a text or number string depending on your requirements. So far the Sumif statement is looking up and down column D for the value “JAN”. When it finds the value of JAN, it needs to SUM the values from ……

SUM_RANGE : just as the help says. The sum range is the range of cells you wish to evaluate, or sum.

I have created a query that is a simple job list with the job number, job description and original contract amount. I will return this information to Sheet1 of my Excel Workbook.

On Sheet2, I will create another query that will pull information from the Job_Chg table. This query will include the Job_id, the status and the total_income_adj fields.

Sheet1 – Job_id, description, original contract

Sheet2 job_id, status, total_income_adj

The sumif equation to sum the values in Sheet2, column C, based on the values in Sheet2 column A that meet the same criteria as Sheet1 cell A2 is :

=sumif(Sheet2!A:A,Sheet1!A2,Sheet2!C:C) If you need a moment, read the above again… and again, and again. It will eventually make sense.

The nice thing about the Sumif equations is that there are no “wrong” results, no #N/A errors. (So we do not need to use the iferror formula to make our sheet look nice. The sumif formula only works with numeric values. We would not be able to use the sumif formula with the first example in this lesson (finding the project_manager description).

EXTRA:

Sumifs statement (available in Excel 2007 ONLY). This will allow for multiple criteria within a Sumif statement.

The =sumifs formula may be used to sum values if multiple criteria are met. (A total of 127 criteria may be defined in a sumifs statement).

This example shows that we want to separate by Job_id and by Status of A for approved, P for Pending and I for Internal Change Orders.

Column D: =SUMIFS(Sheet2!C:C,Sheet2!A:A,Sheet1!A4,Sheet2!B:B,"A")

Column E: =SUMIFS(Sheet2!C:C,Sheet2!A:A,Sheet1!A4,Sheet2!B:B,"P")

Column F: =SUMIFS(Sheet2!C:C,Sheet2!A:A,Sheet1!A4,Sheet2!B:B,"I")