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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
thegusman
Helper I
Helper I

Distinct Count in Query Editor... Is Group by the only solution

I have multiple  columns that I need to do a distinct count within query editor.  It seems that Group By is the only option...  Do I need to preform a group by for every column, delete the extra columns created, and rename them when I'm done?  Is this best practice for distinct column value counts in query editor?

8 REPLIES 8
artemus
Microsoft Employee
Microsoft Employee

If you want a distinct count of various columns you could:

 

[

   Column1Count = List.Count(List.Distinct(Table[Column1])),

   Column2Count = List.Count(List.Distinct(Table[Column2])),

   Column3Count = List.Count(List.Distinct(Table[Column3])),

   ...

]

Thanks Artemus, 

 

I may have mispoke on what I was trying to accomplish.  

Column 1 contains many different values.  I want to see the frequecy of every value within that column in another column.  I could do that with a group by Count, and add All Rows, but my problem is that there are many columns in that table that I also need the value's frequency. 

I am dreading the thought of doing a group by 10+ times, and then cleaning.  Especially since my data is 300k+ rows.

What is the format of the table you want to have at the end? E.g.

 

ColumnNameTermCount
Column1Value14
Column1Value26
Column2Value11

FruitVegetableFruitCountVegetableCount
AppleCarrot12
OrangeCarrot32
PeachCucumber22
PeachTomato21
OrangeOnion31
OrangeCucumber32

Ok, you do realize that your table isn't normalized though? E.g. In your example above, you have Orange 3 times, and for all 3 times FruitCount is 3. This can cause issues when you aggergate it on the visualization layer.

 

It is possible to do this, but it may not be what you want.

I realize this table is very simple, but I have many columns that need to be counted.

I used a Text.Combine to group the columns that need to be counted together, since there are so many. 

Hi @thegusman 

You could create calculated columns in Data model view,

Capture6.JPG

fruit count = CALCULATE( COUNT('Table'[Fruit]),ALLEXCEPT('Table','Table'[Fruit]))

vege count = CALCULATE( COUNT('Table'[Vegetable]),ALLEXCEPT('Table','Table'[Vegetable]))

 

or create measures which can change with slicers

fruit count m = CALCULATE( COUNT('Table'[Fruit]),FILTER(ALLSELECTED('Table'),'Table'[Fruit]=MAX('Table'[Fruit])))

vege count m = CALCULATE( COUNT('Table'[Vegetable]),FILTER(ALLSELECTED('Table'),'Table'[Vegetable]=MAX('Table'[Vegetable])))

 

Best Regards
Maggie
Community Support Team _ Maggie Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Hi @thegusman ,

 

same as above in the @artemus post, but agnostic to column names:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Vc3LDQAhCEXRXli7APxSi6H/NsanYjILSe4J6pykLEaJhNcY5AmiCJE1LAQhehYv7arHL2VUw8hB5W2VIBTvcwE3zp8tqKPsR+O9VMn9Aw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Year = _t, Sales = _t, Costs = _t]),
    DistinctCount = List.Accumulate(Table.ColumnNames(Source), [], (s,a)=> Record.AddField(s, a, List.Count(List.Distinct(Table.Column(Source, a)))))
in
    DistinctCount

Kind regards,

JB

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.