Nesting DATEDIF and IF Statements in Excel - excel

I am trying to create an Excel formula that will display someone's age in units of days, weeks, months, or years, depending on how old they were at the time of a test. To clarify...
If younger than 7 days, report the age in days
If older than 7 days but younger than 8 weeks, report the age in weeks
If older than 8 weeks but younger than 24 months, report the age in months
If older than 24 months, report the age in years
I have tried several functions for this, all using the DATEDIF function. For each, I have returned the #NAME? error. Please see below
KEY:
E600 = Date of Test
F600 = Date of Birth
I am using 7 days = 1 week, 60 days = 2 months, 730 days = 24 months = 2 years, etc.
Note: I also want to display "d/o" (days old), "w/o" (weeks old), "m/o" (months old), or "y/o" (years old) after each value, depending on the units that age is being reported in.
\\\\\\\\\\\\
=IFS((E600-F600)<7,DATEDIF(F600,E600,”d”)&” d/o”,AND((E600-F600)>7,(E600-F600)<60),(DATEDIF(F600,E600,”d”)/7)&” w/o”,AND((E600-F600)>60,(E600-F600)<730),DATEDIF(F600,E600,”m”)&” m/o”,(E600-F600)>730,DATEDIF(F600,E600,”y”)&” y/o”)
\\\\\\\\\\\\\
=IFS(DATEDIF(F600,E600,”d”)<=6,DATEDIF(F600,E600,”d”)&” d/o”,AND(DATEDIF(F600,E600,”d”)<=59,DATEDIF(F600,E600,”d”)>=7),DATEDIF(F600,E600,”d”)/7&” w/o”,AND(DATEDIF(F600,E600,”d”)>=60,DATEDIF(F600,E600,”m”)<=24),DATEDIF(F600,E600,”m”)&” m/o”,DATEDIF(F600,E600,”m”)>25,DATEDIF(F600,E600,”y”)&” y/o”)
\\\\\\\\\\\\\
=DATEDIF(F600,E600,IFS((E600-F600)<7,”d”&” d/o”,AND((E600-F600)>7,(E600-F600)<60),”d”/7&” w/o”,AND((E600-F600)>60,(E600-F600)<730),”m”&” m/o”,(E600-F600)>730,”y”&” y/o”))
\\\\\\\\\\\\\\
Any help on this would be greatly appreciated!

=LOOKUP(E600-F600,{0,8,56,730},INT(DATEDIF(F600,E600,{"d","d","m","y"})/{1,7,1,1})&" "&{"d","w","m","y"}&"/o")
though your listed criteria state only "if younger than", "if older than", so it's not clear in which category someone who, for example, is precisely 7 days old should lie.

#Jos Woolley's example worked great. Another solution is posted below if anyone is troubleshooting their own formula in the future.
=IF(E600 < F600 + 7, DATEDIF(F600, E600, "d") & " d/o", IF(AND(E600 >= F600 + 7, E600 <= EDATE(F600, 2)), ROUNDDOWN(DATEDIF(F600, E600, "d") / 7, 0) & " w/o", IF(AND(E600 >= EDATE(F600, 2), E600 <= EDATE(F600, 24)), DATEDIF(F600, E600, "m") & " m/o", DATEDIF(F600, E600, "y") & " y/o")))

Related

Why is my process of finding the number of integer years, integer months, and integer days from the number of days not working?

I need to find out the number of integer years, number of integer months, and number of integer days from the number of days, given as an input. However, my process is not working. I am getting the wrong answer.
Note: Assume, each year to be 365 days and each month to be 30 days. Also, please do not consider leap years.
user=int(input("Please enter the number of days: "))
def year(num):
year=int(num/365)
month=int((num%365)/30)
day=num%30
print(f"{year} years, {month} months and {day} days")
year(user)
I am getting 11 years, 10 months and 10 days as the output. However, the question prompt also gave me some sample outputs. In their sample output the output for 4330 was 11 years, 10 months and 15 days. Why are we getting different outputs? My code is flawless. I am getting what I expected to get. However, my expected output is not matching with the output given in my prompt.
I'm not sure why you're calculating the number of days using day=int((((num/365-int(num/365))*12)-month)*30), which I can't understand logically. A very simple, readable way would be:
def year(num):
year=num//365
month=(num%365)//30
day=num-(year*365)-(month*30)
print(f"{year} years, {month} months and {day} days")
year(4330) # prints 11 years, 10 months and 15 days
Edit:
day=num%30 wouldn't work, because it doesn't factor in the fact that an year has 365 days. Consider num = 730, which is 2 years and 0 days. Using your formula, you'll get day = 10, which is wrong.

Excel DATEDIF not showing the correct answer

I'm trying to make a calculator that calculates the amount of years, months and days from one date to another. The problem is that the calculation of days not always is accurate, or at least I don't think it is. I have downloaded four age calculators from GooglePlay store, and two of them sometimes give me a different result.
When I use the dates as shown in the picture some of the two of the calculators give me the answer 6 years, 8 months and 9 days.
The formulas I use is as follow:
C4 =DATEDIF(DATE(C3;E3;F3);DATE(C2;E2;F2);"Y")
E4 =DATEDIF(DATE(C3;E3;F3);DATE(C2;E2;F2);"ym")
F4 =DATEDIF(DATE(C3;E3;F3);DATE(C2;E2;F2);"md")
Is there a way to figure out if my calculation is correct?
Clearly on 22 April 2019 they would be exactly 6 years 0 months 0 days.Also 22 December 2019 they would be exactly 6 years 8 months 0 days. Starting from there:
Based on inspection the value of 10 days appears correct.

Calculate Hours depending on workdays, month, hours per day

I made a formula in excel, yet when trying it out, it responds with NAME?
=IF(MAAND(VANDAAG()) > MAAND(D1); (NETTO.WERKDAGEN(D1; D3)) * D5; D6)
My goal is to calculate how many Hours I still need This Month in Row 8.
depending on startdate(startdatum and current date (huidige datum) (not enddate(einddatum)) and how many work days there are between those dates times the hours.
Any ideas on how to solve this?
UPDATE
translations:
Maand = Month
Vandaag = Today
Netto.WerkDagen = NETWORKDAYS
Solved. I had to use ALS (dutch translation).....

Calculate past 12 and 6 month Average

The excel user will export the data from an online website to excel (12 months data), so the data will be all the time different.
I need the past 6 months and 12 months average (However, the calculation need to use the months I have in the data, and sometimes there will be less then 6 or 12 months), but I still need to get the average and frequency for it , however, I am not sure how to get it.
I am trying to write a code, but it is not complete and is not working as well, I don't get an error; it just doesn't work.
I am open for Excel formulas as well, the problem may be the last row and that it need to use the data I have to calculate and not 6 and 12 months full.
PS: I post a similar question on https://www.ozgrid.com/forum/index.php?thread/1227312-dynamic-way-to-calculate-the-last-6-months-average/
Will This formulas work for you?
Average for last 6 months:
=AVERAGEIF(A:A;">="&EDATE(MAX(A:A);-6);B:B)
Frequency for last 6 months:
=IF(MONTH(MAX(A:A)-MIN(A:A))>=6;COUNTIF(A:A;">="&EDATE(MAX(A:A);-6))/6;COUNTIF(A:A;">="&MIN(A:A))/MONTH(MAX(A:A)-MIN(A:A)))
Average for last 12 months:
=AVERAGEIF(A:A;">="&EDATE(MAX(A:A);-12);B:B)
Frequency for last 12 months:
=IF(MONTH(MAX(A:A)-MIN(A:A))>=12;COUNTIF(A:A;">="&EDATE(MAX(A:A);-12))/12;COUNTIF(A:A;">="&MIN(A:A))/MONTH(MAX(A:A)-MIN(A:A)))

Adding the Sum of 2 Date Ranges using DATEDIF Function (Showing in Years, Months, Days Format)

I'm working on an Assignment where 2 different Date Ranges are available. I'm trying to add their differences and formatting them in XX Years, XX Months, XX Days (where XX is the Sum Number).
Now for a single range, I've searched on internet and Found DATEDIF Function useful like in Below image:
Formula
=DATEDIF(Start_Date_1,End_Date_1,"y")&" Years, "&DATEDIF(Start_Date_1,End_Date_1,"ym")&" Months, "&DATEDIF(Start_Date_1,End_Date_1,"md")&" Days"
It works Fine for 1 Date Range. But I've 2 different Ranges and 2 Different Differences. So I've tried to Sum the Each Parameter (Year, Month & Days) in the above formula Like Below:
Formula:
=DATEDIF(Start_Date_1,End_Date_1,"y")+DATEDIF(Start_Date_2,End_Date_2,"y")&" Years, "&DATEDIF(Start_Date_1,End_Date_1,"ym")+DATEDIF(Start_Date_2,End_Date_2,"ym")&" Months, "&DATEDIF(Start_Date_1,End_Date_1,"md")+DATEDIF(Start_Date_2,End_Date_2,"md")&" Days"
Now the Formula works fine until the Months/Days Range exceed 12/31 Respectively. As Seen in the picture, The months range has summed up to 13 while Days to 32. Now I'm looking for any solution or even Different approach to do the same formatting. Any Solution or Suggestion will be highly appreciated.
Thanks in Advance :).
If you define a month as 30 days you could just nestle IF's to compensate:
=IF(DATEDIF(Start_Date_1,End_Date_1,"md")+DATEDIF(Start_Date_2,End_Date_2,"md")>30,IF((DATEDIF(Start_Date_1,End_Date_1,"ym")+DATEDIF(Start_Date_2,End_Date_2,"ym")+1)>11,(DATEDIF(Start_Date_1,End_Date_1,"y")+DATEDIF(Start_Date_2,End_Date_2,"y")+1)&" Years, "&(DATEDIF(Start_Date_1,End_Date_1,"ym")+DATEDIF(Start_Date_2,End_Date_2,"ym")-11)&" Months, "&(DATEDIF(Start_Date_1,End_Date_1,"md")+DATEDIF(Start_Date_2,End_Date_2,"md")-30)&" Days",(DATEDIF(Start_Date_1,End_Date_1,"y")+DATEDIF(Start_Date_2,End_Date_2,"y"))&" Years, "&(DATEDIF(Start_Date_1,End_Date_1,"ym")+DATEDIF(Start_Date_2,End_Date_2,"ym")+1)&" Months, "&(DATEDIF(Start_Date_1,End_Date_1,"md")+DATEDIF(Start_Date_2,End_Date_2,"md")-30)&" Days"),DATEDIF(Start_Date_1,End_Date_1,"y")+DATEDIF(Start_Date_2,End_Date_2,"y")&" Years, "&DATEDIF(Start_Date_1,End_Date_1,"ym")+DATEDIF(Start_Date_2,End_Date_2,"ym")&" Months, "&DATEDIF(Start_Date_1,End_Date_1,"md")+DATEDIF(Start_Date_2,End_Date_2,"md")&" Days")
So IF days are over Thirty we check if months+1 are over 11, if both yes we minus 30 from days, 11 from months (11 to compensate a year plus the month from days) and add 1 to years, if months+1 aren't over 11 we simply add one to months and minus 30 from days, if neither are over the limits then we display the unadulterated formula.
(using your images as an example:
=IF(DATEDIF(A3,B3,"md")+DATEDIF(E3,F3,"md")>30,IF((DATEDIF(A3,B3,"ym")+DATEDIF(E3,F3,"ym")+1)>11,(DATEDIF(A3,B3,"y")+DATEDIF(E3,F3,"y")+1)&" Years, "&(DATEDIF(A3,B3,"ym")+DATEDIF(E3,F3,"ym")-11)&" Months, "&(DATEDIF(A3,B3,"md")+DATEDIF(E3,F3,"md")-30)&" Days",(DATEDIF(A3,B3,"y")+DATEDIF(E3,F3,"y"))&" Years, "&(DATEDIF(A3,B3,"ym")+DATEDIF(E3,F3,"ym")+1)&" Months, "&(DATEDIF(A3,B3,"md")+DATEDIF(E3,F3,"md")-30)&" Days"),DATEDIF(A3,B3,"y")+DATEDIF(E3,F3,"y")&" Years, "&DATEDIF(A3,B3,"ym")+DATEDIF(E3,F3,"ym")&" Months, "&DATEDIF(A3,B3,"md")+DATEDIF(E3,F3,"md")&" Days")
Will work for you)

Resources