1

我想按我的 ID 列组合两个表。两个表都有重复的 ID 值。

表格1:

ID         Reminder_date     Type
119128      05-Jan           Pear
11601368    07-Jan           Apple
119128      05-Jan           Apple
8445018     04-Jan           Pear
11601368    05-Jan           Grape
119128      04-Jan           Pear
11601368    14-Jan           Grape
5688401     14-Jan           Grape
119128      11-Jan           Pear
11601368    21-Jan           Pear
11045680    11-Jan           Orange

表 2:

ID           Purchase_date
11601368     12-Jan
11601368     13-Jan
11601368     11-Jan
119128       11-Jan
119128       29-Jan
8445018      24-Jan
8445018      11-Jan
5688401      28-Jan
11045680     10-Jan

我不能使用两列来使用索引匹配,因为它们没有两个公共列,我希望在我的最终表格中有更多行。

最终我想要:

ID         Reminder_date     Type   Purchase_date
119128      05-Jan           Pear         11-Jan
119128      05-Jan           Pear         29-Jan
11601368    07-Jan           Apple        12-Jan
11601368    07-Jan           Apple        13-Jan
11601368    07-Jan           Apple        11-Jan
119128      05-Jan           Apple        11-Jan
119128      05-Jan           Apple        29-Jan
8445018     04-Jan           Pear         24-Jan
8445018     04-Jan           Pear         11-Jan
11601368    05-Jan           Grape        12-Jan
11601368    05-Jan           Grape        13-Jan
11601368    05-Jan           Grape        11-Jan
119128      04-Jan           Pear         11-Jan
119128      04-Jan           Pear         29-Jan
11601368    14-Jan           Grape        11-Jan
11601368    14-Jan           Grape        13-Jan
11601368    14-Jan           Grape        12-Jan
5688401     14-Jan           Grape        28-Jan
119128      11-Jan           Pear         11-Jan
119128      11-Jan           Pear         29-Jan
11601368    21-Jan           Pear         12-Jan
11601368    21-Jan           Pear         13-Jan
11601368    21-Jan           Pear         11-Jan
11045680    11-Jan           Orange       10-Jan

11601368 12 个条目,119128 8 个条目,8445018 2 个条目,5688401 1 个条目,11045680 1 个条目

4

1 回答 1

2

您可以使用Power QueryWindows Excel 2010+ 和 Office 365 Excel中的 获得所需的输出

  • 在原始表格中选择一些单元格
  • Data => Get&Transform => From Table/Range或者From within sheet
  • 当 PQ UI 打开时,导航到Home => Advanced Editor
  • 记下代码第 2 行中的表名称。
  • 用下面的M代码替换现有代码
  • 将粘贴代码的第 2 行中的表名更改为您的“真实”表名
  • 检查任何评论和Applied Steps窗口,以更好地理解算法和步骤

M代码

let

//Be sure to change table name in Source lines to actual name in your workbook

//Read in Table 1 and set data types
    Source = Excel.CurrentWorkbook(){[Name="Tbl_1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Reminder_date", type date}, {"Type", type text}}),

//Add Index column to retain sort order
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),

//Read in Table 2 and set data types
    Source1 = Excel.CurrentWorkbook(){[Name="Tbl_2"]}[Content],
    #"Changed Type1" = Table.TransformColumnTypes(Source1,{{"ID", Int64.Type}, {"Purchase_date", type date}}),

//Join the tables with a FullOuter join
    joined = Table.NestedJoin(#"Added Index","ID",#"Changed Type1","ID","Joined",JoinKind.FullOuter),

//resort to original order
//then delete index column
    #"Sorted Rows" = Table.Sort(joined,{{"Index", Order.Ascending}}),
    #"Removed Columns" = Table.RemoveColumns(#"Sorted Rows",{"Index"}),

//expand the Purchase date column 
    #"Expanded Joined" = Table.ExpandTableColumn(#"Removed Columns", "Joined", {"Purchase_date"}, {"Purchase_date"})
in
    #"Expanded Joined"

Tbl_1 和 Tbl_2
在此处输入图像描述

结果
在此处输入图像描述

于 2022-02-17T22:14:56.367 回答