2017-10-17 174 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加入了表格,但在嵌套表格中沒有與國家/地區字段相關聯的緯度/經度。儘管感謝您的鏈接! –