简体   繁体   English

PowerQuery:如何从同一Excel工作表中单独列中的文件夹中导入多个文本文件

[英]Powerquery: how to import multiple text files from a folder in separate columns in the same excel sheet

I want to import multiple text files from a folder, each file containing two columns, into a single excel sheet, so that every new file starts in a new column. 我想从一个文件夹中导入多个文本文件,每个文件包含两列,将它们导入到一个Excel工作表中,以便每个新文件都从一个新列开始。 Ideally, I need the two columns from the first file and only the second column from every additional text file. 理想情况下,我需要第一个文件中的两列,而每个其他文本文件中只需要第二列。 In powerquery, I tried to use the "Import From Folder (Import metadata and links about files in a folder)" functionality followed by query editor and expanding the binaries and the result was that every new file was appended at the end of the previous one. 在powerquery中,我尝试使用“从文件夹导入(导入元数据和有关文件夹中文件的链接)”功能,然后使用查询编辑器并展开二进制文件,结果是每个新文件都附加在上一个文件的末尾。 But I want every file to start a new column in the same sheet and I don't know how to do that. 但是我希望每个文件都在同一张纸中开始一个新列,但我不知道该怎么做。

How can I direct powerquery to do that? 我该如何指示powerquery做到这一点? Thanks in advance for your help! 在此先感谢您的帮助!

My proposal includes 2 rather difficult steps added via the advanced editor, but it is dynamic with regard to the number of .txt files in the folder. 我的建议包括通过高级编辑器添加的2个相当困难的步骤,但是对于文件夹中.txt文件的数量,它是动态的。 I added a ton of comments so it should be self explanatory. 我添加了很多评论,所以应该可以自我解释。

/*  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"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在同一个Excel工作表中导入多个文本文件 - Import Multiple Text Files in the same excel Sheet powerquery:从同一表中的两个表所在的文件夹中的所有Excel文件导入和转换数据 - powerquery: importing & transforming data from all Excel files in a folder where two tables on the same sheet Excel-Powerquery:来自文件夹:多个文件的起始行未固定 - Excel - Powerquery : From folder : Multiple files with unfixed starting row 如何使用PowerQuery将文件夹中的多个Excel文件中的多个表缝合/合并(向左扩展)? - How to stitch/merge (LEFT OUTER JOIN) multiple tables together from multiple Excel files located in a folder using PowerQuery? 如何将多个文本文件导入单个Excel工作表的列 - How to import multiple text files into columns of single excel worksheet 如何从多个文件中提取相同的 excel 表? - How can I extract the same excel sheet from multiple files? 使用python从文件夹中的多个文本文件中提取特定值并将其存储在Excel工作表中 - Extracting specific value from multiple text files from folder using python and store it in Excel sheet 我可以将多个文本文件导入一个Excel工作表吗? - Can I import multiple text files into one excel sheet? 将文本文件导入Excel-每个文件中的行都放在同一行的单独列中 - Importing text files to Excel - lines from each file are placed in separate columns on same row 在 excel 的单独工作表中自动填充多列 - Auto populate multiple columns in separate sheet in excel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM