I am attempting to calculate a dynamic tenure field in an SSRS report. If the employee has been employed less than a month then display the tenure in days, if employed less than a year then display the tenure in months, then finally display the tenure in years. I have an employee that was hired on 10/31/2016 and is currently displaying a tenure of 2 years. My expression is listed below. Can anyone point out my flaw on this one? It looks right to me.
=IIF(DateDiff(DateInterval.Month,Fields!ASSC_HIRE_DATE.Value,Now())<1,
DateDiff(DateInterval.Day,Fields!ASSC_HIRE_DATE.Value,Now()) & " Days",
IIF(DateDiff(DateInterval.Year,Fields!ASSC_HIRE_DATE.Value,Now())<1,
DateDiff(DateInterval.Month,Fields!ASSC_HIRE_DATE.Value,Now()) & " Months",
DateDiff(DateInterval.Year,Fields!ASSC_HIRE_DATE.Value,Now()) & " Years"))
DateDiff does not work exactly how you think it does. DateDiff(DateInterval.Year,Fields!ASSC_HIRE_DATE.Value,Now()) is evaluating to 2, as you discovered, since it is comparing 2018 to the hire date of 2016. You can check this by replacing Now() with the dates 12/31/2017 and 1/1/2018 - the former will return 1 and the latter will return 2. This works similarly in the first part of your IIf where you are checking months: that evaluates to 16 when it has really only been 15 months and a few days. Essentially, DateDiff rounds whatever the decimal result is up to the next int.
The way you resolve this will ultimately depend on exactly what your requirements are: can you define a month as 30 days and a year as 365 days? Or is it important to consider that some months have more/less days, and some years are leap years?
If you can assume 30 day months and 365 day years, then you could use something like the following (also, I would recommend using Switch for this, since you have more than 2 possible outcomes - cleaner and easier to read in my opinion)
=Switch(
DateDiff(DateInterval.Day,Fields!ASSC_HIRE_DATE.Value,Now()) < 30, DateDiff(DateInterval.Day,Fields!ASSC_HIRE_DATE.Value,Now()) & " Days",
DateDiff(DateInterval.Day,Fields!ASSC_HIRE_DATE.Value,Now()) < 365, DateDiff(DateInterval.Day,Fields!ASSC_HIRE_DATE.Value,Now()) / 30 & " Months",
true, DateDiff(DateInterval.Day,Fields!ASSC_HIRE_DATE.Value,Now()) / 365 & " Years"
)
If you want to also make this round down (for example, someone with a hire date of 10/31/2016 would not display as having a tenure of 2 years until 10/31/2018, instead of allowing the cell formatting to round 1.5 years up to 2), you can wrap the entire Switch statement in a Floor function.
Related
I am trying to calculate the difference in days between 2 data sets and then if the value is between 1 and 2 days then it should show compliant if the value is a negative number or greater than 2 days I want it to show non-compliant. I am not sure what I have wrong, it runs however they all show compliant
Background on the calculation needed.
IMM Discharge compliance - Hospitals must
deliver a copy of the signed notice to each beneficiary not more than two (2) days before the day of discharge. Follow-up notice is not required if delivery of the initial IM falls within two (2)
calendar days of discharge.
FYI - the first IFF statement is because some do not have dates so that was to account for those
=IIF(
IsNothing(Lookup(Fields!Account_Number.Value,Fields!Account_Number.Value,Fields!Intervention_Date_Of_Service.Value, "Interventions")),
"No Intervention",
IIF(
DateDiff("d",Fields!Actual_Discharge_Date.Value,Lookup(Fields!Account_Number.Value,Fields!Account_Number.Value,Fields!Intervention_Date_Of_Service.Value, "Interventions")) <=2,
"Compliant",
"Non-compliant")
)
I have tried multiple variations =1 or 2, etc if I use just the =2 they all show non-compliant
You have two conditions for non-compliance - the difference being negative or greater than 2 days so you have to check both conditions. Try something like this:
=IIF(IsNothing(Lookup(Fields!Account_Number.Value, Fields!Account_Number.Value, Fields!Intervention_Date_Of_Service.Value, "Interventions")),
"No Intervention",
IIF(DateDiff(DateInterval.Day, Fields!Actual_Discharge_Date.Value, Lookup(Fields!Account_Number.Value, Fields!Account_Number.Value, Fields!Intervention_Date_Of_Service.Value, "Interventions")) < 1,
"Non-compliant",
IIF(DateDiff(DateInterval.Day, Fields!Actual_Discharge_Date.Value, Lookup(Fields!Account_Number.Value, Fields!Account_Number.Value, Fields!Intervention_Date_Of_Service.Value, "Interventions")) > 2,
"Non-compliant",
"Compliant"
)
)
)
I have a database of documents with a "last reviewed" date field, another field has a number which states how many months until this document expires (6, 12, 18, 24)
I would like to display all the document which expire each month based on the review dates X the months till expire.
for example
if 3 documents have a date of 28/03/2017 and a 6 month review i would like a box on a menu which states 3 documents need to be reviewed in September.
any help would be great, thanks in advance
First find the future month to check, then calculate the expire date for the documents, and finally compare these.
Then you can use DCount in an expression for your textbox counting the documents:
=DCount("*","YourTable","DateDiff('m',DateAdd('m'," & [YourMonthsForwardTextbox] & ", Date()),DateAdd('m',[Expires],[Last Reviewed]))=0")
I have a table named "Resolved Request", in which I have two specific columns: Date Assigned and Date Resolved. I have a third column named Time Spent Resolving, which should be the amount of time between Date Assigned and Date Resolved, in days hours and minutes. I am using MS Access 2013 but the file I am modifying has to be compatible with MS Access 2007.
I know how to calculate the difference between two dates (Date1-Date2 as calculated column), however I cannot get the format I want. I need for example to have Time Spent Resolving show up as "34 days, 2 hours and 5 minutes". How would I retrieve the amount of time between two dates in that kind of format within MS Access?
Also as an added bonus question, the "Date Assigned" field is a duplicate of a field I have in another table named "In Progress Request". Would I be able to calculate the difference between "Date Resolved" in "Resolved Request" and "Date Assigned" in "In Progress Request"? If not, could I somehow link both Date Assigned fields in both tables to update each other automatically?
Here is the formula that you need to display the format that you require (34 days, 2 hours and 5 minutes). I don't think you should have this formula for a calculated column as it's a waste of space in your table.
Replace txtInsertDateTime and txtAuditDateTime in the formula with the columns that you have.
=CStr((DateDiff("n",[txtInsertDateTime],[txtAuditDateTime])\60)\24) & " days," & CStr((DateDiff("n",[txtInsertDateTime],[txtAuditDateTime])/60) Mod 24) & " hours and " & CStr(DateDiff("n",[txtInsertDateTime],[txtAuditDateTime]) Mod 60) & " minutes"
Not sure about your bonus question, so cannot answer.
I am facing issue in calculating past 5 week data based on current date (excluding currrent week)
For e.g:
Suppose we are in 40th week of this year, I need to get the sum of all the transactions for the previous 5 weeks (39, 38, 37, 36 & 35).
Currently calculating based on the calendar day but as Calendar day is giving the granular level of data, inorder to increase the performance I need to use the calendar week (sample data like (2012/40).
Is there a way to do this?
I'd construct a flag field (either in the back-end or in the universe) using datediff (in SQL Server terms).
Failing that, you could construct a variable in Web Intelligence using the Week function.
Pseudo-code but something like:
=IF(Transaction_Date < Week(CurrentDate()) AND Transaction_Date >= (Week(CurrentDate())-5); "TRUE"; "FALSE")
Examples:
'DD/MM/YYYY
"1/1/2009" should give `1`
"31/1/2009" should give `5`
"1/2/2009" should also give `5`
Format("1/2/2009", "ww") returns 6.
So, how can I get the correct result?
It's doing two things here which don't match your expectations, I think:
Assuming you want the week with Jan 1 in as week 1, and using Sunday as first day of the week
So it has week 1 running from Sunday 28th December 2008 to Saturday 3rd Jan 2009.
Week 6 would begin on Sunday 1st Feb by this method.
The ISO standard is for week 1 to be the one containing 4 days of January, or the first Thursday of the year (different ways of expressing the same thing).
You can specify this method of calculation and the first day of the week:
Format(SomeDate,"ww",vbMonday,vbFirstFourDays)
see here for syntax:
https://support.office.com/en-US/article/Format-Function-6F29D87B-8761-408D-81D3-63B9CD842530
Regardless of the day of the week your week starts on, you need to pass unambiguous date values. "31/1/2009" can only be one date (Jan 31st), but "1/2/2009" could be Jan. 2 (US style) or Feb. 1st (everybody else who has more sense that we USAns).
In this case, I'd use DateSerial() to make sure the date is not misinterpreted:
Format(DateSerial(2009,2,1), "ww", vbMonday)
While this is not causing your problem, because Access helpfully utilizes your system's localized date settings, I think it's something you should do anyway. You certainly are forced to do so in SQL in Access, so I don't think it's a bad habit in code and expressions.
This might work: Format(YourDate, "ww",vbMonday)
"Correct result" depends on the locale. Maybe VBA will let you pick a calendar-system, otherwise you're pretty much out of luck.
Note that First-Day-On-xxDay isn't your only problem. There is also variation on what a complete week is so Week 1 in one system could be Week 53 of the previous year in another system.
So test thoroughly and don't be seduced to "correct by 1".
There is a whole standard for week numbers: ISO-8601
http://en.wikipedia.org/wiki/ISO_8601#Week_dates
I had the same problem.
It showed week 53 and week 1, yet days in week 53 and week 1 are all in week 1
I first tried changing the date format in the Access Query to this:
OrderWeek: Format([OrderDate],"yyyy-ww",1,3) <-- But it did not do the trick.
You get dates like 2014-52 for week 52 and 2015-52 where it was week 1 before.
Also the sorting was not how I liked. It sorted the data as 2014-1, 2014-11, 2014-2 etc. I want it to show as 2014-01, 2014-02 .. 2014-11 etc.
So here is the new code to display both the year and the week correctly in an Access Query:
ActualWeek: IIf(DatePart("ww",[SomeDate])=53,DatePart("yyyy",[SomeDate])+1,DatePart("yyyy",[SomeDate])) & "-" & IIf(DatePart("ww",[SomeDate])=53,"01",IIf(DatePart("ww",[SomeDate])<10,"0" & DatePart("ww",[SomeDate]),DatePart("ww",[SomeDate])))
This now shows any days from week 53 as being part of week 1
If sunday is the first day of the week (as it is in some locales) then 6 is the correct weeknumber for "1/2/2009" (february 1. 2009)
In terms of the sorting, I had the same issue and used this code to resolve it:
IIf(Format([SomeDate],"ww")<10,Format([SomeDate],"yyyy-") & "0" & Format([SomeDate],"ww"),Format([SomeDate],"yyyy-ww"))
If the week number is less than 10, add a zero, else leave it as is.
Now the sorting is fine. Hope this helps somebody.