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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
eacy
Helper II
Helper II

Group By together with Distinct Count does not work after Index Column is added

Hi,

I'm trying to add an Index Column before I do a Group By and this makes the Distinct Count return a result that I do not understand.

Small sample to illustrate my problem below:

 

Before Index Column is added, everything work fine. Distinct Count figure out that the value for the property is different and return 2 instead of 1.

2017-10-04 15_28_19-.png

 

After the Index Colunm is added the Distinct Count is returning 2 eventhrough the value is still the same for the property belonging to "Food for Money" Client.

2017-10-04 15_29_17-.png

 

Is this an error or just me not understanding how Group By works.

I would expect Group By to group and count in the same way eventhrough there is an Index Column displayed.

 

 

Any help is appriciated, thanks

5 REPLIES 5
Bordalos
Helper I
Helper I

That's is exactly due to the index.  Let's see,

You want to count distinct rows.

When you add the index you are making each row distinct. then when you group by  client&property you have a distinct entry for each row. ie,  [food for money]&[Number of Stores]&[1] and [food for money]&[Number of Stores]&[2].

When you ask how many distinct entries for [food for money]&[Number of Stores] the result is =2 because you have index =1 +

index = 2.

Make sense?

 

Hi Bordalos,

 

Ok, Now after I have played a little more with this, it makes perfectly sense. Stupid me 🙂

 

Now in my real world scenario I got an existing ID and an Index column in the table when I want to make my distict count. Do you have any ideas on how I can get rid of them while I do my distinct count and afterwards get them back again?

 

BR

Esben

Esben, what about you first group by and in the end add the index?

 

This would be my approach:

1- Group by Client - count Distinct rows and keep all other rows

2- Expand other rows 

3- add index column

 

 

Capture.PNG

Capture2.PNG

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcsvPT1FIyy9SyM3PS61U0lHyK81NSi1SyE9TKC7JL0otBgoZKsXqkKLSF6QArNQvvyQjMy+dCoqNlGJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Client = _t, Property = _t, value = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Client", type text}, {"Property", type text}, {"value", Int64.Type}}),
#"Grouped Rows" = Table.Group(#"Changed Type", {"Client"}, {{"Distinct", each Table.RowCount(Table.Distinct(_)), type number}, {"other", each _, type table}}),
#"Expanded other" = Table.ExpandTableColumn(#"Grouped Rows", "other", {"Property", "value"}, {"Property", "value"}),
#"Reordered Columns" = Table.ReorderColumns(#"Expanded other",{"Client", "Property", "value", "Distinct"}),
#"Added Index" = Table.AddIndexColumn(#"Reordered Columns", "Index", 1, 1)
in
#"Added Index"

 

MarcelBeug
Community Champion
Community Champion

Your pictures are too small. Please zoom in on the tables.

 

Additionally, please post your code as text (you can use the button "Insert code" next to the smiley button)

Specializing in Power Query Formula Language (M)

Just click on the image and potential press the full screen button

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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