23/01/2025
In this blog post I am going to explain about Linear Regression and step by step guidance to implement Simple Linear Regression in Power BI using DAX function LINEST.
What is Linear Regression?
Linear Regression is an algorithmn which provides the linear relationship between one or more independant (predictors or features) and dependant (target or out come) variables. The goal is to determine the line (or hyperplane in higher dimensions) that best fits the data, which can then be used for prediction or analysis.
There are 3 concepts in Linear Regression
1. Independant variable - The variable(s) you use to predict the dependent variable.
2. Dependant variable - The variable you want to predict or explain
3. Line of best fit - A straight line that minimizes the difference (error) between the actual data points and the predictions made by the model.
What is simple Linear Regression?
Linear regression which involves with one dependent variable and one independent variable. Following equation is used in simple linear regression.
y = mx + b , where :
y= Dependant variable
x = Independant variable
m= Slope of the line
b= Intercept
Implement Simple Linear Regression in Power BI
In this part of the blog I am going to guide you to implement Linear Regression in Power BI using a DAX Function LINEST. For this demonstration I have downloaded a small dataset from kaggle. There are two columns namely Years Experience and Salary. For demostration of data points in Power BI scatter plot, I have added an extra index column from Power Query. If you need to learn how to add an index column to a table refer the Official Microsoft Page here.
First You need to add a scatter chart into the canvas from the visualization pane. Then add Years experience to X axis, Salary to Y axis and IndeX column to Values Field. Then enable the Trend line from further analyses section. You should get a scatter plot like below, you can customize the colors as your wish.
This is how you can visualize Linear Regression. Are we done? NO! now we are going to predict salary based on user defined years of experience. Cool Right? Here's how you can do it.
First you need to create a calculated table from DAX using LINEST function. If you need to learn more about this function refer the official Microsoft Site here. Create the new table like below image, then you will get a new table containing columns for variables in regression equation like Slope , Intercept etc .
Now we are going to create a measure to predict salaries. First you need to click on new measure add the following code to it.
Add this to a card visual you can see the predicted Salary. But we are not finished yet. How can we show salary based on user defined no of experience? For that you need to create Numeric Range What if parameter. If you want learn to implement a parameter in Power BI refer the Official Microsoft page here. After creating parametr you will get a new visual like below.
Now you need to change the DAX in the predicted salary measure like below.
Yes! now we are done. Check the demo video here. You can see when you change no of experience in the slicer, card visual displays precidicted salary. Pretty cool !
Now we’ve reached the end of this blog post. I hope you’ve gained a clear understanding of linear regression and how to implement it in Power BI. By following the steps outlined above, you can easily apply simple linear regression to any dataset. Adding this to your reports unlocks exciting ways to explore relationships and make data-driven predictions directly within your dashboards. It’s a straightforward yet powerful tool to transform raw data into actionable insights and tell more compelling data stories. Keep experimenting and happy visualizing !