根据列的值组合多行内的多列,直到值发生变化

根据列的值组合多行内的多列,直到值发生变化

参照附图中的样本数据,我想将多行中的日期、人员和注释合并为一行,每组组合值都有换行符。每个注释占用一行,名称和 ID 为空白,因此主行下的每一行都需要合并。请参阅附图了解数据和所需输出。

样本数据:

+-------+--------------+------------------+---------+-----------------------+
| Name  |      ID      |       Date       | Person  |         Notes         |
+-------+--------------+------------------+---------+-----------------------+
| Danny | UID11224512  | 11/5/2019        | Joe     | <p>Note for Danny</p> |
|       |              | 11/5/2019        | John    | <p>Note 2</p>         |
| Joe   | UID11224956  | 11/5/2019 18:54  | Someone | <p>Note 1</p>         |
|       |              | 11/5/2019 18:54  | Someone | <p>Note 2</p>         |
| Jane  | UID11224959  | 11/5/2019 18:54  | Danny   | <p>Note 1 </p>        |
|       |              | 11/5/2019 17:33  | Shawn   | <p>Note 3</p>         |
|       |              | 11/6/2019 18:54  | Jane    | <p>Note 2</p>         |
| Tom   | UID115466652 |                  |         |                       |
| Eric  | UID168998955 |                  |         |                       |
| Paula | UID166559885 |                  |         |                       |
| Frank | UID112249569 | 11/5/2019 18:54  | Someone | <p>Note 1</p>         |
|       |              | 11/6/2019 18:54  | Someone | <p>Note 2</p>         |
|       |              | 11/7/2019 18:54  | Someone | <p>Note 3</p>         |
|       |              | 11/8/2019 18:54  | Someone | <p>Note 4</p>         |
|       |              | 11/9/2019 18:54  | Someone | <p>Note 5</p>         |
|       |              | 11/10/2019 18:54 | Someone | <p>Note 6</p>         |
| Paul  | UID1665588   |                  |         |                       |
+-------+--------------+------------------+---------+-----------------------+

期望输出:

+-------+--------------+-----------------------------------+
| Name  |      ID      |               Notes               |
+-------+--------------+-----------------------------------+
| Danny | UID11224512  | "11/5/2019 (Joe) - Note for Danny |
|       |              | 11/5/2019 (John) - Note 2"        |
| Joe   | UID11224956  | "11/5/2019 (Someone) - Note 1     |
|       |              | 11/5/2019 (Someone) - Note 2"     |
| Jane  | UID11224959  | "11/5/2019 (Danny) - Note 1       |
|       |              | 11/5/2019 (Shawn) - Note 2        |
|       |              | 11/5/2019 (Jane) - Note 3"        |
| Tom   | UID115466652 |                                   |
| Eric  | UID168998955 |                                   |
| Paula | UID166559885 |                                   |
| Frank | UID112249569 | "11/5/2019 (Someone) - Note 1     |
|       |              | 11/6/2019 (Someone) - Note 2      |
|       |              | 11/7/2019 (Someone) - Note 3      |
|       |              | 11/8/2019 (Someone) - Note 4      |
|       |              | 11/9/2019 (Someone) - Note 5      |
|       |              | 11/10/2019 (Someone) - Note 6"    |
| Paul  | UID1665588   |                                   |
+-------+--------------+-----------------+---------+-----------------------------------+


    

样本数据:
样本数据

所需输出:
所需输出

答案1

一种方法是使用 Windows Excel 2010+ 和 Office 365 中提供的 Power Query。

  • 选择数据表中的某个单元格
    • Data => Get&Transform => from Table/Range
    • 在 Power Query UI 中:Home => Advanced Editor
    • 记下第 2 行的表名称
    • 将下面的 MCode 粘贴到窗口中
    • 将第 2 行中的表名称更改为您的实际表名称
    • 阅读代码、注释并检查Applied Steps窗口以了解发生了什么。

简要算法

  • Fill down名称和 ID 列的空白
  • Group按名称和 ID 列
  • 提取第一的日期、人物和备注,如您在单独的列中显示的那样
  • 将所有日期/人物/备注(以换行符分隔)合并到最后一列

M 代码
在下面的代码中,请确保将 Line2 中的表名称更改为您的实际表名称

let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"ID", type text}, {"Date", type datetime}, {"Person", type text}, {"Notes", type text}}),
    
//Fill down to fill in the blanks for Name and ID columns
    #"Filled Down" = Table.FillDown(#"Changed Type",{"Name", "ID"}),

//Group on Name and ID -- should be the same so this is a double check.
    #"Grouped Rows" = Table.Group(#"Filled Down", {"Name", "ID"}, {

//For each Name/ID
    //Extract first Date as a date string if it has no time component, otherwise leave it as a datetime 
        {"Date", each if DateTime.Time([Date]{0}) = #time(0,0,0)
                            then DateTime.ToText([Date]{0},"MM/dd/yy")
                            else [Date]{0}},
    //Extract first Person
        {"Person", each [Person]{0}},

    //extract first Note
        {"Notes.1", each [Notes]{0}},

    //Extract and combine, for each note, the Date, person, and note
    //  Then split the combination for each entry by linefeed.
        {"Notes.2", (t)=> Text.Combine(
                            List.Generate(() => [Count=1, 
                            dt =DateTime.ToText(t[Date]{0},"MM/dd/yyyy") & " (" & 
                                t[Person]{0} & 
                                ") - " & t[Notes]{0}],
                         each [Count] <= Table.RowCount(t), 
                         each [Count = [Count] + 1, dt = DateTime.ToText(t[Date]{[Count]},"MM/dd/yyyy") & 
                                    " (" & t[Person]{[Count]} & 
                                    ") - " & t[Notes]{[Count]}],
                         each [dt]),"#(lf)")
                    }
        })
in
    #"Grouped Rows"

数据
在此处输入图片描述

结果
在此处输入图片描述

在 Excel 中,您需要将Notes.2列格式化为Wrap Text并将Date列格式化为mm/dd/yyyy hh:mm在日期列中,没有时间的日期将作为文本字符串返回,因此不会具有相同的格式 - 正如您在问题中所显示的那样

答案2

确保从主页功能区为整个评论列设置“自动换行”。使用此公式将文本值与换行符组合在一起:

="comment1" & CHAR(10) & "Comment2"

如果要合并两个独立的表,可能值得研究 Excel 中的 Power Query。

相关内容