这也可以使用 Windows Excel 2010+ 和 Excel 365(Windows 或 Mac)中提供的 Power Query 来完成
使用 Power Query
- 选择数据表中的某个单元格
Data => Get&Transform => from Table/Range
- 当 PQ 编辑器打开时:
Home => Advanced Editor
- 记下第 2 行中的表 Name
- 粘贴下面的 M 代码代替您看到的内容
- 将第 2 行中的表名称更改回最初生成的名称。
- 阅读 cmets 并探索
Applied Steps 以了解算法
M 代码
如果您的列名不是自动生成的,您可能需要将 Column1 和 Column2 编辑为实际的列名是(例如:Column1=>ColumnA)
let
//change Table name in next line to actual table name
Source = Excel.CurrentWorkbook(){[Name="Table7"]}[Content],
//set data types
#"Changed Type" = Table.TransformColumnTypes(Source,{
{"Column1", type text}, {"Column2", type text}}),
//for each entry in Column 1, create a List of all the entries in Column 2
//then remove column 2 and expand the custom column
#"Added Custom" = Table.AddColumn(#"Changed Type", "List of all in Column2",
each #"Changed Type"[Column2]),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Column2"}),
#"Expanded List of all in Column2" = Table.ExpandListColumn(
#"Removed Columns", "List of all in Column2"),
//remove the rows with nulls
// (happens if one column longer than the other
#"Filtered Rows" = Table.SelectRows(#"Expanded List of all in Column2",
each ([Column1] <> null) and ([List of all in Column2] <> null)),
//merge the two columns with space delimiter to get Results
#"Merged Columns" = Table.CombineColumns(#"Filtered Rows",
{"Column1", "List of all in Column2"},
Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"Result")
in
#"Merged Columns"