cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Frequent Visitor

Passing Parameters/Variables into python Script

Dear Community,

 

I am trying to build a function for web scrapping using a python script. I want to pass a parameter as a variable to the python script.  I tried following the parameter passing formula in R script. But that is not working for python file. 

 

Is there a way to declare the search 'query' as a parameter and have it passed through or stored as a variable to be used in each time the function is invoked?

I have also shared my code snippet for reference. 

 

Thanks.

 

code.JPG

7 REPLIES 7
Highlighted
Microsoft
Microsoft

Re: Passing Parameters/Variables into python Script

Hi @JNahar ,

Could you have refered to below link about Using Python in Query Editor:

https://docs.microsoft.com/en-us/power-bi/desktop-python-in-query-editor

Maybe you could just enter put the python code into the Run Python Script dialog.

 

Regards,
Daniel He

Community Support Team _ Daniel He
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Highlighted
Frequent Visitor

Re: Passing Parameters/Variables into python Script

Hi @v-danhe-msft 

 

Thank you for your reply. 

I was trying to build a function with the python script. The parameter passing is not working for the function. 

 

But the python script is working fine when it was added as a new Step. 

I was wondering if there is any way I could make the same script as a function.

 

Regards,

Jannatun Nahar

 

 

Highlighted
Advocate I
Advocate I

Re: Passing Parameters/Variables into python Script

Hey, @JNahar 

 

Can you explain more about "passing as a new step"? Were you able to find a workaround to use the parameter as part of your Python script?


Thanks!

Highlighted
Frequent Visitor

Re: Passing Parameters/Variables into python Script

Hi @tebow ,

 

Thank you very much for your reply. I am trying to create a custom invoked function using python or R script to extract tweets in powerBI. Instead of hardcoding the search term in the original R script, I wanted to pass it as a parameter. But the code is not working in this way. I am attaching a copy of my code here :

 

(Name as text) as table=>
let
    
  Source = R.Execute(" #(lf)   library(twitteR)
                         #(lf)   library(stringi)
                         #(lf)   consumer_key <- ""abcd "" 
                         #(lf)   consumer_secret <- "" adbc ""
                         #(lf)   access_token <- ""abcd ""
                         #(lf)   access_secret <- ""abcd""
                         #(lf)   setup_twitter_oauth(consumer_key, consumer_secret, access_token, access_secret)
                         #(lf)   str1 <- ""#""
                         #(lf)   str2 <- ""&Name&""   (passing serach term as a parameter here)
                         #(lf)   search_term <- paste(str1,str2,sep="") 
                         #(lf)   number_of_tweets <- 100
                         #(lf)   language <-'en'
                         #(lf)   tweets <- searchTwitter(search_term, number_of_tweets,language)
                         #(lf)   df_tweets <- twListToDF(tweets)
                         #(lf)   df_tweets <- df_tweets[,c('text','favorited','favoriteCount','created','retweetCount',
                                                                                              'isRetweet','retweeted')]
                         #(lf)   df_tweets$text <- stri_encode(df_tweets$text, '', 'UTF-8')"),
    df_tweets1 = Source{[Name="df_tweets"]}[Value]
in
    df_tweets1

I am working on this for my MSC capstone project. I would really appreciate any suggestion to pass through the error. 

 

Thanks agaian.

 

Highlighted
Frequent Visitor

Re: Passing Parameters/Variables into python Script

Can someone please confirm whether this is possible or not? 

Highlighted
Regular Visitor

Re: Passing Parameters/Variables into python Script

This seems to work - note that dataset2 takes the dataset1 value and multiplies it by 2:

 

let
Source = Python.Execute("import pandas#(lf)dataset = pandas.DataFrame([1, 2, 3])"),
dataset1 = Source{[Name="dataset"]}[Value],
dataset2 = Python.Execute("
import pandas
dataset = dataset*2
dataset.to_csv('C:/Users/arman.schwarz/dataset_to_csv.csv')", [dataset = dataset1]),
dataset3 = dataset2{[Name="dataset"]}[Value],
#"Changed Type" = Table.TransformColumnTypes(dataset3,{{"0", Int64.Type}})
in
#"Changed Type"

 

Highlighted
Frequent Visitor

Re: Passing Parameters/Variables into python Script

You need to concatenate text with & and not +

 

The following works fine for me:

 

let
Source = Python.Execute("import pandas#(lf) azureAppid='" & paramAzureAppID & "'#(lf)",

df1 = Source{[Name="df"]}[Value]
in
df1

Helpful resources

Announcements
Community Blog

Community Blog

Visit our Community Blog for articles, guides, and information created by fellow community members.

Using the Community

Using the Community

Need help with the Power BI Community? Our 'Using the Community' support articles are a great place to start.

Community Summit North America

Community Summit North America

Innovate, Collaborate, Grow. The top training and networking event across the globe for Microsoft Business Applications

Power Platform 2020 release wave 2 plan

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Top Solution Authors
Top Kudoed Authors