Find the Earliest Date in Excel - 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.

Related

How do I get a string value of the 5 digit serial date format from a cell into my script?

I have a formula in a cell that is doing a vlookup to another sheet by combining the values of two cells on my spreadsheet. It's a table of names on the left and dates up top. I'm combining the name and date values into a single string value and using that as by vlookup key value, which works great. Now I need to basically do the same thing in my google script, but the date value has me at a loss. In the vlookup cell formula, the date value is the 5 digit date serial code. However when I combine the two cell values in my script, I cannot get the same 5 digit serial code out of the cell. I have tried all conversions of number, string, text, ect. What function can I use to get this 5 digit date value as a string in my script?
function serialDatevalue(){
var WB = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var NewDate = new Date();
var NewDate = WB.getRange('B1').getValue();
var JSdate = WB.getRange('B1').getValue();
Browser.msgBox(Number(NewDate));
// Some others that I have tried
// var JSdate = Date.parse(DateValue);
// var JSdate = Number(DateValue);
// var JSdate = Utilities.formatDate(DateValue, 'PST', '%');
}
for example, April 13, 2019 = 43568. I want to get this 5 digit value as a string.
I keep getting 'Sat Apr 13 2019 00:00:00 GMT-0700 (PDT)', or other longer integer values.
Please help.
From Tanaike's answer to Getting the number equalent of duration using GAS
var serialNumber = (dateObject.getTime() / 1000 / 86400) + 25569; // Reference: https://stackoverflow.com/a/6154953
The easier way to get the serialized number corresponding to a certain cell date value in Google Sheets is by using a the built-in Google Sheets function [TO_PURE_NUMBER][1]. This because Google Sheets and JavaScript, which is used by Google Apps Script, used different epoch (reference date used as 0 for the serialized numbers) and use different time units.
From https://developers.google.com/sheets/api/guides/concepts#datetime_serial_numbers
Date/Time serial numbers
Google Sheets, like most other spreadsheet
applications, treats date/time values as decimal values. This lets you
perform arithmetic on them in formulas, so you can increment days or
weeks, add or subtract two date/times, and perform other similar
operations.
Google Sheets uses a form of epoch date that is commonly used in
spreadsheets. The whole number portion of the value (left of the
decimal) counts the days since December 30th 1899. The fractional
portion (right of the decimal) counts the time as a fraction of one
day. For example, January 1st 1900 at noon would be 2.5, 2 because
it's two days after December 30th, 1899, and .5 because noon is half a
day. February 1st 1900 at 3pm would be 33.625.
Note that Google Sheets correctly treats the year 1900 as a common
year, not a leap year.
By the other hand, JavaScript use January 1, 1970 00:00 UTC as the epoch date and use milliseconds as the unit.
A word of caution: Custom code to make serialized number conversions from one to the other should consider if the spreadsheet and the Google Apps Script are using the same timezone or not.
Related
How to read value of fetched cell data as date google sheets API
Getting the number equalent of duration using GAS

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))

How do I write an Excel formula to compare year-to-date to prior years & also account for leap years?

I’m trying to compare a measure as of today through the same day and month for the prior 4 years (e.g. through June 6 of 2016, 2015, 2014, etc.).
For each year, I decided to count the number of days since the beginning of the year, and sum my values through that number of days for each year.
To identify whether a date should be included in the year to date comparison, I used the formula where my date is in cell A1:
=IF((A1-DATE(YEAR(A1),1,1)+1)<=(TODAY()-DATE(YEAR(TODAY()),1,1)+1),1,0)
I’m looking for a way around the issue of the extra day added to leap years. In other words, after February 28th, the day count will always be off by one in a leap year, and trying to use Februrary 29th in a non-leap year will return an error.
I’d like to adjust this formula, but I’m open to using a different function & formula if it gets me the right results.
you can check any information about February, 29th. If an error occurs, you know its no leap year. Catch that error with =IFERROR(;).
Assuming a table structure like this:
A:Date | B:Value
----------------------
01/01/2016 | 0
01/01/2015 | 1
01/01/2014 | 2
01/01/2013 | 3
01/01/2012 | 4
Formula
To - for example - calculate the average of the previous four (excluding the current) years on January 1st (today is 01/01/2016):
=SUMPRODUCT(
(MONTH(A:A)=MONTH(compare))*
(DAY(A:A)=DAY(compare))*
(YEAR(A:A)>YEAR(compare)-5)*
(YEAR(A:A)<YEAR(compare))*
(B:B)
) / (
SUMPRODUCT(
(MONTH(A:A)=MONTH(compare))*
(DAY(A:A)=DAY(compare))*
(YEAR(A:A)>YEAR(compare)-5)*
(YEAR(A:A)<YEAR(compare))*
1
)
)
Result
For the above example, the result is 2.5
Explanation
To select only those rows representing the same month and day:
(MONTH(A:A)=MONTH(compare))*(DAY(A:A)=DAY(compare))
To select only those values from the previous 4 years (excluding the current):
(YEAR(A:A)>YEAR(compare)-5)*(YEAR(A:A)<YEAR(compare))*
The actual values we are interested in:
(B:B)
Divide by 4 for the average over the last four years. This assumes there is no missing data which might be an issue. You could use another SUMPRODUCT (replace B:B with 1) to count the number of resulting rows and divide by that number to handles this case. This seems to be rather slow, but it works.
Note
For performance reason you should not use A:A (a full column) in the formula, just use the actual range you need, which will likely be much faster.

Excel date values compute incorrectly

I am trying to create an excel sheet where each line contains a date/time field for each 15 minute interval for the entire year. I put "00:15:00" in A1 and "1/1/2014" in A2 and "=A2+$A$1" in A3. I then copied A3 down enough times to go thru the year. I set column B equal to col A. I set the format in col A to Short Date and col B to Time. Then I converted everything to values. When I do that, all the entries for midnight are not right. For example the entry between Jan 1 and Jan 2, displays as 1/2/2014 and 12:00:00AM. But the actual value is "1/1/2014 12:00:00 AM". This value is incorrect, it is 24 hours off. What's going on here? How can I fix it?
I'm using Office 2010.
It's standard for 12:00 AM to denote the midnight that starts the day. So, in 15-minute increments, 1/1/14 11:45 PM would typically be followed by 1/2/14 12:00 AM, followed by 1/2/14 12:15 AM.
See: http://en.wikipedia.org/wiki/12-hour_clock#Confusion_at_noon_and_midnight

Resources