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

Join the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register now

Reply
Anonymous
Not applicable

Power BI Slicer as What-If parameter in python script

Hi all,

 

I have a Matplotlib plot in Power BI Python Script. I have created a sample dataset as below.

I am looking to apply a slicer instead of hard coding the line 

 

 

 

 

tol = 15 #default tolerance

 

 

 

 

in the python script below.

How can I get this to work. Thanks.

 

 

 

 

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

def dist(x,y):
    p1=np.array([0,0])
    p2=np.array([100,100])
    p3=np.array([x,y])
    return abs(np.cross(p2-p1,p3-p1)/np.linalg.norm(p2-p1))

# define X and y
np.random.seed(10)
x = np.arange(0,100)
y = np.random.randint(0,100,100)
df = pd.DataFrame(dict(x=x, y=y))

tol = 15 #default tolerance
max_dist = dist(0,tol)
df["within_boundary"] = df.apply(lambda row: dist(row["x"], row["y"])<=max_dist, axis=1)

# plot definition
fig = plt.figure(dpi=100,figsize=(8,8))
ax = fig.subplots(1)
plt.subplots_adjust(bottom=0.25)
plt.xticks(np.arange(0,101,10))
plt.yticks(np.arange(0,101,10))
plt.grid(True)
plt.xlim([0,100])
plt.ylim([0,100])

percentage = df['within_boundary'].sum() / len(x)
figtext = plt.figtext(0.5,0.01, f"{percentage:.1%} in Spec", ha="center", va="center", fontsize=18, bbox={"facecolor":"grey", "alpha":0.5})

p0 = ax.scatter(x,y,c=df["within_boundary"]) # points
p1 = ax.plot([0,100],[0,100],'-',color="red")  # middle line
p2, = ax.plot([0,100-tol],[tol,100],'--',color="black")  # upper line
p3, = ax.plot([tol,100],[0,100-tol],'--',color="black")  # lower line
plt.show()

 

 

@TomMartens 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Anonymous  ,

It depends on the purpose of your Python script.

You can either pass arguments to "Run Python Scripts" or to "Power BI Queries".

Passing command-line arguments

Let's suppose you run a Python script like explained in Run Python Scripts in Power BI Desktop - Power BI | Microsoft Docs or How to run Python Script on powerBI

Since the script is called like a regular program from a command-line there can also be passed command-line arguments to the script. Those arguments can then interpreted by your Python script as parameters.

Here are some similar case that you can refer to : Solved: Slicers as input for Python script - Microsoft Power BI Communit, powerbi - Pass variable value to a Python script via Power BI - Stack Overflow

 vbinbinyumsft_1-1665479390717.png

 

Best regards,
Community Support Team_ Binbin Yu
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

1 REPLY 1
Anonymous
Not applicable

Hi @Anonymous  ,

It depends on the purpose of your Python script.

You can either pass arguments to "Run Python Scripts" or to "Power BI Queries".

Passing command-line arguments

Let's suppose you run a Python script like explained in Run Python Scripts in Power BI Desktop - Power BI | Microsoft Docs or How to run Python Script on powerBI

Since the script is called like a regular program from a command-line there can also be passed command-line arguments to the script. Those arguments can then interpreted by your Python script as parameters.

Here are some similar case that you can refer to : Solved: Slicers as input for Python script - Microsoft Power BI Communit, powerbi - Pass variable value to a Python script via Power BI - Stack Overflow

 vbinbinyumsft_1-1665479390717.png

 

Best regards,
Community Support Team_ Binbin Yu
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

 

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

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

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.