【问题标题】:Power Query read multiple ranges from multiple sheetsPower Query 从多个工作表中读取多个范围
【发布时间】:2021-09-09 04:28:06
【问题描述】:

我附上了“简单”的 excel 文件作为示例。每张纸上的三张纸我们有:一张,两张,三个有数据的四个范围(没有表格)。最初,该文件有 21 张不同范围数量的工作表,每张工作表上都有数据。所有表格的格式相同。我想要一张合并表。因此,如果在工作表 PL 中我们有三个范围,我希望它们一个接一个,然后从工作表 DE 我们有两个范围,我希望它们低于 PL 的最后一个条目,依此类推......

Example Excel file

正如您在“I”和“W”列的最后一张表 TOTAL 中看到的那样,我有这些表,但我想连接的不是列,而是那些我想在第 31 行之后的底部附加的范围。

【问题讨论】:

    标签: excel powerquery


    【解决方案1】:

    编辑:这将适用于没有表或命名范围的工作簿。

    1. 获取数据 > 从文件 > 从工作簿。选择工作簿名称,然后 选择转换数据

    2. 选择数据列。右键,删除其他列

    3. 单击双箭头展开列。取消选中“使用原始列名作为前缀”复选框。

    4. 主页选项卡 > 使用第一行作为标题

    5. 打开高级编辑器并添加这两个步骤:

      #"RemovedColumnList" = List.Select(Table.ColumnNames(#"Changed Type"), each Text.StartsWith(_, "Column")),
      #"RemovedColumns" = Table.RemoveColumns(#"Changed Type", RemovedColumnList),
      
    6. 将 ID 列过滤为不等于“ID”

    7. 转换选项卡 > 反透视列

    8. 选择属性列,从转换选项卡中选择提取。提取分隔符前的文本,使用“_”作为分隔符。

    9. 添加列>索引列>从1

    10. 打开高级编辑器,插入这一步:

    #"Replaced Value" = Table.ReplaceValue(#"Added Index",each [Index], each Number.RoundUp([Index]/5),Replacer.ReplaceValue,{"Index"}),
    
    1. 选择属性列。变换选项卡 > 透视列。为值列选择值。点击高级,从下拉列表中选择不聚合。
    2. 选择索引列并删除。

    完整的 M 代码:

    let
        Source = Excel.Workbook(File.Contents("C:\Users\Jody.Highroller\Documents\excel\Copy of ExampleFinal.xlsx"), null, true),
        #"Removed Other Columns" = Table.SelectColumns(Source,{"Data"}),
        #"Expanded Data" = Table.ExpandTableColumn(#"Removed Other Columns", "Data", {"Column1", "Column2", "Column3", "Column4", "Column5", "Column6", "Column7", "Column8", "Column9", "Column10", "Column11", "Column12", "Column13", "Column14", "Column15", "Column16", "Column17", "Column18", "Column19", "Column20", "Column21", "Column22", "Column23", "Column24", "Column25", "Column26", "Column27", "Column28", "Column29", "Column30", "Column31"}, {"Column1", "Column2", "Column3", "Column4", "Column5", "Column6", "Column7", "Column8", "Column9", "Column10", "Column11", "Column12", "Column13", "Column14", "Column15", "Column16", "Column17", "Column18", "Column19", "Column20", "Column21", "Column22", "Column23", "Column24", "Column25", "Column26", "Column27", "Column28", "Column29", "Column30", "Column31"}),
        #"Promoted Headers" = Table.PromoteHeaders(#"Expanded Data", [PromoteAllScalars=true]),
        #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"ID", type any}, {"Name", type text}, {"Status 1", type any}, {"Status 2", type any}, {"Collection", type text}, {"Column6", type any}, {"Column7", type any}, {"Column8", type any}, {"ID_1", type any}, {"Name_2", type text}, {"Status 1_3", type any}, {"Status 2_4", type any}, {"Collection_5", type text}, {"Column14", type any}, {"Column15", type any}, {"Column16", type any}, {"Column17", type any}, {"ID_6", type any}, {"Name_7", type text}, {"Status 1_8", type any}, {"Status 2_9", type any}, {"Collection_10", type text}, {"Column23", type any}, {"Column24", type any}, {"Column25", type any}, {"Column26", type any}, {"ID_11", Int64.Type}, {"Name_12", type text}, {"Status 1_13", Int64.Type}, {"Status 2_14", Int64.Type}, {"Collection_15", type text}}),
        RemovedColumnList = List.Select(Table.ColumnNames(#"Changed Type"), each Text.StartsWith(_, "Column")),
        #"RemovedColumns" = Table.RemoveColumns(#"Changed Type", RemovedColumnList),
        #"Filtered Rows" = Table.SelectRows(RemovedColumns, each ([ID] <> "ID")),
        #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Filtered Rows", {}, "Attribute", "Value"),
        #"Extracted Text Before Delimiter" = Table.TransformColumns(#"Unpivoted Columns", {{"Attribute", each Text.BeforeDelimiter(_, "_"), type text}}),
        #"Added Index" = Table.AddIndexColumn(#"Extracted Text Before Delimiter", "Index", 1, 1, Int64.Type),
        #"Replaced Value" = Table.ReplaceValue(#"Added Index",each [Index], each Number.RoundUp([Index]/5),Replacer.ReplaceValue,{"Index"}),
        #"Pivoted Column" = Table.Pivot(#"Replaced Value", List.Distinct(#"Replaced Value"[Attribute]), "Attribute", "Value"),
        #"Removed Columns" = Table.RemoveColumns(#"Pivoted Column",{"Index"})
    in
        #"Removed Columns"
    

    【讨论】:

    • 感谢您的回答。这个文件每个月都在变化。因此,手动执行该工作(将范围更改为名称或表格)不是一个好的选择,更改 21 张纸,每张纸有 2 到 4 个范围。我将这些范围作为表格 - 任务很明显。但我有我所拥有的:21 张纸,每张纸上有很多范围 - 没有表格或名称范围 :(
    • 简洁的解决方案。我认为最好先过滤源以仅选择工作表,不包括 TOTAL 工作表,以防在执行初始合并后更新一个或多个工作表(或添加命名范围)。此外,不需要删除空列,因为所有这些空值都在 unpivot 步骤中被删除。
    • 感谢@Patrick FitzGerald 的提示,它节省了使用 GUI 无法完成的两个步骤。我的假设是这将在一个单独的工作簿中运行,而不是数据所在的工作簿,但我同意你的观点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    相关资源
    最近更新 更多