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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
bejazep
Regular Visitor

Expand custom column nested in JSON record - API

I have a table with Latitude and Longitude fields and would like to retrieve the country associated with the latitude and longitudes from a column created by an invoked function using an API. I am using the Bing maps API to get the location information from the point provided, however the information is stored within nested tables and I'm not sure how to get the data into it's own column in the original table. Power BI is only allowing me to "Expand to new rows", which leads to multiple problems:

    1) when I expand rows there are 4 times the number of rows I started with, 

    2) some of the countries when fully expanded are incorrect (says France when the actual country is not), and 

    3) there are multiple rows for what was initially 1 row, so there is no way to identify the correct initial row or filter out the wrong rows (some locations might be in France so I cannot just filter by that).

 

I would like to make a column with the correct Country for the given set of coordinates, which seems to be located in the address table of the second Record of each coordinate pair's list. The other records hold an address in France for some reason.

When I try to "Expand to new rows", I get multiple rows for 1 row and France seems to always show up (the other Records have an address of France, so I assume that is what is expanded. It should only be Poland for the below coordinates).

1 ACCEPTED SOLUTION
v-chuncz-msft
Community Support
Community Support

@bejazep,

 

The following custom function works for me, you may have a look.

let
    LocationByPoint = (latitude as text, longitude as text) =>
let
    Source = Xml.Tables(Web.Contents("http://dev.virtualearth.net/REST/v1/Locations/" & latitude & "," & longitude & "?includeEntityTypes=countryRegion&o=xml&key=BingMapsKey")),
    ResourceSets = Source{0}[ResourceSets],
    ResourceSet = ResourceSets{0}[ResourceSet],
    Resources = ResourceSet{0}[Resources],
    Location = Resources{0}[Location]{0}[Name]
in
    Location
in
    LocationByPoint
Community Support Team _ Sam Zha
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

2 REPLIES 2
v-chuncz-msft
Community Support
Community Support

@bejazep,

 

The following custom function works for me, you may have a look.

let
    LocationByPoint = (latitude as text, longitude as text) =>
let
    Source = Xml.Tables(Web.Contents("http://dev.virtualearth.net/REST/v1/Locations/" & latitude & "," & longitude & "?includeEntityTypes=countryRegion&o=xml&key=BingMapsKey")),
    ResourceSets = Source{0}[ResourceSets],
    ResourceSet = ResourceSets{0}[ResourceSet],
    Resources = ResourceSet{0}[Resources],
    Location = Resources{0}[Location]{0}[Name]
in
    Location
in
    LocationByPoint
Community Support Team _ Sam Zha
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Yes, this works to expand the columns! Thank you so much for explaining how to get the nested values. However after getting the nested values, some of the values were incorrect (not due to your method, the returned data was just not uniform in the way I had initially thought). Some default countries showed up as France, which is incorrect, so the target record was not always the second index... there was a lot of variation.

 

I was able to get the correct country by modifying the Bing maps API request to only include the countryRegion field, and the request was incorrect syntax because I needed to remove "point=".

 

Thanks again for your help!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.