如何清理power bi中的位置数据

How to clean location data in power bi

我目前有两个 table。我有一个 table,其中包含这样一个位置列表:

Zagreb (Croatia)

Seattle, WA, USA

New York City, NY

Kazakhstan, Almaty

我还有一个包含 20 万个城市的主列表:

Zagreb | Croatia

Seattle | USA

New York City | USA

Almaty | Kazakhstan

我想要的输出是向第一个 table 添加一个新列,如下所示:

Zagreb (Croatia) | Croatia

Seattle, WA, USA | USA

New York City, NY | USA

Kazakhstan, Almaty | Kazakhstan

这是从我无法控制数据质量的实时源更新的,因此任何解决方案都必须是动态的。

任何想法表示赞赏!

一种可能的方法是在第一个 table 中添加一个自定义列,用于搜索出现在第二个 table City 列中的任何城市的字符串。

 = Table.AddColumn(#"Changed Type", "City",
       (L) => List.Select(Cities[City], each Text.Contains(L[Location], _)))

这给出了匹配城市的列表。展开该列表以获得以下内容:

然后您可以与 Cities table 合并(匹配每个 table 的 City 列)以拉过 Country 列。


这是我在高级编辑器中查询的全文:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WikpML0pNUtBwLspPLMlM1FSK1YlWCk5NLCnJSdVRCHfUUQgNdgQL+qWWK0TmF2UrOGeWVOoo+EWCRb0TqxKzM4pLEvN0FBxzchNLKpViYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Location = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Location", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "City", (L) => List.Select(Cities[City], each Text.Contains(L[Location], _))),
    #"Expanded City" = Table.ExpandListColumn(#"Added Custom", "City"),
    #"Merged Queries" = Table.NestedJoin(#"Expanded City",{"City"},Cities,{"City"},"Cities",JoinKind.LeftOuter),
    #"Expanded Cities" = Table.ExpandTableColumn(#"Merged Queries", "Cities", {"Country"}, {"Country"})
in
    #"Expanded Cities"

将第一个 table 命名为 "location",包括 1 个名为 "location" 的列。 将第 2 个 table 命名为 "city",包括名为 "city" 和 "country" 的 2 列。 代码是:

let
    location = Excel.CurrentWorkbook(){[Name="location"]}[Content],
    city = Excel.CurrentWorkbook(){[Name="city"]}[Content],
    result = Table.AddColumn(location,"city",each Table.SelectRows(city,(x)=>Text.Contains([location],x[city]))[country]{0})
in
    result