How to Integrate Excel and Google Maps via PowerQuery

    Using the Google Maps API

    By Jeff Robson

    PowerQuery is indeed a powerful tool for pulling in data from all kinds of different data sources.

    More and more websites are making their data available via APIs (Application Programming Interfaces). These are essentially the back-end interfaces that allow programmers to provide all the lovely integration features we all take for granted!

    Using PowerQuery, you can now use Excel to get access to the data they make available.

    In this example, we show how to use PowerQuery to get the driving times between places via the Google Maps API.

    Free Download

    Step by Step

    Google Maps API documentation tells us that the syntax for queries is:

    http://maps.googleapis.com/maps/api/directions/output?parameters #sthash.QNnv1OZQ.dpuf

    Where output is either json or XML, depending on what format you want the results in, and parameters are any of the parameters listed in their documentation.

    PowerQuery > From Web

    googlemapsapi_01

    Then enter the following URL:

    http://maps.googleapis.com/maps/api/directions/xml?origin=perth+wa&destination=melbourne+vic

    googlemapsapi_02

    This produces a table which (so far) doesn’t look very useful! Click on the word Table in the route column to drill down.googlemapsapi_03

    We now need to click on the button highlighted below to choose which parts of the leg column we actually want in our results.

    googlemapsapi_04(1)

    I’ll just choose the duration for this example.

    googlemapsapi_05(1)

    Click OK to select duration, then Table in the leg column to drill into this column.

    googlemapsapi_06

    You should now see a table with one row and 2 columns. The value column shows the estimated driving time in seconds (great for calculating or charting), while the text column shows a much friendlier version of this number.

    Right-click on the value column and select Remove to drop this column since we won’t need it for our example.
    You should be left with something like this:

    googlemapsapi_07
    Click the Properties button and change the name to DrivingTime

    googlemapsapi_08

     

    Click the Advanced Editor button and change the code as highlighted below:

    let
        GetDrivingTime = (start as text, finish as text) =>
     
    let
        Source = Xml.Tables(Web.Contents(“http://maps.googleapis.com/maps/api/directions/xml?origin=“& start &”&destination=“& finish)),
        #”Changed Type” = Table.TransformColumnTypes(Source,{{“status”, type text}}),
        route = #”Changed Type”{0}[route],
        #”Changed Type1″ = Table.TransformColumnTypes(route,{{“summary”, type text}, {“copyrights”, type text}}),
        #”Expand leg” = Table.ExpandTableColumn(#”Changed Type1″, “leg”, {“duration”}, {“leg.duration”}),
        #”leg duration” = #”Expand leg”{0}[leg.duration],
        #”Changed Type2″ = Table.TransformColumnTypes(#”leg duration”,{{“value”, Int64.Type}, {“text”, type text}}),
        #”Removed Columns” = Table.RemoveColumns(#”Changed Type2″,{“value”})
    in
        #”Removed Columns”
    in
        GetDrivingTime

     

    googlemapsapi_09

    You’ve now created a new custom function in PowerQuery that can be applied to other tables.

    googlemapsapi_10

    Click the Green dropdown in the top left-hand corner and choose Close and Load. This will take you back to your workbook with your new function showing under Workbook Queries.
    Don’t worry if it says “Load is disabled.” It’s a function so it doesn’t load anything directly into your workbook.

    Next, create a table in your workbook with 3 cities you want to measure driving times between. For example:

    googlemapsapi_11

    Click a cell within the table, then click the From Table button to import your table of cities into PowerQuery.

    googlemapsapi_12

    Go to Add Column > Add Custom Column

    googlemapsapi_13

    Enter whatever name you want for your new column, then type the formula shown:

    =DrivingTime([Start], [Finish])
    This will use the values from the Start and Finish columns as inputs for your custom formula.

    googlemapsapi_14

    You may get asked about Privacy. If so, click Continue and make the query Public.

    You’ll then see a new Drive Time column and if you expand this, you can access the text column.

    googlemapsapi_15

    The finished product in PowerQuery:

    googlemapsapi_16

    And after you click the green button > Close and Load, you’ll see the same thing in Excel:

    googlemapsapi_17 (1)

    Voila! Perth is a REALLY long way from anywhere! Our closest capital city is 28 hours’ drive away!

    Warning: don’t try to do this with lots of data! Google specifically limits how many queries you can do per second, even for paying customers.