This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. We're covering it all. You won't want to miss it.
Learn moreLevel up your Power BI skills this month - build one visual each week and tell better stories with data! Get started
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 List | Continent | Number of Distinct Country Per Continent (expected result) |
| China | Asia | 2 |
| India | Asia | 2 |
| England | Europe | 3 |
| France | Europe | 3 |
| Germany | Europe | 3 |
| France | Europe | 3 |
| USA | North America | 1 |
| Egypt | Africa | 1 |
| India | Asia | 2 |
| USA | North America | 1 |
Solved! Go to Solution.
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:
The result looks like:
I hope this is what you are expecting.
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:
The result looks like:
I hope this is what you are expecting.
Check out the April 2026 Power BI update to learn about new features.
Sign up to receive a private message when registration opens and key events begin.
If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.
| User | Count |
|---|---|
| 36 | |
| 33 | |
| 31 | |
| 24 | |
| 18 |
| User | Count |
|---|---|
| 70 | |
| 50 | |
| 33 | |
| 24 | |
| 24 |