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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
SushilSingh10
New Member

We are trying to Pass parameter to python Script

We have created two parameter named as storeval and itemval. we want to to pass these two parameter to our python Script code but we are not getting the table output from our Python Script please check below Snap 

SushilSingh10_0-1707115288654.png

SushilSingh10_1-1707115912361.png

 

 

1 ACCEPTED SOLUTION
tharunkumarRTK
Super User
Super User

Hi Sushil,

 

There might be many ways of solving this requirement. Here is my solution

 

Financials is the name of the query and paramSegment is the parameter name.
Screenshot 2024-02-05 at 4.15.19 PM.png
Pass the data as well as the parmeter values as input to the Python.Execute function and then you will be able to access them, I am attaching the code for your reference

let
  Source = Excel.Workbook(
    File.Contents(
      <Path>
    ),
    null,
    true
  ),
  financials_Table = Source{[Item = "financials", Kind = "Table"]}[Data],
  #"Changed Type" = Table.TransformColumnTypes(
    financials_Table,
    {
      {"Segment", type text},
      {"Country", type text},
      {"Product", type text},
      {"Discount Band", type text},
      {"Units Sold", type number},
      {"Manufacturing Price", Int64.Type},
      {"Sale Price", Int64.Type},
      {"Gross Sales", type number},
      {"Discounts", type number},
      {" Sales", type number},
      {"COGS", type number},
      {"Profit", type number},
      {"Date", type date},
      {"Month Number", Int64.Type},
      {"Month Name", type text},
      {"Year", Int64.Type}
    }
  ),
  GatherParameters = Table.ExpandRecordColumn(
    Table.FromList(
      {[Parameter = "Segment", Value = paramSegment]},
      Splitter.SplitByNothing(),
      null,
      null,
      ExtraValues.Error
    ),
    "Column1",
    {"Parameter", "Value"}
  ),
  PrepareInputforPY = Table.ExpandRecordColumn(
    Table.FromList({[Data = #"Changed Type", Params = GatherParameters]}, Splitter.SplitByNothing()),
    "Column1",
    {"Data", "Params"}
  ),
  #"Run Python script" = Python.Execute(
    "# 'dataset' holds the input data for this script#(lf)# dataset.drop(dataset[dataset['Segment'] == 'Government'].index, inplace=True)#(lf)segment = Params[Params['Parameter'] == 'Segment']['Value'][0]#(lf)dataset.drop(dataset[dataset['Segment'] != segment].index, inplace=True)#(lf)#(lf)",
    [dataset = PrepareInputforPY[Data]{0}, Params = PrepareInputforPY[Params]{0}]
  ),
  dataset = #"Run Python script"{[Name = "dataset"]}[Value]
in
  dataset


Hope this answers your question. If yes then please accept my solution.

Thanks

Tharun

View solution in original post

1 REPLY 1
tharunkumarRTK
Super User
Super User

Hi Sushil,

 

There might be many ways of solving this requirement. Here is my solution

 

Financials is the name of the query and paramSegment is the parameter name.
Screenshot 2024-02-05 at 4.15.19 PM.png
Pass the data as well as the parmeter values as input to the Python.Execute function and then you will be able to access them, I am attaching the code for your reference

let
  Source = Excel.Workbook(
    File.Contents(
      <Path>
    ),
    null,
    true
  ),
  financials_Table = Source{[Item = "financials", Kind = "Table"]}[Data],
  #"Changed Type" = Table.TransformColumnTypes(
    financials_Table,
    {
      {"Segment", type text},
      {"Country", type text},
      {"Product", type text},
      {"Discount Band", type text},
      {"Units Sold", type number},
      {"Manufacturing Price", Int64.Type},
      {"Sale Price", Int64.Type},
      {"Gross Sales", type number},
      {"Discounts", type number},
      {" Sales", type number},
      {"COGS", type number},
      {"Profit", type number},
      {"Date", type date},
      {"Month Number", Int64.Type},
      {"Month Name", type text},
      {"Year", Int64.Type}
    }
  ),
  GatherParameters = Table.ExpandRecordColumn(
    Table.FromList(
      {[Parameter = "Segment", Value = paramSegment]},
      Splitter.SplitByNothing(),
      null,
      null,
      ExtraValues.Error
    ),
    "Column1",
    {"Parameter", "Value"}
  ),
  PrepareInputforPY = Table.ExpandRecordColumn(
    Table.FromList({[Data = #"Changed Type", Params = GatherParameters]}, Splitter.SplitByNothing()),
    "Column1",
    {"Data", "Params"}
  ),
  #"Run Python script" = Python.Execute(
    "# 'dataset' holds the input data for this script#(lf)# dataset.drop(dataset[dataset['Segment'] == 'Government'].index, inplace=True)#(lf)segment = Params[Params['Parameter'] == 'Segment']['Value'][0]#(lf)dataset.drop(dataset[dataset['Segment'] != segment].index, inplace=True)#(lf)#(lf)",
    [dataset = PrepareInputforPY[Data]{0}, Params = PrepareInputforPY[Params]{0}]
  ),
  dataset = #"Run Python script"{[Name = "dataset"]}[Value]
in
  dataset


Hope this answers your question. If yes then please accept my solution.

Thanks

Tharun

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.