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

Level up your Power BI skills this month - build one visual each week and tell better stories with data! Get started

Reply
NSBS
Helper I
Helper I

Distinct Row Count with Condition

Hi,

 

Kindly help to advise the M language code to count distinct number of country per continent as per below table
Note: all rows should be remained the same

Country ListContinentNumber of Distinct Country Per Continent (expected result)
ChinaAsia2
IndiaAsia2
EnglandEurope3
FranceEurope3
GermanyEurope3
FranceEurope3
USANorth America1
EgyptAfrica1
IndiaAsia2
USANorth America1
1 ACCEPTED SOLUTION
PC2790
Community Champion
Community Champion

Here is the Power Query code to do it:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs7IzEtU0lFyLM5MVIrViVbyzEvJRBFwzUvPScxLAQq5lhblF6SCBd2KEvOSU1HF3FOLchPzKgkrDA12BAr45ReVZCg45qYWZSZDbUqvLCgBWZ0GF8JwDVa9sQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Country List" = _t, Continent = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Country List", type text}, {"Continent", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Continent"}, {{"Count", each Table.RowCount(Table.Distinct(_)), Int64.Type}, {"All", each _, type table [Country List=nullable text, Continent=nullable text]}}),
    #"Expanded All" = Table.ExpandTableColumn(#"Grouped Rows", "All", {"Country List"}, {"All.Country List"})
in
    #"Expanded All"

I have simply used the concept of Grouping here.

Grouping parameters look like:

PC2790_0-1649822058749.png

The result looks like:

PC2790_1-1649822088592.png

I hope this is what you are expecting.

 

 

View solution in original post

2 REPLIES 2
PC2790
Community Champion
Community Champion

Here is the Power Query code to do it:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wcs7IzEtU0lFyLM5MVIrViVbyzEvJRBFwzUvPScxLAQq5lhblF6SCBd2KEvOSU1HF3FOLchPzKgkrDA12BAr45ReVZCg45qYWZSZDbUqvLCgBWZ0GF8JwDVa9sQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Country List" = _t, Continent = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Country List", type text}, {"Continent", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Continent"}, {{"Count", each Table.RowCount(Table.Distinct(_)), Int64.Type}, {"All", each _, type table [Country List=nullable text, Continent=nullable text]}}),
    #"Expanded All" = Table.ExpandTableColumn(#"Grouped Rows", "All", {"Country List"}, {"All.Country List"})
in
    #"Expanded All"

I have simply used the concept of Grouping here.

Grouping parameters look like:

PC2790_0-1649822058749.png

The result looks like:

PC2790_1-1649822088592.png

I hope this is what you are expecting.

 

 

amitchandak
Super User
Super User

@NSBS , refer how to get subtotal in power query

https://www.youtube.com/watch?v=ad7HIQTs_t8

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

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

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.