How to count events in a calendar year?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,676
Office Version
  1. 365
Platform
  1. Windows
Suppose I have a log of lunches I have attended and I want to find out how many times I had lunch with each person overall and in each year. I was able to get the count of the total number of times, but my formula for the count in a year gets a Spill error.

Any suggestions? Is there a better way? Thanks

Lunch log test.xlsm
BCDEFGHI
3Table Name: TblDatesTable Name: TblNames
4DateName(s)LocationNameTotal20222023
59/15/22Sleepy, GrumpyBrew'd AwakeningBugs Bunny3#SPILL!
68/01/22Scarlet O'Hara, Henrietta Horne, Helen BacqueThai Me UpElbert Wonmug2#SPILL!
77/04/22Henrietta Horne, Scarlet O'HaraLox, Stock, & BagelsFred Flintstone1#SPILL!
86/25/22Helen Bacque, Rhett ButlerWish You Were BeerGrumpy1#SPILL!
96/16/22Bugs BunnyGarden of Eat'nHelen Bacque4#SPILL!
106/05/22Henrietta Horne, Helen BacqueDairy GodmotherHenrietta Horne4#SPILL!
116/03/22Bugs BunnyLettuce Turnip the BeetNeal Andertal1#SPILL!
125/19/22Rhett Butler, Scarlet O'Hara, Pogo PossumCajun DelightPete Moss1#SPILL!
133/01/22Helen BacqueHank's Hot RibsPogo Possum1#SPILL!
141/15/22Pete Moss, Bugs BunnyGarden CenterRhett Butler2#SPILL!
1512/10/21Scarlet O'HaraBread ZeppelinScarlet O'Hara5#SPILL!
168/25/21Scarlet O'HaraAesop's BagelsSleepy1#SPILL!
176/13/21Elbert WonmugAesop's Tables
183/05/21Neal Andertal, Fred Flintstone, Elbert WonmugHard Rock Café
192/03/21Henrietta HorneAwful Annie's
Lunch Tallies
Cell Formulas
RangeFormula
G5:G16G5=COUNTIFS(TblDates[Name(s)],"*" & [@Name] & "*")
H5:H16H5=COUNTIFS(TblDates[Name(s)], "*" & [@Name] & "*", TblDates[Date], YEAR(TblDates[Date])=TblNames[[#Headers],[2022]])
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Please try this
Book1
BCDEFGHI
3Table Name: TblDatesTable Name: TblNames
4DateName(s)LocationNameTotal20222023
59/15/2022Sleepy, GrumpyBrew'd AwakeningBugs Bunny431
68/1/2022Scarlet O'Hara, Henrietta Horne, Helen BacqueThai Me UpElbert Wonmug200
77/4/2022Henrietta Horne, Scarlet O'HaraLox, Stock, & BagelsFred Flintstone100
86/25/2022Helen Bacque, Rhett ButlerWish You Were BeerGrumpy110
96/16/2022Bugs BunnyGarden of Eat'nHelen Bacque541
106/5/2022Henrietta Horne, Helen BacqueDairy GodmotherHenrietta Horne430
116/3/2022Bugs BunnyLettuce Turnip the BeetNeal Andertal100
125/19/2022Rhett Butler, Scarlet O'Hara, Pogo PossumCajun DelightPete Moss110
133/1/2022Helen BacqueHank's Hot RibsPogo Possum110
141/15/2022Pete Moss, Bugs BunnyGarden CenterRhett Butler220
1512/10/2021Scarlet O'HaraBread ZeppelinScarlet O'Hara530
168/25/2021Scarlet O'HaraAesop's BagelsSleepy110
176/13/2021Elbert WonmugAesop's Tables
183/5/2021Neal Andertal, Fred Flintstone, Elbert WonmugHard Rock Café
192/3/2021Henrietta HorneAwful Annie's
202/15/2023Bugs BunnyDoods Deluge
213/23/2023Helen BacqueDans Dojo
Sheet1
Cell Formulas
RangeFormula
G5:G16G5=COUNTIFS(TblDates[Name(s)],"*" & [@Name] & "*")
H5:H16H5=SUM(COUNTIFS(TblDates[Name(s)], "*" & [@Name] & "*", TblDates[Date], ">="&DATE(TblNames[[#Headers],[2022]],1,1),TblDates[Date],"<="&DATE(TblNames[[#Headers],[2022]],12,31)))
I5:I16I5=SUM(COUNTIFS(TblDates[Name(s)], "*" & [@Name] & "*", TblDates[Date], ">="&DATE(TblNames[[#Headers],[2023]],1,1),TblDates[Date],"<="&DATE(TblNames[[#Headers],[2023]],12,31)))
 
Upvote 0
Jennifer here is an alternative with Power Query

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
    Year = Table.TransformColumns(Source,{{"Date", Date.Year, Int64.Type}}),
    SplitColumnbyDelimiter = Table.ExpandListColumn(Table.TransformColumns(Year, {{"Name(s)", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Name(s)"),
    TrimmedText = Table.TransformColumns(SplitColumnbyDelimiter,{{"Name(s)", Text.Trim, type text}}),
    GroupedByYearAndGuest = Table.Group(TrimmedText, {"Date", "Name(s)"}, {{"Count", each Table.RowCount(_), Int64.Type}})
in
    GroupedByYearAndGuest

Book3
ABCDEFG
2DateName(s)LocationDateName(s)Count
39/15/2022Sleepy, GrumpyBrew'd Awakening2022Sleepy1
48/1/2022Scarlet O'Hara, Henrietta Horne, Helen BacqueThai Me Up2022Grumpy1
57/4/2022Henrietta Horne, Scarlet O'HaraLox, Stock, & Bagels2022Scarlet O'Hara4
66/25/2022Helen Bacque, Rhett ButlerWish You Were Beer2022Henrietta Horne3
76/16/2022Bugs BunnyGarden of Eat'n2022Helen Bacque4
86/5/2022Henrietta Horne, Helen BacqueDairy Godmother2022Rhett Butler2
96/3/2022Bugs BunnyLettuce Turnip the Beet2022Bugs Bunny3
105/19/2022Rhett Butler, Scarlet O'Hara, Pogo PossumCajun Delight2022Pogo Possum1
113/1/2022Helen BacqueHank's Hot Ribs2022Pete Moss1
121/15/2022Pete Moss, Bugs BunnyGarden Center2021Scarlet O'Hara1
133/7/2022Scarlet O'HaraBread Zeppelin2021Elbert Wonmug2
148/25/2021Scarlet O'HaraAesop's Bagels2021Neal Andertal1
156/13/2021Elbert WonmugAesop's Tables2021Fred Flintstone1
163/5/2021Neal Andertal, Fred Flintstone, Elbert WonmugHard Rock Café2021Henrietta Horne1
172/3/2021Henrietta HorneAwful Annie's
Sheet2
 
Upvote 0
Please try this

That clearly works. Thanks. I guess CountIfs won't allow me to pass the criteria to a function for processing prior to making the comparison, like this:

Excel Formula:
=SUM(COUNTIFS(TblDates[Names], "*" & [@Name] & "*",   YEAR(TblDates[Date]), TblNames[[#Headers],[2022]]))

It seems like it should work as well as the last column here where I extracted the year to a separate column.

Lunch log test.xlsm
BCDEFGHIJK
3Table Name: TblDatesTable Name: TblNames
4DateYearNamesLocationNameTotal202120222022-2
52/03/212021Henrietta HorneAwful Annie'sBugs Bunny3033
63/05/212021Neal Andertal, Fred Flintstone, Elbert WonmugHard Rock CaféElbert Wonmug2200
76/13/212021Elbert WonmugAesop's TablesFred Flintstone1100
88/25/212021Scarlet O'HaraAesop's BagelsGrumpy1011
912/10/212021Scarlet O'HaraBread ZeppelinHelen Bacque4044
101/15/222022Pete Moss, Bugs BunnyGarden CenterHenrietta Horne4133
113/01/222022Helen BacqueHank's Hot RibsNeal Andertal1100
125/19/222022Rhett Butler, Scarlet O'Hara, Pogo PossumCajun DelightPete Moss1011
136/03/222022Bugs BunnyLettuce Turnip the BeetPogo Possum1011
146/05/222022Henrietta Horne, Helen BacqueDairy GodmotherRhett Butler2022
156/16/222022Bugs BunnyGarden of Eat'nScarlet O'Hara5233
166/25/222022Helen Bacque, Rhett ButlerWish You Were BeerSleepy1011
177/04/222022Henrietta Horne, Scarlet O'HaraLox, Stock, & Bagels
188/01/222022Scarlet O'Hara, Henrietta Horne, Helen BacqueThai Me Up
199/15/222022Sleepy, GrumpyBrew'd Awakening
Lunch Tallies
Cell Formulas
RangeFormula
H5:H16H5=COUNTIFS(TblDates[Names],"*" & [@Name] & "*")
I5:I16I5=SUM(COUNTIFS(TblDates[Names], "*" & [@Name] & "*", TblDates[Date], ">="&DATE(TblNames[[#Headers],[2021]],1,1), TblDates[Date],"<="&DATE(TblNames[[#Headers],[2021]],12,31)))
J5:J16J5=SUM(COUNTIFS(TblDates[Names], "*" & [@Name] & "*", TblDates[Date], ">="&DATE(TblNames[[#Headers],[2022]],1,1), TblDates[Date],"<="&DATE(TblNames[[#Headers],[2022]],12,31)))
K5:K16K5=COUNTIFS(TblDates[Names], "*" & [@Name] & "*", TblDates[Year], "="&TblNames[[#Headers],[2022]])
C5:C19C5=YEAR([@Date])
 
Upvote 0
I guess CountIfs won't allow me to pass the criteria to a function for processing prior to making the comparison,
Unfortunately Countifs (incl the other varitions ie Sumifs) spefically require a Range in the Criteria Lookup position and won't accept an array.
Putting a function around the range converts it to an array.
=countifs( criteria_range, criteria,...)

I am not sure you get much mileage out of using the Structured referencing for getting the value from the heading.
Here is another option.

Excel Formula:
=SUM(FILTER(--ISNUMBER(TblDates[Date]), (YEAR(TblDates[Date])=VALUE(H$4)) * (ISNUMBER(SEARCH([@Name],TblDates[Name(s)]))) ,0) )
 
Last edited:
Upvote 0
Solution
Unfortunately Countifs (incl the other varitions ie Sumifs) spefically require a Range in the Criteria Lookup position and won't accept an array.
Putting a function around the range converts it to an array.
=countifs( criteria_range, criteria,...)

I am not sure you get much mileage out of using the Structured referencing for getting the value from the heading.
Here is another option.

Excel Formula:
=SUM(FILTER(--ISNUMBER(TblDates[Date]), (YEAR(TblDates[Date])=VALUE(H$4)) * (ISNUMBER(SEARCH([@Name],TblDates[Name(s)]))) ,0) )

Cool. Another very useful function that I was not aware of. 🤔 Thanks

Why do you say that it is better (mileage?) than the other (structured) solution. Isn't a "structured" reference simply a table reference? And aren't you using table references in your solution?
 
Upvote 0
I now have three solutions.
  1. The 2021 column uses the Jeffrey Mahoney solution.
  2. The 2021-2 column uses the Alex Blankenburg solution.
  3. The 2021-3 column makes use of a helper column ([Year]).
Lunch log test.xlsm
BCDEFGHIJKL
3Table Name: TblDatesTable Name: TblNames
4DateYearNamesLocationNameTotal1Total220212021-22021-3
52/03/212021Henrietta HorneAwful Annie'sBugs Bunny33000
63/05/212021Neal Andertal, Fred Flintstone, Elbert WonmugHard Rock CaféElbert Wonmug22222
76/13/212021Elbert WonmugAesop's TablesFred Flintstone11111
88/25/212021Scarlet O'HaraAesop's BagelsGrumpy11000
912/10/212021Scarlet O'HaraBread ZeppelinHelen Bacque44000
101/15/222022Pete Moss, Bugs BunnyGarden CenterHenrietta Horne44111
113/01/222022Helen BacqueHank's Hot RibsNeal Andertal11111
125/19/222022Rhett Butler, Scarlet O'Hara, Pogo PossumCajun DelightPete Moss11000
136/03/222022Bugs BunnyLettuce Turnip the BeetPogo Possum11000
146/05/222022Henrietta Horne, Helen BacqueDairy GodmotherRhett Butler22000
156/16/222022Bugs BunnyGarden of Eat'nScarlet O'Hara55222
166/25/222022Helen Bacque, Rhett ButlerWish You Were BeerSleepy11000
177/04/222022Henrietta Horne, Scarlet O'HaraLox, Stock, & Bagels
188/01/222022Scarlet O'Hara, Henrietta Horne, Helen BacqueThai Me Up
199/15/222022Sleepy, GrumpyBrew'd Awakening
Lunch Tallies
Cell Formulas
RangeFormula
H5:H16H5=COUNTIFS(TblDates[Names],"*" & [@Name] & "*")
I5:I16I5=COUNT(SEARCH([@Name],TblDates[Names]))
J5:J16J5=SUM(COUNTIFS(TblDates[Names], "*" & [@Name] & "*", TblDates[Date], ">="&DATE(TblNames[[#Headers],[2021]],1,1), TblDates[Date],"<="&DATE(TblNames[[#Headers],[2021]],12,31)))
K5:K16K5=SUM(FILTER(--ISNUMBER(TblDates[Date]), (YEAR(TblDates[Date])=VALUE(TblNames[[#Headers],[2021]])) * (ISNUMBER(SEARCH([@Name],TblDates[Names]))),0) )
L5:L16L5=COUNTIFS(TblDates[Names], "*" & [@Name] & "*", TblDates[Year], "="&TblNames[[#Headers],[2021]])
C5:C19C5=YEAR([@Date])
 
Upvote 0
Why do you say that it is better (mileage?) than the other (structured) solution. Isn't a "structured" reference simply a table reference? And aren't you using table references in your solution?
You misread my comment and in doing so changed my suggested formula in your summarised version.
I was questioning whether there is any harm in deviating from using structured referencing when it doesn't suit ?
I did not use
VALUE(TblNames[[#Headers],[2021]]))
per your summarised version, in my suggestion since that structured reference hard codes the heading column to 2021 (absolute referencing it) so that when you copy it to the next column it doesn't change to 2022, then 2023 in the next etc.

I used
VALUE(H$4)
Only locking in the row but leaving the column as relative so that it could easily be copied across.
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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