Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python script in Power BI returns date as Microsoft.OleDb.Date

Tags:

python

powerbi

After importing a csv file with two columns [Date, Value] and transforming it using Home > Edit Queries > Transform > Run Python Script, the Date column shows Microsoft.OleDb.Date instead of dates. Before running the script, the same values in the same column are displayed as 12.10.2018 (input format from csv is 2018-10-12). This causes any later step in Query Settings to break. How do you work around this?

Sample data:

Date,Value
2108-10-12,1
2108-10-13,2
2108-10-14,3
2108-10-15,4
2108-10-16,5

Code example:

# 'dataset' holds the input data for this script
dataset['Value2'] = dataset['Value']*10
dataset

Error:

enter image description here

like image 268
vestland Avatar asked Aug 20 '18 11:08

vestland


People also ask

How do I fix date format in Power BI?

To begin with, the Power BI Date Format, navigate to the Transform Tab. The Date and Time choices are available under this tab. You can format the dates using these drop-down options. The drop-down list of different data formats is displayed when you click the down arrow beside the Date.

How do I change the date format from YYYY MM DD in Power BI?

To change the date column format to dd/mm/yyyy in power bi. Click on the Date column, it will open the column tools in the ribbon. In the formatting section, Click on the format dropdown and select the dd/mm/yyyy.

How do I change the date format in Power BI dataset?

Simply right click on the date column and select Change Type/Using Locale. Now you can choose the format (int, string, date , etc.) and the the Locale and that's it.


1 Answers

This is the same issue as described for R scripts here, but for the sake of clarity, I'll add an answer for Python in Power BI as well.

Short answer:

Just change the Date data type from date to Text before you insert the Python Script.

The details:

If you import the csv file using the Home > Get Data functionality, and then go to Home > Edit Queries, this is what you'll see:

enter image description here

Before you do anything else, just click on the symbol next to date and select Text.

enter image description here

Now you can click Run Python Script, insert your code and clikc OK. This takes you to one last intermediate step where you'll have to click Table here:

enter image description here

This is it:

enter image description here

Notice that the datatype of Date is changed to a date format. If not you can easily change it again with the right-click drop-down menu as explained above.

like image 85
vestland Avatar answered Oct 11 '22 00:10

vestland