简体   繁体   中英

Copying excel query to power bi

I'm trying to copy excel query into PowerBI query. In excel I have

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)}), 

In excel this works all good but in Power BI I get error. ( In excel howering over the query it shows data source: X:...... .txt ) I tried copying the file path into the code but because of the "Date" section I still get errors.

Expression.Error: We couldn't find an Excel table named 'Request'. Details: Request.

The Excel.CurrentWorkbook() part means it's referring to a table within your Excel file. If you are using Power BI, there is no current workbook. If you want to connect to a data table in an Excel file, then it will look more like

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

I recommend using the GUI to connect to sources (Home > New Source > Excel).

How to load an Excel sheet named .txt

Problem is that the file is not an excel file but a plain .txt ( I can't change it )

That still works. (Extensions don't actually mean anything, or change the contents of files. it's just like changing the file name.)

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 paramaters (verses functions)

"Excel.CurrentWorkbook part with an actual source path without losing its parameters in the code

You can use Report/Worksheet Parameters . It's easy to change the values, without having to touch the Power Query itself.

在此处输入图片说明

You can even make the parameter automatically fill in files.

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

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM