2017-10-17 170 views
3

我试图从列表类型列中的一个记录中创建一个新列。该值是与纬度和经度字段相对应的国家/地区。这些信息是从Bing Map API获取的,该API使用从Web获取数据(在此教程后面:https://sqldusty.com/2016/04/26/power-bi-and-the-bing-maps-api/)。Power BI从嵌套记录值创建列

基本上我需要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

导航到在查看>高级编辑器高级编辑。

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 

这适用于我现在的需求,但也许这是一个好主意,保持这个打开看是否有人知道如何从嵌套表值创建表?感谢你的帮助!

+0

你能否详细说明如何从Bing的API和功能是什么,将调用到了你的数据获取记录列表?以一些示例数据为出发点,然后按照所有步骤获取数据很好。 – Joe

+0

@Joe我刚刚更新了问题,包括我如何从Bing API获取数据,并使用了函数 –

回答

2

这听起来像一个XY Problem给我。让我尝试澄清:

  1. Table.ExpandListColumn功能扩展记录多行因为确实从API端点返回的记录多行。

    • 除非您事后应用过滤器逻辑,否则代码无法理解要选择哪一行记录。
  2. 不应有从API返回的多行记录。 (查找countryRegion对于给定(lat, long)


所以通过问题看完之后,真正的问题在于你使用的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=是没有必要的。 (是的,documentation是稍显混乱)

所以,你可以按以下步骤更新您的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" 

希望它能帮助:)

+0

是的,这是问题所在!感谢您寻找并解决问题 - 完全是'问题x'的解决方案! –

+0

@DeniseMoran很高兴帮助!这就是为什么你在问题中包含所有细节以避免XY问题很酷的原因:) –

0

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

+0

它看起来并不像我正在寻找的那样。他们使用ID加入了表格,但在嵌套表格中没有与国家/地区字段相关联的纬度/经度。尽管感谢您的链接! –