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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
inm
Helper II
Helper II

Change column type change value from another column (Python script)

Hello.

 

I made a python script and it runs in power query, it returns one date and one float column. When I try to change column types according to their values, they change. Changing the column type also changes the values in the same column. Here an example.

 

This is what returns python script.

 

 

power query.png 

I try to change the type of date column to date type and and the values in the prediction column change.

 

power query text to date.png

In report view the values are also different from those shown in power query.

 

In the python script, a model is created with neural networks from the keras library and returns the predictions of the initial data. I believe is due to keras, how can I avoid this?

 

I appreciate any help. Thank you for your ideas and time.

 

 

 

4 REPLIES 4
edhans
Super User
Super User

what happens if in the step before you do a the change type if you wrap it in a Table.Buffer() function? Just put Table.Buffer() around the entire line, and then change the type to Date.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Hi.

I tried what you said but that also change the values.

v-yingjl
Community Support
Community Support

Hi @inm ,

Could you please consider sharing a sample file without any sesentive information or the sample python script query code for further discussion?

 

Best Regards,
Community Support Team _ Yingjie Li

Python script:


import pandas as pd
import numpy as np
from keras.layers import Dense, LSTM
from keras.models import Sequential
from keras.preprocessing.sequence import TimeseriesGenerator

 

def get_prediction_dates(num_prediction, df):

           last_date = df['date'].max()

           date_test = pd.date_range(last_date, periods=num_prediction+1, freq='M').to_frame(index=False,            name='date')

           date_test = date_test['date'].apply(lambda x : x.replace(day=1))

           return date_test

 

def predict(num_prediction, model):

           prediction_list = arr[-look_back:]

           for _ in range(num_prediction):

                      x = prediction_list[-look_back:]

                      x = x.reshape((1, look_back, 1))

                      out = model.predict(x)[0][0]

                      prediction_list = np.append(prediction_list, out)

           prediction_list = prediction_list[look_back-1:]

return prediction_list

 

look_back = 12
batch_size = 4

arr = dataset['quantity'].values
arr = arr.reshape((-1, 1))

generator = TimeseriesGenerator(arr, arr, length=look_back, batch_size = batch_size)

model = Sequential()
model.add(
LSTM(128,
activation='linear',
input_shape=(look_back, 1),
name="LSTM")
)
model.add(Dense(1, activation='linear', name="Dense"))
model.compile(optimizer='adam', loss='mse', metrics=['mean_absolute_percentage_error'])

model.fit(generator, epochs=70)

num_prediction = 6

forecast = predict(num_prediction, model)
forecast_dates = get_prediction_dates(num_prediction, dataset)

dataset = pd.DataFrame(list(zip(forecast_dates, forecast)), columns=['date', 'prediction'])

dataset['date'] = pd.to_datetime(dataset['date'])

 

I placed a seed, but is there any other way to prevent this from happening?

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors