简体   繁体   English

如何将多列 excel 表转换为附有 header 的单列

[英]How to convert multi column excel table to single column with header attached

Hi can anyone let me know if there is a good way to convert table 1 to table 2 in excel?嗨,谁能告诉我在 excel 中是否有将表 1 转换为表 2 的好方法? this is just a example the actual data is in thousands of rows.这只是一个示例,实际数据有数千行。

表格1

表 2

Thank you!谢谢!

If you have Excel O365, you could opt for:如果您有 Excel O365,您可以选择:

Formula in E1 : E1中的公式:

=SORT(TRANSPOSE(CHOOSE({1,2},TRANSPOSE(FILTERXML("<t><s>"&TEXTJOIN("</s><s>",,A2:C4&"</s><s>"&A1:C1)&"</s></t>","//s[position() mod 2 = 1]")),TRANSPOSE(FILTERXML("<t><s>"&TEXTJOIN("</s><s>",,A2:C4&"</s><s>"&A1:C1)&"</s></t>","//s[position() mod 2 = 0]")))),1,1)

在此处输入图像描述

You can use Power Query, available in Excel 2010+您可以使用 Excel 2010+ 中提供的 Power Query

It is a part of Excel 2016+ and available as a free Microsoft provided add-in in the earlier versions.它是 Excel 2016+ 的一部分,在早期版本中作为免费的 Microsoft 提供的插件提供。

  • Data / Get & Transform / From Table/Range
  • If A , B and C are not the Headers then (In the Power Query UI)如果ABC不是Headers ,则(在 Power Query UI 中)
    • Home / Transform / Use First Row as Headers
  • Select all the columns and Select 所有列和
    • Transform / Any Column / Unpivot Columns
  • Sort by Attribute and Value , in that orderAttributeValue排序
  • Move the Value column to the first column positionValue列移动到第一列 position
  • Home / Close / Close & Load

在此处输入图像描述

All of the above steps can be done from the Power Query UI, but here is the generated M-Code上述所有步骤都可以从 Power Query UI 完成,但这里是生成的 M-Code

M-Code M-代码

let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"A", Int64.Type}, {"B", Int64.Type}, {"C", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {}, "Attribute", "Value"),
    #"Sorted Rows" = Table.Sort(#"Unpivoted Columns",{{"Attribute", Order.Ascending}, {"Value", Order.Ascending}}),
    #"Reordered Columns" = Table.ReorderColumns(#"Sorted Rows",{"Value", "Attribute"})
in
    #"Reordered Columns"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM