【发布时间】:2018-06-11 18:07:15
【问题描述】:
我有一个包含许多 CSV 文件的文件夹,其中前八列具有相同的标题,但前八列之后的列数量不同。我正在尝试使用 Excel 2016 中的查询和连接来完成此操作。
以下是CSV文件格式的示例(假设A-H中有字符串值):
----------------------------------------------------------------------------------------------------------------
| A | B | C | D | E | F | G | H | Company 1 | Value (1) | Company 2 | Value (2) | etc... |
----------------------------------------------------------------------------------------------------------------
| Product 1 | | | | | | | | .05 | 25.00 | .08 | 14.00 | |
----------------------------------------------------------------------------------------------------------------
| Product 2 | | | | | | | | .16 | 43.00 | .06 | 18.00 | |
----------------------------------------------------------------------------------------------------------------
再次重申:每个 CSV 文件的 A - H 列都相同,但每个文件的公司/值的数量不同(每个文件的公司名称不同)。
我已经为一个 CSV 文件完成了必要的步骤,并希望有某种方法可以将其用作其他文件的模板。
以下是我需要在 Excel 的查询编辑器中执行的步骤:
- 删除一些“A-H”列(这些标题匹配所有文件,应该不难)
- 删除所有“值 (#)”列 - 每个 CSV 文件中这些列的数量不同
- 取消透视所有“公司#”列(现在是匹配列 A - H 之后的每一列,因为在上一步中删除了“值 (#)”列)
- 将包含所有公司名称的列重命名为“公司”
- 将每个公司下之前包含所有值的列重命名为“成本”
以下是“高级查询编辑器”中的应用步骤:
#"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"A", type text}, {"B", type text}, {"C", type text}, {"D", type text}, {"E", type number}, {"F", type text}, {"G", Currency.Type}, {"H", Currency.Type}, {"Company 1", type text}, {"Value (1)", Currency.Type}, {"Company 2", type text}, {"Value (2)", Currency.Type}, {"Company 3", type text}, {"Value (3)", Currency.Type}, {"Company 4", type text}, {"Value (4)", Currency.Type}, {"Company 5", type text}, {"Value (5)", Currency.Type}, {"Company 6, type text}, {"Value (6)", Currency.Type}, {"Company 7", type text}, {"Value (7)", Currency.Type}, {"Company 8", type text}, {"Value (8)", Currency.Type}, {"Company 9", type text}, {"Value (9)", Currency.Type}}),
#"Removed Other Columns" = Table.SelectColumns(#"Changed Type",{"A", "C", "D", "E", "Company 1", "Company 2", "Company 3", "Company 4", "Company 5", "Company 6", "Company 7", "Company 8", "Company 9"}),
#"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Removed Other Columns", {"A", "C", "D", "E"}, "Attribute", "Value")
有没有什么方法可以导入 CSV 文件的文件夹并将上述步骤应用于每个文件,以便对所有 CSV 文件进行一次查询? (我希望它在下面看起来如何的简短示例)
----------------------------------------------------
| A | C | D | E | Company | Cost |
----------------------------------------------------
| Product 1 | | | | Company 1 | .05 |
----------------------------------------------------
| Product 1 | | | | Company 2 | .08 |
----------------------------------------------------
| Product 2 | | | | Company 1 | .16 |
----------------------------------------------------
| Product 2 | | | | Company 2 | .06 |
----------------------------------------------------
【问题讨论】:
标签: excel csv powerquery excel-2016