按 A 列值分组,转置 B 列、C 列行值为每个分组的 A 列值

Group by column A value, transpose column B, column C row values for each grouped column A value

这是在 Excel 2016 年。我有一个电子表格,其中每一行代表一个独特学生对两个问题“Qa”和“Qb”的回答。电子表格列是:“部分”(学生所在的 class 部分)、“Qa”和“Qb”。 因此,如果三个学生从同一个 class 部分回答,该部分将在“部分”下列出三次,每个学生的答案都在其他列中。

我想按部分分组并将每个问题的答案分散在单独列中的一行中。要创建的列数将默认为具有最多独特响应的部分

在这种情况下,10003 的响应数最多,所以我想得到以下最终结果。

我不知道如何进行。类似于按部分分组但在该组内调换行?

正如@ScottCraner 指出的那样,您可以使用 Power Query 获得所需的输出,在 Windows Excel 2010+ 和 Office 365 Excel[=17= 中可用]

  • Select 你原来的一些单元格 table
  • Data => Get&Transform => From Table/Range
  • 当 PQ UI 打开时,导航至 Home => Advanced Editor
  • 记下代码第 2 行中的 Table 名称。
  • 将现有代码替换为下面的M-代码
  • 将粘贴代码第 2 行中的 table 名称更改为您的“真实”table 名称
  • 检查任何评论,以及 Applied Steps window,以更好地理解算法和步骤

M码

let

//Change table name in next row to actual table name in workbook
    Source = Excel.CurrentWorkbook(){[Name="Table20"]}[Content],

//set data type
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Section", Int64.Type}, {"Qa", type text}, {"Qb", type text}}),

//Group by Section
//Add a 1-based Index column to each Group
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Section"}, {
        {"Row", each Table.AddIndexColumn(_,"Row",1,1)}}),

//Expand the grouped tables
    #"Expanded Row" = Table.ExpandTableColumn(#"Grouped Rows", "Row", {"Qa", "Qb", "Row"}, {"Qa", "Qb", "Row"}),

//Unpivot
//Merge Row and Attribute columns to create the q-number headers
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Expanded Row", {"Section", "Row"}, "Attribute", "Value"),
    #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Unpivoted Other Columns", 
        {{"Row", type text}}, "en-US"),{"Attribute", "Row"},
        Combiner.CombineTextByDelimiter("-", QuoteStyle.None),"Merged"),

//Pivot on the Sorted Merged column with no aggregation
    #"Pivoted Column" = Table.Pivot(#"Merged Columns", List.Sort(List.Distinct(#"Merged Columns"[Merged])), "Merged", "Value")
in
    #"Pivoted Column"

注意没有空栏(iow,没有 Qa-4)
如果您确实需要一个空列,请在开头插入一个步骤,用空白替换空值

let

//Change table name in next row to actual table name in workbook
    Source = Excel.CurrentWorkbook(){[Name="Table20"]}[Content],

//set data type
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Section", Int64.Type}, {"Qa", type text}, {"Qb", type text}}),

//if you really need a blank Qa column since you have four distinct Qb rows but only 3 Qa rows,
// then we insert the next line
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type",null,"",Replacer.ReplaceValue,{"Qa", "Qb"}),

//Group by Section
//Add a 1-based Index column to each Group
    #"Grouped Rows" = Table.Group(#"Replaced Value", {"Section"}, {
        {"Row", each Table.AddIndexColumn(_,"Row",1,1)}}),

//Expand the grouped tables
    #"Expanded Row" = Table.ExpandTableColumn(#"Grouped Rows", "Row", {"Qa", "Qb", "Row"}, {"Qa", "Qb", "Row"}),

//Unpivot
//Merge Row and Attribute columns to create the q-number headers
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Expanded Row", {"Section", "Row"}, "Attribute", "Value"),
    #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Unpivoted Other Columns", 
        {{"Row", type text}}, "en-US"),{"Attribute", "Row"},
        Combiner.CombineTextByDelimiter("-", QuoteStyle.None),"Merged"),

//Pivot on the Sorted Merged column with no aggregation
    #"Pivoted Column" = Table.Pivot(#"Merged Columns", List.Sort(List.Distinct(#"Merged Columns"[Merged])), "Merged", "Value")
in
    #"Pivoted Column"