2016-12-21 50 views
0

我想將一個文件夾中的多個文本文件(每個文件包含兩列)導入到一個Excel表中,以便每個新文件從一個新的專欄開始。理想情況下,我需要第一個文件中的兩列以及每個其他文本文件中的第二列。 在powerquery中,我嘗試使用「從文件夾導入(導入文件夾中的文件的元數據和鏈接)」功能,隨後是查詢編輯器並擴展了二進制文件,結果是每個新文件都附加在前一個末尾一。但我希望每個文件在同一張表中開始一個新列,我不知道該怎麼做。Powerquery:如何從一個文件夾中的多個文本文件導入到同一張Excel表中的不同列中

我該如何直接使用powerquery來做到這一點? 在此先感謝您的幫助!

回答

1

我的建議包括通過高級編輯器添加的2個相當困難的步驟,但它對文件夾中的.txt文件數量是動態的。我添加了很多評論,所以它應該是自我解釋。

/* In this query, .txt files from a folder are combined. 
Each source file has 2 columns. 
The resulting table consists of both columns from the first file and each second column from the other files. 
Tables are joined using each first column as key and with a left outer join 
It is assumed that each file has column headers in the first row, that the first column header is the same for each file 
    and, preferably, the second column header differs per file, although this is not necessary. 
This query is tested with the following file contents: 

File1.txt: 
    ID,File1 
    1,A 
    2,B 
    3,C 
    4,D 

File2.txt: 
    ID,File2 
    1,W 
    2,X 
    3,Y 

Another file was added later on, to test for .txt files being added to the folder: works fine! 
*/  

let 
// Standard UI: 
Source = Folder.Files("C:\Users\Marcel\Documents\Forum bijdragen\StackOverflow Power Query\Multiple files in 1 folder"), 

// Standard UI; step renamed 
FilteredTxt = Table.SelectRows(Source, each [Extension] = ".txt"), 

// Standard UI; step renamed 
RemovedColumns = Table.RemoveColumns(FilteredTxt,{"Name", "Extension", "Date accessed", "Date modified", "Date created", "Attributes", "Folder Path"}), 

// UI add custom column "FileContents" with formula Csv.Document([Content]); step renamed 
AddedFileContents = Table.AddColumn(RemovedColumns, "FileContents", each Csv.Document([Content])), 

// Standard UI; step renamed 
RemovedBinaryContent = Table.RemoveColumns(AddedFileContents,{"Content"}), 

// In the next 3 steps, temporary names for the new columns are created ("Column2", "Column3", etcetera) 
// Standard UI: add custom Index column, start at 2, increment 1 
#"Added Index" = Table.AddIndexColumn(RemovedBinaryContent, "Index", 2, 1), 

// Standard UI: select Index column, Transform tab, Format, Add Prefix: "Column" 
#"Added Prefix" = Table.TransformColumns(#"Added Index", {{"Index", each "Column" & Text.From(_, "en-US"), type text}}), 

// Standard UI: 
#"Renamed Columns" = Table.RenameColumns(#"Added Prefix",{{"Index", "ColumnName"}}), 
// Now we have the names for the new columns 

// Advanced Editor: create a list with records with FileContents (tables) and ColumnNames (text) (1 list item (or record) per txt file in the folder) 
// From this list, the resulting table will be build in the next step. 
ListOfRecords = Table.ToRecords(#"Renamed Columns"), 

// Advanced Editor: use List.Accumulate to build the table with all columns, 
// starting with Column1 of the first file (Table.FromList(ListOfRecords{0}[FileContents][Column1], each {_}),) 
// adding Column2 of each file for all items in ListOfRecords. 
BuildTable = List.Accumulate(ListOfRecords, 
          Table.FromList(ListOfRecords{0}[FileContents][Column1], each {_}), 
          (TableSoFar,NewColumn) => 
           Table.ExpandTableColumn(Table.NestedJoin(TableSoFar, "Column1", NewColumn[FileContents], "Column1", "Dummy", JoinKind.LeftOuter), "Dummy", {"Column2"}, {NewColumn[ColumnName]})), 

// Standard UI 
#"Promoted Headers" = Table.PromoteHeaders(BuildTable) 

in 

    #"Promoted Headers" 
相關問題