COUNT distinct by date

jimrward

Well-known Member
Joined
Feb 24, 2003
Messages
1,877
Office Version
  1. 2016
  2. 2013
  3. 2011
  4. 2010
  5. 2007
Platform
  1. Windows
part 2 of trying to learn Power Query
my set of data below is an attendance log, where a person can participate in an activity more than once in a day, so what I would like to do is summarise the data, by person showing total activity count and the count of individual days, so even if a person does 3 activities in a day that counts as 1 visit
CurrentData.xlsx
ABCDEFGHI
1DateYearQuarterActivityNameNameActivityCountVisits
220/03/202020201S5 M10 T15BruceJim205
320/03/202020201S4 M10 T14BruceBruce114
413/03/202020201S5 M10 T15BruceMike105
513/03/202020201S5 M10 T15Bruce
606/03/202020201S9 M10 T19Bruce
706/03/202020201S4 M14 T18Bruce
828/02/202020201S5 M10 T15Bruce
928/02/202020201S3 M10 T13Bruce
1028/02/202020201S4 M10 T14Bruce
1128/02/202020201S6 M10 T16Bruce
1228/02/202020201S3 M10 T13Bruce
1330/01/202320231S5 M10 T15Mike
1423/01/202320231S7 M10 T17Mike
1523/01/202320231S3 M10 T13Mike
1616/01/202320231S4 M10 T14Mike
1716/01/202320231S3 M10 T13Mike
1809/01/202320231S8 M10 T18Mike
1912/12/202220224S12 M0 T12Mike
2012/12/202220224S5 M10 T15Mike
2112/12/202220224S3 M10 T13Mike
2212/12/202220224S1 M10 T11Mike
2303/02/202320231S7 M20 T27Jim
2403/02/202320231S4 M20 T24Jim
2503/02/202320231S5 M10 T15Jim
2603/02/202320231S5 M10 T15Jim
2730/01/202320231S6 M10 T16Jim
2830/01/202320231S5 M10 T15Jim
2930/01/202320231S5 M10 T15Jim
3027/01/202320231S4 M10 T14Jim
3127/01/202320231S6 M20 T26Jim
3227/01/202320231S20 M10 T30Jim
3327/01/202320231S7 M20 T27Jim
3420/01/202320231S9 M10 T19Jim
3520/01/202320231S3 M20 T23Jim
3620/01/202320231S5 M20 T25Jim
3720/01/202320231S9 M11 T20Jim
3820/01/202320231S3 M10 T13Jim
3920/01/202320231S14 M10 T24Jim
4013/01/202320231S17 M20 T37Jim
4113/01/202320231S4 M20 T24Jim
4213/01/202320231S4 M10 T14Jim
Sheet1
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
This should do

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    grp = Table.Group(Source, {"Name"}, {{"Count", each _, type table [Date=nullable datetime]}}),
    agg = Table.AggregateTableColumn(grp, "Count", {{"Date", List.Count, "ActivityCount"}, {"Date", each List.Count(List.Distinct(_)), "Visits"}})
in
    agg
 
Upvote 2
Many thanks for the quick reply I will try and disseminate it, step by step, whilst power query is powerful it ain’t half obtuse in its syntax
 
Upvote 0
@JEC once again many thanks, what would I need to add to the above to group by year and quarter
 
Upvote 0
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Result = Table.Group(Source, {"Name", "Year", "Quarter"}, {{"Activity Count", each List.Count(_[Date])},{"Visits", each List.Count(List.Distinct(_[Date]))}})
in
    Result
 
Upvote 2
Thanks to both works perfectly, now applying it to my mountains of data, unfortunately the forum doesnt allow a double tick for solution so I will try an like both solutions
 
Upvote 0
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Result = Table.Group(Source, {"Name", "Year", "Quarter"}, {{"Activity Count", each List.Count(_[Date])},{"Visits", each List.Count(List.Distinct(_[Date]))}})
in
    Result
I always love your (SICK!) code, and envy your ability to use as few steps as possible. However, in this case the Result table doesn't quite fit the bill (even with removing unwanted columns):
Book1
GHI
6NameActivity CountVisits
7Bruce114
8Mike64
9Mike41
10Jim205
Sheet1

While not quite as efficient as yours, @JEC 's code does result in the table requested:
Book1
KLM
1NameActivityCountVisits
2Bruce114
3Mike105
4Jim205
Sheet1

I only mention it because my fix was boring - just adding another Grouping by name and summing the other 2 columns, but I'm sure you'd have a more elegant solution!
 
Upvote 0
JGordon11’s code is for grouping by year and quarter
JEC’s code covers the initial remit
 
Upvote 0

Forum statistics

Threads
1,215,136
Messages
6,123,246
Members
449,093
Latest member
Vincent Khandagale

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top