Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Mons
Regular Visitor

Categorizing book ratings

Hi! I'm new to PowerBI.

Képernyőkép 2022-03-16 161039.png

I have a dataset of books, one column including the user ratings for each book. 

I've written a Python script which categorizes these ratings in the following way:

 

 

import matplotlib.pyplot as plt
dataset.dropna(0, inplace=True)

def segregation(data):
    values = []
    for val in data.average_rating:
        if val>=0 and val<=1:
            values.append("Between 0 and 1")
        elif val>1 and val<=2:
            values.append("Between 1 and 2")
        elif val>2 and val<=3:
            values.append("Between 2 and 3")
        elif val>3 and val<=4:
            values.append("Between 3 and 4")
        elif val>4 and val<=5:
            values.append("Between 4 and 5")
        else:
            values.append("NaN")
    return values

dataset['Ratings_Dist'] = segregation(dataset)
ratings_pie = dataset['Ratings_Dist'].value_counts().reset_index()
labels = ratings_pie['index']
colors = ['blue','purple', 'orange','red', 'black']
percent = 100.*ratings_pie['Ratings_Dist']/ratings_pie['Ratings_Dist'].sum()
fig, ax1 = plt.subplots()
ax1.pie(ratings_pie['Ratings_Dist'],colors = colors, 
        pctdistance=0.85, startangle=90, explode=(0.05, 0.05, 0.05, 0.05, 0.05))
centre_circle = plt.Circle((0,0), 0.70, fc ='white')
fig1 = plt.gcf()
fig1.gca().add_artist(centre_circle)

plt.show()

 

 

 

This results in the following chart: 

Képernyőkép 2022-03-16 161529.png

 

 

 

 

 

 

But I'd like to display the following on the chart written besides each color and their percentages too:

 

 

Avg rating distribution
Between 4 and 5 - purple
Between 3 and 4 - blue
Between 2 and 3 - orange
Between 1 and 2 - black
Between 0 and 1- red

 

 

 

Goal is to see how many users gave a rating between x and y for each number interval.

How can I accomplish this in PowerBI? 

1 ACCEPTED SOLUTION
johnt75
Super User
Super User

You need to create a new column on your table for the rating group. You can either do that by adding a conditional column in Power Query or you can create a calculated column using the below DAX

Rating group = SWITCH( TRUE(),
'Table'[average_rating] >= 0 && 'Table'[rating] <= 1, "Between 0 and 1",
'Table'[average_rating] > 1 && 'Table'[rating] <= 2, "Between 1 and 2",
'Table'[average_rating] > 2 && 'Table'[rating] <= 3, "Between 2 and 3",
'Table'[average_rating] > 3 && 'Table'[rating] <= 4, "Between 3 and 4",
'Table'[average_rating] > 4 && 'Table'[rating] <= 5, "Between 4 and 5"
)

Then create a measure to count the number of occurences, 

Num occurences = COUNTROWS('Table')

and create a donut chart with the measure as the value and the new column as labels.

View solution in original post

2 REPLIES 2
DuaneMusgra
Regular Visitor

This is a wonderful book. It especially helps in writing Python script and Java scripts. Therefore, I am very grateful for useful materials that can help. Although, I encountered a different kind of task, and it caused me difficulties. This source helped me in preparing the work on swot analysis https://edubirdie.com/examples/swot-analysis/ I realized that the examples are just a godsend and they give a lot of fresh ideas and inspiration for writing.

 

johnt75
Super User
Super User

You need to create a new column on your table for the rating group. You can either do that by adding a conditional column in Power Query or you can create a calculated column using the below DAX

Rating group = SWITCH( TRUE(),
'Table'[average_rating] >= 0 && 'Table'[rating] <= 1, "Between 0 and 1",
'Table'[average_rating] > 1 && 'Table'[rating] <= 2, "Between 1 and 2",
'Table'[average_rating] > 2 && 'Table'[rating] <= 3, "Between 2 and 3",
'Table'[average_rating] > 3 && 'Table'[rating] <= 4, "Between 3 and 4",
'Table'[average_rating] > 4 && 'Table'[rating] <= 5, "Between 4 and 5"
)

Then create a measure to count the number of occurences, 

Num occurences = COUNTROWS('Table')

and create a donut chart with the measure as the value and the new column as labels.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.