Csv.Document()
从 同一文件中的指定工作表获取数据。我该怎么做?
这是我从光盘上的另一个文件获取数据的方法:
= Csv.Document(File.Contents("C:\Users\foo\OneDrive\Desktop\test.csv"),[Delimiter=",", Columns=1, Encoding=65001])
但我更喜欢将 CSV 内容粘贴到同一文件中的工作表中,然后使用 Power Query 获取其数据以将其放入数据模型中。原因是该文件是通过 OneDrive 共享的,因此对于打开该文件的不同用户来说,任何路径都不会相同。我想要粘贴到工作表中的 CSV 内容示例:
"balance_transaction_id,""created_utc""
"txn_4OD1SdHRzWob2tzd0LBUQkU0,""2023-11-16 09:02:08""
"txn_4OEexkHRzWob2tzd1h68iYud,""2023-11-20 21:25:01""
将文本粘贴到工作表中后,您可以使用
Data=>From Table/Range
,然后将高级编辑器中的代码替换为以下代码。如果您的文档并非全部采用相似的格式,则可能需要进行一些更改。
let
//change next line to reflect actual Table or Named Range
Source = Excel.CurrentWorkbook(){[Name="Table8"]}[Content],
//May or may not need this step depending on how you set up your table
#"Demoted Headers" = Table.DemoteHeaders(Source),
//Remove leading double quote and replace doubled double quotes with a single quote
#"Remove extra quotes" = Table.ReplaceValue(
#"Demoted Headers",
each [Column1],
null,
(x,y,z)=> Text.TrimStart(Text.Replace(y,"""""",""""),""""),
{"Column1"}),
#"Split Column by Delimiter" =
Table.SplitColumn(#"Remove extra quotes", "Column1",
Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Column1.1", "Column1.2"}),
#"Promoted Headers" = Table.PromoteHeaders(#"Split Column by Delimiter", [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"balance_transaction_id", type text}, {"created_utc", type datetime}})
in
#"Changed Type"