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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

Reply
DnsLeu
Frequent Visitor

Python script changes data in date column.

Hello!

After running a python script in power query, one of my columns containing date data is completely changed to some text. The column is not even part of the python script. All the dates are replaced with "Microsoft.OleDb.Date". 

The source data is an Excel file located on SharePoint. Any idea as to what could cause it or how to revert the data back?

Best regards,
Denis

 after.pngbefore.png

6 REPLIES 6
lbendlin
Super User
Super User

The column is not even part of the python script

Then how else does it participate in the output?  Your python script is supposed to create a dataframe that is handed back to Power Query as a table.

Hey,

I have 17 columns. The script that I run only adds a new column at the end. The results in this column are based on the values in 3 of columns. 

When I run the script, I get all the original dataframe back, with all 17 columns + 1 (the 1 computed by python). In these original 17 columns, only 1 of them (presented above) gets its values changed. The other 16 columns maintain their values. 

Perhaps I did not do a good job clarifying in the first place. 

Maybe you can show a sanitized version of the Power Query script.

Sure!

# 'dataset' holds the input data for this script 

import pandas as pd 

   

def calculate_Ordering(row): 

    curr_row = row['Chapter'] 

    row_grp = row['Name'] 

    row_main = row['Main'] 

    prj = row['Project'] 

  

    main_groups = dataset[(dataset['Main'] == True) &  

                     (dataset['Name'] != row_grp) &  

                     (dataset['Project'] == prj)] 

     

    rel_chpt = main_groups[main_groups['Chapter'] < curr_row]['Chapter'].max() 

     

    if row_main: 

        return curr_row 

    else: 

        if str(curr_row).startswith(str(rel_chpt)): 

            return rel_chpt 

        else: 

            return curr_row 

  

dataset['Order'] = dataset.apply(calculate_Ordering, axis=1) 

print(dataset)

The column affected is "Deadline", which only has date type data. It is not included in the script and is the only one affected. A possible work around would be to have it as text before running the Python script and then convert it back to date type, but this still does not explain why the data gets lost / modified in the first place.

There is no point in the last print(dataset)  command. 

 

your dataset will completely replace any prior output, including that date column.

 

I'd need to see more of the Power Query script before and after the Python part  to be able to assist more.

Prior and after the script there are few Power Query steps like renaming and replace value. I removed print(dataset), but has the same result.

Right now, the only solution I have found is just to transform all the columns to text before running the script and then transform them back to their respective type after the script. 

Thanks for the help anyways!

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code FABINSIDER for a $400 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

Check out the February 2025 Power BI update to learn about new features.

March2025 Carousel

Fabric Community Update - March 2025

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