如何调用自定义函数以应用于具有不同列范围的查询文件?

时间:2018-09-12 14:37:12

标签: excel powerbi powerquery

我一直遵循this tutorial从文件夹中导入文件并清理它们。但是,对于该示例,每个.xls文件都具有相同数量的列。就我而言,(清洁后)我有这个东西:

| Col1 | Col2 |
|------|------|
| 1    | 3    |
| 4    | 2    |

+------+------+------+
| Col1 | Col2 | Col3 |
+------+------+------+
|    3 |    4 |    8 |
|    4 |    7 |    1 |
+------+------+------+

我想得到这个结果:

+------+------+------+
| Col1 | Col2 | Col3 |
+------+------+------+
|    1 |    3 |    - |
|    4 |    2 |    - |
|    3 |    4 |    8 |
|    4 |    7 |    1 |
+------+------+------+

我的自定义函数看起来像这样(将列替换为etc,因为它们几乎是2000)。我想我必须同时删除两个#"Changed Type"步骤,但是如何删除不需要的行呢?

(ExcelFile) =>
let
Source = Excel.Workbook(ExcelFile, null, true),
#"CMG Barras pesos_Sheet" = Source{[Item="CMG Barras pesos",Kind="Sheet"]}[Data],
#"Changed Type" = Table.TransformColumnTypes(#"CMG Barras pesos_Sheet",{"Column1", type any}, etc)
#"Removed Top Rows" = Table.Skip(#"Changed Type",8),
#"Promoted Headers" = Table.PromoteHeaders(#"Removed Top Rows", [PromoteAllScalars=true]),
#"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Barra", type any}, etc),
#"Removed Top Rows1" = Table.Skip(#"Changed Type1",1)
in
#"Removed Top Rows1"

1 个答案:

答案 0 :(得分:0)

"#Changed Type"步骤是函数中唯一单独引用列的步骤。其他人只是删除(跳过)表中的几行并将行提升为列名。

有多种方法可以处理可变数量的列,但就您而言,我认为您不需要做任何花哨的事情。只需执行以下步骤:

    (ExcelFile) =>
let
    Source = Excel.Workbook(ExcelFile, null, true),
    #"CMG Barras pesos_Sheet" = Source{[Item="CMG Barras pesos",Kind="Sheet"]}[Data],
    #"Removed Top Rows" = Table.Skip(#"CMG Barras pesos_Sheet",8),
    #"Promoted Headers" = Table.PromoteHeaders(#"Removed Top Rows", [PromoteAllScalars=true]),
    #"Removed Top Rows1" = Table.Skip(#"Promoted Headers",1)
in
    #"Removed Top Rows1"