Excel - Multiple Conditions with 2 variables - excel

I am working on a excel data set whereby I have two variables -
1) One time Spend
2) Installment Spend.
Now, the observations under these two variables contain 4 possibilities.
A - One time Spend>0,Installment Spend=0
B - One time Spend=0,Installment Spend>0
C - One time Spend=0,Installment Spend=0
D - One time Spend>0,Installment Spend>0.
I want to create a new variable called Spend type which can classify the Spend based on
A-Onetime Spend,
B-Installment Spend,
C-No Spend,
D-Both One time and Installment.
Can anybody help me with creating this new variable in excel?
enter image description here

Do you mean like a formula? I have added an invalid category in case of empty cells.
=IF(OR(ISBLANK(A2),ISBLANK(B2)),"invalid",IF(AND(A2=0,B2=0),"No spend",IF(AND(A2>0,B2>0),"Both",IF(AND(A2=0,B2>0),"Installment spend","One time spend"))))

Here is a slightly different formula approach:
=INDEX({"No Spend","Onetime Spend","Installment Spend","Both One time and Installment"},MATCH((A2>0)+(B2>0)*2,{0,1,2,3},0))
Or this shorter version:
=CHOOSE((A2>0)+(B2>0)*2+1,"No Spend","Onetime Spend","Installment Spend","Both One time and Installment")

Related

Caclulate if Now() is between 2 times across midnight Excel

I feel a bit noob asking this but been scratching my head trying to figure out a solution for this problem. (ADHD hitting hard as well)
I have a list of people that are schedule to work in various time segments, some of them with shifts that crosses midnight, and after comparing it with a different data set know if the person if the person is working or not.
Column A contains the list of people,
Column B and C contains the days off, combined with + =Today()
Column D contains the time when the shift begins, (As it time only, added =Today)
Column E contains the time when the shift ends,
Column F will indicate if that specific person is in their day off or they need to work, used the following code for it
=IF(OR(B3=(TEXT(TODAY(),"ddd")),C3=(TEXT(TODAY(),"ddd"))),"Week-Off","Workday")
In column G I want to know if the person is working or they are absent after matching it with another table using this formula
=IF(F2="Workday",IF(AND(Now()>=$E2,(Now()<=$F2),"Scheduled","Off-Shift")," ")
So far the code has worked for everyone who has schedules that ends at 11:59 pm, but when the shift ends at midnight or it crosses midnight it provides incorrect results (Shows Offshift instead of Scheduled)
In Short I want to know if =Now() is between 2 times, when is in the same day or the shift crosses midnight.
Code (Asume Now() is 1:30 am)
=IF(F4="Workday",IF(AND(Now()>=$D4,(Now()<=$E4),"Scheduled","Off-Shift")," ")
Expected Result
Schedule
Actual Result
Off-Shift
As on now i feel is a pretty easy solution but unable to wrap around a solution, and somehow my brain is focus on using IF. As an extra bonus if it can be done by Excel-VBA it will be a plus
After looking a bit further found the solution in the following post: Determine if a given time is between start and end time

Excel Group rows based on time interval

I have the following excel result:
I want to group the above result in groups based on sessions i.e. if the time gap between two successive timestamps is greater than 5 minutes, it must be a new row.
For example :
I need some formula to achieve this. As I'm fairly new to Excel this is causing to be a major headache for me. Please help me, if anyone knows how to do it or at least point me in a direction.
Thanks a ton !!!
Judging by your screenshot, it appears your timestamps are actually text values. Text by default is usually left aligned where as numbers are right aligned. You seem to have a space at the end of your time stamp suggesting that it is probably left aligned and therefore text. You can test it with the following formula which will return TRUE if its text.
=ISTEXT(P2)
where P2 is one of your time stamps.
CONVERT TIMESTAMPS TO TIME
There are a variety of ways to do this. Some will depend on system settings. Take a look at the following functions as each might be useable depending on your system. The first two are a guarantee, the last two are more dependent on system settings.
DATE
TIME
DATEVALUE
TIMEVALUE
Something important to remember here is that in excel dates are integers counting the days since 1900/01/01 with that date being 1. Time is stored as a decimal and represents fraction/percentage of a day. 24:00:00 is not a valid time in excel though some functions may work with it.
So in order to convert your time stamp in P2 I used the following formula to pull out the date:
=DATE(LEFT(P2,4),MID(P2,FIND("-",P2)+1,2),MID(P2,FIND(" ",P2)-2,2))
Basically it goes into the text and strips out the individual numbers for Year, Month and Day.
To pull out the time, I could have done the same procedure but elected to demonstrate the TIMEVALUE method which is a little more robust than DATEVALUE and not a subjective to system settings as much. With the following formula I stripped out the whole time code (MINUS"UTC"):
=TIMEVALUE(TRIM(MID(P2,FIND(" ",P2)+1,FIND("UTC",P2)-FIND(" ",P2)-1)))
I also made an assumption that you are not mixing and matching UTC with other time zones which means it can be ignored. Now to get DATE and TIME all in one cell, you just need to add the two formulas together to get:
=DATE(LEFT(P2,4),MID(P2,FIND("-",P2)+1,2),MID(P2,FIND(" ",P2)-2,2))+TIMEVALUE(TRIM(MID(P2,FIND(" ",P2)+1,FIND("UTC",P2)-FIND(" ",P2)-1)))
In the example at the end, I placed that formula in Q2 and copied down
DELTA TIME
Since you want to break your groups out based on a time difference between individual entries, I used a helper column to store the time difference. In my example at the end I stored this difference in Column S. The first entry is blank as there is no time before it. I used the following formula in S3 and copied it downward.
=Q3-Q2
I applied the custom formatting of [h]:mm:ss to the cell to get it to display as shown.
FIND GROUP BREAK POINTS
In my example I am using helper column T to hold breakpoint flags. At a minimum, you will have two break points. Your first time entry and your last time entry. To make like simple I simply hard coded my first breakpoint flag in T2 as 1. Stating in T3, Three checks need to be made. If any of them are TRUE then the next flag needs to be added with a value increase by one. the three checks are:
Is this the last entry
Is the next time delta greater than 5 minutes (means end of a group)
Is this time delta greater than 5 minutes (means start of a group)
Based on those three checks I placed the following formula in T3 and copied down:
=IF(OR(S4="",S4>TIME(0,5,0),S3>TIME(0,5,0)),MAX($T$2:T2)+1,"")
Note the $ on the first part of the range for the MAX function. This will lock the start of the range while the formula gets copied down while the end of the range increases accordingly.
Also the row after the last time entry must be blank. IF it is not blank and has a set value in it, change the S4="" to S4="set value".
GENERATE TABLE
There are multiple ways to reference the flags and pull the corresponding times. a couple of formulas you can look into are:
INDEX / MATCH
LOOKUP
In this example I elected to use LOOKUP though I believe INDEX and MATCH are more appropriate and robust. For starters we want to generate a list of ODD number and EVEN numbers. These represent the start and end of the groups and correspond to the flags set in column T. One way to generate ODD and EVEN numbers as you copy down is:
=ROW(A1)*2-1 (ODD)
=ROW(A1)*2 (EVEN)
The next step is to find the generated number in Column T and then pull its corresponding timestamp in Column Q. I did this with the following formula in V2 and copied down.
=LOOKUP(ROW(A1)*2-1,T:T,Q:Q)
And in W2
=LOOKUP(ROW(A1)*2,T:T,Q:Q)

How to pull a random object from a list and remove it in Excel?

I'm trying to create a rotational schedule in Excel that will randomly assign a building once a week without duplicates. I need each building to have a morning/afternoon shift and alternate the following week.
So what I was thinking is there are 20 shifts every 2 weeks, Morning/Afternoon 5 days a week and I have 10 buildings to go to so that works out nicely. I want it to be random so that way I am not always in the same building at the same time every week.
So I used =RAND() in Column B of sheet 1 to generate a random number for each building and then pasted it as just values to get rid of the formula in that column.
Then on sheet 2, I tried having =INDEX(Sheet1!$A$2:$A$11, RANDBETWEEN(1,COUNTA(Sheet1!$B$2:$B$11))) in each cell of 5x4 schedule.
The problem is there are duplicates and I want the second week to actually be opposite morning/afternoon but still random days. I think that I'll probably have to have it randomize morning/afternoon the first week, then do days and the second week have it opposite whatever the random morning/afternoon was and then do random days again but I'm just not sure how to go about it.
I'm new to programming in Excel and my programming is a little rusty in general...
EDIT: I was able to use =CHOOSE(RANDBETWEEN(1,2),"Morning","Afternoon") to have a column choose morning/afternoon randomly but it doesn't ensure equal number of mornings to afternoons which I need. And I still wouldn't know how to have the opposite results elsewhere for the following week.
I think you can use if condition to make sure you select opposite morning/afternoon after you selected random days.
like this:
=IF(AND(B3="Morning",C3="Morning"),"Afternoon",IF(AND(B3="Afternoon",C3="Afternoon"),"Morning",C3))

Calculate minutes in specific period

I have a spreadsheet which has employee working times, listed as Sat-In and Sat-Out for a specific date. The employee shift spans several ours and each "In-Out" period is recorded as a separate line which means the time between the Sat-Out and the next Sat-In means the employee is on a break. I also calculate the time, in minutes of each "sitting" period.
What I can't seem to figure out is how to add a formula which takes the data and further refines it in this manner:
1. I have a core period of 1030-1530, as an example, which is the busy time and requires the maximum employee coverage. The shifts of employees generally spans this core, but in some cases their shift may start or end in the core.
2. I want to calculate how many minutes the employee worked within the core only. I can obviously do this manually using the data, but a formula would be preferred, if possible.
3. As an example, if a person sat-in at 1445 and sat-out at 1545, the core time calculation would be 45 minutes (1445-1530).
I've attached a snapshot of the data to help my explanation.
FYI - the information is pulled from a database as JSON data and converted to excel. I'm not very familiar with JavaScript, but if someone knows a way to do it programatically, I'm willing to give it a try and learn.
Thanks!
![excel]: https://photos.app.goo.gl/dRSTE72CXNa18RzP8
In below example I've used: =MAX(0,MIN($O$2,H2)-MAX($O$1,G2)), and formatted like [mm].
In Excel, units are days, so if you want to calculate the amount of minutes between two timestamps, you need to subtract both and multiply the differencee by 24*60 (being the amount of minutes in one day).
E.g. You start working at 09:07 (cell B2), and finish at 18:07 (cell B3), having a 45-minutes break. Then the time you worked in minutes, is:
=(B3-B2)*24*60-45
Make sure the cell formatting is correct (general), you'll get : 495.

How do I count employees who work per hour including midnight using VBA?

I need to count the number of employee who work in a particular hour:
For ex: My shift is 11:00pm to 8:00am, and I'm looking for the number of employees who's working within 11:00pm to 12:00am. My current shift is within the provided time (1 hour). If I'm looking for 8:00am to 9:00am and my shift is not within the provided time, then no count for me.
The problem is the formula doesn't counting time that passes midnight.
I tried several formulas and converted it into VBA code, but I failed. Here's the formula that I recently converted to code:
=COUNTIFS(B$2:B$7,"<"&F9,C$2:C$7,">"&E9)
and
=SUMPRODUCT(--(B2:B7<F2),--(C2:C7>F1))
Any ideas?
My solution utilizes two formulas.
Your current problem is to tell if a worker who works (for example) between 10:00pm - 3:00am. In order to reduce the risk of confusing the system, I opted to use a 24 hour clock. To minimize the need for input such as a date in addition to the time, I created a table as such:
Each cell contains the formula =IF($H$3>C3,IF(IF(C3>D3,D3+24,D3)>$H$3,"X",""),""). The cells under "Active?" displays an "X" as long as the worker is currently on the clock.
The cell to check how many employees are on the clock, it runs a simple count if formula =COUNTIF(E3:E6,"X") to count how many instances of "x" in the Active column are present.
To operate, you simply extend the range for any additional workers and fill out the cells accordingly. Once the table is populated, you can verify who is working by typing in the desired time in H3. Hope this helps!

Resources