Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python console in Power BI

Tags:

python

powerbi

I'm currently working of Power BI with python scripting. I'd like to print (using the function print) but I'm not able to find a way to see my printed message anywhere.

I've already search on Google and Stack Overflow if a console exists on Power BI to have the Python output

import pandas as pd

dataset = pd.DataFrame(dataset.loc[:1, 'access_token'])

access_token = dataset.iloc[0]
print(access_token)

I'd like to have the output of print(access_token)

like image 566
Benjamin Audet Avatar asked May 21 '19 14:05

Benjamin Audet


People also ask

How can you use Python in Power BI?

In the Home tab of the ribbon, select 'Get data' to bring up the full list of data connections. Select the 'Other' category and find 'Python script' on the list. This will allow you to write a Python script to import a dataset.

How do I run a Python script in power query?

Open Power Query Editor by selecting Transform data from the Home tab in Power BI Desktop. In the Transform tab, select Run Python Script and the Run Python Script editor appears (shown in the next step).

Is Python supported in Power BI?

You can run Python scripts directly in Power BI Desktop and import the resulting datasets into a Power BI Desktop data model.


2 Answers

I've finally found an efficient way to print debug in Python in Power BI.

As long as we cannot use the function print because we don't have a display of the standard output of Python in Power BI, we can raise exception in order to display a variable or anything else.

You can use raise Exception(TheVariableYouWantToPrint)`

For example, you can do raise Exception(dataset) if you want to check the content of the dataset global variable

You can also print a string like raise Exception("Hello World")

It's not the best way to print something but still, that's the only way I've found to do it easily and efficiently

like image 152
Benjamin Audet Avatar answered Sep 30 '22 20:09

Benjamin Audet


Sorry, no console. But there are other ways to inspect your variables. Not using print() though. You haven't specified whether you're working with a Python visual directly on the Report tab, or the Run Python Script option under the Transform tab in the Power Query Editor. I'd suggest the latter.

Try this:

1: In the report tab, click Click Home > Enter Data > Load without inserting any data.

2: Click Edit Queries and select Transform > Run Python Script

3: Insert the following snippet in the dialog box:

import pandas as pd
d = {'col1': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)

I've just used some sample data since I don't have your dataset. The important thing here is that both df in my sample and dataset in your sample are of type pandas.core.frame.DataFrame.

4: Click OK and you should get this:

enter image description here

Most variables (we'll get to that later) that you import or create should be made available there. In our case we have d and df. Click on table to the right of df to see this:

enter image description here

I hope this helps!


End note:

About the 'Most variables...' part. It seems that whether or not your variables are made available will depend on the datatype of said variable. I tried using a version of the snippet in your question and found that access_token = df.iloc[0] returns a variable of type pandas.core.series.Series. This will for some reason not be available for direct inspection as is the case for d and df that are of type dict and pandas.core.frame.DataFrame, respectively. So to make access_token availale to you, you should try access_token = dataset.iloc[0].to_frame().

like image 24
vestland Avatar answered Sep 30 '22 21:09

vestland