Power BI Pass Parameter Value to python Script Variable in get Data

Question:

I have a Power BI report that uses Python Script to get data.Is it possible to pass the parameter value give my the user as a variable in this python script?

In the below example I want the user to pass their user name and password as parameter into this script.

Sample Code

import requests

import pandas as pd

user = “UserName”

Pass = “Password”

….

Kindly advise. Thank You

Asked By: Marshall

||

Answers:

It is possible to pass a value to Python script.

In Power Query M language, Python.Execute is the connector function which invokes Python code. Although I cannot find any documentation, the description of Python.Execute would be described as below (resembling the official Microsoft documentation style).


Python.Execute

Syntax

Python.Execute(script as text, optional datasets as nullable record) as table

About

Executes Python code.

An optional datasets may be included to give variables in the namespace where the Python code is evaluated. Each field value of datasets must be a table. Field names will be bound to the keys of locals() dictionary, and field values will be converted to pandas DataFrames.

After evaluation of the the Python code, every member in locals() dictionary will be included in the returned table as long as its type is pandas DataFrame.

Example 1

let
    Source = Table.FromRecords({
        [Col1 = 10, Col2 = 3],
        [Col1 = 20, Col2 = 5]
    }),
    Script = 
"import pandas as pd
dataset['Col3'] = dataset['Col1'] + dataset['Col2']
result = dataset
",
    Result = Python.Execute(Script, [dataset = Source])
in
    Result{[Name = "result"]}[Value]
Col1  Col2  Col3
  10     3    13
  20     5    25

Therefore, returning back to the question, you can

  1. Put all the parameters into a table
  2. Wrap the table in a record
  3. Pass the record in the second argument of Python.Execute
Answered By: Kosuke Sakai

I had the same issue. It’s possible to pass a parameter to the Python script.

username = """ & UsernameParameter & """
password = """ & PasswordParameter & """

I hope it helps you!

Answered By: Volodya Senchak

To expand on the accepted answer a bit. Assume you pasted your Python script in with hard coded values for "user" and "Pass" and it tests OK. (Do this from Home, "Get Data" search Python Script, connect and paste.)

Next step is to create Power BI parameters for your variables.
Select Transform Data to bring up the "Power Query Editor". From Home, find Manage Parameters, select New Parameter and add one for "pUser". (This is my standard for naming parameters). Set the value as your actual username. Repeat for pPwd. You can edit these later from Manage Parameters.

Next, in the Power Query Editor, select View and from the ribbon select Advanced Editor. You will see your script converted to a single string, inside the PBI command, Source = Python.Execute(" ... ").
Now you can break this string and insert the parameters. For example, if your code had,

user = 'UserName'

change it to

user = '" & pUser & "'

Note that we leave the original quotes from the python code and just patch in the PBI parameter.
Repeat for Pass.

Note that if you prefer double quotes in your Python code, the string version will repeat them, eg "". Now your code would change from

user = ""UserName""

to

user = "" " & pUser & " ""

At runtime, PBI evaluates this string (like vBA) resolves the parameters and executes the python code.

I haven’t found any way to reference these variables in the code, like how you would pick up environment variables with os.environ.

Answered By: InnocentBystander