0

我正在尝试使用 PowerQuery M 完成以下任务

1) 打开一组文件(位于共享驱动器上的文件夹中)

2) 在名为“BC Ongoing Projects”的表格中获取数据

3)重新排列数据(主要是取消透视一组列(每个文件的列名更改)4)将它们附加在彼此下方

我已经尝试使用下面的代码,但它给了我一个错误“无法识别名称'ReOrganizedTable'。确保拼写正确。

let

//Get data from file in folder
Source = Folder.Files(Link_StaffDataLocation),
#"Get the files" = Table.SelectRows(Source, each [Extension] = ".csv" or [Extension] = ".txt" or Text.StartsWith([Extension], ".xls")),
#"Removed Columns" = Table.RemoveColumns(#"Get the files",{"Date accessed", "Date modified", "Date created", "Attributes", "Folder Path"}),
GetExcelContentIntoColumn = Table.AddColumn(#"Removed Columns", "Custom", each if Text.StartsWith([Extension], ".xls") then Excel.Workbook( [Content]) else null),
ExpandContent = Table.ExpandTableColumn(GetExcelContentIntoColumn, "Custom", {"Name", "Data", "Kind"}, {"Custom.Name", "Custom.Data", "Custom.Kind"}),
#"Filtered Rows" = Table.SelectRows(ExpandContent, each ([Custom.Name] = "BC Ongoing Projects") ),

//Create files name list
FileNameListStep1 = Table.Column(#"Filtered Rows","Name"),
FileNameListStep2 = List.Skip(FileNameListStep1,1),

//Filter on first filename
IterationFunction = (IterationList) =>
let
    CurrentFilenameList = List.Skip(IterationList,1),
    CurrentFilename = CurrentFilenameList{0},
    FilterOneDataSetAtATime = Table.SelectRows(#"Filtered Rows", each ([Name] = CurrentFilename)),
    // Make necessary adjustments to data
    #"Custom Data1" = #"Filtered Rows"{0}[Custom.Data],
    #"Filtered Rows1" = Table.SelectRows(#"Custom Data1", each ([Column1] <> null and [Column1] <> "BC Ongoing Projects" and [Column1] <> "Filters Used To Select These Practitioners:" and [Column1] <> "Pract" and [Column1] <> "Scheduled Hours" and [Column1] <> "Total:")),
    #"Removed Columns1" = Table.RemoveColumns(#"Filtered Rows1",{"Column5"}),
    #"Replaced Value_DK" = Table.ReplaceValue(#"Removed Columns1","DK","",Replacer.ReplaceText,{"Column4"}),
    #"Replaced Value_SE" = Table.ReplaceValue(#"Replaced Value_DK","SE","",Replacer.ReplaceText,{"Column4"}),
    #"Replaced Value" = Table.ReplaceValue(#"Replaced Value_SE",null,0,Replacer.ReplaceValue,{"Column6", "Column7", "Column8", "Column9", "Column10", "Column11", "Column12", "Column13", "Column14", "Column15", "Column16", "Column17"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Replaced Value",{{"Column6", type text}, {"Column7", type text}, {"Column8", type text}, {"Column9", type text}, {"Column10", type text}, {"Column11", type text}, {"Column12", type text}, {"Column13", type text}, {"Column14", type text}, {"Column15", type text}, {"Column16", type text}, {"Column17", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type"),
    //Create List of week dates    
    MondayInWeekStep1 = Table.ColumnNames(#"Promoted Headers"),
    MondayInWeekStep2 = List.Skip(MondayInWeekStep1,4),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Promoted Headers", {"Name", "Client", "Type", "Key"}, "MondayInWeek", "Staff Hours"),
    ReOrganizedTable = Table.TransformColumnTypes(#"Unpivoted Columns",{{"MondayInWeek", type date}, {"Staff Hours", Int64.Type}})
in
    ReOrganizedTable,
    AppendTablesTogether = Table.Combine(ReOrganizedTable,IterationFunction(CurrentFilenameList))
in
AppendTablesTogether

非常感谢你能给我的帮助

BR乔纳斯

4

2 回答 2

0

ReOrganziedTable只存在于IterationFunction你定义的。您可能想要执行以下操作:

  1. CurrentFilenameList步骤移到前一行IterationFunction
  2. 改为take IterationFunctioninCurrentFilename而不是IterationList. 换句话说,删除CurrentFilename步骤IterationFunction并更改(IterationList) =>(CurrentFilename) =>
  3. 用于List.Transform构建表格列表,然后将该列表转换为表格。该List.Transform部分应该做类似的事情List.Transform(FileNameListStep2, (filename) => IterationFunction(filename))
于 2016-11-02T18:21:22.200 回答
0

对于任何想要查看完成代码的人:

let

//Get data from file in folder
Source = Folder.Files(Link_StaffDataLocation),
#"Get the files" = Table.SelectRows(Source, each [Extension] = ".csv" or [Extension] = ".txt" or Text.StartsWith([Extension], ".xls")),
#"Removed Columns" = Table.RemoveColumns(#"Get the files",{"Date accessed", "Date modified", "Date created", "Attributes", "Folder Path"}),
GetExcelContentIntoColumn = Table.AddColumn(#"Removed Columns", "Custom", each if Text.StartsWith([Extension], ".xls") then Excel.Workbook( [Content]) else null),
ExpandContent = Table.ExpandTableColumn(GetExcelContentIntoColumn, "Custom", {"Name", "Data", "Kind"}, {"Custom.Name", "Custom.Data", "Custom.Kind"}),
#"Filtered Rows" = Table.SelectRows(ExpandContent, each ([Custom.Name] = "BC Ongoing Projects") ),

//Create files name list
FileNameListStep1 = Table.Column(#"Filtered Rows","Name"),
CurrentFilename = FileNameListStep1{0},

//Filter on first filename
IterationFunction = (CurrentFilename) =>
let
    FilterOneDataSetAtATime = Table.SelectRows(#"Filtered Rows", each ([Name] = CurrentFilename)),
    // Make necessary adjustments to data
    #"Custom Data1" = #"Filtered Rows"{0}[Custom.Data],
    #"Filtered Rows1" = Table.SelectRows(#"Custom Data1", each ([Column1] <> null and [Column1] <> "BC Ongoing Projects" and [Column1] <> "Filters Used To Select These Practitioners:" and [Column1] <> "Pract" and [Column1] <> "Scheduled Hours" and [Column1] <> "Total:")),
    #"Removed Columns1" = Table.RemoveColumns(#"Filtered Rows1",{"Column5"}),
    #"Replaced Value_DK" = Table.ReplaceValue(#"Removed Columns1","DK","",Replacer.ReplaceText,{"Column4"}),
    #"Replaced Value_SE" = Table.ReplaceValue(#"Replaced Value_DK","SE","",Replacer.ReplaceText,{"Column4"}),
    #"Replaced Value" = Table.ReplaceValue(#"Replaced Value_SE",null,0,Replacer.ReplaceValue,{"Column6", "Column7", "Column8", "Column9", "Column10", "Column11", "Column12", "Column13", "Column14", "Column15", "Column16", "Column17"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Replaced Value",{{"Column6", type text}, {"Column7", type text}, {"Column8", type text}, {"Column9", type text}, {"Column10", type text}, {"Column11", type text}, {"Column12", type text}, {"Column13", type text}, {"Column14", type text}, {"Column15", type text}, {"Column16", type text}, {"Column17", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type"),
    //Create List of week dates    
    MondayInWeekStep1 = Table.ColumnNames(#"Promoted Headers"),
    MondayInWeekStep2 = List.Skip(MondayInWeekStep1,4),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Promoted Headers", {"Name", "Client", "Type", "Key"}, "MondayInWeek", "Staff Hours"),
    ReOrganizedTable = Table.TransformColumnTypes(#"Unpivoted Columns",{{"MondayInWeek", type date}, {"Staff Hours", Int64.Type}}),
    AddFilenameColumn= Table.AddColumn(ReOrganizedTable, "FileName", each CurrentFilename)
in
    AddFilenameColumn,
    ListOfTables = List.Transform(FileNameListStep1, (filename) => IterationFunction(filename)),
    #"Converted to Table" = Table.FromList(ListOfTables, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Expanded Column1" = Table.ExpandTableColumn(#"Converted to Table", "Column1", {"Name", "Client", "Type", "Key", "MondayInWeek", "Staff Hours", "FileName"}, {"Name", "Client", "Type", "EmployeeNr", "MondayInWeek", "Staff Hours", "FileName"})
in
  #"Expanded Column1"
于 2016-11-03T10:46:35.893 回答