Countifs with a regular criteria and a criteria with a date

AusSteelMan

Board Regular
Joined
Sep 4, 2009
Messages
208
Hi everyone,
Firstly, thanks for reading my question.

I have an issue where I would like to count the number of classes per subject that have occurred since a date.

This table shows the subjects that I would like to count:
Sheet1

*ABCDEFG
1DateMissed classes_P1Missed classes_P2Missed classes_P3Missed classes_P4Missed classes_P5Missed classes_P6
229/01/2016******
31/02/2016****EnglishMaths
42/02/2016****Inv MathsGraphics
53/02/2016****HSIEGraphics
64/02/2016PDHEnglishScienceHSIEMathsCAT
75/02/2016MathsInv MathsGraphicsGraphicsCATEnglish
88/02/2016****ScienceEnglish
99/02/2016MathsInv MathsScienceEnglishGraphicsHSIE
1010/02/2016EnglishMathsPDHHSIEScienceCAT
1111/02/2016ScienceMathsEnglishCATSportSport
1212/02/2016*EnglishInv MathsScienceGraphicsHSIE
1315/02/2016****EnglishMaths
1416/02/2016****Inv MathsGraphics
1517/02/2016***ScienceHSIEGraphics
1618/02/2016PDHEnglishScienceHSIEMathsCAT
1719/02/2016****CATEnglish
1822/02/2016****ScienceEnglish
1923/02/2016MathsInv MathsScienceEnglishGraphicsHSIE
2024/02/2016EnglishMathsPDHHSIEScienceCAT
2125/02/2016ScienceMathsEnglishCATSportSport
2226/02/2016MathsEnglishInv MathsScienceGraphicsHSIE
231/03/2016MathsHSIESport*Inv MathsGraphics
242/03/2016MathsEnglishSport***
253/03/2016PDHEnglishScience***
264/03/2016MathsInv MathsGraphicsGraphicsCATEnglish
277/03/2016GraphicsHSIEInv MathsInv MathsScienceEnglish
288/03/2016MathsInv MathsScienceEnglishGraphicsHSIE
299/03/2016EnglishMathsPDHHSIEScienceCAT
3010/03/2016ScienceMathsEnglishCATSportSport
3111/03/2016MathsEnglishInv MathsScienceGraphicsHSIE
3214/03/2016ScienceHSIEInv MathsCATEnglishMaths
3315/03/2016MathsHSIESportScienceInv MathsGraphics
3416/03/2016MathsEnglishSportScienceHSIEGraphics
3517/03/2016PDHEnglishScienceHSIEMathsCAT
3618/03/2016MathsInv MathsGraphicsGraphicsCATEnglish
3721/03/2016GraphicsHSIEInv MathsInv Maths**
3822/03/2016******

<colgroup><col style="font-weight:bold; width:30px; "><col style="width:64px;"><col style="width:137px;"><col style="width:137px;"><col style="width:137px;"><col style="width:137px;"><col style="width:137px;"><col style="width:137px;"></colgroup><tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4

This table shows each subject, the last date (i.e. I want to count each subject that falls after this date), the column where I have been trying to calculate it and finally the results that I think I should have (colour coded from the first table just to make it easier to see. it serves no other purpose)

Sheet1

*IJKL
1SubjectLast DateLessons missed since last attendanceAnswer should be
2Maths3/03/2016#VALUE!10
3English21/03/2016#VALUE!0
4Science21/03/2016#VALUE!0
5Inv Maths22/02/2016#VALUE!13
6Graphics2/03/2016#VALUE!10
7HSIE3/03/2016#VALUE!9
8CAT3/03/2016#VALUE!6
9PDHNever#VALUE!7
10Sport17/02/2016#VALUE!8

<colgroup><col style="font-weight:bold; width:30px; "><col style="width:68px;"><col style="width:136px;"><col style="width:252px;"><col style="width:134px;"></colgroup><tbody>
</tbody>

Spreadsheet Formulas
CellFormula
K2=COUNTIFS(Table1[[Missed classes_P1]:[Missed classes_P6]],[@Subject],Table1[Date],">="&[@[Last Date]])

<tbody>
</tbody>

<tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4


I will note that in the real workbook:

  • the "Last date" is a calculated answer
  • the "Missed classes_Pn" are calculated
  • Both tables are Excel Tables not just pretty formatting

Can anybody please help out with fixing the formula so it works?

I have tried the first part of the formula (counting all occurrences of each subject) by itself and it works OK.
I have tried a few variants of the date portion but just can't get it.
I have tried using on a fixed date without any greater than or greater than equal to where I know a subject exists (so should get a count of 1) but still doesn't work at all.
I have searched this site and others but just can't find anything that works

If I need to use helper columns etc, that is OK. For a number of reasons I'd prefer to avoid any VBA.
The "Never" "Last Date" can easily be replaced with a real date (such as 28/1/2016)

Many thanks in advance.

Cheers,
Darren
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
So I have come up with a helper column "solution" that really is fairly ugly but seems to work.

Sheet1

*ABCDEFGHIJK
1DateMissed classes_P1Missed classes_P2Missed classes_P3Missed classes_P4Missed classes_P5Missed classes_P6MathsEnglishGraphicsPDH
229/01/2016******0000
31/02/2016****EnglishMaths0000
42/02/2016****Inv MathGraphics0000
53/02/2016****HSIEGraphics0000
64/02/2016PDHEnglishScienceHSIEMathsCAT0001
75/02/2016MathsInv MathGraphicsGraphicsCATEnglish0000
88/02/2016****ScienceEnglish0000
99/02/2016MathsInv MathScienceEnglishGraphicsHSIE0000
1010/02/2016EnglishMathsPDHHSIEScienceCAT0001
1111/02/2016ScienceMathsEnglishCATSportSport0000
1212/02/2016*EnglishInv MathScienceGraphicsHSIE0000
1315/02/2016****EnglishMaths0000
1416/02/2016****Inv MathGraphics0000
1517/02/2016***ScienceHSIEGraphics0000
1618/02/2016PDHEnglishScienceHSIEMathsCAT0001
1719/02/2016****CATEnglish0000
1822/02/2016****ScienceEnglish0000
1923/02/2016MathsInv MathScienceEnglishGraphicsHSIE0000
2024/02/2016EnglishMathsPDHHSIEScienceCAT0001
2125/02/2016ScienceMathsEnglishCATSportSport0000
2226/02/2016MathsEnglishInv MathScienceGraphicsHSIE0000
231/03/2016MathsHSIESport*Inv MathGraphics0000
242/03/2016MathsEnglishSport***0000
253/03/2016PDHEnglishScience***0001
264/03/2016MathsInv MathGraphicsGraphicsCATEnglish1020
277/03/2016GraphicsHSIEInv MathInv MathScienceEnglish0010
288/03/2016MathsInv MathScienceEnglishGraphicsHSIE1010
299/03/2016EnglishMathsPDHHSIEScienceCAT1001
3010/03/2016ScienceMathsEnglishCATSportSport1000
3111/03/2016MathsEnglishInv MathScienceGraphicsHSIE1010
3214/03/2016ScienceHSIEInv MathCATEnglishMaths1000
3315/03/2016MathsHSIESportScienceInv MathGraphics1010
3416/03/2016MathsEnglishSportScienceHSIEGraphics1010
3517/03/2016PDHEnglishScienceHSIEMathsCAT1001
3618/03/2016MathsInv MathGraphicsGraphicsCATEnglish1020
3721/03/2016GraphicsHSIEInv MathInv Math**0010
3822/03/2016******0000
39Total******100107

<colgroup><col style="font-weight:bold; width:30px; "><col style="width:64px;"><col style="width:137px;"><col style="width:137px;"><col style="width:137px;"><col style="width:137px;"><col style="width:137px;"><col style="width:137px;"><col style="width:64px;"><col style="width:64px;"><col style="width:64px;"><col style="width:64px;"></colgroup><tbody>
</tbody>

Spreadsheet Formulas
CellFormula
H26=IF([@Date]>INDEX(Table2[Last Date],MATCH(Table1[[#Headers],[Maths]],Table2[Subject],0)),COUNTIF(Table1[@[Missed classes_P1]:[Missed classes_P6]],Table1[[#Headers],[Maths]]),0)
I26=IF([@Date]>INDEX(Table2[Last Date],MATCH(Table1[[#Headers],[English]],Table2[Subject],0)),COUNTIF(Table1[@[Missed classes_P1]:[Missed classes_P6]],Table1[[#Headers],[English]]),0)
J26=IF([@Date]>INDEX(Table2[Last Date],MATCH(Table1[[#Headers],[Graphics]],Table2[Subject],0)),COUNTIF(Table1[@[Missed classes_P1]:[Missed classes_P6]],Table1[[#Headers],[Graphics]]),0)
K26=IF([@Date]>INDEX(Table2[Last Date],MATCH(Table1[[#Headers],[PDH]],Table2[Subject],0)),COUNTIF(Table1[@[Missed classes_P1]:[Missed classes_P6]],Table1[[#Headers],[PDH]]),0)

<tbody>
</tbody>

<tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4

Each helper column (only 4 at this stage) counts that subject (column name) if the date if after the "Last Date" in the second table.


Sheet1

*MNOPQ
1SubjectLast DateLessons missed since last attendanceAnswer should beTotals (Index/Match)
2Maths3/03/2016101010
3English21/03/2016000
4Science21/03/2016*0#N/A
5Inv Math22/02/2016*13#N/A
6Graphics2/03/2016101010
7HSIE3/03/2016*9#N/A
8CAT3/03/2016*6#N/A
9PDH28/01/2016*77
10Sport17/02/2016*8#N/A

<colgroup><col style="font-weight:bold; width:30px; "><col style="width:68px;"><col style="width:136px;"><col style="width:252px;"><col style="width:134px;"><col style="width:147px;"></colgroup><tbody>
</tbody>

Spreadsheet Formulas
CellFormula
O2=SUM(Table1[Maths])
Q2=INDEX(Table1[#Totals],MATCH([@Subject],Table1[#Headers],0))

<tbody>
</tbody>

<tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4

Then the desired answer is a sum of each helper column. Then in the extra column I used an Index/Match after turning on the Totals row on the first table to "lookup" the result. this extra step allows each row formula to be the same, whereas the other requires each cell to have its own formula (defeating a primary aim of an Excel Table)

Like I say, I don't like it much since it uses many extra columns and calculations to do something that seems logical that SUMIFS should be able to do.

Anybody got any comments and/or suggestion/fixes?

Cheers,
Darren
 
Upvote 0
Can any legends out there help me out please?

I have been searching again but no luck. i see there were a few other countif questions since my last post, but I don't see how I can apply any of those specific answers.


A question though:
Have i been clear enough in my question to date? Is there any other info I could provide that may help?

I have a table with a date reference followed by the 6 classes columns.
I need to count the number of times a subject appears in the classes column but only after the corresponding date for each subject.

Many thanks,
Darren
 
Upvote 0
I
J
K
L
1
SubjectLast DateLessons missed since last attendanceAnswer should be
2
Maths
3/3/2016​
10​
10​
3
English
3/21/2016​
0​
0​
4
Science
3/21/2016​
0​
0​
5
Inv Maths
2/22/2016​
13​
13​
6
Graphics
3/2/2016​
10​
10​
7
HSIE
3/3/2016​
9​
9​
8
CAT
3/3/2016​
6​
6​
9
PDH
7​
7​
10
Sport
2/17/2016​
8​
8​
11

<tbody>
</tbody>


K2=SUMPRODUCT((TABLE1[[Missed classes_P1]:[Missed classes_P6]]=[@Subject])*(TABLE1[Date]>[@[Last Date]])) copy down


PDH EMPTY ERASE "NEVER"
 
Upvote 0
Solution
So I came up with another "not-so-friendly" way. This involved customising the COUNTIF range based on the required date. This needed ADDRESS and INDIRECT. Works great, just not as clean as MARZIOTULLIO's SUMPRODUCT method that I found here when I came to post my way.

So I added this method too, as shown below

Sheet1

*MNOPQ
1SubjectLast DateLessons missed since last attendanceAnswer should beMARZIOTULLIO's method
2Maths3/03/2016101010
3English21/03/2016000
4Science21/03/2016000
5Inv Math22/02/2016131313
6Graphics2/03/2016101010
7HSIE3/03/2016999
8CAT3/03/2016666
9PDH29/01/2016777
10Sport17/02/2016888

<colgroup><col style="font-weight:bold; width:30px; "><col style="width:68px;"><col style="width:136px;"><col style="width:344px;"><col style="width:134px;"><col style="width:237px;"></colgroup><tbody>
</tbody>

Spreadsheet Formulas
CellFormula
O2=COUNTIF(INDIRECT(ADDRESS(MATCH([@[Last Date]],Table1[Date],0)+1,2)&":"&ADDRESS(MATCH(MAX(Table1[Date]),Table1[Date],0)+1,7)),[@Subject])
Q2=SUMPRODUCT((Table1[[Missed classes_P1]:[Missed classes_P6]]=[@Subject])*(Table1[Date]>[@[Last Date]]))

<tbody>
</tbody>

<tbody>
</tbody>


Excel tables to the web >> Excel Jeanie HTML 4



I had tried a SUMPRODUCT at an early stage but I haven't used them much before and couldn't get it to work.

So big thanks to MARZIOTULLIO for your response.

Regards to all,
Darren
 
Upvote 0

Forum statistics

Threads
1,216,115
Messages
6,128,923
Members
449,479
Latest member
nana abanyin

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