Combining Excel COUNTIFS with aggregate functions - excel

Say I have an Excel spreadsheet containing student details and dates of courses the student attended. Lets say the row headers are:
Name - Student Grade - Date of course A - Date of course B - Date of course C etc...
Then obviously a separate row per student containing their grade and dates of the courses the student was present in. I want to avoid making modifications to the sheet as the format has been established for a while.
I'm looking for some way of counting all the students with a grade a specific grade, which attended courses between specific dates. For example, count all students with a "C" grade, which attended courses between 1st Jan 2012 to 31st March 2012.
I guess the final formula will be some sort of combination of COUNTIFS and MAX on the date range columns, but I can't see how I can apply this on a row-by-row basis.
All suggestions much appreciated!
Best regards,
Chris

Assuming you have grades in B2:B100 and dates in 5 columns C2:G100 then you can use this formula to count the number of students with a specific grade who took courses in a specific date period.
=SUMPRODUCT((MMULT((C$2:G$100>=J2)*(C$2:G$100<=K2)*(B$2:B$100=I2),{1;1;1;1;1})>0)+0)
where J2 and K2 are the start and end dates of the period (1-Jan-2012 and 1-Mar-2012) and I2 is a specific grade (C)
the {1;1;1;1;1} part depends on the number of date columns, so if there are 7 date columns then you need to change that to {1;1;1;1;1;1;1}.....or you make the formula dynamically adjust by using this version
=SUM((MMULT((C$2:G$100>=J2)*(C$2:G$100<=K2)*(B$2:B$100=I2),TRANSPOSE(COLUMN(C2:G100))^0)>0)+0)
The latter formula, though, is an "array formula" which you need to confirm with CTRL+SHIFT+ENTER
Update
For the number of distinct grades within a specific date range then assuming you have a finite list of possible grades then list those somewhere on the worksheet, e.g. M2:M10 and then you can use this "array formula"
=SUM(1-ISNA(MATCH(M$2:M$10,IF(MMULT((C$2:G$100>=J2)*(C$2:G$100<=K2),{1;1;1;1;1}),B$2:B$100),0)))
confirmed with CTRL+SHIFT+ENTER

Related

Excel function to count row once for two criteria found in same cell

My excel knowledge is very basic using my own research here and there so I would need some help from you guys.
I have a situation where I need to calculate the number of rows that contain certain date values which I converted it to text for certain reason (didn’t want to list them in separate rows).
E.g.
Col A. Col B.
Customer Name, Date Purchase
Andy. 1/2/2021 , 2/3/2021
Jenny. 2/2/2021 , 3/4/2021
Mary. 1/3/2021 , 1/5/2021
I want to count the number of unique customers who purchased between February and May 2021 for the above case, but I didn’t know how I should proceed from an existing countifs formula I had that looks like the following:
=countif(B:B, “/2/2021”) + countif(B:B, “/3/2021”) + countif(B:B, “/4/2021”) + countif(B:B, “/5/2021”)
This formula had returned the value as 9 based on the following results
February : 2
March : 2
April : 4
May : 1
which I didn’t want as I want the count number of different customers I had instead (for this case I want the cell with a formula that can return the result as “3” , not the number of time purchased).
Please help me to make a suitable formula works! Thanks 🙏
By adding an indicator in Column C that shows whether or not each customer satisfies your purchase requirements you could simply sum these up at the bottom to count the number of unique customers.
Hopefully the image below containing the formula in Column C (essentially checking that each customer has at least one purchase between February and May) will resolve your issue. I added a Dummy customer that doesn't satisfy these dates just a check so the total unique customers is still 3 (Cell C6 which uses a formula of '=COUNTIF(C2:C5,"Y")').
You mention the possibility of having 100 purchase dates, are these all recorded in text format in Column B? I would have thought that would make things more difficult to track than having 100 individual columns (though this doesn't affect your query above).

Filtering or Importing data for specific date

I was wondering if anyone knows if it's possible to import a data, in a sort of a COUNTIF function but only for the data dated today?
I have 2 sheets, Attendance and Department.
In the Department tab, I have a list of employee's in column A. In column B and so on, I have a drop down option to select which department they're are stationed for each day. Each column represent a day, B being the 1st, C being 2nd, ... and so on. The header for each column has the day for the day as well.
On the attendance sheet, I have the list of Department in column A and in B, it is used to record how many employee are stationed at that department. I also have the day for the day on this sheet on A1.
Is it possible that when I change the date, the number of employee stationed in each department will change according to the date?
Is it possible to do this with just functions?
I've tried using the COUNTIF function, but I've found that I will have to change the range on a daily basis.
Use The Function: in B4 of Attendance Tab
=COUNTIF(INDIRECT("Department!" &ADDRESS(3,MATCH($A$1,Department!$A$2:$P$2,0)) & ":" &ADDRESS(6,MATCH($A$1,Department!$A$2:$P$2,0))),$A4)
Results: Attendance Tab
Department Tab:
I have used all the references as in the screenshots, you can change them if they are different in your sheet.
your question can be solved by SUMPRODUCT function. See below solution picture:
SOLUTION
Just to quickly explain my solution, I gave a name to the date on the Department sheet, called it 'Date_Lookup'; and I gave a name to the dates on the Attendance sheet, called it 'Date_List'. Names will simplify my formula especially when referencing across worksheets.
You will eventually find a lot of COUNIF scenario can be solved by SUMPRODUCT and this is just one example. SUMPRODUCT can do a lot IFs than COUNTIFS can do. In your case there are two criteria, one is which department did an employee attend to, and the other is on which date did the employee attend to that particular department. So my SUMPRODUCT formula can be understood as:
=SUMPRODUCT(('from the list of dates'='the particular date I want to see')*('from the full attendance record'='how many employees went to the given department'))
Let me know if you have difficulties understand my answer. A good 1 hour read through some online articles regarding SUMPRODUCT will help.
Cheers :)
Your first step would be to use a function to work out in which column the entered date appears. So in your Department sheet you enter a date in A1, then set B1 to:
=IFERROR(MATCH(A1, Attendance!2:2, 0), 0)
I use the IFERROR as I don't like seeing errors on my sheet. Assuming the result isn't zero, use a COUNTIF looking for the department letter in a range in the result column. I'd simply use on OFFSET to get the right range, but sticklers would tell you to find a different way of doing it. So long as your spreadsheet isn't going to increase in size to a massive complexity, OFFSET is fine.

How to count values in a data range with multiple filters

I need to create a module that will count the amount of values in specified date ranges, with other criteria.
For example, I have a list of products (Products A, B , C, D) in column C, and their sale date in column G.
I need to count all of product A sold before 1/1/1998. I then need to calculate product A sold between 1/1/1998 and 1/1/2005 etc.
I need to be able to run this for all the types of products, and group products together.
E.g. count all of product A & B sold before 1/1/1998.
This has to be done for a new workbook on a weekly basis so ideally needs to be able to be run for a new workbook each week. The tab names always remain the same.
Any help on how to get started would be appreciated
This answer will assume that your dates are entered as excel dates in column G and not a text. You can test this by using the formula =ISNUMBER(G3) where G3 is one of your dates. If it returns true, then your date is properly stored for use by excel formulas and this answer.
=SUMPRODUCT((($C$1:$C$100="A")+($C$1:$C$100="B"))*($G$1:$G$100<Date(1998,1,1))
That is how to hard code it. Personally I would build a table. Each row of the table would be a product you are interested in knowing the count for and a sum of the count would give you combined totals. Repeat the table if you need multiple combinations.
In the following example a single product was counted and then the total for all products listed was the total. The formula for the example in L3 and copied down for each product was:
=SUMPRODUCT(($C$2:$C$9=$J4)*($G$2:$G$9<K4))
The total at the bottom of the table was a simple SUM formula. Because SUMPRODUCT performs array like operations, avoid using full column references and try to restrict it to the data that needs to be checked. Otherwise you may notice a slow down in your system as multiple excess calculations are being perfomed.

Having trouble with counting two criteria with countif

I have two criterias in excel.
I was counting anything that is 2014 and grade k-8 using countifs: =COUNTIFS(A2:C3,"2014",D2:E3,"Grades K-8") but it seems to fail.
Year 1 Year 2 Year 3 Grades K-8 Grades 9-12 Teachers
2014 2015 Grade K-8 Grade 9-12 Teachers
2016 2017 2018 Grade K-8 Grade 9-12 Teachers
I keep getting value error. Any help is much appreciated!
This should do the trick :)
In cell H3, which search for the criteria Grade 9-12 and 2014
=SUMPRODUCT((E2:E6="Grade 9-12")*(A2:C6=2014))
The SUMPRODUCT looks for all combinations given the two ranges and gives you how many times they occur.
Since you search row wise for one criteria and column wise for another you can't use countifs. Or more precisely, your are looking in a range that across both multiple rows and columns when you search for year. So you want countif to compare each item in that array against the item of another array.
Based on the layout of your data and that you are only looking in 1 column for the grade, and the years you are looking for is limited to only 3 columns you can use the following formula:
=COUNTIFS(A:A,2018,D:D,"Grade K-8")+COUNTIFS(B:B,2018,D:D,"Grade K-8")+COUNTIFS(C:C,2018,D:D,"Grade K-8")
Basically the check inside the COUNTIFS, the multiple criteria are all AND checks. SInce the mutiple columns for each year column is an OR, you need to check each year column individually. You can use + like an OR operations and repeat the columns checks.
If you need to check multiple columns and combinations of columns the formula will get unruly and you are better off with an array type formula like Wizhi has for an answer.

Count values in one column if dates fall in range

Rewording the question as I've tried the responses (thank you very much for those) but they're not doing what I need.
Master tab has hundreds of candidate records on it from a large range of dates over the last year. I'm interested in 2 columns.
Col A contains the date the candidate took the exam
Col B contains the candidate grade (Distinction, Credit, Referral)
On a new tab, I need to report on how many Distinctions were achieved, how many Credits and how many referrals for last week only
This will need to be repeated each week on a new tab (difficult boss!) so I need to make it as easy to repeat as possible
Please list the grades uniquely (say in ColumnA starting in Row1) then in B1:
=COUNTIFS(Sheet1!A:A,">42005",Sheet1!A:A,"<42012",Sheet1!B:B,A1)
and copy down, where your data is assumed to be in Sheet1. The numbers (42005, 42012) are the numeric representations of the relevant date limits (eg 42005 is what my system shows for 1/1/2015 in Numeric format).
I think this should work. So for column A1 to A6 I check whether the date falls between a certain range. In my case between 7/03/2015 and 19/03/2015. Then I just want to count the items containing DISTINCT/CREDIT/REFERRAL.
=COUNTIFS($A$1:$A$6;"<20/03/2015";$A$1:$A$6;">06/03/2015";$B$1:$B$6;"DISTINCT")
=COUNTIFS($A$1:$A$6;"<20/03/2015";$A$1:$A$6;">06/03/2015";$B$1:$B$6;"CREDIT")
=COUNTIFS($A$1:$A$6;"<20/03/2015";$A$1:$A$6;">06/03/2015";$B$1:$B$6;"REFERRAL")

Resources