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

Did you hear? There's a new SQL AI Developer certification (DP-800). Start preparing now and be one of the first to get certified. Register now

Reply
Anonymous
Not applicable

Correct Python variables calls

Hi,

 

I am trying to build a proportion table of late vendors based off orders. I built this basic code back in october through Jupyter Notebook and it still works fine there. I am trying to implement it on power bi now and I am confused whether it is necessary to declare the variable I am using, even though I have selected the column in Power BI. I get a name error as follows: LateDays is not defined. LateDays is the field that I get on power bi by using Odata, a service my company uses to fetch live data.

 

this is the code:

 

df = pandas.DataFrame(LateDays)
df = dataset.drop_duplicates()

import pandas as pd
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
matplotlib.style.use('seaborn')
import seaborn as sns
sns.set(font_scale = 2)
# Paste or type your script code here:

def is_late(x):
return x > 0
df['Late'] = df['LateDays'].apply(is_late)

group_by_vendor = df.groupby(['CompanyName', 'Late'])

delayed_by_vendor = df.groupby(['CompanyName', 'Late']).size().unstack().reset_index()
delayed_by_vendor['orders_count'] = (delayed_by_vendor[False] + delayed_by_vendor[True])
delayed_by_vendor['Proportion_Late'] = delayed_by_vendor[True] / delayed_by_vendor['orders_count']
delayed_by_vendor.sort_values('proportion_late', ascending=False)
 
Right at the first string of code I get the name error. How can I define the variable in the code if the variable is fetched off a live service with huge amounts of data? thanks.
1 REPLY 1
Anonymous
Not applicable

Hi,

 

It seems on my Power BI desktop, Python Visual fetches the data and saves it as the variable "dataset".

 

Try to replace "df" by "dataset" in your script.

 

Hope it will solve your problem.

 

Thanks,

 

Ty 

 

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.

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

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.