week 11: tables look ups pivot tables

52
MTH4114: Computing and Data Analysis with Excel Week 11: Tables Look ups Pivot Tables 1

Upload: others

Post on 05-Jun-2022

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Week 11: Tables Look ups Pivot Tables

MTH4114: Computing and Data Analysis with Excel

Week 11:Tables

Look upsPivot Tables

1

Page 2: Week 11: Tables Look ups Pivot Tables

Tables

We can tell Excel that part of a spreadsheet is a table. When we do this Excel does several things it thinks are useful for a table (you may disagree!)

2

Page 3: Week 11: Tables Look ups Pivot Tables

Formatting Tables

First lets see how to tell Excel that some data is a table.

1. Select a cell in the table.

2. Click on 'Format as Table' in the 'Home' ribbon.

3. Choose a style. My preference is the one I have marked, but it obviously your choice!

3

Page 4: Week 11: Tables Look ups Pivot Tables

Formatting Tables

1. A dialogue box appears.

2. Choose the data range (usually Excel will get this right.

3. Tick the box if your table has headers (it is generally better if you make sure your table has headers before you format it as a table, but it not required).

4. Click OK.

4

Page 5: Week 11: Tables Look ups Pivot Tables

Formatting Tables

You should see something like:

The key things you see are:

1. The rows are coloured alternately to make it easier to see which row is which as you go across the table.

2. Excel has formatted the headers in bold.

3. Excel has added the "Filter" buttons on the headers.

5

Page 6: Week 11: Tables Look ups Pivot Tables

Properties of Tables

• If you type something in a row or column next to a table, then the table will automatically expand adding a row or column as appropriate.

• I find the formatting quite ugly, but it does show how big the table is.

• There are lots of table properties: you can see them by going to the 'Table Design' Ribbon. (This Ribbon button only appears when you have clicked in the table)

• The ribbon looks like:

6

Page 7: Week 11: Tables Look ups Pivot Tables

Table formulas

• Let's add a column to our table for the average mark. We type "Average" in cell G1 and the table expands

• Now type a formula for the "Average" in cell G2: we can use =AVERAGE(C2:F2). Excel automatically copies the formula to the whole column.

7

Page 8: Week 11: Tables Look ups Pivot Tables

Table formulas

• Moreover, if we add a new row Excel will automatically include the formula in the new row.

• There are lots of nice features here:

1. The formula works,

2. the autofilling (enter once and it applies to the whole column)

3. the way the formula is added to new rows.

8

Page 9: Week 11: Tables Look ups Pivot Tables

Naming Tables

• The table has a name. This is at the top left of the 'Table Design' Ribbon. In this case the name is 'Table4'.

• We can edit the name to something moreinformative/memorable. For example"Module_Marks"

9

Page 10: Week 11: Tables Look ups Pivot Tables

Naming Columns

• Assuming your table had headers, Excel will have named the columns for you: the name of a column isTable[Header]. We do need the square brackets.

• For example in this table Column C can be referred to as 'Table4[Calculus]' so you can use formulas like =AVERAGE(Table4[Calculus])

10

Page 11: Week 11: Tables Look ups Pivot Tables

Naming Rows

• Excel doesn't name the rows by default, but does have a special notation for "Column X in this row". TABLE[@HEADER] means this row in TABLE in column with header HEADER.

• This is useful for formulas inside the table.

• For example in this table we can reference Column C in the current row as =Table4[@Calculus]

11

Page 12: Week 11: Tables Look ups Pivot Tables

Naming Rows

• More generally, we could get the average mark by=AVERAGE(Table4[@[Calculus]:[Computing]])

which says to average the columns between Calculus and Computing in the current row.

• You can see this has calculated the average mark.

• Again this formula will automatically be added to any new rows.

12

Page 13: Week 11: Tables Look ups Pivot Tables

Table Names

• You don't need to remember most of this syntax. Excel will enter it for you if you just highlight the regions.

• But is useful to understand what Excel has done.

13

Page 14: Week 11: Tables Look ups Pivot Tables

Removing a Table

• If you want to delete a table then you can just select it and delete it. But what if you want to make it back into normal Excel Cells?

• Select a cell in the table, and go to the 'Table Design' Ribbon.

• Choose 'Convert to Range'

• Excel will check you want to dothis (click "yes").

• The "Table" has been removed. But the formatting stays. If you want to remove the formatting you need to use 'Clear Formats' under 'Clear' on the Home Ribbon.

14

Page 15: Week 11: Tables Look ups Pivot Tables

MTH4114: Computing and Data Analysis with Excel

Lookups

Page 16: Week 11: Tables Look ups Pivot Tables

Lookups

• A common issue in 'real life' Excel use is to combine data.

• For example we might have a worksheet with some students’ Calculus marks, and another worksheet with the same group of students’ Probability marks.

• We want to form one worksheet with their marks for both modules.

• To do this we want to 'lookup' each students mark.

16

Page 17: Week 11: Tables Look ups Pivot Tables

XLOOKUP

• XLOOKUP is a new Excel function that is much nicer to use than the older equivalents VLOOKUP and HLOOKUP.

• Roughly you give XLOOKUP a lookup_value and XLOOKUP:

1. Finds this value in one column (or range).

2. Returns (outputs) the entry in the same row in a different column (or range).

17

Page 18: Week 11: Tables Look ups Pivot Tables

XLOOKUP• The basic syntax is

=XLOOKUP(lookup_value, lookup_range, return_range)

• lookup_value is the value you want to lookup.

• lookup_range is the region (typically a column) you want Excel to look for the lookup_value.

• return_range is the range (again typically a column) containing the information you want.

• XLOOKUP looks for lookup_value in the lookup_rangeand returns the corresponding entry from the return_range.

18

Page 19: Week 11: Tables Look ups Pivot Tables

XLOOKUP

• We show this in the example we discussed above. Suppose we have two worksheets called Calculus and Probability each of which contains the student's student-id in Column A, their name in Column B and their mark for the module in Column C.

• We want to add a column to the Calculus Sheet containing the student’s Probability mark.

• That is, we want to lookup their Probability mark.

19

Page 20: Week 11: Tables Look ups Pivot Tables

XLOOKUP

• We use the student-id as the lookup value.

• We ask Excel to look for the student-id in Column of A (the student-id column) of the Probability sheet.

• We ask Excel to output the corresponding entry Column C (the module mark column) of the Probability sheet.

• Putting this together the formula in cell D2 is=XLOOKUP(A2, Probability!A:A, Probability!C:C)

• If we AutoFill or Copy/Paste this down Column D we have the marks we want.

20

Page 21: Week 11: Tables Look ups Pivot Tables

XLOOKUP comments

• Why did we use Student-id as the lookup value, rather than the name?

• This would be fine if we knew that everyone has different names.

• But, it will go wrong if two people have the same name! If they do Excel will just pick the first match and return that value for both (all) people with that name.

21

Page 22: Week 11: Tables Look ups Pivot Tables

XLOOKUP comments

• In this example, there are two people called Nikhil.1020 got 90 and 1033 got 14.

• But if we use XLOOKUP with the name they bothget 90.

• Note: Excel does not give an error!

22

Page 23: Week 11: Tables Look ups Pivot Tables

XLOOKUP comments

• What if there is no match? Suppose there is someone taking Calculus but not Probability.

• Excel will give a #N/A error

• With XLOOKUP you can add an extra argument telling Excel what to do if there is no match.

• =XLOOKUP(A2, Probability!A:A, Probability!C:C, "-")tells Excel to output a "-" if there is no match.

23

Page 24: Week 11: Tables Look ups Pivot Tables

XLOOKUP comments• What if there are some people taking Calculus but not

Probability, and some people taking Probability but not Calculus?

• The best solution is to create a new worksheet containing the combined scores.

24

Page 25: Week 11: Tables Look ups Pivot Tables

Remove Duplicates• The first step is to put all the student-ids and names

into columns A and B of the new worksheet. Just use copy and paste and put the Probability ids/names after the Calculus ones.

• Most people will occur twice.

• Select both these columns.

• Now go to the Data Ribbonand select

'Remove Duplicates'

25

Page 26: Week 11: Tables Look ups Pivot Tables

Remove Duplicates• You will get a dialogue box:

• Just click OK.

• Excel will remove all rowswhere the Student Id andName match another row.

• You now have a list of allstudents taking either Calculus or Probability or both.

• Use XLOOKUP to fill in the module marks for each of these: once for Calculus and once for Probability. This is exactly as we saw earlier.

• See the Week 11 Lab Quiz for an example.

26

Page 27: Week 11: Tables Look ups Pivot Tables

VLOOKUP• VLOOKUP is similar to XLOOKUP but it is less nice to

use.

• I mention it because

1. XLOOKUP is very new: you may be using a version of Excel without XLOOKUP

2. You may be working with an existing Spreadsheet which uses VLOOKUP.

3. I may ask you to use it in the Exam!

• The key idea is exactly the same as XLOOKUP. But the syntax is unpleasant.

27

Page 28: Week 11: Tables Look ups Pivot Tables

VLOOKUP• The syntax is

=VLOOKUP(lookup_value, table_array, column_index_number, FALSE)

• lookup_value is the value you want to lookup.

• Table array is a region. Excel will try to find lookup_value in the first column of this region.

• Column_index_number is the column in table_arraythat contains the information you want. 1 would be the first column of the range, 2 the second column etc.

• Strictly speaking the last argument could be TRUE, but its essentially never what you want. You must say FALSE or Excel will return stupid and wrong answers.

28

Page 29: Week 11: Tables Look ups Pivot Tables

VLOOKUP

• In our example the formula in Cell D2 of the Calculus Sheet is

=VLOOKUP(A2, Probability!A:C, 3, FALSE)

• Which says find the value A2 (the student-id)

• in column A of the Probability sheet (the first column of the region Probability!A:C)

• Return the entry from that row in the third column in the region: i.e., from column C of the Probability sheet.

• FALSE because you must do this to avoid Excel giving stupid answers.

29

Page 30: Week 11: Tables Look ups Pivot Tables

VLOOKUP

Why is VLOOKUP worse than XLOOKUP?

1. The return column has to be to the right of the lookup column: you can't ask Excel to look up the value in Column B and return the entry from Column A.

2. You have to count columns so can tell Excel which column you want it to return the value from. Suppose you want it to lookup the value in Column B and return the value from Column T you want Column 19.

=VLOOKUP(…,B1:T100, 19, …)

3. If you insert or delete columns between the lookup column and the return column the formula just breaks.

4. Motto: use XLOOKUP if you can!30

Page 31: Week 11: Tables Look ups Pivot Tables

HLOOKUP

• HLOOKUP is very similar to VLOOKUP but looks up a value in one row, and returns the corresponding value in a different row.

• HLOOKUP is much less common than VLOOKUP, and XLOOKUP can do both cases, so we don't cover HLOOKUP in this module.

31

Page 32: Week 11: Tables Look ups Pivot Tables

Pivot Tables

• Pivot tables are one of the more advanced features of Excel.

• They are often thought of as difficult.

• But they are not. They just summarise or combine or aggregate data.

• The basic idea is that you have a big table with lots of information: typically each row containing information for one "thing" – perhaps a sale, or a student's marks, or the results of a sports game.

• In many cases this complete list contains much more information than you want – you want to summarise it.

32

Page 33: Week 11: Tables Look ups Pivot Tables

Pivot Tables

• It is easiest to see in an example.

• I downloaded all the premier league games from the 2018/19 season.

• Each row contains the information for one game: here you see that on the 10/08/2018 Manchester United played Leicester City at Home. The Score was 2-1 so Manchester United scored 3 points for this game.

33

Page 34: Week 11: Tables Look ups Pivot Tables

Pivot Tables

• The table contains the results for 380 games (there were 20 teams, so each team has 19 different opponents and they play them both home and away.)

• But from this list it is hard to see anything about what happened – you can't even see who won the league!

• We could analyse the data with formulas. For example, if we just want to know who won, that would be fairly easy (using SUMIF for example).

• But maybe we don't know what we want to know, and then a pivot table comes into its own – it is easy to try things and see if it is interesting.

34

Page 35: Week 11: Tables Look ups Pivot Tables

Pivot Tables

To create a pivot table:

1. First format the original data as a table

2. Click on Insert Pivot Table on the 'Insert' Ribbon.

3. This brings up a dialogue box

4. You need to tell Excelwhere the data is (youcan use the table name or a range e.g.A1:G381)

5. The rest you can leave.

6. Click OK

35

Page 36: Week 11: Tables Look ups Pivot Tables

Pivot Tables

7. This takes you to a new worksheet. On the right there is a complicated looking pane with several options.

36

Page 37: Week 11: Tables Look ups Pivot Tables

Pivot Tables

• The top of the box lists the columns of the table. This is the main data we can summarise.

• Let's start by summarizing by team: select eam.

• We get a list of teams (there are more than shown).

• And if we look at the bottom halfof the pivot table pane we see thatExcel has put "Teams" as a 'row'.

• That is why we see each team as a row.

37

Page 38: Week 11: Tables Look ups Pivot Tables

Pivot Tables

• Now let's select 'Points' (leaving 'Teams' selected).

• We see the total pointsfor each team.

• And we see Excel has added 'Points' to the 'Values' section in the pivot table pane.

• It has also said "Sum of Points" – that is, it is totaling the points.

38

Page 39: Week 11: Tables Look ups Pivot Tables

Pivot Tables

• Now let's add 'Month' as a 'column'. Just drag the 'Month' item into the 'Column' box in the pivot table pane.

• We now have columns for the points each team got in each month, as well as the Total points they got.

39

Page 40: Week 11: Tables Look ups Pivot Tables

Pivot Tables

• And this is really what Pivot Tables are all about!

• Excel has split the data up into rows (teams), columns (months), and each entry in the table says how many points were scored (the 'value') by that team (row) in that month (column).

• Almost everything else in Pivot tables is tweaking and modifying what we have just done.

• It is worth playing with Pivot tables just to see what they can do. Excel makes it very easy for you to experiment.

• But I give some examples.

40

Page 41: Week 11: Tables Look ups Pivot Tables

Pivot Tables

• We could have put 'Teams' as a column, and 'Month' as a 'row' and we would get:

• The same information as before but with rows and columns interchanged.

41

Page 42: Week 11: Tables Look ups Pivot Tables

Pivot Tables

• We could have put both 'Teams' and 'Months' as rows. The Excel divides by 'Teams' and the subdivides by 'Months'

• Again, the same information but presented differently.

42

Page 43: Week 11: Tables Look ups Pivot Tables

Pivot Tables (sorting)• Let's switch back to the teams as rows/months as

columns setting.

• There are many ways we might want to sort the table.

• Broadly these fall into two categories

1. We might want to sort the teams in reverse alphabetical order, or the months in reverse order.

2. We might want to sort by total points, or total points in March.

• The first type is sorting the column or rows based on the headers; the second type is sorting based on values in the pivot table.

43

Page 44: Week 11: Tables Look ups Pivot Tables

Pivot Tables (sorting)• To sort based on headers just click on the dropdown

next to ‘Row Labels’ or ‘Column Labels’and choose the sortyou want.

• To sort based on values, just click in the relevant column and then use ‘Sort & Filter’ from the Home Ribbon.

• By default that will sort the column in the Pivot Table; use custom sort to sort the row.

44

Page 45: Week 11: Tables Look ups Pivot Tables

Pivot Tables (average)

• We might think totalling the points in each month is "unfair": some teams may have played more games in a particular month. We could decide to use the average points per match instead of the total points.

• To do this click on 'Sum of Values' in the'Values' section of the Pivot table Paneand select 'Value Field Settings'

45

Page 46: Week 11: Tables Look ups Pivot Tables

Pivot Tables (average)

• This brings up a dialogue and you can select "Average"

• We get the table below.

• (You might want to hide somedecimal places!)

46

Page 47: Week 11: Tables Look ups Pivot Tables

Pivot Tables (count max min)

• The Value Field Settings dialogue offers several other choices including Count, Max and Min.

• Count: this counts the number of times that a pair occurs: for example the number of times each team played in each month.

47

• We can see that Bournemouth played 4 times in both March and April; Arsenal only played twice in March but 6 times in April.

Page 48: Week 11: Tables Look ups Pivot Tables

Pivot Tables (Filters)

• The final feature I will discuss is Filters. We can add an entry to the 'Filters' box in the Pivot Table Pane.

• Lets add 'Home/Away'.

• Excel adds the following just above the pivot table.

• If we click the drop-down next to (All) we can select just the Home games, or justthe Away games.

48

Page 49: Week 11: Tables Look ups Pivot Tables

Pivot Tables (Filters)

• If we select "Home" then we get.

• And now Excel is showing us the number of points each team scored each month when playing at Home.

49

Page 50: Week 11: Tables Look ups Pivot Tables

Pivot Tables (refreshing)

• Warning! By default Pivot tables do not update when you change the data in the table.

• You need to click on 'Refresh' under the 'PivotTable Analyse' Ribbon (which only appears when you select a cell in the pivot table.)

• Normally, this is not an issue as we normally use Pivot Tables to analyse data that is fixed and not going to change (like the Premier League results in 2018/19)

50

Page 51: Week 11: Tables Look ups Pivot Tables

Pivot Tables (preparation)

• Finally, you may need to do some work on your data before you start with the Pivot Table.

• In our example, I removed some columns I didn't want, I added a column calculating the "points" from the game (3 for a win, 1 for a draw, 0 for a loss).

• I also added each game twice once for each team playing that game.

• None of this was hard, but it did make the Pivot Table Analysis much easier.

51

Page 52: Week 11: Tables Look ups Pivot Tables

MTH4114: Computing and Data Analysis with Excel

End of Module

Next WeekRevision Lecture

1