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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Anonymous
Not applicable

Grouping

Hi there,

 

I am trying to group using a dataset similar to the example that i have set out below, i am trying create a column that says for each squad, how many different countries resources are based in, i seem to be struggling to do this at the moment using the group function, the grouping that i create shows me the breakdown of the number of people in each countries, rather than just the total number of different locations that resources are based in.  Using the rather simplistic data example i have set out below i would like to create a grouping that has two rows, one for Apple with the count showing 3 (representing the number of locations for that squad) and one for orange showing 1 (representing the number of locations) Any suggestions? 

 

Worker IDSquad NameLocation
1AppleUK
2AppleUK
3ApplePoland
4AppleUS
5AppleUS
6AppleUS
7OrangeUK
8OrangeUK
9OrangeUK
10OrangeUK
11OrangeUK
12OrangeUK
1 ACCEPTED SOLUTION
Nathaniel_C
Super User
Super User

Hi @Anonymous ,
Go to Power Query, use Group By Twice.

 


Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
Nathaniel
Original table

Nathaniel_C_1-1653937034239.png

First grouping

Nathaniel_C_0-1653936975566.png


Second grouping

Nathaniel_C_2-1653937100865.png

 

Add this to Advanced Editor to create the above table:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCs8vyk4tUvB0UdJRCi4sTUxR8EvMTQVyfPKTE0sy8/OUYnWilQyBAo4FBTkgiVBvsJARppAxklBAfk5iXgpY2ARZZTBYyBRTyAxTyBzI9C9KzEtHssMCi5glFjFDA2yChtgEjdAFYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t, #"(blank).2" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"(blank)", type text}, {"(blank).1", type text}, {"(blank).2", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Worker ID", Int64.Type}, {"Squad Name", type text}, {"Location", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type1", {"Squad Name", "Location"}, {{"Count", each Table.RowCount(_), Int64.Type}}),
    #"Grouped Rows1" = Table.Group(#"Grouped Rows", {"Squad Name"}, {{"Count", each Table.RowCount(_), Int64.Type}}),
    #"Renamed Columns" = Table.RenameColumns(#"Grouped Rows1",{{"Count", "Number of Countries"}})
in
    #"Renamed Columns"







Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

3 REPLIES 3
Ashish_Mathur
Super User
Super User

Hi,

This m code works with just a single grouping

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"Squad Name"}, {{"Count", each List.Count(List.Distinct([Location])), Int64.Type}})
in
    #"Grouped Rows"

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Nathaniel_C
Super User
Super User

Hi @Anonymous ,
You are very welcome!

Nathaniel





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Nathaniel_C
Super User
Super User

Hi @Anonymous ,
Go to Power Query, use Group By Twice.

 


Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos 👍are nice too.
Nathaniel
Original table

Nathaniel_C_1-1653937034239.png

First grouping

Nathaniel_C_0-1653936975566.png


Second grouping

Nathaniel_C_2-1653937100865.png

 

Add this to Advanced Editor to create the above table:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCs8vyk4tUvB0UdJRCi4sTUxR8EvMTQVyfPKTE0sy8/OUYnWilQyBAo4FBTkgiVBvsJARppAxklBAfk5iXgpY2ARZZTBYyBRTyAxTyBzI9C9KzEtHssMCi5glFjFDA2yChtgEjdAFYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"(blank)" = _t, #"(blank).1" = _t, #"(blank).2" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"(blank)", type text}, {"(blank).1", type text}, {"(blank).2", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers",{{"Worker ID", Int64.Type}, {"Squad Name", type text}, {"Location", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type1", {"Squad Name", "Location"}, {{"Count", each Table.RowCount(_), Int64.Type}}),
    #"Grouped Rows1" = Table.Group(#"Grouped Rows", {"Squad Name"}, {{"Count", each Table.RowCount(_), Int64.Type}}),
    #"Renamed Columns" = Table.RenameColumns(#"Grouped Rows1",{{"Count", "Number of Countries"}})
in
    #"Renamed Columns"







Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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