简体   繁体   English

Excel 从宽格式转换为长格式的脚本或公式,但需要注意

[英]Excel script or formula to transform from wide to long format, with a caveat

For the purposes of simplicity, I'll say I have this table (the original table extends much wider, with let's say, up to 30 items, prices and amounts):为了简单起见,我会说我有这张表(原始表扩展得更宽,比方说,最多 30 个项目、价格和数量):

 Date| Requester|  Item1|  Amount1|  Price1|  Item2|  Amount2|  Price2|
12-10|         A|   Shoe|      200|      30|  Shirt|       40|      10|
12-10|         B|  Socks|       20|      10|       |         |        |
13-10|         A|       |         |        | Gloves|        5|       3|

And I need exactly this output:我正好需要这个 output:

 Date| Requester|   Item|   Amount|   Price|  
12-10|         A|   Shoe|      200|      30|  
12-10|         A|  Shirt|       40|      10| 
12-10|         B|  Socks|       20|      10|
13-10|         A| Gloves|        5|       3|

I've already come across some unpivot formulas that display the 3 variables in a single column called "variable", but that's not suitable for me, I'd like to reduce the number of columns, yes, but also keep the 3 key variables split in 3 columns.我已经遇到了一些在称为“变量”的列中显示 3 个变量的逆透视公式,但这不适合我,我想减少列数,是的,但也保留 3 个关键变量分成 3 列。 Also, since the original table has records by the thousands, I'd like the output not to create unnecesary rows for empty columns, or else it might crash or become unmanageable.此外,由于原始表有数千条记录,我希望 output 不要为空列创建不必要的行,否则它可能会崩溃或变得难以管理。 All scripts I've found create unnecesary rows for empty data.我发现的所有脚本都会为空数据创建不必要的行。 Note that In the example I posted, the output table doesn't print an unnecesary row if the item,amount and price data are empty.请注意,在我发布的示例中,如果项目、金额和价格数据为空,output 表不会打印不必要的行。

Thanks.谢谢。

You can do this with Power Query , available in Excel 2010+您可以使用Power Query执行此操作,可在 Excel 2010+ 中获得

Step through the "applied steps' window of the PQ Editor to better understand what is going on通过 PQ 编辑器的“应用步骤”window 来更好地了解发生了什么

  • Replace any blanks with null (So they will be excluded at the next step)null替换任何空白(因此它们将在下一步中被排除)
  • UNPIVOT the columns except for Date and Requestor UNPIVOT 除 Date 和 Requestor 之外的列
  • We now will want to group in bunches of three: Item, Amount, Price我们现在要将三个一组分组:项目、金额、价格
    • Add an Index column添加索引列
    • Add an Integer/Divide column based on the Index column which will result in a series like {0,0,0,1,1,1,2,2,2...}添加一个基于索引列的整数/除法列,这将导致类似{0,0,0,1,1,1,2,2,2...}的系列
  • GroupBy the Integer/Divide column GroupBy 整数/除法列
  • Extract the required data from Group table从 Group 表中提取所需数据
  • Rename the new columns and delete the unneeded columns.重命名新列并删除不需要的列。

M-Code M代码

let
    Source = Excel.CurrentWorkbook(){[Name="Table5"]}[Content],
    //Replace blanks with null
    #"Replaced Value" = Table.ReplaceValue(Source,"",null,Replacer.ReplaceValue,Table.ColumnNames(Source)),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Replaced Value", {"Date", "Requester"}, "Attribute", "Value"),
    #"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Attribute"}),
    #"Added Index" = Table.AddIndexColumn(#"Removed Columns", "Index", 0, 1, Int64.Type),
    #"Inserted Integer-Division" = Table.AddColumn(#"Added Index", "Integer-Division", each Number.IntegerDivide([Index], 3), Int64.Type),
    #"Removed Columns1" = Table.RemoveColumns(#"Inserted Integer-Division",{"Index"}),
    #"Grouped Rows" = Table.Group(#"Removed Columns1", {"Integer-Division"}, {{"Group", each _, type table [Date=nullable date, Requester=nullable text, Value=any, #"Integer-Division"=number]}}),
    #"Removed Columns2" = Table.RemoveColumns(#"Grouped Rows",{"Integer-Division"}),
    #"Added Custom" = Table.AddColumn(#"Removed Columns2", "Date", each Table.Column([Group],"Date"){0}),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Requester", each Table.Column([Group],"Requester"){0}),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Custom", each Table.Column([Group],"Value")),
    #"Extracted Values" = Table.TransformColumns(#"Added Custom2", {"Custom", each Text.Combine(List.Transform(_, Text.From), ";"), type text}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Extracted Values", "Custom", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), {"Custom.1", "Custom.2", "Custom.3"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Date", type date}, {"Custom.1", type text}, {"Custom.2", Int64.Type}, {"Custom.3", Currency.Type}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"Custom.1", "Item"}, {"Custom.2", "Amount"}, {"Custom.3", "Price"}}),
    #"Removed Columns3" = Table.RemoveColumns(#"Renamed Columns",{"Group"})
in
    #"Removed Columns3"

You should be able to add more "triplets" of data to any of the rows;您应该能够向任何行添加更多的“三元组”数据; or more rows;或更多行; without having to change the code.无需更改代码。

在此处输入图像描述

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

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