英文:
Power Query Add Conditional Column
问题
假设我有3个国家:
国家 |
---|
美国 |
英国 |
日本 |
而且每个国家都有他们著名的城市,例如:
美国 - 纽约,西雅图,迈阿密
英国 - 伦敦,伯明翰
日本 - 东京,大阪
现在我想要做的是根据国家添加一个条件列,并列出每个国家的城市:
国家 | 城市 |
---|---|
美国 | 纽约 |
美国 | 西雅图 |
美国 | 迈阿密 |
英国 | 伦敦 |
英国 | 伯明翰 |
日本 | 东京 |
日本 | 大阪 |
我尝试了一些方法,但我认为它是错误的:
let
//反映实际数据源
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"更改类型" = Table.TransformColumnTypes(Source,{{"Country", type text}}),
//添加带有城市的列
#"添加城市" = Table.AddColumn(#"更改类型", "City",
each if [Country] = "US" then {"New York","Seattle","Miami"}
else if [Country] = "UK" then {"London","Birmingham"}
else {"Tokyo","Osaka"}),
//展开列表以创建新行
#"展开城市" = Table.ExpandListColumn(#"添加城市", "City")
in
#"展开城市"
英文:
Assume I have 3 countries:
Country |
---|
US |
UK |
JP |
And each of them has their famous cities for example:
US - New York, Seattle, Miami
UK - London, Birmingham
JP - Tokyo, Osaka
Now what I want to do is to add a conditional column while based on the country and list out the cities for each:
Country | City |
---|---|
US | New York |
US | Seattle |
US | Miami |
UK | London |
UK | Birmingham |
JP | Tokyo |
JP | Osaka |
I tried to do something like but I guess it's mistaken:
let
//reflect actual data source
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Country", type text}),
//Add columns with city
#"Add City" = Table.AddColumn(#"Changed Type", "City",
each if "Country" = "US" then each {"New York","Seattle","Miami"}
else if "Country" = "UK" then each {"London","Birmingham"}
else {"Tokyo","Osaka"}),
//Expand the lists to new rows
#"Expanded City" = Table.ExpandListColumn(#"Add City", "City")
in
#"Expanded City"
答案1
得分: 1
这将是您的代码的正确版本,主要区别在于使用[Country]代替"Country"。
let
//反映实际数据源
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"更改类型" = Table.TransformColumnTypes(Source,{{"Country", type text}}),
//添加具有城市的列
#"添加城市" = Table.AddColumn(#"更改类型", "City",
each if [Country] = "US" then {"New York","Seattle","Miami"}
else if [Country] = "UK" then {"London","Birmingham"}
else {"Tokyo","Osaka"}),
//展开列表为新行
#"展开城市" = Table.ExpandListColumn(#"添加城市", "City")
in #"展开城市"
尽管如此,这不是最佳方法。创建一个单独的表,使用左外连接在Country字段上执行合并。
然后展开City字段。
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"合并查询" = Table.NestedJoin(Source, {"Country"}, Table2, {"Country"}, "Table2", JoinKind.LeftOuter),
#"展开Table2" = Table.ExpandTableColumn(#"合并查询", "Table2", {"City"}, {"City"})
in #"展开Table2"
<details>
<summary>英文:</summary>
This would be the correct version of your code, the main difference being using [Country] in place of "Country"
let
//reflect actual data source
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Country", type text}}),
//Add columns with city
#"Add City" = Table.AddColumn(#"Changed Type", "City",
each if [Country] = "US" then {"New York","Seattle","Miami"}
else if [Country] = "UK" then {"London","Birmingham"}
else {"Tokyo","Osaka"}),
//Expand the lists to new rows
#"Expanded City" = Table.ExpandListColumn(#"Add City", "City")
in #"Expanded City"
That said, it is not the way to go. Create a separate Table, do a (home .. merge) merge using left outer join on the Country field
[![enter image description here][1]][1]
Then expand the City field
let Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Merged Queries" = Table.NestedJoin(Source, {"Country"}, Table2, {"Country"}, "Table2", JoinKind.LeftOuter),
#"Expanded Table2" = Table.ExpandTableColumn(#"Merged Queries", "Table2", {"City"}, {"City"})
in #"Expanded Table2"
[![enter image description here][2]][2]
[1]: https://i.stack.imgur.com/adarh.png
[2]: https://i.stack.imgur.com/Ena9G.png
</details>
# 答案2
**得分**: 1
// 添加城市列
#"添加城市" = Table.AddColumn(#"更改类型", "城市", each
if [国家] = "美国" then {"纽约","西雅图","迈阿密"}
else if [国家] = "英国" then {"伦敦","伯明翰"}
else {"东京","大阪"}),
<details>
<summary>英文:</summary>
Some syntax issues but close:
//Add columns with city
#"Add City" = Table.AddColumn(#"Changed Type", "City", each
if [Country] = "US" then {"New York","Seattle","Miami"}
else if [Country] = "UK" then {"London","Birmingham"}
else {"Tokyo","Osaka"}),
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论