Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
donnellyk
Frequent Visitor

Using Google Matrix API to calculate distance between locations in my dataset

I have a dataset returning 77,000 records, each with both Buyer & Seller locations.
The Seller location is always in the UK
I'm trying to use the Google Matrix API to calculate distance between buyer & seller locations.
If the buyer location is in the UK, I want to calculate the distance between buyer & seller locations.
If the buyer locaion is not in the UK, I just want to calculate the distance between the seller location & a set UK location, e.g Southampton Shipping Port

 

I've created a function, as below, which returns distances fine
But when I Invoke this custom function as a column, I get problems:

Non UK buyer addresses return "null" as distance cannot be calculated by the API & I'm not sure how to just get the distance to southhampson for those
& also I get "Load Query1

OLE DB or ODBC error: [Expression.Error] The column 'Column1' of the table wasn't found.."

 

= (Origin as text, Destination as text)=>
let
    Source = Json.Document(Web.Contents("https://maps.googleapis.com/maps/api/distancematrix/json?destinations="&Destination&"&origins="&Origin&"&units=imperial&keyMYAPIKEY")),
    #"Converted to Table" = Table.FromRecords({Source}),
    #"Changed Type" = Table.TransformColumnTypes(#"Converted to Table",{{"destination_addresses", type any}, {"origin_addresses", type any}, {"rows", type any}, {"status", type text}}),
    rows = #"Changed Type"{0}[rows],
    #"Converted to Table1" = Table.FromList(rows, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table1", "Column1", {"elements"}, {"Column1.elements"}),
    #"Expanded Column1.elements" = Table.ExpandListColumn(#"Expanded Column1", "Column1.elements"),
    #"Expanded Column1.elements1" = Table.ExpandRecordColumn(#"Expanded Column1.elements", "Column1.elements", {"distance"}, {"Column1.elements.distance"}),
    #"Expanded Column1.elements.distance" = Table.ExpandRecordColumn(#"Expanded Column1.elements1", "Column1.elements.distance", {"value"}, {"Column1.elements.distance.value"})
in
    #"Expanded Column1.elements.distance"

 

1 ACCEPTED SOLUTION
donnellyk
Frequent Visitor

Thanks for your feedback, it helped point me in the right direction.
Basically, I ammended my SQL query so that if it was a non UK address, I returned the address of the UK port of entry & then the google matrix api was able to calculate all the distances for me

View solution in original post

4 REPLIES 4
donnellyk
Frequent Visitor

Thanks for your feedback, it helped point me in the right direction.
Basically, I ammended my SQL query so that if it was a non UK address, I returned the address of the UK port of entry & then the google matrix api was able to calculate all the distances for me

Hi @donnellyk ,

Glad to know that your issue is now resolved. Thanks for sharing your approach, and please continue to use the Fabric Community for any future questions or support.

Best Regards, 
Community Support Team

 

kushanNa
Super User
Super User

Hi @donnellyk 

 

From what I understand, your function works fine with UK buyers but has an issue with non-UK buyers. If that is the case, check it in an API testing tool like Postman and see what output you get when you input a UK buyer’s location and a non-UK buyer’s location. If the API returns null in that case for No UK buyer's, then this is likely a Google Maps-side issue.

GeraldGEmerick
Memorable Member
Memorable Member

@donnellyk You are getting the Column1 error because it is returning an empty table.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.