Counting Ports of Call with a PivotTable

by Allen Wyatt
(last updated April 27, 2019)

6

Dutch has a worksheet he uses to track all of the cruises he has taken. Each row is one cruise. The left-hand columns contain ship name, departure date, booking number, etc. The ten right-most columns are labeled "Port 1" through "Port 10" to record the ports visited, in order. Nassau, for instance, might be the first stop on one cruise and the third stop on another. Dutch can't figure out a simple, straightforward way to use a PivotTable (as he does, e.g., to see how many times he's cruised on a particular ship, etc.) to see how many times he's visited each port. He wonders if a PivotTable can be used to count the number of times each unique value appears in a range of cells as opposed to a single row or column.

Dutch can solve this problem without even resorting to a PivotTable. This can be done by selecting the cells containing the ports and giving that range a name, such as "Ports." (How you set up a named range has been covered in other ExcelTips.) Then, you can set up a list of all the ports. This could, for instance, be placed on the same worksheet that Dutch is using, or it could be on a different worksheet in the same workbook.

Let's say, for instance, that the port names are in column A, starting below the detailed data in row 22. To the right of the ports, in column B, you could add a simple COUNTIFS formula that references the named range you created:

=COUNTIFS(Ports, A22)

Copy this down as many cells as necessary, and you will have a count of how many times each port was visited. (See Figure 1.)

Figure 1. Getting a Count of Ports

The downside to this approach, of course, is that you'll need to make sure that the Ports named range refers to the proper range whenever you add new cruises. You'll also need to manually add any ports desired to the port list.

Another approach is to restructure your data. PivotTables are used to analyze the contents of rows and columns, as implied in Dutch's original question about whether a range could be analyzed. So, instead of having 10 columns to track each port of call, you could have multiple rows that define each cruise, with each row being a port of call. (See Figure 2.)

Figure 2. Getting a Count of Ports

With the data in a format such as this, you can easily create a PivotTable based on the contents of the Port column that would show the count for each port visited. Restructuring in this way—with each row being a port visited—makes the data much more conducive to analyzing with a PivotTable.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13625) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Office 365.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Updating Multiple PivotTables at Once

PivotTables are a great way to process huge amounts of data and make sense of that data. If you have a number of ...

Discover More

Merging Table Cells

Want to create cells that span multiple columns or multiple rows? You do this in Word by merging cells together. Here's ...

Discover More

Averaging the Last Numbers in a Column

Need to calculate a running average for the last twelve values in a constantly changing range of values? The formula ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Text Truncated in PivotTable

When you create a PivotTable based on data that contains lots of text, you may be surprised to find that your text is ...

Discover More

Formatting a PivotTable

You can format PivotTables using either manual formatting or automatic formatting. You need to be careful, however, as ...

Discover More

Updating Multiple PivotTables at Once

PivotTables are a great way to process huge amounts of data and make sense of that data. If you have a number of ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is nine more than 0?

2019-05-14 16:01:45

Dutch Williams

Thanks, Andy.

I just might have to knock some dust off the gray matter and learn something new!


Dutch


2019-05-14 07:49:41

Andy

Hi Dutch,

Some of the new features in Excel, such as Power Query and the new array functions, are really exciting!

To answer your question, I found a way to use a single pivot table, based on the Figure 2 data, to generate the port counts.

At the step where you create the pivot table, select the "add this data to the Data Model" option: https://imgur.com/1w8ZQcV.

Then when you create your pivot table you get an additional summary option, distinct count: https://imgur.com/pXzkzW5.

And the result is: https://imgur.com/HiCeKk0.

This is based on the visible data in Figure 2 above. I think this is what Andrea was getting at in her comment.

I have also pasted the raw Figure 2 data below - to assist anyone else who would like to play with it to save them from retyping the data.

Cheers,

Andy

Ship Name,Departure,Booking,Port #,Port
Magic,1/01/2019,123456,1,Ft. Lauderdale
Magic,1/01/2019,123456,2,Nassau
Magic,1/01/2019,123456,3,Amber Cove
Magic,1/01/2019,123456,4,Grand Turk
Horizon,2/01/2019,234567,1,Miami
Horizon,2/01/2019,234567,2,Ocho Rios
Horizon,2/01/2019,234567,3,Grand Cayman
Horizon,2/01/2019,234567,4,Cozumel
Fascination,3/01/2019,345678,1,San Juan
Fascination,3/01/2019,345678,2,St. Thomas
Fascination,3/01/2019,345678,3,St. Maarteen
Fascination,3/01/2019,345678,4,St. Kitts
Fascination,3/01/2019,345678,5,St. Lucia
Fascination,3/01/2019,345678,6,Barbados
Glory,4/01/2019,456789,1,New Orleans
Glory,4/01/2019,456789,2,Mahogany Bay
Glory,4/01/2019,456789,3,Belize
Glory,4/01/2019,456789,4,Cozumel
Glory,5/01/2019,567890,1,New Orleans
Glory,5/01/2019,567890,2,Key West
Glory,5/01/2019,567890,3,Freeport
Glory,5/01/2019,567890,4,Nassau
Glory,5/01/2019,567890,5,Amber Cove
Glory,5/01/2019,567890,6,Grand Turk
Norwegian Gem,6/01/2019,134679,1,New York
Norwegian Gem,6/01/2019,134679,2,Aruba
Norwegian Gem,6/01/2019,134679,3,Bonaire


2019-05-13 23:09:58

Dutch Williams

Thanks, Andy.

I must admit that since retiring over five years ago, I've not kept up with the latest developments (Power Query, BI, dashboards, etc.). It was always "work" that drove me to explore and learn - ever since I started with VisiCalc in the early 80's! I do, however, enjoy reading Allan's tips and that's where I first heard about the UNIQUE function.

Regarding your suggestion, though, if I base a (SIMPLE!) pivot table on Figure 2 data, wouldn't it tell me I'd sailed on the Glory ten times, rather than twice? I'm assuming I'd need to do some fancy two-stepping to achieve the results I'm looking for - which I'm getting with ease already, the "Ports Visited" summary aside. The other totals that I track (Ships Sailed, Departure Ports, Cruise Lengths) are all handled easily with my current "one row per cruise" data format. It's only the "Ports Visited" that I can't update quickly and simply with one click on "Refresh All".

I do appreciate your interest and your offer, and please forgive my ignorance if I've failed to understand something about your suggestion. But at this point I'm still hoping the UNIQUE function will provide the answer I'm looking for!


Dutch


2019-05-04 04:36:47

Andy

Hi Dutch,

Have you looked at power query as a way of turning your existing table into the format shown in Figure 2? It can be done very easily, and then right-click on the table to refresh it. You can then base your pivot table on Figure 2. I can post instructions if that would help.

Andy


2019-04-27 16:25:01

Dutch Williams

Thanks, Allen.

So, the answer to my question is - No, a pivot table is not capable of providing the simple, straightforward solution I'm looking for. I'm handling the process manually now (though in a different manner the suggested in the COUNTIF suggestion given). The second solution suggested would (if set up exactly as shown) mangle my existing pivot table counts of ships sailed, etc., as well as not lend itself to producing a nice, one-page printout of the raw data.

Obviously, this is not a big deal to me personally - I was really trying to learn if Excel had a capability I hadn't yet discovered how to access. I'm hoping that when the UNIQUE function becomes available I'll be able to use it (probably in conjunction with COUNTIF) to simplify matters.

Thank you again for your response. I really enjoy reading ExcelTips!


Dutch Williams


2019-04-27 07:58:09

Andrea Carr

Hi Allan,
I saw this issue raised recently and hoped your answer was going to use the new Excel feature related to linking tables fo create one-to-many relationships. I've only tried this once with ok results, but it has the potential to really add to what you can do with Excel. I hope you'll add some tips related to this new feature.
Andrea


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.