简体   繁体   中英

Count multiple values with multiple conditions and ranges in EXCEL/GOOGLE SHEETS

In my company employees should get a day off if:
1. they work on a friday nightshift(NS)
2. they work on a thursday NS and a saturday NS
3. they work friday morning and saturday NS.

I need to count how many dayoffs every employee should get

MORNING                                  NIGHT                   WEEKDAY |DAY
PLACE1|PLACE2|PLACE3 PLACE4|PLACE5|PLACE6|
DAV FL RI RI 1 10
DAV RI FL DAV 2 11
FL RI DAV FL 3 12
DAV RI FL DAV 4 13
FL RI DAV 5 14
6 15
7 16
DAV FL RI RI 1 17
DAV RI FL DAV 2 18
FL RI DAV FL 3 19
DAV RI FL DAV 4 20
FL RI DAV 5 21
6 22
7 23

In this example FL gets 2 days off
RI gets 1 day off
DAV gets 2 days off

Counting the nightshifts of friday could be achieved with COUNTIFS.

=COUNTIFS(D3:D34,"DAV",G3:G34,"6")+COUNTIFS(E3:E34,"DAV",G3:G34,"6")+COUNTIFS(F3:F34,"DAV",G3:G34,"6")

but things get complicated when I have to use the name condition on 6 different columns, each with a different weekday condition, ie employee on T+SAT or F+SAT, Is there any way to achieve this using only built-in formula w/o scripts?

You can try the following formulas:

Friday nights simply:

=SUMPRODUCT((L2=$D$3:$F$16)*(6=$G$3:$G$16))

Thursday nights + Saturday nights more complicated:

=MIN(SUMPRODUCT((L2=$D$3:$F$16)*(5=$G$3:$G$16)),SUMPRODUCT((L2=$D$3:$F$16)*(7=$G$3:$G$16)))

The principle of calculation is that Thursday nights worked are counted separately and Saturday nights are counted separately then from the two numbers are selected smallest. This in turn means that a person can work one week on a Thursday and after two weeks on a Saturday and get a day off. See if it's right.

Friday mornings + Saturday nights by analogy:

=MIN(SUMPRODUCT((L2=$A$3:$C$16)*(6=$G$3:$G$16)),SUMPRODUCT((L2=$D$3:$F$16)*(7=$G$3:$G$16)))

在此处输入图片说明

I transferred your sample data to a worksheet so that your morning shift data are in columns A:C and the night shift in columns E:G. The critical weekday column is column H. I presumed that your data have 1 = Sunday.

I created two named ranges, "AM" and "PM", to cover the columns for morning and night shifts. It's important that the ranges start from row 1. Note : It's also of critical importance that my formula not be written in a row above row 3 in the worksheet.

I added columns to your layout on the right, one for each employee, and wrote the employees' name in row 2 of each column. In my worksheet these columns are at K:M, and the formula goes to K3. From there it is copied to all other cells in my three columns.

=IF($H3=6,IF(COUNTIF(INDEX(PM,ROW(),0),K$2),1,""),IF($H3=7,IF(COUNTIF(INDEX(PM,ROW(),0),K$2),IF(OR(COUNTIF(INDEX(AM,ROW()-1,0),K$2),COUNTIF(INDEX(PM,ROW()-2,0),K$2)),1,""),""),""))

The three columns I added show a 1 for each earned off-day.

The formula discards all days which aren't either Friday or Saturday. On Fridays it awards a "1" if the name appears in the PM shift. For Saturday PM work and off-day is awarded if there was also work on either Friday AM or Thursday PM in the same week. From this rule follows that you can't start your months on the first day if that is a Saturday. The preceding Thursday must be included in the month's data. If it's not no error will occur (unless the formula is in row 2) but the result will be wrong due to the referenced row containing no valid data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM