我有两个具有相同结构的数据集,类似
A 栏 | B 栏 | DS1 列 | C 栏 | D 栏 |
---|---|---|---|---|
细胞1 | 细胞2 | 电池 DS1a | 4 号电池 | 5 号小区 |
6 号电池 | 7 号细胞 | 电池 DS1b | 9号细胞 | 10 号电池 |
和
A 栏 | B 栏 | DS2 列 | C 栏 | D 栏 |
---|---|---|---|---|
细胞1 | 细胞2 | 电池 DS2a | 4 号电池 | 5 号小区 |
11 号小区 | 12 号电池 | 电池 DS2b | 13号小区 | 14 号电池 |
我已经合并了它们(完整的外部)并且我已经得到了
| Column A | Column B | Column DS1 | Column C | Column D | DS2.Column A | DS2.Column B | DS2.Column DS2 | DS2.Column C | DS2.Column D |
但我希望最终得到一张看起来像这样的桌子
A 栏 | B 栏 | DS1 列 | DS2 列 | C 栏 | D 栏 |
---|---|---|---|---|---|
细胞1 | 细胞2 | 电池 DS1a | 电池 DS2a | 4 号电池 | 5 号小区 |
6 号电池 | 7 号细胞 | 电池 DS1b | 空 | 9号细胞 | 10 号电池 |
11 号小区 | 12 号电池 | 空 | 电池 DS2b | 13号小区 | 14 号电池 |
我怎样才能实现这一目标?
我不确定你的例子有多现实,但使用你发布的数据,
Column DS2
添加到最终表中。此代码与您发布的数据配合使用来实现此目的:
阅读代码注释并探索
Applied Steps
以更好地理解算法
let
//Read in the tables
Source1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
colNames1 = Table.ColumnNames(Source1),
#"Changed Type1" = Table.TransformColumnTypes(Source1,List.Transform(colNames1, each {_, type text})),
Source2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
colNames2 = Table.ColumnNames(Source2),
#"Changed Type2" = Table.TransformColumnTypes(Source2,List.Transform(colNames2, each {_, type text})),
//We will do the join using the column names that are common to both tables
commonColNames=List.Intersect({colNames1,colNames2}),
Join = Table.NestedJoin(#"Changed Type1",commonColNames,#"Changed Type2",commonColNames,"Joined",JoinKind.FullOuter),
//Replace the unmatched (nulls) in the left table with the values from the right table
#"Replace Nulls" = Table.TransformRows(Join, (r)=>
Record.TransformFields(r, List.Transform(commonColNames, (cn)=>{cn, each if _ =null
then Table.Column(r[Joined], cn){0} else _}))),
//Add Column DS2 to the records
#"Add DS2" = List.Accumulate(
#"Replace Nulls",
{},
(s,c)=> s & {Record.AddField(c,"Column DS2", Table.Column(c[Joined],"Column DS2"){0})}),
//Convert the list of records to a table and expand it
#"Converted to Table" = Table.FromList(#"Add DS2", Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", List.Distinct(List.Combine({colNames1,colNames2}))),
//Set the desired column order
#"Reordered Columns" = Table.ReorderColumns(#"Expanded Column1",{"Column A", "Column B", "Column DS1", "Column DS2", "Column C", "Column D"}),
//Add a column to sort on and then do the sort
#"Added Custom" = Table.AddColumn(#"Reordered Columns", "SortOn", each Number.From(Text.Split([Column A]," "){1}), type number),
#"Sorted Rows" = Table.Sort(#"Added Custom",{{"SortOn", Order.Ascending}}),
//Remove the column we used for sorting
#"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"SortOn"}),
//Set the data types for the columns
#"Type Columns" = Table.TransformColumnTypes(#"Removed Columns", List.Transform(Table.ColumnNames(#"Removed Columns"), each {_, type text}))
in
#"Type Columns"
感谢您的建议,但我发现......由于这些数据集是通过某些条件提取的更大数据集(我们称之为 MainDS)的子集,因此我在 MainDS 中添加了一个索引列,该索引列在其他结果数据集。然后,我附加从 MainDS 中提取的所有数据集,从索引列中删除重复项,并将 MainDS 合并(内连接)到附加的指向索引列的数据集以获得所需的输出。