In Power BI, when you filter data, it affects the dataset being passed to the Python script, resulting in changes in the plotted graph. However, you can potentially work around this issue by passing all data points to the Python script and implementing the filtering within the script itself based on the filters applied in Power BI.
Here's how you might adjust your script to handle this:
here is modified code:
# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script:
# dataset = pandas.DataFrame(Name, Home Function, GRUE Category , Type of Move)
# dataset = dataset.drop_duplicates()
# Paste or type your script code here:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
def quadrant_chart(XXXXX_1, XXXXX_2, Name, TiR, TiS, TiG, xtick_labels=None, ytick_labels=None, ax=None):
# Ensure XXXXX_1 and XXXXX_2 have the same length
if len(XXXXX_1) != len(XXXXX_2):
raise ValueError("XXXXX_1 and XXXXX_2 must have the same length")
# Convert 'Yes' to 10 and 'NO' to 1 in both columns
XXXXX_1[XXXXX_1 == 'Yes'] = 10
XXXXX_1[XXXXX_1 == 'NO'] = 1
XXXXX_2[XXXXX_2 == 'Yes'] = 10
XXXXX_2[XXXXX_2 == 'NO'] = 1
# make the data easier to work with by putting it in a dataframe
data = pd.DataFrame({'XXXXX_1': XXXXX_1, 'XXXXX_2': XXXXX_2, 'Name': Name, 'TiR': TiR, 'TiS': TiS, 'TiG': TiG})
# Apply filtering based on criteria from Power BI filters
# Example: If 'Filter_Column' is a column in your dataset that is filtered in Power BI
# You can apply the filter like this:
# data = data[data['Filter_Column'] == 'Desired_Value']
# let the user specify their own axes
ax = ax if ax else plt.axes()
# rest of your plotting code...
# (unchanged from your original script)
# Assuming the data is already loaded into Power BI and accessible via the DataFrame 'dataset'
# Fetch relevant columns from the dataset
XXXXX_1 = dataset['XXXXX_1'].to_numpy()
XXXXX_2 = dataset['XXXXX_2'].to_numpy()
Name = dataset['Name'].to_list()
TiR = dataset['TiR'].to_numpy()
TiS = dataset['TiS'].to_numpy()
TiG = dataset['TiG'].to_numpy()
# Call quadrant_chart function with error handling
try:
quadrant_chart(
XXXXX_1=XXXXX_1,
XXXXX_2=XXXXX_2,
Name=Name,
TiR=TiR,
TiS=TiS,
TiG=TiG,
xtick_labels=['PROMOTION', 'LATERAL MOVE'],
ytick_labels=['GROW', 'EXPORT']
)
except ValueError as e:
print("Error:", e)
i hope it will work for you
Best regard
Danish hafeez | QA Assistant
Here's how you might adjust your script to handle this:
- Pass all the data points to Python script from Power BI.
Modify the quadrant_chart function to filter data based on the criteria specified in Power BI filters.
Ensure that your Power BI filters are set up to interact with the Python script properly.
here is modified code:
# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script:
# dataset = pandas.DataFrame(Name, Home Function, GRUE Category , Type of Move)
# dataset = dataset.drop_duplicates()
# Paste or type your script code here:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
def quadrant_chart(XXXXX_1, XXXXX_2, Name, TiR, TiS, TiG, xtick_labels=None, ytick_labels=None, ax=None):
# Ensure XXXXX_1 and XXXXX_2 have the same length
if len(XXXXX_1) != len(XXXXX_2):
raise ValueError("XXXXX_1 and XXXXX_2 must have the same length")
# Convert 'Yes' to 10 and 'NO' to 1 in both columns
XXXXX_1[XXXXX_1 == 'Yes'] = 10
XXXXX_1[XXXXX_1 == 'NO'] = 1
XXXXX_2[XXXXX_2 == 'Yes'] = 10
XXXXX_2[XXXXX_2 == 'NO'] = 1
# make the data easier to work with by putting it in a dataframe
data = pd.DataFrame({'XXXXX_1': XXXXX_1, 'XXXXX_2': XXXXX_2, 'Name': Name, 'TiR': TiR, 'TiS': TiS, 'TiG': TiG})
# Apply filtering based on criteria from Power BI filters
# Example: If 'Filter_Column' is a column in your dataset that is filtered in Power BI
# You can apply the filter like this:
# data = data[data['Filter_Column'] == 'Desired_Value']
# let the user specify their own axes
ax = ax if ax else plt.axes()
# rest of your plotting code...
# (unchanged from your original script)
# Assuming the data is already loaded into Power BI and accessible via the DataFrame 'dataset'
# Fetch relevant columns from the dataset
XXXXX_1 = dataset['XXXXX_1'].to_numpy()
XXXXX_2 = dataset['XXXXX_2'].to_numpy()
Name = dataset['Name'].to_list()
TiR = dataset['TiR'].to_numpy()
TiS = dataset['TiS'].to_numpy()
TiG = dataset['TiG'].to_numpy()
# Call quadrant_chart function with error handling
try:
quadrant_chart(
XXXXX_1=XXXXX_1,
XXXXX_2=XXXXX_2,
Name=Name,
TiR=TiR,
TiS=TiS,
TiG=TiG,
xtick_labels=['PROMOTION', 'LATERAL MOVE'],
ytick_labels=['GROW', 'EXPORT']
)
except ValueError as e:
print("Error:", e)
i hope it will work for you
Best regard
Danish hafeez | QA Assistant
buran write Mar-05-2024, 07:00 AM:
Please, stop sharing spam link in your signature. This is not advertising platform.
Please, stop sharing spam link in your signature. This is not advertising platform.