代码之家  ›  专栏  ›  技术社区  ›  user4192303

Power BI从嵌套记录值创建列

  •  3
  • user4192303  · 技术社区  · 7 年前

    我正在尝试从列表类型列中的一条记录创建一个新列。该值是与纬度和经度字段相对应的国家。这些信息是从Bing地图API中检索的,该API是使用Get Data from Web获得的(遵循此处的教程: https://sqldusty.com/2016/04/26/power-bi-and-the-bing-maps-api/ ).

    基本上我需要列表。记录[1]。住址countryRegion。是否可以在不进行“扩展到新行”的情况下创建一个包含此特定值的列?问题是,一些列随法国返回,行数增加到1000多行,但应该只有250行左右。

    enter image description here

    enter image description here enter image description here

    enter image description here

    1. 从web获取数据

    Get data from the web

    使用了基本选项,并用bing地图键粘贴了一个位置的工作API请求。然后单击“确定”。

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

    enter image description here

    3. 导航到视图中的高级编辑器>高级编辑器。

    enter image description here

    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

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

    7. enter image description here

    8. 该列显示在右侧。我过滤掉了除“resourceSets”之外的所有列,因为其中包含地址值。

    enter image description here

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

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

    2 回复  |  直到 7 年前
        1
  •  2
  •   Foxan Ng    7 年前

    XY Problem 对我来说。让我试着澄清一下:

    1. 这个 Table.ExpandListColumn 函数将记录扩展到多行,因为确实存在从API端点返回的多行记录。

      • 除非随后应用过滤器逻辑,否则代码无法理解要选择哪一行记录。
    2. countryRegion 对于给定的 (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
    

    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密钥公开:)

    因此,应该只有一个 乡村地区

    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"
    

    希望有帮助:)

        2
  •  0
  •   Lowteast    7 年前

    是否使用了参数来筛选所需的行? Get only the data I want from a db but keep structure