Forecasting in Power BI using Python

Ashish Kumar
TheCyPhy
Published in
5 min readNov 9, 2022
Photo by Waranont (Joe) on Unsplash

Cities like these feel like the lighted-up motherboard of a computer. Are we in a simulation ?šŸ˜±.

Hello Curious PersonšŸ™‚ , If you want to create a dynamic forecasting dashboard in Power BI using the power of Python, then you have come to the right place. In my previous post I have done the same thing in Tableau so letā€™s play with Power BI this time.

Letā€™s get our data first, download it from here. Now it's time to jump to Power BI. Open Power BI, Now Click File ā†’ Get data ā†’ Text/CSV and select the AirPassenger dataset file. A dialog box will open up, Click on Transform Data.

It's time to enter the Power Query editor world. We should see something like this below.

First thing is to delete the ā€œChanged Typeā€ step from the ā€œApplied Stepsā€ section. We can do this by right-clicking the ā€œChanged Typeā€ step and then clicking on Delete.

Letā€™s create a parameter first, named ā€œForecast Periodā€, to do that Click on Home ā†’ Manage Parameters ā†’ New Parameter. Create a parameter as below

This Parameter will help us to set the number of time periods we want to forecast ahead. For example in our case now, the Current value 12 means we are going to forecast for 12 months ahead.

Now it's time to bring our old friend Python šŸ. First click on AirPassengers dataset then click on Transform ā†’ Run Python script.

It will open up a dialog box where we have to write our python script. Copy paste the below script there

# 'dataset' holds the input data for this script
import pandas as pd
from statsmodels.tsa.holtwinters import ExponentialSmoothing
forecast_period = 12
dataset["Month"]= pd.to_datetime(dataset["Month"], format="%Y-%m")
dataset.set_index("Month", inplace=True)
model = ExponentialSmoothing(dataset, trend= "additive", seasonal= "additive", seasonal_periods=12,
).fit()
actual = pd.Series(list(dataset["#Passengers"].values))
forecast = pd.Series(list(model.predict(start= len(dataset), end=len(dataset)+ forecast_period-1).values))
forecast = forecast.apply(lambda x: 0 if x < 0 else x)
dataset.reset_index(inplace=True)
dates= pd.date_range(start=dataset.loc[len(dataset)-1,'Month'].date(), periods=forecast_period+1 ,freq= pd.infer_freq(dataset["Month"]))[1:]
d = {'Month': dates, 'Forecast':forecast.values}
dataset= dataset.append(pd.DataFrame(data=d))

After executing the above script, we will see a dataset with two columns ā€œNameā€ and ā€œValueā€. Click on expand on the ā€œValueā€ column and untick the ā€œUse original column name as prefixā€ option. Then delete the ā€œNameā€ column. Then Right click on the ā€œMonthā€ column and then click on ā€œChange Typeā€ and then change it to Date. Similarly, change the type of ā€œ#Passengersā€ and ā€œForecastā€ columns to Decimal numbers.

Now we should see something like below.

We can see, we have added a Forecast column to our dataset which contains forecasted values.

Now itā€™s time to make it a little bit dynamic. Click on ā€œRun Python scriptā€ in the ā€œApplied Stepsā€ section and then expand the query editor.

We can see our Python code here, we have to modify it a little bit to make use of our ā€œForecast Periodā€ parameter. Find the line ā€œforecast_period = 12ā€ and change it to forecast_period = ā€œ&Text.From(#ā€Forecast Periodā€)&ā€. Now our python code is connected to the ā€œForecast Periodā€ parameter and whatever value we will give to the ā€œForecast Periodā€ parameter, our python code will forecast ahead for that period of time. For example, in our case, if we set our ā€œForecast Periodā€ parameter to 6, we will get forecasted values for the upcoming 6 months.

Note- If you faced some issues due to privacy levels in Power BI. We can resolve that by setting the privacy levels for Python and our Dataset. To do that Click Home ā†’ Data source settings ā†’ Global permissions. Select your dataset file and click on Edit Permissions. Then set the ā€œPrivacy levelā€ to ā€œPublicā€. Similarly, do it for Python.

Phewww šŸ¤Æ, Thatā€™s a lot of work in Power query for now. Click on Home ā†’ Close & Apply. Now itā€™s time to do some visualizations using python. Click on the Python visual in the Visualizations section. Drag and drop the ā€œ#Passengersā€, ā€œForecastā€ and ā€œMonthā€ columns in the ā€œValuesā€ field. Remove the Date Hierarchy from the ā€œMonthā€ column. In the python script editor, copy and paste the below code

import matplotlib.pyplot as pltax = plt.gca()dataset.sort_values(by=['Month'], inplace=True)  ## very important to sort the valuesdataset.plot(kind='line',x='Month',y='Forecast',color='red', ax=ax)dataset.plot(kind='line',x='Month',y='#Passengers', color='blue', ax=ax)plt.show()

And we are done with the work. We should see something like below

As of now, python visuals are not interactive as compared to other visuals in Power BI. So to do the above visual I will recommend Line Chart visual in Power BI.

Power BI has to catch up with Tableau in terms of integration with python. I guess Power BI needs some more Power šŸ’Ŗ.

Congratulations on creating your dynamic forecasting dashboard in Power BI. Play with it and see the future šŸ˜Ž.

I will be back with more interesting articles like this, till then Enjoy your day šŸ˜‡.

Feel free to connect with me at linkedin.com/in/ashish ā€” kumar for a Cup of Chat

--

--

Ashish Kumar
TheCyPhy
Writer for

I write and share interesting articles. Follow me if you are an avid reader. Connect with me at https://topmate.io/ashish_kumar17