Calculation of Night shift hours - sql-server

I am stuck with an issue, I have to create an attendance system where I have an employee id and punching time.
There can be multiple punches in a day. So firstly for night shift employees I can't determine his actual in and out time and thus not the duration too.
For example a person came office at 11 pm on 1st April and left at 4 am on 2nd April. In between he also went out for dinner or tea .
I don't have a fixed number of punches. And I need to handle 2 dates as well. So min and max logic wont work.
The major problem is there are multiple punch-ins and row_number wont work as punches are of 2 different dates in case of night shift.
here tktno is employeeID , date is the punching time and sno, is number of punches in a day. I have sent you a sample data of a night shift employee.
I require data in given below format -
But here as you can see the hours calculation is not correct . I want to correct it .

Related

How to get a YTM calculation instead of YTD in Google Data Studio

I have an earnings table in my Google Data Studio report from January 21 - YTD.
But what i want is a table that shows me YTM (so this year until last month) Jan-21 til Sep-21 and that dynamicly on 1st Nov, Okt-21 is added to the table, and on 1st dec Nov-21
is added etc.
Because the Total YTD value is other wise too high, due to cost being booked at the end of the month and earnings through the month.
Filters wont let you do it dynamicly,
date range filters same,
always possible to do it manualy with date range but this i cannot ask from my client.
any suggestions??
Kind regards
I found the answer,
i left the question for when someone else wants to do it,
it is actualy simpel fixed by using advanced date filter and then leaving the start date on 1-jan, and end date to today minus 1 month.
this gave me the exact result i needed yt last month

Is there a way to consolidate multiple formulas into one

all:
I am trying to design a shared worksheet that measures salespeople performance over a period of time. In addition to calculating # of units, sales price, and profit, I am trying to calculate how many new account were sold in the month (ideally, I'd like to be able to change the timeframe so I can calculate larger time periods like quarter, year etc').
In essence, I want to find out if a customer was sold to in the 12 months before the present month, and if not, that I will see the customer number and the salesperson who sold them.
So far, I was able to calculate that by adding three columns that each calculate a part of the process (see screenshot below):
Column H (SoldLastYear) - Shows customers that were sold in the year before this current month: =IF(AND(B2>=(TODAY()-365),B2<(TODAY()-DAY(TODAY())+1)),D2,"")
Column I (SoldNow) - Shows the customers that were sold this month, and if they are NOT found in column H, show "New Cust": =IFNA(IF(B2>TODAY()-DAY(TODAY()),VLOOKUP(D2,H:H,1,FALSE),""),"New Cust")
Column J (NewCust) - If Column I shows "New Cust", show me the customer number: =IF(I2="New Cust",D2,"")
Column K (SalesName) - if Column I shows "New Cust", show me the salesperson name: =IF(I2="New Cust",C2,"")
Does anyone have an idea how I can make this more efficient? Could an array formula work here or will it be stuck in a loop since its referring to other lines in the same column?
Any help would be appreciated!!
EDIT: Here is what Im trying to achieve:
Instead of:
having Column H showing me what was sold in the 12 months before the 1st day of the current month (for today's date: 8/1/19-7/31/20);
Having Column I showing me what was sold in August 2020; and
Column I searching column H to see if that customer was sold in the timeframe specified in Column H
I want to have one column that does all three: One column that flags all sales made for the last 12 months from the beginning of the current month (so, 8/1/19 to 8/27/20), then compares sales made in current month (august) with the sales made before it, and lets me know the first time a customer shows up in current month IF it doesn't appear in the 12 months prior --> finds the new customers after a dormant period of 12 months.
Im really just trying to find a way to make the formula better and less-resource consuming. With a large dataset, the three columns (copied a few times for different timeframes) really slow down Excel...
Here is an example of the end result:
Example of final product

Limiting datediff by Hours and Days

Long time lurker and now i have my first question:
I'm designing a SQL Report. One Task is to calculate the amount of minutes between two Times. They can be the same day or on different days. In the Database there are 4 Columns given
The Start Date (as Datetime e.g. 24.10.2017 00:00:00)
The Start Time (as Datetime e.g. 01.01.1899 11:25:00)
The End Date (formated as above)
The End Time (formated as above)
I'm calculating three Filds, all in Minutes
Days between: =DateDiff("n", Fields!StartDatum.Value,Fields!QualDatum.Value)
Minutes between: =DateDiff("n",Fields!StartZeit.Value,Fields!QualZeit.Value)
Adding those up: =Fields!QualiZeitTage.Value+Fields!QualiZeitMinuten.Value
All of this is working great and produces the desired output.
My Problem is, that i don't need the full time between those events. I only want to count minutes that are between 7:00 am and 8:00 pm. Also, i want to exclude Saturdays and Sundays. How would i go about limiting the datediff function to my desired times?
Second Problem: The Endtime and Date are only written when the event actually is finished. If it's still ongoing those Fields are empty producing a negative number (-1060764480 for example). Since i'm only using those to produce Boolean output on surpassing a certain length, it's no problem. I would like to handle that more "cleanly" though. Any thoughts?

Short Formula To Pull Upcoming Holidays For a Title Merge In A Huge Database

I am Using excel 2007 and running Windows 7 Pro. I have 50 Thousand rows with data.
I am trying to do a formula that gives me a value of the next upcoming holiday. I have 4 columns of holidays.
For example: Today's date is 6/3/2015 next upcoming holiday would be Father's Day and after that would be Halloween.
So, from Today's date to Father's Day the value would contain "Father's Day"
After Father's day, I want the formula to automatically replace "Father's day" with "Halloween" And doing the same to upcoming holidays after that.
I have researched forums and gotten ideas to compose a formula. I was able to create one, but the only way I got it to work was with the holiday's date instead of there name. If there is no other way of doing it besides that way I would be okay with it.
Only thing i'm concerned about is Opening, Saving, and Editing a file that large with a formula that big is very time consuming and unresponsive sometimes. I need help and would like to have a formula that dose what I need more efficiently and quicker.Any Ideas on shortening the formula?
Here is my formula:
=IF(SMALL(IF($A$2:$D$2>TODAY(),$A$2:$D$2),1)=DATE(YEAR(TODAY()),2,14),"Valentine's Day",IF(SMALL(IF($A$2:$D$2>TODAY(),$A$2:$D$2),1)=DATE(YEAR(TODAY()),4,5),"Easter Day",IF(SMALL(IF($A$2:$D$2>TODAY(),$A$2:$D$2),1)=DATE(YEAR(TODAY()),5,10),"Mother's Day",IF(SMALL(IF($A$2:$D$2>TODAY(),$A$2:$D$2),1)=DATE(YEAR(TODAY()),6,21),"Father's Day",IF(SMALL(IF($A$2:$D$2>TODAY(),$A$2:$D$2),1)=DATE(YEAR(TODAY()),10,31),"Halloween",IF(SMALL(IF($A$2:$D$2>TODAY(),$A$2:$D$2),1)=DATE(YEAR(TODAY()),12,25),"Christmas"))))))
I have dates in Array A2:D2
I have two differnt ways i can lay out my data. One way is with the holiday's name, and the other is with the holidays date. Examples are shown below... My formula displayed above is created to work with the first example below.
A B C D
2/14/2015 6/21/2015 10/31/2015 12/25/2015
OR
A B C D
Mother's day Father's day Halloween Christmas
I need it to read A-D search for the next upcoming holiday and return the value.
Thanks in advance for any help, tips, and ideas.
If the dates and names for the corresponding holidays are set up as such:
A B
2/14/2015 Valentine's Day
4/5/2015 Easter Day
5/10/2015 Mother's day
6/21/2015 Father's day
10/31/2015 Halloween
12/25/2015 Christmas
This formula can work to get the next holiday from today's date.
=INDEX(B2:B7,MATCH(TODAY(),A2:A7,1)+1)
If the names for the corresponding holidays are not within the list. You can hardcode them in the INDEX formula. Replace the first parameter with an array of hardcoded days, i.e:
{"Valentine's Day","Easter Day","Mother's Day","Father's day","Halloween","Christams"}
Even if we have the dates in a horizontal range, lets say A1:D1 we can still hardcode the days in an INDEX as such:
=INDEX({"Mother's Day","Father's day","Halloween","Christams"},MATCH(TODAY(),$A$1:$D$1,1)+1)

Calculate Facebook likes, comments, and shares for different time zones from saved UTC

I've been struggle with this for a while and hope someone can give me an idea to tackle this.
We have a service that goes out and collects Facebook likes, comments, and shares for each status update multiple times a day. The table that stores this data is something like this:
PostId EngagementTypeId Value CollectedDate
100 1(for likes) 10 1/1/2013 1:00
100 2 (comments) 2 1/1/2013 1:00
100 3 0. 1/1/2013 1:00
100. 1. 12 1/1/2013 3:00
100. 2. 3. 1/1/2013 3:00
100. 3 5. 1/1/2013 3:00
Value holds the total for each engagement type at the time of collection.
I got a requirement to create a report that shows new value per day at different time zones.
Currently,I'm doing the calculation in a stored procedure that takes in a time zone offset and based on that I calculate the delta for each day. If this is for someone in California, the report will show 12 likes, 3 comments, and 5 shares for 12/31/2012. But if someone with the time zone offset of -1, he will see 10 likes on 12/31/2012 and 2 likes on 1/1/2013.
The problem I'm having is doing the calculation on the fly can be slow if we have a lot of data and a big date range. We're talking about having the delta pre-calculated for each day and stored in a table and I can just query from that ( we're considering SSAS but that's for the next phase). But doing this, I would need to have the data for each day for 24 time zones. Am I correct (and if so, this is not ideal) or is there a better way to approach this?
I'm using SQL 2012.
Thank you!
You need to convert UTC DateTime stored in your column to Date based on users UTC time. This way you don't have to worry about any table that has to be populated with data. To get users date from your UTC column you will use something like this
SELECT CONVERT(DATE,(DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), '01/29/2014 04:00')))
AS MyLocalDate
The select statement above figures out Local date based on the difference of UTC date and local Date. You will need to replace GETDATE() with users DATETIME that is passed in to your procedure and replace '01/29/2014 04:00' with your column. This way when you select any date from your table it will be according to what that date was at users local time. Than you can calculate other fields accordingly.

Resources