Recently, I had a peculiar problem. I have a list of zip codes and I wanted to find out nearest zip codes for each of them.
Now, If I wanted to find out near by zip codes for one area, I could go and search in Google. But, how to do it for dozens of them?
Today, lets understand how you can use Excel (that’s right) to do this automatically. We will be using Excel 2013 for this.
A little background – Excel 2013 Web Formulas
In Excel 2013, Microsoft has introduced 3 powerful new formulas. These will help you fetch & parse XML / HTML data from web. The formulas are,
- ENCODEURL: to encode web URLs (replaces special characters in URLs with % codes like space becomes %20, / becomes %2F etc…)
- WEBSERVICE: connects to a webservice / website and fetches response as XML / HTML.
- FILTERXML: extracts a portion of XML/HTML using specified XPATH.
Using these formulas and web services, we can quickly fetch near by zipcodes for any input value.
Step 1: Find a web-service that can tell us near by zipcodes
I am sure there are many web sites that can offer a service like this. After searching a while, I came across a website called as geonames.org which has many webservices around address / zip code search. The service I have used is,
This service is available as XML & JSON. Since Excel 2013 formulas only process XML data, I went with XML service. The service API url is this:
http://api.geonames.org/findNearbyPostalCodes?postalcode=ZIPCODE&country=US&radius=15&username=UNAME&maxRows=10
ZIPCODE is where you enter the zipcode from which you want to find nearby zipcodes
UNAME is where you enter your user name for geonames.org. Click here to register with geonames.org.
Step 2: List all original Zip codes in a column
This is simple. Just paste all original zip codes in a column.
Step 3: Write WEBSERVICE Formula
First enter the API URL in a cell like B1. (Make sure your user name is included in the service url)
Now write WEBSERVICE formulas so that we can fetch XML listing for each of the zip codes. Assuming zip codes are in A3:Ax, in adjacent column write =WEBSERVICE(SUBSTITUTE($B$1,”ZIPCODE”,A3))
And drag it down to fill down the formula for all zipcodes.
Step 4: Write FILTERXML formulas
Now that we have full XML corresponding to each zip code, we need to parse this XML to extract the nearby zip code numbers. The original XML looks something like this:
To extract the zipcodes alone, we need to use FILTERXML formula.
FILTERXML takes 2 inputs – XML text, Xpath.
XML text is what WEBSERVICE has generated.
XPATH will tell Excel, which portion of XML to extract.
What is XPATH?
If you imagine XML as a tree, then XPATH is the language you use to tell how to navigate to a certain node in that tree. Since XPATH is a complex world, I think explaining all the syntax & nuances can be hard. So I will leave you with 2 useful links.
So what is the XPATH for nearby zip code.
As you can see in above image, the response from geonames has 10 code nodes, each containing one zip code (in the postalcode child node).
If we write =FILTERXML(b3,”/geonames/code/postalcode”) we will get all the postalcodes as an array.
Since Excel cannot show arrays in cells, it will show one of the 10 values.
So we need 10 cells to show these 10 zip codes. Once you have 10 cells, you can use either INDEX formula or alternative XPATH syntax (/geonames/code[1]/postalcode for first code, ../code[2]/.. for second code etc.) to extract all the 10 zip codes.
Things to keep in mind
Web formulas (WEBSERVICE formula to be specific) can be really slow depending on your net connection and webserver speeds. Since for most data, we do not need a live connection once the data is fetched, it would be good idea to replace WEBSERVICE formula with results once you have the XML.
Also, working with XPATH can be frustrating if the source XML is not correctly formatted or you are not familiar with right XPATH commands. In such cases, use SUBSTITUE or Text formulas to strip away un-necessary portions of webservice text before feeding it to FILTERXML.
Last but not least, Web formulas are compatible only with Excel 2013 or above. So you need to replace all formulas with results when emailing the workbooks to colleagues who are using older versions of Excel.
Download Example File – Finding Nearby Zipcodes
Click here to download the Excel workbook. Play with it to understand how the formulas are working. Please note that this file is protected as I do not want you to use my username for geonames.org.
Do you use Excel Web formulas?
Although Excel 2013 includes only 3 web formulas, they can let us do several interesting things. I am playing with them often to see what additional uses we can put them to.
What about you? Have you used Excel 2013 web formulas? What is your experience like? Please share using comments.
More on using Excel to get data from web
If you often need data from external websites for your Excel analysis work, check out below articles too:
30 Responses to “18 Tips to Make you an Excel Formatting Pro”
For my 2 cents worth:
Less is more !
Keep styles simple and in line with the corporate requirements of your employer/client
The table formatting is really useful, but I have found two sticky points:
1. Cannot move or copy a sheet with a table in it.
2. Cannot 'table format' multiple sheets at once.
May be ways around these issues, but these are what keep me from using the table format more than I already do.
Remove gridlines in sheet
Use dotted lines as internal borders in tables
And just keep it simple - it's the substance that matters and there's already way too much eye candy out there
I write a lot of financial reports conveying complex data in a userfriendly manner. I don't use colour (as it costs 7p/sheet verses B/W at 1p/sheet). The trick is to generate a table that someone will skim over for "the story" and then can refer back to understand it. very muck like Ulrik said, keep it simple.
Some simple guidelines that I use:
(a) align headings based on data (if data is text that means left, if data is numbers that means right)
(b) do not align central numbers (unless all similar) i.e. how hard is it to read a column of numbers that contains €1.25 and €125
(c) use borders to group columns and rows, don't format every line/column but allow the data to draw your eyes along it. "White lines" are as useful as borders
(d) thin borders are better than fat borders - the fatter they are, the more they draw the eye... so use them to draw attention to key numbers (like a total) only.
(e) use units to make numbers easier to read. Generally people cannot skim numbers with more than 3 d.p or 5 significant figures. so report in millions/thousands (or the other way as in ml)
(f) avoid making text too small or too big. too small (less than 10) and people can't read it. too big (>14) and people struggle to skim over it (their eyes have to move too much)
......I don’t use colour (as it costs 7p/sheet verses B/W at 1p/sheet).....
Not necessarily..
Don't compromise on how good a sheet can be made to look on monitor. To print black and white, simply configure in page setup to print in black and white.
Like This post !!
I m always using ALT + EST, not verymuch confirtable with cell style. will try to use color schemes (new feature)
Regards
!$T!
Hi Stephen,
Do you have some non-proprietary samples you may share on drop box or Windows Live SkyDrive?
Thanks
w
Great post!
Which key ist EST from the shortcut "ALT+EST".
I am using a german keyboard layout and have never heard something about an EST key.
Thanks
Carsten
Hi Carsten...
If you are using English version of Excel, then press ALT+E then leave the alt key, E key and then press S, then press T
For German version of Excel, the keys would be different. I am not sure what they are.
it was nice MS come up with all the color schemes. However, corporate culture (or your boss) sometimes dominate or predetermine what style a spreadsheet should look like. So I hardly get a chance to use #1 to #3 shown above.
Most of the times, it is someone else who wants a certain report or analysis gets to decide how s/he wants it to look like. I see myself more like a line chef or engineer. Others get to be the architect and I'm just a builder transforming a design into a real home. I don't get much say in it unless they are asking me to build a multistoried building on a single tooth pick as foundation.
Hi Chandoo,
thank you for your reply. Now I understand. It's something like searching for the ANY Key, because some program is displaying "Press any key to continue..."
But to find the german version of this shortcut:
ALT+E calls the Edit-menue? And for what are the S and T. Just tell me the english names of the menueitems, please.
I think then I will find it.
Carsten
@Carsten
Alt+EST is
(E)dit;
paste (S)pecial;
forma(T)s
Excellent post guys!
@Carsten,
Try to know how to find the shortcuts in the excel menu bar itself.
You click Alt + any of the underline character in the menu bar, then excel will take you to that particular menu field.
Now you can find different options in the dropdown menu. And each option has the name. Each name has underline in any of the characeter. That underline character is nothing but the shortcut key to execute that option.
Like this you can find in excel all the options and their shortcut keys.
Coming to the above example..
Once you click alt + E, it will take you to the "EDIT" drop down menu. Under Edit there are so many options like cuT, Copy, Paste, paste Special, fIll.... etc., I think you can find underline under 't' in cut..'p' in paste..'s' in paste Special. You need to click the underlined character for the required options...Here the 'S' underlines for Paste Special option...
Once you click 'S' it will open paste special options box...again you will find the same underlines in each of the names...here you can find different opetions like All, Formulas, Values, formaTs...etc. 'v' is nothing but Values option. Once you click V in the key board..it will execute paste special values option.
As Summary Alt + (E)dit + paste (S)pecial + (V)alues
Now you can find the shortcuts your own. all the best.
Regards,
Saran
lostinexcel.blogspot.com
You can also customize the quick access toolbar.. Once you find the icon you regularly use, right click and then select Add to quick access toolbar and once you are done, when you press Alt key it will be highlighted 1,2,3,4 etc depending upon the sequence of the icon..
Ctrl-ES is sooooo 2003.
Ctrl+Alt+V all the way baby!!!
You can DOUBLE-CLICK Format painter button to copy the formatting multiple times. Once you are done, press ESC key.
//
Jinesh,
This is a great tip that I use multiple times daily. People are always in awe when they see this one!
Jesse
Hi,
How to apply the custom styles for cells from the sql table, by using c# program.
Thanks & Regards,
Satheesh
[…] You can use the Page Layout section in Excel to apply colour themes to your reports. Chandoo.org has some useful Excel tips. […]
[…] http://chandoo.org/wp/2011/12/05/excel-formatting-tips/ […]
Hi i want to print a page which have bottom line to print on each page end how to do that pls explain
Thanks Sir
Thanks alot
Very useful thanks
thank you too much
your tips are awesome.
How to show a table with around 20-25 columns in the dashboard in the first page itself? I mean, within the dashboard area.
Is there anyway we can add a horizontal scroll bar for the table?
@Kiran
You never add tables directly to a dashboard
You add cells that reference a table
By reference I mean it gives you the ability via Formula or VBA to scroll up/down, Left/right or re-order the data
Think of it as a window into the table
This is discussed regularly in Chandoo's dashboard samples
Have a look at the 2 links in Item 1: http://chandoo.org/wp/welcome/
I'd then suggest asking a specific question in the Chandoo.org Forums and attach a sample file for a specific answer.
love it!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
I have a table of value for a month, with no data for few dates.
I created a chart basing on above data.
In the chart I find calendar dates, even though few dates with no data are not available in the table.
How to remove the dates in the chart for those without data?