• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Identifying a particular status within a range

Dear All,

I have a problem in hand which I am not able to solve. I have an attendance sheet where 'L' means Leave and the rule is that if there are holidays or sundays in between two leaves, then the holidays or sundays will also be counted as Leaves. I am unable to put this into a certain logic where for a particular employee the leave is calculated precisely keeping this rule intact.

If anyone has any answer, please help.

Regards,
Santanu
 

Attachments

  • EXCERCISE1.xlsx
    11.2 KB · Views: 12
Hi, Santanu Chatterjee!

As a new user you might want (I'd say should and must) read this:
http://chandoo.org/forum/forums/new-users-please-start-here.14/

And regarding your issue, a few doubts:
a) Worksheet Sheet1 is where you enter data, but how and where do you want to get your data displayed as per your question?
b) How do you identify holidays? Is there a list, and if yes where is it?

Could you upload a new file with the layout of the required output manually filled with data according to your input sheet?

Regards!
 
Sir,
I would like to get the data in the two columns that are named "Days Present" & "Days Absent" in the same worksheet. There is definitely a list of holidays that are allowed in a year but as we are a call centre, holidays may also be declared as an incentive for the callers.

The whole problem is that I am not getting the logic as to how can a formula in excel identify that if there is a leave taken on a particular day is followed by holiday(s)(can be multiple) and (or) a Sunday and again followed by a leave should be understood and counted as Leaves only?

Suppose if there are two holidays that has been marked in that span of time (as mentioned in the worksheet), how will it work out?

Regards,
Santanu
 
Hi, Santanu Chatterjee!

Regarding:
a) Worksheet Sheet1 is where you enter data, but how and where do you want to get your data displayed as per your question?
Does it mean that you'll enter the data in the same exact way as in the uploaded file?

And about:
b) How do you identify holidays? Is there a list, and if yes where is it?
Please answer.

Finally:
Could you upload a new file with the layout of the required output manually filled with data according to your input sheet?
Could you?

Help us help you.

Regards!
 
Hi, Santanu Chatterjee!

Regarding:

Does it mean that you'll enter the data in the same exact way as in the uploaded file?

And about:

Please answer.

Finally:

Could you?

Help us help you.

Regards!

Dear Sir,

I am arranging the file to be uploaded. It will be up by today.

Regards,
Santanu

EDITED
 
Last edited by a moderator:
Hi Santanu,
You have an interesting problem.

For the benefit of other readers, I will present a summary of the problem here:
  1. You have a series of cells of dates where an employee took Leave (indicated by "L")
  2. However, if the Leave was taken before AND after a holiday ("H") or Sunday ("S"), those Sunday and Holiday days are also counted as part of the Leave.
  3. Days that the employee is present for work is indicated by a "P"

The following are some sample patterns, and the expected count of Leave days
PPPHPLLHLPPPPLLLSHLP --> results in 10 days of Leave
LPLPLPHHHHHLHPLPLHHHSL --> results in 11 days of Leave

It was an interesting exercise finding which Holidays and Sundays had Leave days before and after them. I learned a new technique in the process!

I am sure the following array formula can be optimized further, but this is what I have so far. Enter in cell Z3 and copy down:
=COUNT(1/(LOOKUP(-LOOKUP(IF(LOOKUP(IF(($G3:$X3="Sunday")+($G3:$X3="Holiday"), COLUMN($G3:$X3)), IF(NOT(($G3:$X3="Sunday")+($G3:$X3="Holiday")), COLUMN($G3:$X3)), $G3:$X3)="L", -(COLUMN($G3:$X3))), -LARGE(IF(NOT(($G3:$X3="Sunday")+($G3:$X3="Holiday")), COLUMN($G3:$X3)),ROW(OFFSET(A$1,,,COLUMNS($G3:$X3))))), COLUMN($G3:$X3), $G3:$X3)="L"),1/($G3:$X3="L"))

enter with Ctrl + Shift + Enter

Attached is your workbook with the formula added.

Cheers,
Sajan.
 

Attachments

  • Chandoo-Calculate Holidays in Leave days.xlsx
    13.6 KB · Views: 15
Thanks Sajan. I am also uploading the file with all clarifications with this message for SirJB7. All the rules regarding the attendance has been mentioned in the Sheet 1 of the file and a holiday list has been provided in sheet2.

Regards,
Santanu
 

Attachments

  • EXCERCISE1.xlsx
    17.6 KB · Views: 5
Hi Santanu,
Now that you have a way to count holidays/sundays that need to be counted as leave days, the rest of the counts should be straightforward. Let us know if you need help with that.

-Sajan.
 
Hi Santanu,

Let me start by restating the problem:
  • Need to count the number of Holidays and Sundays as Leave Days, when the day prior and day after are Leave Days.
  • For example, LHSL would get counted as four Leave Days, with the 1 Holiday and 1 Sunday days included in that count.
  • LHHHSL would get counted as six Leave Days, with the three Holidays and one Sunday included in the count.
  • However, PHHSL would get counted as a single Leave Day, since the day before the Holiday is not a Leave Day.

We can break up the problem as follows:
  1. Since Holidays and Sundays behave the same way, we can treat them as the same. So in the explanation below, any reference to Holidays apply to Sundays also.
  2. We need to find the Holidays that are preceded by a Leave Day (L)
  3. Then for those Holidays that are preceded by an L, we need to find if they are followed by an L as well.
  4. Once we combine the result from #2 and#3, we get the count of Holidays that need to be counted as Leave Days.

For #2:
We can use a simple LOOKUP to determine if the day before the Holiday is a Leave Day. As you may recall, LOOKUP finds the previous value in an array that does not exceed the value being sought.

For #3:
We can again use LOOKUP, but this time we need to find the next value in a sequence. (i.e. we need to find the next value, after the series of Holidays and Sundays.

Here is an explanation of the formula...

=COUNT(1/(LOOKUP(-LOOKUP(IF(LOOKUP(IF(($G3:$X3="Sunday")+($G3:$X3="Holiday"), COLUMN($G3:$X3)), IF(NOT(($G3:$X3="Sunday")+($G3:$X3="Holiday")), COLUMN($G3:$X3)), $G3:$X3)="L", -(COLUMN($G3:$X3))), -LARGE(IF(NOT(($G3:$X3="Sunday")+($G3:$X3="Holiday")), COLUMN($G3:$X3)),ROW(OFFSET(A$1,,,COLUMNS($G3:$X3))))), COLUMN($G3:$X3), $G3:$X3)="L"),1/($G3:$X3="L"))

Starting from the right, the segment in blue 1/($G3:$X3="L") counts the number of cells that have an "L" in them.

The segment in green returns the value for the day before a Holiday. (It looks up the column numbers for days=Holidays in the column numbers for days <>Holidays. That would ensure that the previous column number gets returned. Finally, the last part of this segment returns the actual cell value where the previous column number was found.)

We can now check if the value from the green segment is an "L". If so, we have identified a Holiday that will is a candidate to be counted as a Leave Day.

The next outer segment of the formula (simplified for ease of description below):
IF(<segment in green>="L", -COLUMN($G3:$X3))
returns the Column Numbers whereever an L was found, but as negative values. The reason for using negative values will become clear shortly.

But before we discuss the reason for the negative values, let us look at the next segment of the formula. the segment in red:
It finds the column numbers where the days are not Holidays.
It then reverses the order so that the array is in descending order.
Something like:
{24;18;17;16;15;14;12;10;9;8;7;#NUM!;#NUM!;#NUM!;#NUM!;#NUM!;#NUM!;#NUM!}
The errors can be ignored since LOOKUP is very forgiving!

Now is time to explain the reason for using the negated numbers.
Normally, if you LOOKUP(4, {1,3,5,7}), you get 3 (which is the value that does not exceed the value you are looking for). But what if you want it to return 5 (which is the value larger than the value you are looking for)?
Fortunately for us, LOOKUP also works with negative numbers.
So, =LOOKUP(-4,{-7,-5,-3,-1}) would return -5. If we ignore the negative sign, you would notice that the returned value is higher (5) than the value we are seeking (4).

The segment in bold (with the green and red portions) (besides looking like a Christmas tree) does the following:
For every column number where an "L" was found (which is the cell before the Holidays), it finds the column number of the cell after the Holidays.

Now we are in the home-stretch!
The outermost LOOKUP then negates the result from the bold section (resulting in a positive value), and locates it in the column numbers for the full range. The outermost LOOKUP thus returns the value of the cell immediately after a Holiday.

All we have to do now is to COUNT the instances where the outermost LOOKUP resulted in an "L".
1/(LOOKUP(...)="L") ensures that only matches are counted. (As you may recall, COUNT only counts numbers.)

That is it! Enjoy!!

Cheers,
Sajan.
 
This was a tough one - even after a few days i'm struggling to figure out Sajan's formula, the idea to reverse LOOKUP with negative values is nice. Not sure it's much simpler, but for the record, here's my attempt...

This basically checks if there is nothing other than Holidays and Sundays between leave days and if so counts the number of Holidays and Sundays. The last part of the sum adds the number of leave days to this. (If the range begins with a continuous block of holiday this formula counts it as leave.)

=SUM(IF(FREQUENCY(IF(($G3:$X3<>"Sunday")*($G3:$X3<>"Holiday"),COLUMN($G3:$X3)),($G3:$X3="L")*COLUMN($G3:$X3))*FREQUENCY(COLUMN($G3:$X3),COLUMN($G3:$X3))=1,FREQUENCY(IF(($G3:$X3="Sunday")+($G3:$X3="Holiday"),COLUMN($G3:$X3)),($G3:$X3="L")*COLUMN($G3:$X3))),N($G3:$X3="L"))
 
Last edited:
Hi Lori,
Thanks for the FREQUENCY based solution. I had initially attempted it, but could not get it to handle all of my test scenarios, including the range starting with a holiday. Besides, I am hooked on LOOKUP now, ever since I found out about its versatility in a recent Formula Challenge post! I now see the world as arguments to a LOOKUP function! :)

I am planning to revisit the FREQUENCY based solution when I get a chance.

Regards,
Sajan.
 
Sajan, i guess you're right that ranges beginning with Holidays should not be counted since these depend on leave days outside the range. For my suggestion i think one way to handle this would be to make the following insertion...

replace:=1
with:*(1-FREQUENCY(1,($G3:$X3="L")*COLUMN($G3:$X3)))=1

[or else if you wanted to include runs of Holidays at both ends...
replace: *FREQUENCY(COLUMN($G3:$X3),COLUMN($G3:$X3))
with: +FREQUENCY(9^9,COLUMN($G3:$X3))]

Am sure similar tweaks are possible with the LOOKUP solution for different cases but i still need to understand it better!
 
Last edited:
Back
Top