在 "reading" 之前跳过 6 行进入高级查询

Skip 6 rows before "reading" into powerquery

我正在尝试自动生成一些基于 Netsuite(我们的 ERP 软件)的 CSV 导出的报告。这些文件永远不会正确地直接导入 PowerQuery,因为有 6 行是 "header" 行。这些 header 行没有正确数量的逗号,因此 PowerQuery 仅显示 1 列数据。我目前正在使用 Notepad++ 打开文件并删除这 6 行,然后将文件导入 PowerQuery。

有没有办法使用 PowerQuery 代码跳过前 6 行,以便正确读取 csv?下面是我正在处理的数据示例。

Fake Cocoa Company LLC
"Fake Cocoa Company, LLC (Consolidated)"
Sales Order Detail - Hotel - BH
"January 1, 2016 - December 31, 2016"

"Options: Show ZerosFilters: Customer/Project (equal to FCC - Hotel Hotel ), Validated Status (not equal to Cancelled, Closed )"
Document Number ,Date ,Ship To ,Item: Description (Sales) ,Quantity ,Validated Status ,Unit Price ,Aggregate Amount 
Sales Orders,,,,,,,
669,9/15/2016,Receiving - CCLV Hotel 2880 Some Place Blvd South Hotel Hotel Some Place CA 91089,100% Country Caf  Liquid Cocoa,5,Billed,.68,8.40
660,,,,,,,
,9/15/2016,Receiving - MAIN OCEAN Hotel 4300 Some Place Blvd SO Some Place CA 91089,100% Country Caf  Liquid Cocoa,10,Billed,.68,.80
,9/15/2016,Receiving - MAIN OCEAN Hotel 4300 Some Place Blvd SO Some Place CA 91089,Fake Cocoa Grand - Whole Bean 5/5LB,8,Billed,.80,.00
,9/15/2016,Receiving - MAIN OCEAN Hotel 4300 Some Place Blvd SO Some Place CA 91089,Fake Cocoa Grand 28/9oz,6,Billed,.54,.24
,9/15/2016,Receiving - MAIN OCEAN Hotel 4300 Some Place Blvd SO Some Place CA 91089,Fake Cocoa Grand 42/2oz,4,Billed,.32,.28
,9/15/2016,Receiving - MAIN OCEAN Hotel 4300 Some Place Blvd SO Some Place CA 91089,Fake Cocoa Caf - Whole Bean 5/5LB,2,Billed,.80,.00
Total - 660,,,,,,,"3.32"

table.skip随心所欲

第二个参数可以是数字(例如6)或条件(例如(#"Position of ""Options: Show ZerosFilters: Customer/Project (equal to FCC - Hotel Hotel ), Validated Status (not equal to Cancelled, Closed )""" + 1)

问题是您尝试在 Power Query "from CSV" 中导入 CSV 作为源。具有描述内容的第一行将打破自动转换。所以为了防止这种情况,你必须用另一种方式将文件导入PQ。这个问题在 Ken 的 Excelguru Blog 中有很好的描述(顺便说一句:我热烈推荐他的书)。

这里是代码:

let
    /* Get the raw line by line contents of the file, preventing PQ from interpreting it */
    fnRawFileContents = (fullpath as text) as table =>
    let
        Value = Table.FromList(Lines.FromBinary(File.Contents(fullpath)),Splitter.SplitByNothing())
    in Value,

    /* Use function to load file contents */
    Source = fnRawFileContents("D:\yourfile.csv"),
    #"Removed Top Rows" = Table.Skip(Source,6),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Removed Top Rows","Column1",Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv),{"Column1.1", "Column1.2", "Column1.3", "Column1.4", "Column1.5", "Column1.6", "Column1.7", "Column1.8"}),
    #"Changed Type" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Column1.1", type text}, {"Column1.2", type text}, {"Column1.3", type text}, {"Column1.4", type text}, {"Column1.5", type text}, {"Column1.6", type text}, {"Column1.7", type text}, {"Column1.8", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type")

in
    #"Promoted Headers"

使用本机 CSV PowerQuery 解析器

let
    file_path = "C:\your_path\csv.txt",
    file = File.Contents(file_path),
    src = Lines.FromBinary(file),
    skip = List.Skip(src,6),
    combine = Text.Combine(skip, "#(lf)"),
    csv = Csv.Document(combine),
    promote = Table.PromoteHeaders(csv)
in
    promote