In this article, we will learn how we can add Star Ratings in Power BI desktop using a DAX Expression.
I have attached a sample practice file with this article.
I have one Product table having two columns:
Now, I want to represent ratings in the form of Stars.
Let’s get started.
Create a new measure column in Power BI Desktop.
Add the following DAX expression.
Star Rating =
Now, let’s elaborate on what this DAX expression means.
Here, I have used two DAX functions to achieve this requirement.
This function returns the Unicode character that is referenced by a given numeric value.
Suppose I write UNICHAR(65) then it will return ‘A’ because ASCII value of A is 65.
In our case for filled stars, we have used UNICHAR(9733) which will return a filled star.
UNICHAR(9734) will return a hollow star without filling.
We have concatenated both the stars using logic.
This function repeats text a given number of times. Use REPT to fill a cell with a number of instances of a text string.
REPT(Text, NumberOfTimes) Suppose my rating is 4 then I need to repeat stars four times. So, I will use:
REPT(UNICHAR(9733), AVERAGE('Product'[Rating])) The above statement will repeat Filled stars four times.
Now, I want to show a rating out of five. So, one star will be unfilled. So, I have used the following: REPT(UNICHAR(9734), 5-AVERAGE('Product'[Rating]))
Now let’s drag the fields to a table.
It will look like this.
This is how we can apply Star Rating using DAX expressions.