Power Query - Create date from datenumber and time column - excel

In Excel's powerquery editor I have two columns. One is a daynumber (1 to 365) and the other is the time. I would like to merge the two columns to a datetime column. Day 1 will correspond with 01-01-2000. How can I do this?
For example:
column 1: 365,
column 2: 02:00:00
Transform column 3: 31-12-2000 02:00:00.

You can use this to add a column which combines the relative date and time values:
= Table.AddColumn(#"Changed Type", "DateTime", each DateTime.FromText(Date.ToText(Date.AddDays(#date(1999,12,31),[#".1"])) & " " & Time.ToText([#".2"], "hh:mm:ss")), type datetime)
Check your logic for the date of Day 0, though - if day 1 is 01/01/2000 then day 365 <> 31/12/2000...

Excel dates are built up on float values where the integer is the number of days passed since 1900-01-01 and the fraction is the time of day.
With that in mind 2000-01-01 is 36526.
With the formula:
=36526+A1+B1
You get the datetime value, then just set the prefered cell format
With this formula 365 + 02:00:00 will become as you expect in question.
But 1 can't be 2000-01-01 then, it has to be 2000-01-02. Or you need to specify where to remove a day becasuse the math does not add up.

Related

Excel remove timestamp from date and subtract days

So I have a column say Date1 which has date in datetime stamp. I want to subtract 10 days from Date1 column and keep in another column say Date2. I only want to subtract ten days from date not from datetime.
How to remove the time stamp. Read many solutions online but could not find for excel
Input table
Date1
26-03-2000 21:00:00
25-04-2000 00:00:00
21-03-2000 01:00:00
31-03-2000 13:00:00
05-03-2012 12:00:00
Expected output
Date1 Date2 Date1_no_timestamp
26-03-2000 21:00:00 16-03-2000 26-03-2000
25-04-2000 00:00:00 15-04-2000 25-04-2000
21-03-2000 01:00:00 11-03-2000 21-03-2000
31-03-2000 13:00:00 21-03-2000 31-03-2000
05-03-2012 12:00:00 24-02-2012 05-03-2012 and so on
You could use the TEXT() function.
=TEXT(B2, "DD-MM-YYYY")
Alternatively, as the above solution could cause issue based on timezone formatting, you could remove anything past the first space:
=LEFT(B2, FIND(" ",A2,1)-1)
Place either the following in C2 (assuming those headers exist) and drag down.
You could use:
Method 1:
Date1_no_timestamp:
=TEXT(A2,"dd-mm-yyyy")
Date2:
=TEXT(A2-10,"dd-mm-yyyy")
Method 2
Date1_no_timestamp:
=RIGHT("0"&DAY(A2),2)&"-"&RIGHT("0"&MONTH(A2),2) & "-" & YEAR(A2)
Date2:
=TEXT(DATEVALUE(E2)-10,"dd-mm-yyyy")
Results:
You can also use the INT() and TRUNC() functions:
=INT(A2)
=TRUNC(A2)
Their behavior is identical for positive numbers - the decimal part is sliced off.

Convert sting in datetime like excel format cell does in t-sql

I have END_Date data like 43830.99931. When I paste it in the excel and format the cell to Date. It convert it to 12/31/2019 11:59:00 PM. I want same functionality in T-SQL. How can I achieve same result using T-SQL?
Excel (by default) uses the 1900 date system. This simply means that the date 1 Jan 1900 has a true numeric value of 1, 2 Jan 1900 has a value of 2 etc. These values are called "serial values" in Excel and it is these serial values that allows us to use dates in calculations.
The code to convert:
DECLARE #SerialDate FLOAT;
SET #SerialDate = 43830.99931;
SELECT CAST(#SerialDate - 2 AS DATETIME);
returning back the date time value of:
2019-12-31 23:59:00.383
You may have noticed the -2 in the cast. That's because Excel, due to issues with 29th Feb 1900 and the inclusiveness of the start/end dates. (1900-01-01 is day 1 not day 0, and 1900 was not a leap year but excel calculates it wrongly) we have to subtract 2. Details on that issue can be found here.

Retutn only Buisness days in custom date function in excel

I have a date which I will need to increment the date by a day, month or year from the actual date. This is based of a list of fields below and is different per the input. Also each resultant date has to be a business day i.e not a weekend day or a bank holiday. Also if the resultant date is either Dec 25 or Jan 1st the day needs to move forward to the next business day (not a weekend day).
I have created this in Excel using a couple of formulas though it is a bit clunky.
Below is my data set
Business Date 15/05/2018
Tenor Settlement Value Settlement Period
ON 1 Day
TN 2 Day
SP 2 Day
SN 3 Day
1W 7 Day
2W 14 Day
3W 21 Day
1M 1 Month
2M 2 Month
3M 3 Month
In column E - I am using formula
=IF(D4="Day",$B$1+C4,IF(D4="Month",EDATE($B$1,C4),(TEXT($B$1,"dd/mm/")&(YEAR($B$1)+C4))+0))
In column F - I am using formula
=E4+LOOKUP(WEEKDAY(E4),{1,2,3,4,5,6,7},{1,0,0,0,0,0,2})
In column G - I am using formula
=F4+IF(AND(OR(TEXT(F4,"ddmm")="2512",TEXT(F4,"ddmm")="0101"),WEEKDAY(F4)>=2,WEEKDAY(F4)<=6),LOOKUP(WEEKDAY(F4),{1,2,3,4,5,6,7},{0,1,1,1,1,3,0}),LOOKUP(WEEKDAY(F4),{1,2,3,4,5,6,7},{1,0,0,0,0,0,2}))
In H I format the date in mm/dd/yyyy and I have my desired result.
storax has kindly created a function for me which replicates my excel formula in column E - on this thread Increment a date by a number of days, months or years
Function IncDate(ByVal dt As Date, ByVal add As Long, ByVal dmy As String) As Date
Select Case UCase(dmy)
Case "DAY"
IncDate = DateAdd("d", add, dt)
Case "MONTH"
IncDate = DateAdd("m", add, dt)
Case "YEAR"
IncDate = DateAdd("yyyy", add, dt)
Case Else
IncDate = dt
End Select
Could use some advise on how I could incorporate my formulas in columns F & G to make the process less clunky.
Manipulating the DATE function (DateSerial in vba) with the WORKDAY.INTL function seems to produce the correct business dates.
Put this in E4 and fill down.
=WORKDAY.INTL(DATE(YEAR(B$1)+(D4="year")*C4, MONTH(B$1)+(D4="month")*C4, DAY(B$1)+(D4="day")*C4)-1, 1, 1, holidays)
[holidays] is a named range (Formulas, Defined Names, Defined Name) with a Refers To: of,
=Sheet10!$Z$2:INDEX(Sheet10!$Z:$Z, MATCH(1E+99, Sheet10!$Z:$Z))

Find the Earliest Date in Excel

i want to find the earliest date between the DOB OF FATHER & DOB OF MOTHER in sheet1, by matching the employee code and having the value in earliest date in sheet 2.
Sheet 1
Employee Code DOB OF FATHER DOB OF MOTHER
28883 29/12/1987 28/01/1988
83933 19/11/1988 12/07/1988
55428 21/01/1938 03/10/1938
99999 18/03/1982 11/02/1980
Sheet 2
Employee Code Earliest Date
28883
99999
83933
55428
Sheet1:
A B C
1 Code FatherDOB MotherDOB
2 28883 29/12/1987 28/01/1988
3 83933 19/11/1988 12/07/1988
4 55428 21/01/1938 03/10/1938
5 99999 18/03/1982 11/02/1980
Sheet2:
A B
1 Code EarliestDOB
2 28883 29/12/1987
3 99999 11/02/1980
4 83933 12/07/1988
5 55428 21/01/1938
You can combine two vlookup operations with a min operation:
=MIN(VLOOKUP(A2,Sheet1!$A$2:$C$5,2,FALSE),VLOOKUP(A2,Sheet1!$A$2:$C$5,3,FALSE))
The first vlookup gives you the father's date of birth (using the entire table range but extracting the second column) and the second gives you the mother's date of birth (extracting the third column).
The earliest is then simply the minimum of the two.
If some of the dates may be blank, the easiest solution is probably to set up a D column on sheet 1 to evaluate the earliest date, ignoring blanks. For example D2 would have (split across lines for readability):
=IF(ISBLANK(B2),
B3,
IF(ISBLANK(C2),
B2,
MIN(VLOOKUP(A2,$A$2:$C$5,2,FALSE),
VLOOKUP(A2,$A$2:$C$5,3,FALSE))))
If one of the cells is blank, it uses the other, otherwise it chooses the earliest.
Then you just lookup that new column D in the formula on sheet 2 (example for B2):
=VLOOKUP(A2,Sheet1!$A$2:$D$5,4,FALSE)
I wanted to point out a limitation that has been in Excel forever.
Excel internally doesn't handle dates before 3/1/1900 (March 1, 1900) correctly.
it thinks that 1900 was a leap year
that the day before 3/1/1900 is 2/29/1900
if you enter pass Excel through automation any date between 12/31/1899 and 2/28/1899, it will think it is 1/1/1900 - 2/29/1900
if you attempt to pass it through automation 12/30/1899, it will think it is January 0, 1899.
if you attempt to pass it any date before 12/30/1899, it will throw an error
Various example dates that you can pass to Excel through automation:
Date Excel
-------- ------------------------------
18651001 throws error going into Excel
18991201 throws error going into Excel
18991229 throws error going into Excel
18991230 shows as "12:00:00" in Excel; it refuses to show a date portion
18991231 shows in Excel as 1/1/1900
19000101 shows in Excel as 1/2/1900
19000102 shows in Excel as 1/3/1900
19000103 shows in Excel as 1/4/1900
19000201 shows in Excel as 2/2/1900
19000228 shows in Excel as 2/29/1900
19000229 Feb 29 1900 was not a real date; Excel takes it
19000301 shows in Excel as 3/1/1900
19000601 shows in Excel as 6/1/1900
19001231 shows in Excel as 12/31/1900
19010101 shows in Excel as 1/1/1901
20151128 shows in Excel as 11/28/2015
The VARIANT structure does dictate that a date must be after December 30, 1899 midnight as time zero:
2.2.25 DATE
DATE is a type that specifies date and time information. It is represented as an 8-byte floating-point number.
This type is declared as follows:
typedef double DATE;
The date information is represented by whole-number increments, starting with December 30, 1899 midnight as time zero. The time information is represented by the fraction of a day since the preceding midnight. For example, 6:00 A.M. on January 4, 1900 would be represented by the value 5.25 (5 and 1/4 of a day past December 30, 1899).
tl;dr: If you have any dates in Excel before March 1 1900 (e.g. the birthdate of the oldest woman alive), Excel will not perform the math correctly.
If you wish to display any date before 3/1/1900, it should be presented as text, rather than an actual date.
Anyone attempting to find the minimum date in a range needs to be aware of this limitation in Excel.

Excel - Date Range includes given time of day (a better approach)

Given a set of DateTime ranges in excel, such as:
Start Finish
13/03/2012 10:00:00 14/03/2012 03:00:00
15/03/2012 08:30:00 15/03/2012 10:00:00
And some TimeSpan such as:
Start Finish
07:00:00 09:00:00
How would you determine if the time span falls in some given date range?
An approach like this might be a start:
AND(B2 < DATEVALUE(TEXT(B2, "dd/mm/yyyy")) + TIMEVALUE("07:00:00"),
B3 >= DATEVALUE(TEXT(B3, "dd/mm/yyyy")) + TIMEVALUE("09:00:00"))
Though it relies on the being able to provide the start/finish values explicitly as opposed to two dates in any order. A conditional on start <= finish would do, but seems like it's overly complicated.
Is there a better way?
Edit: Bonus points for a simple approach to finding the percentage of the date range that falls in the time span
Seems like you're assuming that the dates will always be the same day, is that the case?
Try
=AND(MOD(A2,1)<=F2,MOD(B2,1)>=G2)
For percentage
=MAX(0,MIN(MOD(B2,1),G2)-MAX(MOD(A2,1),F2))/(B2-A2)
Update:
If the date range can be unlimited, 1 day or many, then you can use this formula to get the total hours within the timespan
=(INT(B2)-INT(A2))*(G$2-F$2)+MEDIAN(F$2,G$2,MOD(B2,1))-MEDIAN(MOD(A2,1),G$2,F$2)
that assumes that the timespan doesn't cross midnight - if timespan may cross midnight, e.g. could be 08:00 - 11:00 but could also be 22:00 - 03:00 then this formula should work
=(F$2>G$2)*(B2-A2)+SIGN(G$2-F$2)*((INT(B2)-INT(A2))*ABS(G$2-F$2)+MEDIAN(F$2,G$2,MOD(B2,1))-MEDIAN(MOD(A2,1),G$2,F$2))
This should give the number of hours falling in the time span (but it's definitely not simple!):
=MEDIAN(F2,G2+(G2<F2),MOD(B2,1)+(MOD(B2,1)<MOD(A2,1)))
-MEDIAN(F2,G2+(G2<F2),MOD(A2,1))
+(F2<G2)*(MOD(B2,1)<MOD(A2,1))*MAX(MIN(MOD(B2,1),G2)-F2,0)
If this is greater than 0, the date range falls in the time span, divide this by B2-A2 for the percentage.
e.g. Date Range: 6:00PM - 9:00AM, Time span: 7:00AM - 7:00PM returns 03:00 which is 20% of the date range.
Say the date ranges are in columns A and B, and the Timespan in F2 and G2.
Apply the following formulas and drag down.
H2 = IF(AND($F$2>=RIGHT(A2,8),$F$2<RIGHT(B2,8),$G$2>RIGHT(A2,8),$G$2<=RIGHT(B2,8)),1,0)
Column H gives tells you if it's true for a particular date range.
I1 = SUM(H2:H4)/COUNT(H2:H4)
I1 gives you the percentage

Resources