Skip to content

Instantly share code, notes, and snippets.

@sjtalkar
Last active July 21, 2022 14:23
Show Gist options
  • Select an option

  • Save sjtalkar/87f235428f4b089de0a327548a0149cb to your computer and use it in GitHub Desktop.

Select an option

Save sjtalkar/87f235428f4b089de0a327548a0149cb to your computer and use it in GitHub Desktop.
Reading the current workbook filename Excel and Powe Query
Create two cells with names FilePath and FileName in Excel
FilePath cell has formula : G:\My Drive\BBSI\Round 2 PDFs\
FileName Cell has formula : =MID(CELL("filename",A2),FIND("[",CELL("filename",A2))+1,FIND("]", CELL("filename",A2))-FIND("[",CELL("filename",A2))-1)
Create a function name function_get_current_file in Power Query:
function_get_current_file
()=>
let
current_dir = Excel.CurrentWorkbook(){[Name="FilePath"]}[Content]{0}[Column1],
current_filename = Excel.CurrentWorkbook(){[Name="FileName"]}[Content]{0}[Column1],
full_path = current_dir & current_filename
in
full_path
// Reading a sheet from that file
function_read_user_payroll_file
()=>
let
full_file_path = function_get_current_file(),
Source = Excel.Workbook(File.Contents(full_file_path), null, true),
read_configure_sheet = Source{[Item="Configure",Kind="Sheet"]}[Data],
promote_headers = Table.PromoteHeaders(read_configure_sheet, [PromoteAllScalars=true]),
remove_current_file_details = Table.Skip(promote_headers,1),
merge_folder_file = Table.CombineColumns(remove_current_file_details,{"Location", "Filename"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"full_input_file_path")[full_input_file_path]{0}
in
merge_folder_file
In a query where you want to use the filename and path, call the function:
full_file_path = function_get_current_file(),
Source = Excel.Workbook(File.Contents(full_file_path), null, true),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment