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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch 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"

 

If this post helps to answer your questions, please consider marking it as a solution so others can find it more quickly when faced with a similar challenge.

Proud to be a Microsoft Fabric Super User

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!

If this post helps to answer your questions, please consider marking it as a solution so others can find it more quickly when faced with a similar challenge.

Proud to be a Microsoft Fabric Super User

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"

 

If this post helps to answer your questions, please consider marking it as a solution so others can find it more quickly when faced with a similar challenge.

Proud to be a Microsoft Fabric Super User

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.

Top Kudoed Authors