Forecasting in Power BI using Python
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 ExponentialSmoothingforecast_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