简体   繁体   中英

Edit python script used as Data entry in Power BI

I have a python script and used it to create a dataframe in Power BI.

Now I want to edit that dataframe in Power BI but don´t enter from scratch as new data because I want to keep all the charts inside my Power BI model.

For example in my old dataframe i specified some dates inside my script so the information was limited to those dates. Now i want to change the dates to new ones but dont want to lose all the model.

df = df

I hope you're not dong this in a PowerBI Python Visual. If you're using Python under the Transform tab in the Power Query Editor, the key to your problem lies not in Python itself, but rather in the reference function available to you if you right-click the table under queries in the Power Query Editor:

在此处输入图像描述

Try this:

1: Save the following sample data in a csv as C:\pbidata\src.csv file and load it into PowerBI using Get Data > Text/Csv

A,B,C
1,1*0,100
2,20,200
3,30,300

2: Display it as a table:

在此处输入图像描述

3: Open the Power Query Editor through Edit Queries

在此处输入图像描述

4: Add some Python

Here you can insert a Python snippet after the Changed type step under Applied steps with Transform > Run Python Script . Inserting the following example code:

# 'dataset' holds the input data for this script
import pandas as pd
df=dataset.copy(deep=True)
df['D']=df['C']*2

... will give you this:

在此处输入图像描述

5: And let's say that you're happy with this for now and that you'd like to make a plot out of it back on the Power BI Desktop. I'm using a clustered bar chart to get this:

在此处输入图像描述

6: Now, like you're saying, if you'd like to have df['D']=df['C']/4 instead, but retain the same dataset, Python script and figure Plot 1, Py script 1 , go back to the Power Query Editor and use the functionality that I mentioned in the beginning:

在此处输入图像描述

7: And add another Python snippet:

# 'dataset' holds the input data for this script
import pandas as pd
df=dataset.copy(deep=True)
df['D']=df['D']/4

And there we go:

在此处输入图像描述

Now you have two different Python snippets that build on the same dataset. You still have the data from the first snippet, and you can do whatever you want with the second snippet without messing up your data source.

8: Insert another chart to verify:

在此处输入图像描述

9: Maybe have some fun with the whole thing by changing the source file:

Data:

A,B,C
100,10,100
2,20,200
3,30,150

New plots:

在此处输入图像描述

You can edit the python scripts doing the following steps:

  • Open Query Editor
  • At 'Applied steps', the first one, source, contains a small gear symbol just on the right side, click on it.

You can change the script direct into Power Query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM