Power BI从嵌套记录值

时间:2017-10-17 03:00:47

标签: bing-maps powerbi powerquery m

我试图从List类型列中的一个记录创建一个新列。该值是与纬度和经度字段对应的国家/地区。这些信息是从Bing Map API中检索到的,该API使用从Web获取数据(遵循以下教程:https://sqldusty.com/2016/04/26/power-bi-and-the-bing-maps-api/)。

基本上我需要List.Record [1] .address.countryRegion。是否可以创建一个包含此特定值的列而不执行"展开到新行"?问题是一些列回到法国,行数增加到1000以上,但应该只有250左右。

enter image description here

enter image description here enter image description here

enter image description here

以下是我如何获得列表专栏:

1。从网络上获取数据

Get data from the web

2。使用基本选项并使用我的bing maps键粘贴了工作API请求。然后单击“确定”。

http://dev.virtualearth.net/REST/v1/Locations/point=40,-122?&key=BingMapsKey

enter image description here

3。导航到View>中的高级编辑器高级编辑。

enter image description here

4. 制作了一个使用纬度和经度作为输入的函数

let getCountry = (Latitude as text, Longitude as text) =>
let
    Source = Json.Document(Web.Contents("http://dev.virtualearth.net/REST/v1/Locations/point="& Latitude &","& Longitude &"?&key=BingMapsKey"))
in
    Source
in
    getCountry

enter image description here

5. 将该函数重命名为GetCountry,然后导航到所需的表以将列添加到(使用纬度和经度) enter image description here

6. 在目标表格中,导航到添加列>调用自定义函数 enter image description here

7. 从功能列表中选择GetCountry,将类型更改为列名,并将输入分配给相应的列名(纬度和经度)。然后单击确定。 enter image description here

8。该列显示在右侧。除了“资源集”之外,我过滤掉了所有列。因为它有地址值。

enter image description here

编辑我找到了一种减少请求中返回的列表数量的方法,即只将请求国家/地区作为查询参数:

http://dev.virtualearth.net/REST/v1/Locations/40,-122?key=BingMapsKey&includeEntityTypes=CountryRegion

这适用于我现在的需求,但也许最好保持开放状态,看看是否有人知道如何从嵌套表值中创建表格?谢谢你的帮助!

2 个答案:

答案 0 :(得分:2)

对我来说这听起来像XY Problem。让我试着澄清一下:

  1. Table.ExpandListColumn函数将记录扩展为多行,因为确实从API端点返回了多行记录。

    • 除非您之后应用过滤器逻辑,否则代码无法理解要选择哪一行记录。
  2. 不应该从API返回多行记录。 (查找给定countryRegion的{​​{1}})

  3. 因此,在阅读完问题之后,真正的问题在于您正在使用的API端点。

    应该是

    (lat, long)

    而不是

    http://dev.virtualearth.net/REST/v1/Locations/40,-122?key=yourAPIKey
    

    不需要http://dev.virtualearth.net/REST/v1/Locations/point=40,-122?key=yourAPIKey 。 (是的,documentation有点令人困惑)

    因此,您可以按如下方式更新point=功能:

    GetCountry

    (旁注:最好不要将API密钥公开给公开:))

    因此,每个地方应该只有一个let getCountry = (Latitude as text, Longitude as text) => let Source = Json.Document(Web.Contents("http://dev.virtualearth.net/REST/v1/Locations/"& Latitude &","& Longitude &"?key=yourAPIKey")) in Source in getCountry

    result

    我的查询供你参考:

    countryRegion

    希望有所帮助:)

答案 1 :(得分:0)

您是否使用参数来过滤所需的行? 看看那里:Get only the data I want from a db but keep structure