Power BI从嵌套记录值创建列

4
我正在尝试从列表类型的列中创建一个新列。该值是与纬度和经度字段对应的国家/地区。该信息是通过Bing Map 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地图密钥的位置API请求。然后点击确定。

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

enter image description here

3. 进入“视图”>“高级编辑器”,即可进入高级编辑器。

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. 该列显示在右侧。 我过滤掉除'resourceSets'之外的所有列,因为它具有地址值。

enter image description here

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

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

这暂时符合我的需求,但也许保持开放状态以查看是否有人知道如何从嵌套表值中制作表格是个好主意?感谢您的帮助!

你能详细说明一下你是如何从Bing API获取数据的,以及你调用了哪个函数来获取记录列表吗?有一些样本数据作为起点,并跟随你获取数据的所有步骤会很好。 - Joe Gravelyn
@Joe,我刚刚更新了问题并包括了我使用的函数从Bing API获取数据的方法。 - user4192303
2个回答

2
这对我来说听起来像是一个 XY问题。让我来澄清一下:
  1. Table.ExpandListColumn 函数将记录展开为多行,因为从 API 端点确实返回了多行记录。

    • 除非您之后应用筛选逻辑,否则代码无法理解选择哪一行记录。
  2. API 不应该返回多行记录。(查找给定 (lat, long)countryRegion


阅读完问题后,真正的问题在于你使用的API端点。应该是:
http://dev.virtualearth.net/REST/v1/Locations/40,-122?key=yourAPIKey

代替;而不是
http://dev.virtualearth.net/REST/v1/Locations/point=40,-122?key=yourAPIKey

"point="不是必须的。(是的,文档有点令人困惑)
因此,您可以按以下方式更新GetCountry函数:
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

(旁注:最好不要将您的 API 密钥公开 :))
因此,每个地点应该只有一个“countryRegion”。

result

我的查询供您参考:
let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs/PT89JLchJrVDSUTI21zMxMjIwMACydQ2NjPQMLEwMTM2VYnWilRwLgIoUPPPSMvMyS1IVfPLzCyA6jI0NzczN4DqMDQwtLJViYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Place = _t, Lat = _t, Long = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Place", type text}}),
    #"Invoked Custom Function" = Table.AddColumn(#"Changed Type", "GetCountry", each GetCountry([Lat], [Long])),
    #"Expanded GetCountry" = Table.ExpandRecordColumn(#"Invoked Custom Function", "GetCountry", {"resourceSets"}, {"GetCountry.resourceSets"}),
    #"Expanded GetCountry.resourceSets" = Table.ExpandListColumn(#"Expanded GetCountry", "GetCountry.resourceSets"),
    #"Expanded GetCountry.resourceSets1" = Table.ExpandRecordColumn(#"Expanded GetCountry.resourceSets", "GetCountry.resourceSets", {"resources"}, {"resources"}),
    #"Expanded resources" = Table.ExpandListColumn(#"Expanded GetCountry.resourceSets1", "resources"),
    #"Expanded resources1" = Table.ExpandRecordColumn(#"Expanded resources", "resources", {"address"}, {"address"}),
    #"Expanded address" = Table.ExpandRecordColumn(#"Expanded resources1", "address", {"countryRegion"}, {"countryRegion"})
in
    #"Expanded address"

希望能帮上忙 :)

是的,那就是问题所在!感谢您的查找和解决——这正是解决“问题x”的方法! - user4192303
@DeniseMoran 很高兴能帮忙!你在问题中包含了所有细节以避免 XY 问题,这很棒 :) - Foxan Ng

0

看起来那不是我正在寻找的准确内容。他们使用ID连接了表格,但是在嵌套表格中的国家字段没有关联的纬度/经度信息。不过还是谢谢你提供的链接! - user4192303

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接