Forum Discussion

ssmallik's avatar
ssmallik
New Member
2 years ago
Solved

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 valu...
  • Anonymous's avatar
    Anonymous
    2 years ago

    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.