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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
romoguy15
Helper IV
Helper IV

Distinct Count # of unique dates based on column with duplicates

Hello Everyone,

 

I have a two column table, one with ticket IDs and another with dates. The first column of ticket IDs could have uniqe or duplicate ticket IDs. The other date column could have also have unique or the same dates. What I am trying to accomplish in query editor is to transform the ticket ID column and count the total of unique dates. I tried to use the Group By feature to group all the ticket IDs but it does not let me do a unique count of the closed date column. I tried to post a snip below from excel to illustate what the data looks like ontop, and then below what I would like to transform the data into in query editor. Essentially I am trying to show only the unique ticket IDs in one column and in a new column the total distinct count of closed dates. 

 

Also, I have some sample data if it helps.

https://1drv.ms/u/s!AqID1H0nHPOzhAx5J1S-cMv2vPn7?e=YN5zDz

 

image.png

 

Thank you in advance

 

 

1 ACCEPTED SOLUTION

Hi,

This should typically be done directly in the visual but if you must do it in the Query Editor, then this M code works

let
    Source = Excel.Workbook(File.Contents("C:\Users\mathu\Desktop\try.xlsx"), null, true),
    #"Removed Other Columns" = Table.SelectColumns(Source,{"Data"}),
    #"Expanded Data" = Table.ExpandTableColumn(#"Removed Other Columns", "Data", {"Column1", "Column2"}, {"Data.Column1", "Data.Column2"}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Expanded Data", [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Ticket Id", type text}, {"Ticket Closed Date", type date}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Ticket Id", Order.Ascending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Ticket Id"}, {{"Count", each Table.RowCount(Table.Distinct(_)), type nullable date}})
in
    #"Grouped Rows"

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

View solution in original post

10 REPLIES 10
C2
Frequent Visitor

what if I have a visual with a Signout  Date on X axis &  count distinct fo Loads on Y axis, how do I do to be able to add a line to shows the distinct count of loads scheduled for  for that same date of signt out, being that if I  put the Loads column on the Line Y field it will show the count of the Loads that were signed out,  please note that scheduled date is one column & signout its another.

Hi,

Share some data, explain the question and show the expected result.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
romoguy15
Helper IV
Helper IV

Is there any other additional information I could provide to help with the solution? 

Ashish_Mathur
Super User
Super User

Hi,

What problem are you facing when using Group By in the Query Editor?


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

Hi @Ashish_Mathur , when I use Group By, it does succesfully groups all ticket IDs to only show me the unique ID(kind of like removing duplicates) but it is not showing me the count of each unique date. It only does a count of the dates.  Below is a snip

romoguy15_1-1629121360834.png

 

 

For example, if ticket ID148475 had four dates, it would count four total dates even though that ID had two dates that were different and two dates that were the same.

romoguy15_0-1629121248302.png

 

Hi,

This should typically be done directly in the visual but if you must do it in the Query Editor, then this M code works

let
    Source = Excel.Workbook(File.Contents("C:\Users\mathu\Desktop\try.xlsx"), null, true),
    #"Removed Other Columns" = Table.SelectColumns(Source,{"Data"}),
    #"Expanded Data" = Table.ExpandTableColumn(#"Removed Other Columns", "Data", {"Column1", "Column2"}, {"Data.Column1", "Data.Column2"}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Expanded Data", [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Ticket Id", type text}, {"Ticket Closed Date", type date}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Ticket Id", Order.Ascending}}),
    #"Grouped Rows" = Table.Group(#"Sorted Rows", {"Ticket Id"}, {{"Count", each Table.RowCount(Table.Distinct(_)), type nullable date}})
in
    #"Grouped Rows"

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

@Ashish_Mathur this is exactly what I needed, thankyou. I have to do it in query editor specifically because I am merging it to another query within editor and then eliminating everything that does not match. I really apprecaite the solution. Though, I am curious. Did you have another method of doing this in adding a new column with dax?

You are welcome.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
CNENFRNL
Community Champion
Community Champion

# Distinct Dates = DISTINCTCOUNT( 'Table'[Ticket Closed Date] )

Literally, filter context in DAX already plays the role to group and filter dataset; unnecessary to explicitly write "group by" in most cases.


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

@CNENFRNL  yes but that does not take into account the duplicate ticket IDs. Yes I can do a  distinct count of just the dates but it's not factoring the ticket ID. 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.