Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
ssmallik
New Member

How to connect a Python script to PowerBI slicer?

Hi All,

 

I am new to PowerBI and I need your help in acheiving the below workflow:

 

1. Create 2 report slicers (dropdowns): CountrySlicer and FeatureSlicer and allow user to selecta single value only from each

 

2. Load 2 datasets using Python scripts: DF1 and DF2

 

3. Already built a Python function in the following format:

 

 

 

def my_func(CountrySlicerSelectedVal, FeatureSlicerSelectedVal):
# Perform transformations on DF1 based on the value selected in CountrySlicer
# Perform transformations on DF2 based on the value selected in FeatureSlicer
# Return a single transformed data

 

 

 

4. Connect my_func() to the report slicers and store the function output as a separate data-table just like DF1 and DF2

 

I have successfully completed steps 1-3 but I am stuck at step 4, in no way I am unable to execute this Python function based on the slicer selections; ideally I want the function to run everytime the user updates the slicers and regenerate the output table (perhaps a Run macro will be ideal).

 

Can anyone help me in solving this?

 

1 ACCEPTED SOLUTION
v-junyant-msft
Community Support
Community Support

Hi @ssmallik ,

Power BI currently doesn't support triggering Python scripts directly from slicer selections in the way macros might work in other software. However, you can achieve a dynamic interaction between slicers and Python scripts through a workaround using Power Query and the M language.
First, you need to capture the slicer selections. This can be done by creating measures using the SELECTEDVALUE function in DAX for each of your slicers. For example:

SelectedCountry = SELECTEDVALUE('Table'[CountrySlicer])
SelectedFeature = SELECTEDVALUE('Table'[FeatureSlicer])


Next, you'll use Power Query to invoke your Python script with parameters. Unfortunately, Power Query doesn't directly support passing dynamic parameters (like slicer selections) to Python scripts. As a workaround, you can create a parameter table in Power BI that gets its values from the slicer selections (using the measures you created) and then load this table into Power Query.


Since Power BI doesn't automatically trigger Python scripts based on slicer changes, you'll need to manually refresh the data to see updates. This can be done by refreshing your report, or consider publishing your report to the Power BI service, where you can schedule automatic refreshes.

Best Regards,
Dino Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
skai
Frequent Visitor

Any luck on this?

v-junyant-msft
Community Support
Community Support

Hi @ssmallik ,

Power BI currently doesn't support triggering Python scripts directly from slicer selections in the way macros might work in other software. However, you can achieve a dynamic interaction between slicers and Python scripts through a workaround using Power Query and the M language.
First, you need to capture the slicer selections. This can be done by creating measures using the SELECTEDVALUE function in DAX for each of your slicers. For example:

SelectedCountry = SELECTEDVALUE('Table'[CountrySlicer])
SelectedFeature = SELECTEDVALUE('Table'[FeatureSlicer])


Next, you'll use Power Query to invoke your Python script with parameters. Unfortunately, Power Query doesn't directly support passing dynamic parameters (like slicer selections) to Python scripts. As a workaround, you can create a parameter table in Power BI that gets its values from the slicer selections (using the measures you created) and then load this table into Power Query.


Since Power BI doesn't automatically trigger Python scripts based on slicer changes, you'll need to manually refresh the data to see updates. This can be done by refreshing your report, or consider publishing your report to the Power BI service, where you can schedule automatic refreshes.

Best Regards,
Dino Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thanks @v-junyant-msft for providing me the workaround. I am able to create a measure that stores the selected value from the slicers.

Also I have created a Parameter table from Power Query Editor > Manage Parmaters > New and populated a parameter with the respective fields. Now I have 2 follow-up questions:

a. How do I map the Paramter Table and the slicer selected value?
b. How do I pass this info to the python script? COuld you share with me some doc on how to call this in Power query? Will be really helpful if you can provide me a sample code.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.