在 Power Query 中合并多个数据集

问题描述 投票:0回答:2

我有两个具有相同结构的数据集,类似

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 号电池

我怎样才能实现这一目标?

merge powerquery
2个回答
1
投票

我不确定你的例子有多现实,但使用你发布的数据,

  • 执行合并后,
  • 而不是扩展合并表,
  • 您需要将原始表中的空值替换为合并表中的匹配值
  • 然后将
    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"

表1

表2

合并表


0
投票

感谢您的建议,但我发现......由于这些数据集是通过某些条件提取的更大数据集(我们称之为 MainDS)的子集,因此我在 MainDS 中添加了一个索引列,该索引列在其他结果数据集。然后,我附加从 MainDS 中提取的所有数据集,从索引列中删除重复项,并将 MainDS 合并(内连接)到附加的指向索引列的数据集以获得所需的输出。

© www.soinside.com 2019 - 2024. All rights reserved.