Power BI pass parameter value to python script
In Power BI Desktop with Query Editor we can perform advanced custom transformation using Python or R script. In this short post, I will demonstrate how we can in Power BI pass parameter value to Python script.
Table of Contents
Add a new column to a dataset with Python script
This basic line of code adds a new column “new_Column” to my dataset with the value “2” for every row.
dataset["new_Column"]=2
As we can observe from the data viewer our new column has been created with the value “2” for every single row.
Now let’s see how to pass a parameter in the Python script to create a new column based on a parameter.
Reference Power BI Parameter from Python Script
First, we need to create a parameter in Power BI.
I created “param1” which is a list of 10 digits ranging from 1 to 10.
Now I tried to reference the Power BI parameter using the following syntax ” & param1 & “.
And this is what my new column looks like, this is not really what we want so how can we make sure that Python reads my parameter?
The answer is we can’t! And I’m also asking the wrong question…
Power Query executes Python scripts using the function “python.execute()“. Now the question I should ask is simply how can we pass a parameter value into the power query function python.execute()?
Now the answer becomes quite simple; this function is expecting a text as an input (the Python script), and we already know how to add parameter value into a text with power query.
Power BI passes parameter value to Python script
All we need is to use ampersand “&” and double-quotes for the Text variable and the function “Text.From(value)” for variables that are not of type “text”.
Since the param1 type is a number I use the function Text.From() to pass this parameter to the Python script.
As we can see from the image above Power query recognized the parameter param1, now we can run the Python script and dynamically change the value assigned to the new column.
- Click on OK
- Apply changes
- Script Execution: “Run”
And here we are:
Pass multiple parameters to Python script
In the above example I’ve shown how to execute a Python script from PowerBI with one variable, now let’s pass two variables.
Let’s say that we want to add a new column based on the product of two parameters.
Just like the first example I use “&Text.From()&” to pass my two non-text variables to the Python script. This is pretty easy to pass as many variables as we want.
Pass text parameter to Python script
For text parameters, it is even simpler as we only need to wrap the parameter value with double-quotes like on the image above and then assign it like this in the Python script “& param4 &”.
To recap
- Passing parameters to Python or R script is fairly simple
- Once edited the Python script is transformed into an M script and it becomes difficult to edit.
- Does not work in Direct Query
To create visualizations using Python in Power BI you can refer to this post:
CREATE DYNAMIC POWER BI VISUALS BY USING PYTHON IN 1 MINUTE
And this post for R:
https://datakuity.com/2020/12/03/r-power-bi-add-target-marker-to-each-column-in-a-grouped-bar-chart/
9 thoughts on “Power BI pass parameter value to python script”
Love the python content.
Looking forward to more!
Thanks Chris! hopefully there will be more python/R to come
Hey can we take a input from the parameter and use it without it being associated with any columns ?
hi Meghavi,
Yes absolutely once you use the parameters with Power Qury (or pythin script) they are static values used during the refresh time and you may or may not associate them with a column.
How to associate the parameter value with the script then?
Hi Meghavi,
When you create the folder connection you should click on the left icon “ABC” and change it to parameter, make sure to use Text type for your parameter and not Binary.
Is it possible to make it so the end user can modify the parameter?
hi Ed,
Since it’s a Power Query parameter the user needs direct access to the dataset property in the portal so then he can refresh the dataset with the desired parameter so if granting such permission to an end user is OK with you it shouldn’t be an issue.
Otherwise a more complex approach would be to use power automate and the Power BI rest API to trigger and refresh the dataset with a parameter stored in an Excel File in SharePoint for example.