+ All Categories
Home > Documents > Lecture 9: Look-up Functions and Pivot Tables...working with large data sets in Excel: •Look-up...

Lecture 9: Look-up Functions and Pivot Tables...working with large data sets in Excel: •Look-up...

Date post: 20-Dec-2020
Category:
Upload: others
View: 4 times
Download: 0 times
Share this document with a friend
24
Material partly adapted from CCI Learning Solutions Inc. MTH4114: Computing and Data Analysis with Excel Lecture 9: Look-up Functions and Pivot Tables 1
Transcript
Page 1: Lecture 9: Look-up Functions and Pivot Tables...working with large data sets in Excel: •Look-up functions (VLOOKUP, HLOOKUP) are used to search a table or cell range for data corresponding

Microsoft Office

Excel 2013

Material partly adapted from CCI Learning Solutions Inc.

MTH4114: Computing and Data Analysis with Excel

Lecture 9: Look-up Functions and Pivot Tables

1

Page 2: Lecture 9: Look-up Functions and Pivot Tables...working with large data sets in Excel: •Look-up functions (VLOOKUP, HLOOKUP) are used to search a table or cell range for data corresponding

Microsoft Office

Excel 2013

• In this lecture we cover some (relatively) advanced techniques for working with large data sets in Excel:

• Look-up functions (VLOOKUP, HLOOKUP) are used to search a table or cell range for data corresponding to some specified input, e.g.

• Find a student’s grade by inputting their ID number• Find a stock price by inputting its ticker symbol• Find an employee’s department by inputting their name

• Pivot Tables and Pivot Charts are used to summarise data, to reveal patterns and trends, e.g.

Motivation and lecture objectives

2

Page 3: Lecture 9: Look-up Functions and Pivot Tables...working with large data sets in Excel: •Look-up functions (VLOOKUP, HLOOKUP) are used to search a table or cell range for data corresponding

Microsoft Office

Excel 2013

• Before analysing a large data set in Excel, it may be convenient to convert the cell range containing your data into an Excel table

• To do this, select any cell in your range and click Format as Table in the Styles group of the Home tab (or, alternatively, go to the Insertgroup and select Table in the Tables tab):

• Clicking anywhere in your table reveals the contextual “Table Tools” Design tab, where you can name your table, change colours, etc.

Excel tables

3

Page 4: Lecture 9: Look-up Functions and Pivot Tables...working with large data sets in Excel: •Look-up functions (VLOOKUP, HLOOKUP) are used to search a table or cell range for data corresponding

Microsoft Office

Excel 2013

• There are various benefits to using an Excel table, including:• Filtering is automatically enabled • Alternating row colours make data easier to read • Structured references – after naming a table (in the Properties

group of the contextual Design tab), its columns can be referred to in the convenient form “TableName[ColumnName]”, e.g.

Excel tables (continued)

4

Page 5: Lecture 9: Look-up Functions and Pivot Tables...working with large data sets in Excel: •Look-up functions (VLOOKUP, HLOOKUP) are used to search a table or cell range for data corresponding

Microsoft Office

Excel 2013

• The VLOOKUP function allows us to retrieve data from a table based on some input; the “V” stands for “vertical”, meaning that we search a column for the input and retrieve data from the corresponding row

• For example, in the table below we may wish to look-up a student’s ID number (e.g. 961243) and retrieve their Calculus mark (51)

• The syntax for VLOOKUP is:

VLOOKUP

5

=VLOOKUP(lookup_value, table_array, col_index_num, range_lookup)

Page 6: Lecture 9: Look-up Functions and Pivot Tables...working with large data sets in Excel: •Look-up functions (VLOOKUP, HLOOKUP) are used to search a table or cell range for data corresponding

Microsoft Office

Excel 2013

• What do the four parameters mean?• lookup_value – the ‘input’ value being looked up (e.g. student ID)• table_array – the name of your table (or a cell range) • column_index – the number of the column containing the data

we wish to retrieve (e.g. “4” for the student’s Calculus mark)• range_lookup – “0” or “FALSE” to look for an exact match, “1” or

“TRUE” to find the ‘closest match’ (not usually recommended)

• For example, the following inputs give us the Calculus mark of the student with ID 961243 (the table is named “Marks”):

VLOOKUP (continued)

6

Page 7: Lecture 9: Look-up Functions and Pivot Tables...working with large data sets in Excel: •Look-up functions (VLOOKUP, HLOOKUP) are used to search a table or cell range for data corresponding

Microsoft Office

Excel 2013

• Important note: the lookup_value must come from the first columnof your table, e.g. in our table of students’ marks, we can only use the Student IDs as lookup_values – using e.g. Surnames won’t work:

• This is a well-known limitation of VLOOKUP, but there are ways to get around it; one approach is to rearrange the columns so that the column containing the desired lookup_values is the first column

VLOOKUP: Tips and tricks

7

Page 8: Lecture 9: Look-up Functions and Pivot Tables...working with large data sets in Excel: •Look-up functions (VLOOKUP, HLOOKUP) are used to search a table or cell range for data corresponding

Microsoft Office

Excel 2013

• Alternatively, change table_array to a cell range where the desired lookup_value appears in the first column; this can be done using structured references “TableName[[StartingColumn]:[EndingColumn]]”, e.g.

• Note that you will also need to change column_index (e.g. “Calculus” is now column 2, not column 4)

• If you forget this syntax, you can just use the mouse to select the required cell range, or use cell addresses instead

VLOOKUP: Tips and tricks (continued)

8

Page 9: Lecture 9: Look-up Functions and Pivot Tables...working with large data sets in Excel: •Look-up functions (VLOOKUP, HLOOKUP) are used to search a table or cell range for data corresponding

Microsoft Office

Excel 2013

• A third option is to use the Excel functions INDEX and MATCH, as explained here – this approach is not examinable in this module

• It is also important to understand the range_lookup parameter:

• If range_lookup is set to “0” or “FALSE” then VLOOKUP will search for an exact match to the lookup_value parameter; if it does not find an exact match, you will get an error:

VLOOKUP: Tips and tricks (continued)

9

The lookup_value does not appear in the Student ID column

Page 10: Lecture 9: Look-up Functions and Pivot Tables...working with large data sets in Excel: •Look-up functions (VLOOKUP, HLOOKUP) are used to search a table or cell range for data corresponding

Microsoft Office

Excel 2013

• If range_lookup is set to “1” or “TRUE” then VLOOKUP will search for the ‘best possible’ match to the lookup_value parameter

• For this to work properly, the first column of your table must be sorted (in either alphabetical or numerical order), and even then you might get unexpected results, e.g.

• Although there are some situations where inexact VLOOKUP is useful, for this module you should use exact VLOOKUP when possible

VLOOKUP: Tips and tricks (continued)

10

Column is now sorted

The lookup_value is only slightly smaller than Tanya Collins’ actual ID, but we still get the incorrect output because VLOOKUP ‘rounds down’

Page 11: Lecture 9: Look-up Functions and Pivot Tables...working with large data sets in Excel: •Look-up functions (VLOOKUP, HLOOKUP) are used to search a table or cell range for data corresponding

Microsoft Office

Excel 2013

• VLOOKUP can be nested with other functions to perform more complex tasks/calculations…

• e.g. Suppose we have a separate table containing only the student IDs (possibly in a different order), and we want to check which students achieved an “A” grade (70 or more) in Algebra – we can use VLOOKUP together with an IF function:

VLOOKUP: A more advanced example

11

Look-up the student’s Algebra mark Check whether it is at least 70 Output Boolean TRUE or FALSE accordingly

Page 12: Lecture 9: Look-up Functions and Pivot Tables...working with large data sets in Excel: •Look-up functions (VLOOKUP, HLOOKUP) are used to search a table or cell range for data corresponding

Microsoft Office

Excel 2013

• Recall that VLOOKUP searches a column for the input (lookup value) and retrieves data from the corresponding row

• HLOOKUP is very similar, but it searches a row for the input and retrieves data from the corresponding column (“H” = “horizontal”), e.g.

• Note again that the lookup_value (first parameter) must come from the first row of the table/cell range

HLOOKUP

12

Page 13: Lecture 9: Look-up Functions and Pivot Tables...working with large data sets in Excel: •Look-up functions (VLOOKUP, HLOOKUP) are used to search a table or cell range for data corresponding

Microsoft Office

Excel 2013

• Pivot Tables can be used to summarise large data sets

• This can be especially useful in business situations, e.g. consider the ‘cosmetics store’ sales data from the week 4 lab exercises:

• We may need to summarise the data to answer questions such as:• Which product sells the most units at each location?• What is the total revenue for each product across all locations?• How many total units were sold by each salesperson?

Pivot Tables

13

Page 14: Lecture 9: Look-up Functions and Pivot Tables...working with large data sets in Excel: •Look-up functions (VLOOKUP, HLOOKUP) are used to search a table or cell range for data corresponding

Microsoft Office

Excel 2013

To create a Pivot Table: 1. Convert your data to a table (if need be) and select any cell2. Select Summarise with Pivot Table from the table’s Design tab (or

select Pivot Table from the Tables group of the Insert tab)3. Choose where to put the Pivot Table (new or existing worksheet)

Pivot Tables (continued)

14

Page 15: Lecture 9: Look-up Functions and Pivot Tables...working with large data sets in Excel: •Look-up functions (VLOOKUP, HLOOKUP) are used to search a table or cell range for data corresponding

Microsoft Office

Excel 2013

• e.g. Suppose we want to analyse the numbers of units of each product sold at each location:

• To create this particular Pivot Table, we would drag• “Location” to “Rows”• “Product” to “Columns” • “Units” to “Values”

• Now we fill the Pivot Table by dragging Field Names (column headings) into Pivot Table Fields (Filters, Columns, Rows, Values)

Pivot Tables (continued)

15

Page 16: Lecture 9: Look-up Functions and Pivot Tables...working with large data sets in Excel: •Look-up functions (VLOOKUP, HLOOKUP) are used to search a table or cell range for data corresponding

Microsoft Office

Excel 2013

You can add more than one Field Name to each Pivot Table Field, and re-order the Field Names to change the appearance of the table, e.g.

Pivot Tables (continued)

16

Page 17: Lecture 9: Look-up Functions and Pivot Tables...working with large data sets in Excel: •Look-up functions (VLOOKUP, HLOOKUP) are used to search a table or cell range for data corresponding

Microsoft Office

Excel 2013

• In the Values field, the default “Sum” calculation can be changed to Average, Count, Maximum, Minimum, etc.:• Right-click one of the Values in the Pivot Table• Select a calculation from Summarize Values By (or click Value

Field Settings and select from the dialog box, also accessible from the Active Field group of the contextual Analyze tab)

Pivot Tables (continued)

17

• e.g. In our example, choosing “Max” gives us the maximum number of units of each product sold at each location (instead of the total/sum):

Page 18: Lecture 9: Look-up Functions and Pivot Tables...working with large data sets in Excel: •Look-up functions (VLOOKUP, HLOOKUP) are used to search a table or cell range for data corresponding

Microsoft Office

Excel 2013

• Finally, Filters can be used to further refine your data summary

• Rows/columns can be filtered by clicking the corresponding arrow , and we can also add additional filters using the Filters field:• Drag the Field Name(s) you wish to filter by into the Filters field• Use the filter(s) which appears above the Pivot Table

• e.g. filter the sales data by Salesperson:

Pivot Tables (continued)

18

• Applying the filter changes the table, i.e. the calculations now depend only on the chosen value(s), e.g.

Page 19: Lecture 9: Look-up Functions and Pivot Tables...working with large data sets in Excel: •Look-up functions (VLOOKUP, HLOOKUP) are used to search a table or cell range for data corresponding

Microsoft Office

Excel 2013 Pivot Charts

19

• We can also visualise the data in a Pivot Table using a Pivot Chart

• To create a Pivot Chart, click anywhere in your Pivot Table, then go to the Analyze tab and select Pivot Chart from the Tools group

Page 20: Lecture 9: Look-up Functions and Pivot Tables...working with large data sets in Excel: •Look-up functions (VLOOKUP, HLOOKUP) are used to search a table or cell range for data corresponding

Microsoft Office

Excel 2013

• If you change your Pivot Table, the Pivot Chart changes automatically

• e.g. sorting the row labels changes their order in the chart:

Pivot Charts (continued)

20

Sort using the drop-down box

Ordering in chart changes too

Page 21: Lecture 9: Look-up Functions and Pivot Tables...working with large data sets in Excel: •Look-up functions (VLOOKUP, HLOOKUP) are used to search a table or cell range for data corresponding

Microsoft Office

Excel 2013

• Like other charts, Pivot Charts have a contextual Design tab, in which you can change the chart type, chart style, and other settings

• In particular, you can switch the rows and columns, which also changes the Pivot Table accordingly, e.g.

Pivot Charts (continued)

21

Page 22: Lecture 9: Look-up Functions and Pivot Tables...working with large data sets in Excel: •Look-up functions (VLOOKUP, HLOOKUP) are used to search a table or cell range for data corresponding

Microsoft Office

Excel 2013

• Let’s look at one more example (sales data from a travel company):

• Suppose we want to see how popular the various products (“Sale Type”) are during different times of year: create a Pivot Table with• “Sale Type” as Rows• “Date” as Columns• Sum of “Amount Paid” as Values

• Notice that the dates are (conveniently) grouped into months …

Another example

22

Page 23: Lecture 9: Look-up Functions and Pivot Tables...working with large data sets in Excel: •Look-up functions (VLOOKUP, HLOOKUP) are used to search a table or cell range for data corresponding

Microsoft Office

Excel 2013

• … and clicking the button next to a month shows all the dates:

• More generally, you can manually ‘group’ several rows or columns by right-clicking a selection of values and selecting Group, e.g.

Another example (continued)

23

Page 24: Lecture 9: Look-up Functions and Pivot Tables...working with large data sets in Excel: •Look-up functions (VLOOKUP, HLOOKUP) are used to search a table or cell range for data corresponding

Microsoft Office

Excel 2013

• Applying Filters and creating a Pivot Chart allows us to further summarise the data, but the exact steps you need to take depend on the output you want to produce, so you need to think carefully

• e.g. To see the income generated by “bike tour” and “car rental” in each month, we can (i) filter the row labels to include only these two values, (ii) create a Pivot Chart, (iii) switch rows/columns in the chart:

Another example (continued)

24


Recommended