在Excel Power查询中合并多个txt文件

时间:2019-09-18 11:52:28

标签: excel powerquery

我有500个.txt文件。他们只记录列标题之外的一列。 我想垂直连接/组合/合并它们,并删除列标题...如图所示。 Multiple txt files to single table/query

<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="table"></div>

我想像这样合并这些文件的最后只有一个csv / txt:

01.txt                 02.txt               03.txt

txt1                    txt2                  txt3   
date_1                 date_2               date_3
a  1                    a  3                 a  5
b  2                    b  4                 b  3 
c  3                    c  2                 c  1
d  4                    d  1                 d  4

谢谢。

1 个答案:

答案 0 :(得分:0)

最后尝试一下

使用Home ...高级编辑器...并将此函数粘贴到PowerQuery中。命名为:fnReadFile

(zFile)=>
let Source = Csv.Document(File.Contents(zFile),[Delimiter="#(tab)", Columns=1, Encoding=1252, QuoteStyle=QuoteStyle.None]),
#"Added Index" = Table.AddIndexColumn(Source, "Index", 0, 1),
// Date row includes text "Date: "
DateFind = Text.Replace(Table.FindText(#"Added Index" , "Date"){0}[Column1],"Date: ",""),
// Row preceeding data includes text ">>>"
CaratFind = Table.FindText(#"Added Index" , ">>>"){0}[Index],
FilterDate=Table.SelectRows(#"Added Index", each [Index] > CaratFind),
#"Added Custom" = Table.AddColumn(FilterDate, "Date", each DateFind),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Removed Columns", "Column1", Splitter.SplitTextByEachDelimiter({" "}, QuoteStyle.Csv, false), {"Column1.1", "Column1.2"}),
#"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Column1.1", type text}, {"Column1.2", Int64.Type}})
in #"Changed Type"

使用Home ...高级编辑器...,并将第一行更改为适当的目录后,将此查询粘贴到PowerQuery中。它将读取所有.txt文件,并根据上述功能根据需要将其组合

let Source = Folder.Files("C:\directory\subdirectory"),
#"Filtered Rows" = Table.SelectRows(Source, each ([Extension] = ".txt")),
#"Added Custom" = Table.AddColumn(#"Filtered Rows", "Custom", each fnReadFile([Folder Path]&"\"&[Name])),
#"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Column1.1", "Column1.2", "Date"}, {"Column1.1", "Column1.2", "Date"}),
#"Removed Other Columns" = Table.SelectColumns(#"Expanded Custom",{"Column1.1", "Column1.2", "Date"}),
#"Pivoted Column" = Table.Pivot(#"Removed Other Columns", List.Distinct(#"Removed Other Columns"[Date]), "Date", "Column1.2")
in  #"Pivoted Column"