Stuck on a countifs formula

%3CLINGO-SUB%20id%3D%22lingo-sub-2635912%22%20slang%3D%22en-US%22%3EStuck%20on%20a%20countifs%20formula%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2635912%22%20slang%3D%22en-US%22%3E%3CP%3EI've%20managed%20to%20get%20most%20of%20my%20countifs%20formulas%20to%20work%20except%201.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EUse%20Case%3A%3C%2FP%3E%3CP%3EI%20have%20employee%20records%20going%20back%2010%20years%2C%20in%20this%20records%20in%3C%2FP%3E%3CP%3EGender%2C%20Start%20Date%2C%20End%20Date%20(null%20if%20still%20current)%2C%20Age%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20another%20table%20with%20each%20year%20broken%20down%20to%20months%26nbsp%3B%3C%2FP%3E%3CP%3E2020%20Jan%20-%20Dec%3C%2FP%3E%3CP%3E2019%20Jan%20-%20Dec%26nbsp%3B%3C%2FP%3E%3CP%3E2018%20Jan%20-%20Dec%20etc%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20want%20to%20be%20able%20to%20work%20out%20what%20the%20female%20percentage%20was%20month%20by%20month%20going%20back%20the%2010%20years.%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI've%20been%20able%20to%20work%20out%20new%20starters%2C%20terminations%20etc%20based%20on%20a%20month%20as%20they're%20comparing%20dates%20in%20countifs%20and%20edate%20function%20but%20I%20can't%20get%20it%20to%20count%20the%20amount%20of%20Males%20and%20Females%20in%20a%20month%20base%20of%20if%20they%20were%20current%20at%20the%20time.%26nbsp%3B%20%26nbsp%3BI%20want%20to%20also%20work%20out%20average%20age%20for%20that%20month%20of%20my%20employees%20but%20I%20suspect%20that%20should%20be%20easy%20with%20the%20correct%20structure.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECan%20anyone%20help%20me%20please!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2635912%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2636348%22%20slang%3D%22en-US%22%3ERe%3A%20Stuck%20on%20a%20countifs%20formula%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2636348%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1125856%22%20target%3D%22_blank%22%3E%40gillsy%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20consistency%2C%20change%20A11%2C%20A15%20and%20A19%20to%20Female.%3C%2FP%3E%0A%3CP%3EIn%20B10%20as%20an%20array%20formula%20confirmed%20with%20Ctrl%2BShift%2BEnter%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3DSUMPRODUCT((Table13%5BColumn1%5D%3D%24A10)*(Table13%5BStart%20Date%5D%26lt%3B%3DEOMONTH(B%249%2C0))*IF(Table13%5BEnd%20Date%5D%3D%22%22%2CTRUE%2CTable13%5BEnd%20Date%5D%26gt%3B%3DB%249))%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFill%20down%20to%20B11%2C%20then%20to%20the%20right%20to%20column%20M.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESimilarly%20in%20B14%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3DSUMPRODUCT((Table13%5BColumn1%5D%3D%24A14)*(Table13%5BStart%20Date%5D%26lt%3B%3DEOMONTH(B%2413%2C0))*IF(Table13%5BEnd%20Date%5D%3D%22%22%2CTRUE%2CTable13%5BEnd%20Date%5D%26gt%3B%3DB%2413))%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Eand%20in%20B18%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3DSUMPRODUCT((Table13%5BColumn1%5D%3D%24A18)*(Table13%5BStart%20Date%5D%26lt%3B%3DEOMONTH(B%2417%2C0))*IF(Table13%5BEnd%20Date%5D%3D%22%22%2CTRUE%2CTable13%5BEnd%20Date%5D%26gt%3B%3DB%2417))%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EYour%20sample%20workbook%20contains%20gender%20in%20the%20age%20column%2C%20but%20apart%20from%20that%3A%20do%20you%20want%20to%20use%20the%20current%20age%2C%20or%20the%20age%20during%20the%20months%3F%20You'd%20need%20the%20date%20of%20birth%20for%20the%20latter...%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2636360%22%20slang%3D%22en-US%22%3ERe%3A%20Stuck%20on%20a%20countifs%20formula%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2636360%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1125856%22%20target%3D%22_blank%22%3E%40gillsy%3C%2FA%3E%26nbsp%3BI%20think%26nbsp%3B%20I%20see%20you%20problem.%26nbsp%3B%20In%20order%20to%20calculate%20it%20you%20have%20to%20have%20an%20OR%20condition%20to%20take%20care%20of%20the%20blank%20end%20date%20so%20you%20could%20create%20a%20helper%20column%20being%20EndDate-Today%20and%20that%20would%20be%20something%20like%3A%26nbsp%3B%20%3DIF(TABLE13%5BEnd%20Date%5D%2CTABLE13%5BEnd%20Date%5D%2CTODAY())%20and%20then%20you%20could%20use%20the%20COUNTIFS%20and%20AVERAGEIFS%20function.%26nbsp%3B%20That%20would%20make%20it%20a%20bit%20cleaner%20and%20easy%20to%20follow%20and%20instead%20of%20TODAY()%20you%20could%20enter%20a%20future%20date%20to%20assume%20they%20will%20still%20be%20with%20you%20through%20the%20end%20of%20the%20year%20or%20next%2C%20...%20(that%20was%20a%20question%20I%20had%20about%20your%20dates%20in%20the%20future%20if%20present%20employees%20should%20be%20assumed%20to%20stay)%3C%2FP%3E%3CP%3EIn%20the%20attached%20I%20used%20different%20formulas%20instead%20to%20give%20you%20options%20(again%20the%20above%20is%20more%20straight%20forward).%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3E%3DSUMPRODUCT((Table13%5BColumn1%5D%3D%24A10)*(Table13%5BStart%20Date%5D%3CB%3EB%249%2C1))%3C%2FB%3E%3C%2FCODE%3E%3CB%3E%3C%2FB%3E%3C%2FPRE%3E%3CP%3Eand%20for%20the%20Average%20Age%20(I%20assume%20the%20average%20%23%20days%20the%20current%20employees%20have%20been%20employed)%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3E%3DAVERAGE(FILTER(B%249-Table13%5BStart%20Date%5D%2C(Table13%5BStart%20Date%5D%3CB%3EB%249%2C1)%2C0))%3C%2FB%3E%3C%2FCODE%3E%3CB%3E%3C%2FB%3E%3C%2FPRE%3E%3CP%3ESo%20other%20considerations%20will%20be%20how%20to%20handle%20employees%20that%20started%20or%20ended%20in%20that%20particular%20month%2C%20but%20I'll%20leave%20that%20up%20to%20you.%3C%2FP%3E%3CP%3Egood%20luck.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2636391%22%20slang%3D%22en-US%22%3ERe%3A%20Stuck%20on%20a%20countifs%20formula%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2636391%22%20slang%3D%22en-US%22%3EI%20see%20Hans%20submitted%20while%20I%20was%20working%20on%20it%20and%20typing%20the%20response.%20You'll%20notice%20his%20formula%20is%20very%20similar%20to%20mine%20but%20he%20use%20EOMONTH()%20which%20again%20is%20that%20issue%2Fquestion%20about%20including%20or%20excluding%20employees%20that%20start%2Fend%20that%20month.%3CBR%20%2F%3EAlso%2C%20I%20forgot%20to%20also%20mention%20the%20typo%20of%20%22Females%22%20instead%20of%20%22Female%22%20(as%20Hans%20noted)%20but%20also%20each%20year%20needed%20a%20December%20(month%2012)%20instead%20of%202%20Novembers%20(month%2011).%3C%2FLINGO-BODY%3E
New Contributor

I've managed to get most of my countifs formulas to work except 1.

 

Use Case:

I have employee records going back 10 years, in this records in

Gender, Start Date, End Date (null if still current), Age

 

I have another table with each year broken down to months 

2020 Jan - Dec

2019 Jan - Dec 

2018 Jan - Dec etc

 

I want to be able to work out what the female percentage was month by month going back the 10 years.  

 

I've been able to work out new starters, terminations etc based on a month as they're comparing dates in countifs and edate function but I can't get it to count the amount of Males and Females in a month base of if they were current at the time.   I want to also work out average age for that month of my employees but I suspect that should be easy with the correct structure.

 

Can anyone help me please!

 

 

 

 

7 Replies

@gillsy 

For consistency, change A11, A15 and A19 to Female.

In B10 as an array formula confirmed with Ctrl+Shift+Enter:

 

=SUMPRODUCT((Table13[Column1]=$A10)*(Table13[Start Date]<=EOMONTH(B$9,0))*IF(Table13[End Date]="",TRUE,Table13[End Date]>=B$9))

 

Fill down to B11, then to the right to column M.

 

Similarly in B14:

 

=SUMPRODUCT((Table13[Column1]=$A14)*(Table13[Start Date]<=EOMONTH(B$13,0))*IF(Table13[End Date]="",TRUE,Table13[End Date]>=B$13))

 

and in B18:

 

=SUMPRODUCT((Table13[Column1]=$A18)*(Table13[Start Date]<=EOMONTH(B$17,0))*IF(Table13[End Date]="",TRUE,Table13[End Date]>=B$17))

 

Your sample workbook contains gender in the age column, but apart from that: do you want to use the current age, or the age during the months? You'd need the date of birth for the latter...

@gillsy I think  I see you problem.  In order to calculate it you have to have an OR condition to take care of the blank end date so you could create a helper column being EndDate-Today and that would be something like:  =IF(TABLE13[End Date],TABLE13[End Date],TODAY()) and then you could use the COUNTIFS and AVERAGEIFS function.  That would make it a bit cleaner and easy to follow and instead of TODAY() you could enter a future date to assume they will still be with you through the end of the year or next, ... (that was a question I had about your dates in the future if present employees should be assumed to stay)

In the attached I used different formulas instead to give you options (again the above is more straight forward).

=SUMPRODUCT((Table13[Column1]=$A10)*(Table13[Start Date]<B$9)*IF(Table13[End Date],Table13[End Date]>B$9,1))

and for the Average Age (I assume the average # days the current employees have been employed)

=AVERAGE(FILTER(B$9-Table13[Start Date],(Table13[Start Date]<B$9)*IF(Table13[End Date],Table13[End Date]>B$9,1),0))

So other considerations will be how to handle employees that started or ended in that particular month, but I'll leave that up to you.

good luck.

I see Hans submitted while I was working on it and typing the response. You'll notice his formula is very similar to mine but he use EOMONTH() which again is that issue/question about including or excluding employees that start/end that month.
Also, I forgot to also mention the typo of "Females" instead of "Female" (as Hans noted) but also each year needed a December (month 12) instead of 2 Novembers (month 11).

@mtarler 

I didn't even notice the two Novembers...

@Hans Vogelaar  @mtarler 

 

Thanks to both of you, @mtarler I couldn't get yours to work but I understand the concept, 

 

@Hans Vogelaar yours initially wasn't quite right as it was adding the employee the month after they started so I modified it slightly to fit it.  I had a feeling SUMPRODUCT was going to be in the formula somewhere.

 

The final formula being =IF(SUMPRODUCT((Table13[[Gender]:[Gender]]=$A10)*(Table13[[Start Date]:[Start Date]]<(EDATE(B$9,1))*IF(Table13[[End Date]:[End Date]]="",TRUE,(IF(Table13[[End Date]:[End Date]]="",TRUE,Table13[[End Date]:[End Date]]>=B$9)))))=0,"",SUMPRODUCT((Table13[[Gender]:[Gender]]=$A10)*(Table13[[Start Date]:[Start Date]]<(EDATE(B$9,1))*IF(Table13[[End Date]:[End Date]]="",TRUE,(IF(Table13[[End Date]:[End Date]]="",TRUE,Table13[[End Date]:[End Date]]>=B$9))))))

 

Cheers for both your help, I was stuck on that for almost 8 hours.   It's a new formula to add to my knowledge.

@Hans Vogelaar  I'm stuck on averaging the age now. 

 

I want an average age based on the month in question.

@gillsy 

In B24 as an array formula confirmed with Ctrl+Shift+Enter:

 

=AVERAGE(IF((Table134[DOB]<>"")*(Table134[Start Date]<=EOMONTH(B23,0))*IF(Table134[End Date]="",TRUE,Table134[End Date]>=B23),(B23-Table134[DOB])/365.25,""))

 

Fill or copy to the other cells.