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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
tamiribas
Resolver I
Resolver I

Error message in Power Query: NameError: name 'dataset' is not defined

Hi, 

I have a very simple static demo table

CategoryAmount
A1
B

2

 

That table is my source.

I am trying to add a Python script right after the source step.

That script adds the totals of the [Amount] column but am getting an error.

 

Python code:

import pandas as pd
total_sum = dataset['Amount'].sum()
dataset['Total'] = total_sum
result = dataset

 

error:

We encountered an error while trying to connect

Details: "ADO.NET: Python script error.
<pi>NameError: name 'dataset' is not defined
</pi>"

 

Any advice would be appreciated,

 

Thank you,

Tamir

1 ACCEPTED SOLUTION
tamiribas
Resolver I
Resolver I

Thank you @lbendlin ,

Although unnecessary, that line just returns another dataset (the default "dataset" and "result")

The problem was that I was missing the parameter that assigned the step to the dataset.

In your M code, it is this: 

[dataset=#"Changed Type"]

Once I added that parameter to my M code, it worked.

 

Regards, 

Tamir

View solution in original post

2 REPLIES 2
tamiribas
Resolver I
Resolver I

Thank you @lbendlin ,

Although unnecessary, that line just returns another dataset (the default "dataset" and "result")

The problem was that I was missing the parameter that assigned the step to the dataset.

In your M code, it is this: 

[dataset=#"Changed Type"]

Once I added that parameter to my M code, it worked.

 

Regards, 

Tamir

lbendlin
Super User
Super User

Drop the last line. Python scripts by default return the "dataset"  frame back to you.

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTJUitWJVnICsoyUYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Category = _t, Amount = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Amount", Int64.Type}}),
    #"Run Python script" = Python.Execute("# 'dataset' holds the input data for this script#(lf)total_sum = dataset['Amount'].sum()#(lf)dataset['Total'] = total_sum",[dataset=#"Changed Type"]),
    dataset = #"Run Python script"{[Name="dataset"]}[Value]
in
    dataset

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

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

Top Solution Authors