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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

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
Anonymous
Not applicable

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

v-danhe-msft
Employee
Employee

Hi @Anonymous ,

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.
Anonymous
Not applicable

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

 

 

Anonymous
Not applicable

Hey, @Anonymous 

 

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!

Anonymous
Not applicable

Hi @Anonymous ,

 

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.

 

Can someone please confirm whether this is possible or not? 

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"

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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