【问题标题】:Copying excel query to power bi将excel查询复制到power bi
【发布时间】:2020-03-09 14:25:35
【问题描述】:

我正在尝试将 excel 查询复制到 PowerBI 查询中。 在excel中我有

let
    Source = Csv.Document(File.Contents(Excel.CurrentWorkbook(){[Name="Request"]}[Content]{2}[Path]),null,{0, 55, 62, 134, 145, 386, 398, 410},null,1200),
    #"Appended Query" = if Date.Month(Excel.CurrentWorkbook(){[Name="Request"]}[Content]{5}[start])=Date.Month(Excel.CurrentWorkbook(){[Name="CurrentDate"]}[Content]{0}[CurrentDate]) then Source else Table.Combine({Source, Csv.Document(File.Contents(Excel.CurrentWorkbook(){[Name="Request"]}[Content]{3}[Path]),null,{0, 55, 62, 134, 145, 386, 398, 410},null,1200)}), 

在 excel 中这一切正常,但在 Power BI 中出现错误。 (在查询中显示数据源的excel中显示数据源:X:...... .txt)我尝试将文件路径复制到代码中,但由于“日期”部分,我仍然遇到错误。

Expression.Error:我们找不到名为“请求”的 Excel 表。 细节: 请求。

【问题讨论】:

  • 用文件路径改变Excel.CurrentWorkbook(){[Name="Request"]Excel.CurrentWorkbook(){[Name="Request"]}[Content]{5}[start]会报错:Expression.Error: The Date value must contain the Date component.

标签: excel powerbi powerquery


【解决方案1】:

Excel.CurrentWorkbook() 部分表示它指的是 Excel 文件中的表格。如果你使用的是 Power BI,则没有当前工作簿。如果你想连接到 Excel 文件中的数据表,那么它看起来更像

Excel.Workbook(
    File.Contents("\filepath\filname.xlsx"),
    null, true){[Item="TableName",Kind="Table"]}[Data]

我建议使用 GUI 连接到源(主页 > 新源 > Excel)。

【讨论】:

  • 问题是该文件不是一个 excel 文件,而是一个普通的 .txt 文件(我无法更改它),当我使用 GUI 连接到它时,列都搞砸了。给我 2 列而不是 8 列我使用的分隔符。出于某种原因,它在 excel 中运行良好。
  • 好的。这是一个单独的问题。
  • 我的问题是我如何能够成功地更改具有实际源路径的“Excel.CurrentWorkbook 部分而不会丢失代码中的参数。
【解决方案2】:

如何加载名为.txt 的 Excel 工作表

问题是该文件不是excel文件,而是普通的.txt(我无法更改)

这仍然有效。 (扩展名实际上没有任何意义,或者改变文件的内容。它就像改变文件名一样。)

let  
    txt_filepath = "C:\Excel\worksheet.txt",
    worksheet_bytes = File.Contents( txt_filepath ),   
    Source = Excel.Workbook(worksheet_bytes, true),    
    FinalTable = Source{[Item="Sheet1",Kind="Sheet"]}[Data]
in
    FinalTable

WorkBook 参数(诗句函数)

"Excel.CurrentWorkbook 部分具有实际源路径,而不会在代码中丢失其参数

您可以使用Report/Worksheet Parameters。更改值很容易,无需接触 Power Query 本身。

你甚至可以让参数自动填充到文件中。

let
    /*
    this generates files to suggest for report parameter
    */
    Source = Folder_FilesFormatted( Folder.Files( #"Base Path Param" ) ),
    Path_Listing = List.Sort( Source[Full Path] )
in
    Path_Listing

【讨论】:

    猜你喜欢
    • 2021-07-06
    • 2023-03-18
    • 2022-08-12
    • 2019-05-12
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多