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

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
Jimbo1511
New Member

Grouping and counting number of Hogwarts Houses by Points-earning Students

Hi there,

 

Say I have the below list of Points awarded at Hogwarts in a given month, along with the Student who earned them. Gryffindor has 6 unique Students earing Points, Slytherin 3, Ravenclaw 2 and Hufflepuff 1.
Does anyone know how I can group and count the Houses by the number of unique Students earning Points that month?

 

Desired output table:

House size (Points-earning Students)Count of Houses
1 student1
2 students1
3 or more students2

 

Data set:

DateHousePointsStudent
6-MarSlytherin60Draco
7-MarGryffindor40Lee
7-MarSlytherin90Vincent
9-MarGryffindor10Harry
10-MarHufflepuff20Cedric
11-MarGryffindor20Ginny
12-MarSlytherin70Pansy
13-MarGryffindor10Neville
13-MarGryffindor70Hermione
13-MarGryffindor70Ron
16-MarRavenclaw40Luna
16-MarRavenclaw50Padma
18-MarGryffindor70Ron
19-MarRavenclaw40Luna
19-MarRavenclaw50Padma
21-MarSlytherin90Vincent
22-MarRavenclaw50Padma
23-MarSlytherin90Vincent
24-MarGryffindor40Lee
25-MarHufflepuff20Cedric
26-MarSlytherin70Pansy
27-MarGryffindor10Neville

 

1 ACCEPTED SOLUTION
jennratten
Super User
Super User

Hello - this will produce the requested result.  You can group by house to get a count of students, then assign those counts to house sizes and finally group again by house size to get a count of houses by house size.

 

jennratten_0-1694431523632.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jdLPC4IwFAfw/2Vng1ymeS7QQ0UYdJEOYz5psJ6xfuF/36YrSmZ6GRt8+L7H3stzEk42TBGP7GV9O4ESqO/hVB8rxXhFjl5OIksSVZelwKIyj8CYNcCP+A6JDTgI5IC3BsWuGN+olClVN8afWpTey1LCRZ/6QQ1aQqEEb5XvimpUIhBtFHU0FRmzY3i1Ztbb0xYeQkr4w5qsFNRZVDjosgpb8v7vjD0AuWTPz1/ekfWTedt4cbZmMVwpHq7kIp1K1B8zW0pHJM1GJQWD20bnY5aEuja7M3/qXO3f+R9f", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, House = _t, Points = _t, Student = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"House", type text}, {"Points", Int64.Type}, {"Student", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"House"}, {{"House Size", each let i = List.Count ( List.Distinct ( [Student] ) ) in if i = 1 then "1 Student" else if i = 2 then "2 Students" else "3 or More Students" , type text}}),
    #"Grouped Rows1" = Table.Group(#"Grouped Rows", {"House Size"}, {{"Count of Houses", each Table.RowCount(_), Int64.Type}})
in
    #"Grouped Rows1"

 

View solution in original post

3 REPLIES 3
Jimbo1511
New Member

Thanks so much @jennratten ! Makes good sense and gives me exactly what I need. Cheers

You're welcome!

jennratten
Super User
Super User

Hello - this will produce the requested result.  You can group by house to get a count of students, then assign those counts to house sizes and finally group again by house size to get a count of houses by house size.

 

jennratten_0-1694431523632.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jdLPC4IwFAfw/2Vng1ymeS7QQ0UYdJEOYz5psJ6xfuF/36YrSmZ6GRt8+L7H3stzEk42TBGP7GV9O4ESqO/hVB8rxXhFjl5OIksSVZelwKIyj8CYNcCP+A6JDTgI5IC3BsWuGN+olClVN8afWpTey1LCRZ/6QQ1aQqEEb5XvimpUIhBtFHU0FRmzY3i1Ztbb0xYeQkr4w5qsFNRZVDjosgpb8v7vjD0AuWTPz1/ekfWTedt4cbZmMVwpHq7kIp1K1B8zW0pHJM1GJQWD20bnY5aEuja7M3/qXO3f+R9f", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, House = _t, Points = _t, Student = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"House", type text}, {"Points", Int64.Type}, {"Student", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"House"}, {{"House Size", each let i = List.Count ( List.Distinct ( [Student] ) ) in if i = 1 then "1 Student" else if i = 2 then "2 Students" else "3 or More Students" , type text}}),
    #"Grouped Rows1" = Table.Group(#"Grouped Rows", {"House Size"}, {{"Count of Houses", each Table.RowCount(_), Int64.Type}})
in
    #"Grouped Rows1"

 

Helpful resources

Announcements
OCT PBI Update Carousel

Power BI Monthly Update - October 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

October NL Carousel

Fabric Community Update - October 2024

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

Top Solution Authors