BossRoyce
BossRoyce

Reputation: 211

How to get Current Working Directory of PowerBI in Python?

I need to import a file into PowerBI via Python. I need some way to make the file paths in the Python code be dynamic. Is there a way to reference the current working directory of the power bi file in python code? os.getcwd() isn't working in the python script editor of PowerBi.

Upvotes: 2

Views: 1715

Answers (1)

BossRoyce
BossRoyce

Reputation: 211

Found a round about solution for a semi-dynamic way to change file directory:

1) create a parameter in powerbi that has the file path to but not including the file eg: C:\Users\User\Documents\Folder\

2) Need to run your initial import job with static file references in your python code eg: C:\Users\User\Documents\Folder\file.xlsx

3) Once your python created table is in, go into the query editor > Advanced Editor

4) Now for every reference of the full file path we will break the python code and insert the PowerBI parameter object using " & Parameter & " eg: ...df = pd.read_excel(r'C:\Users\User\Documents\Folder\file.xlsx')... becomes: ...df = pd.read_excel(r'" & Parameter & "file.xlsx')...

This allows the end_user to be able to easily change the directory by just modifying the parameter, without having to touch any of the python code.

Upvotes: 2

Related Questions