How to turn addresses into GPS coordinates for your Power BI maps?

Yesterday (2022-11-12) I saw the student presentations among the other lecturers of the MOME dashboard course. Almost all of the presentations used maps.

Maps with countries are not as cool or useful as looking at our business units on a more detailed, county, city or address level.
Let's talk about this with an example. This link contains an Excel file with all the general practitioner offices in Hungary with their exact addresses.
Why are longitude and latitude coordinates better than addresses?

Power BI maps are using the Bing Map API, you can set the data category of your columns to city, country, county, place, postal code or place and the API will try to place your locations to the right place on the maps. Do you even need longitude and latitude coordinates?

In this data source I have city, address, postal code fields, I have put them into their relevant data categories. I created a calculated column as country with "Hungary".
The measure that is driving the size of the bubbles on the map chart:
Nr of doctors = COUNTROWS('Table'). I have 3929 addresses.

Scenario 1:

By placing the street address on the location field we get this very much wrong result. Remember, every address in the data is in Hungary.

Scenario 2:

I created this calculated column to concatenate the location columns. I used Text.Combine to handle the null values.
The result is much better, only 8 locations were placed outside of Hungary.

How do I get GPS coordinates?

I am sure that there are free API-s such as this one but testing it with my home address the result was not exactly the same as what I see on Google Maps. I decided to use the Azure Map Search Services API, https://learn.microsoft.com/en-us/rest/api/maps/search/get-search-address?tabs=HTTP . Pricing:

GET https://atlas.microsoft.com/search/address/json?api-version=1.0&query=15127 NE 24th Street, Redmond, WA 98052

This video showed me how to use this request in Postman and the fact that I will need a key to put in the request as authentication. Postman is a tool to test API-s.

I could try it with my own Azure tenant that I have set-up for these testing purposes. I just logged into my Azure Portal and created an Azure Maps Account which gave me a shared key to put in the request.

https://atlas.microsoft.com/search/address/json?&subscription-key=xxxxxxxxxxxxxxxxxx&api-version=1.0&language=en-US&query=400 Broad St, Seattle, WA 98109

The API returns data in JSON format with metadata of the address such as municipality, score and match confidence besides the latitude and longitude coordinates.

Great, how do I run it for my 4 thousand data points?
I am sure that there are well-documented tutorials doing this via PowerShell or Python but I wanted to use Power BI.
Can I send a request like this in Power Query in a calculated column?

The Web.Contents function is exactly for this scenario. Since the result is in JSON format, I just wrap it in a Json.Document call.

The API_Address is the concatenated column described above.

Json.Document(
Web.Contents(
"https://atlas.microsoft.com/search/address/json?&subscription-key=xxxxxxxxxxxxxxxxxx&api-version=1.0&language=en-US&query=" & [API_Address]
))

result of the API calls

The match confidence score helps testing, if the score is very low then the result may not be accurate.
In my file the lowest score is 0.19 and the highest is 1. Expanding on the calculated column results in new rows as we receive various metadata about an address. To avoid duplicated rows, apply remove duplicates on the API_address column.

Scenario 3:

Scenario 3

Using the lat. and long. coordinates for the map chart, finally every data point is in Hungary. Using a slicer with the match confidence can help identifying the wrong results. Fortunately, only 85 of the 3929 addresses have lower than 0.5 matching confidence.

After you think that you can't further improve the results by changing the calculated columns, you should move the GPS data into a static source such as Excel or a database to avoid using the API. Try to only start calling the API after you finished with other changes in the query as every refresh triggers calling the API.
I would use DAX Studio to extract the data from the Power BI data model.
Disclaimer:
All responsibilities to check the estimated costs of using Azure services is the reader's responsibility. Seek help from seniors, consultants.
Be very careful to avoid unexpected costs!