When analyzing correlations in Power BI, you can enable a trend line in scatter plots. This helps visualize the relationship between two variables. However, Power BI does not natively provide the R-squared (R²) value, which is crucial in determining how well the regression line fits the data.
With this custom DAX measure, we can calculate and display the R² value, filling the missing functionality in Power BI.
What is R-Squared?
R-squared (R²) measures how well the independent variable (X) explains the dependent variable (Y). It ranges from 0 to 1 (or 0% to 100% when displayed as a percentage).
- 🔵 R² = 1 (100%) → Perfect fit, all data points lie exactly on the trend line.
- 🟠R² = 0 → No correlation, the trend line does not explain any variance.
- 🟢 Higher R² → Stronger relationship between variables.
How to Implement R-Squared in Power BI
Use the following DAX measure to calculate R² for a scatter plot:
R Squared Value =
// STEP 1 : GET THE MEAN FOR X AXIS
// STEP 2 : GET THE MEAN FOR Y AXIS
// STEP 3 : GET THE SUMX FOR EACH (X - X MEAN ) * ( Y - Y MEAN )
// STEP 4 : SQRT( SUMX( (X - X MEAN) SQUARED) * SUMX( (Y - Y MEAN) SQUARED)
// STEP 5 : STEP 3 / STEP 4 IS THE R SQUARED VALUE
VAR __muX = CALCULATE(AVERAGE(salary_data[YearsExperience] ))
VAR __muY = CALCULATE(AVERAGE(salary_data[Salary]))
VAR __num = SUMX(salary_data,(salary_data[YearsExperience] - __muX) * (salary_data[Salary] - __muY))
VAR __denum = SQRT(SUMX(salary_data,(salary_data[YearsExperience]- __muX) ^ 2) *
SUMX(salary_data,(salary_data[Salary] - __muY) ^ 2)
)
RETURN
DIVIDE(__num, __denum)
Breaking Down the DAX Measure
VAR __muX = CALCULATE(AVERAGE(salary_data[YearsExperience] ))
VAR __muY = CALCULATE(AVERAGE(salary_data[Salary]))
🔹 This calculates the mean (average) values for the X and Y axes.
VAR __num = SUMX(salary_data,(salary_data[YearsExperience] - __muX) * (salary_data[Salary] - __muY))
🔹 This calculates the numerator: the sum of the products of the differences between each X and Y value from their means.
VAR __denum = SQRT(SUMX(salary_data,(salary_data[YearsExperience]- __muX) ^ 2) *
SUMX(salary_data,(salary_data[Salary] - __muY) ^ 2)
)
🔹 This calculates the denominator: the square root of the product of the sum of squared differences for both variables.
RETURN DIVIDE(__num, __denum)
🔹 Finally, the R² value is computed by dividing the numerator by the denominator.
Customizing the Measure
- 📌 Replace
salary_data
with your dataset’s name. - 📌 Update
YearsExperience
andSalary
to your X and Y axis fields.
Final Thoughts
By using this DAX measure, you can add an R² value to Power BI scatter plots, filling a critical gap in the built-in trend line feature. This helps in better understanding correlations and enhancing data-driven decision-making.
If you regularly work with regression analysis or data visualization, incorporating R-squared into your reports will add more credibility and depth to your insights.
🔥 If you found this helpful, consider sharing this with your colleagues and the Power BI community!
💬 Have questions or suggestions? Drop a comment below!
📢 Follow for more Power BI, DAX, and data analytics content!
Stay tuned for more Power BI tips and tricks—happy analyzing! 🚀
No comments:
Post a Comment
We'd like to hear your comments!