I have an end-of-shift report I'm trying to automate. I am trying to create a preset start and end date parameter for shift schedules. StartDate: (AM Shift = Today # 06:00) & (PM Shift = Yesterday # 18:00) EndDate: (AM Shift = Today # 18:00) & (PM Shift = Today # 06:00). Since there are two shifts, I don't want to hardcode my date/time range into my query. And since the shifts are unchanging, I don't want to just set the calendar and give the user options to change the date/time range.
I changed the parameter type to date/time and am attempting to fix the expression for available values to these predetermined date/times. Because I do not want the user to change the date/time range and only select the specific shift schedule, I am having difficulty getting the expression to work with both date and time separately. All of the below do not work, but are what I've tried. Examples specifically for Yesterday at 18:00.Any assistance would be greatly appreciated. Thank you in advance.
=CDate(DateAdd("d",-1,Today()) + "18:00:00.000")
=DateAdd("d",-1,Today()) + "18:00:00.000"
=DateAdd("d",-1,Today()) & "18:00:00.000"
=Format((DateAdd("d",-1,Today())),"MM/dd/yyyy 18:00:00.000")
You cannot use CDate like this, it's easier to start with a date and then add or subtract from it.
for example
=DateAdd("H", 18, DateAdd("D", -1, Today()))
The second DateAdd gives us Yesterday and because we use Today() rather than Now() it gives us yesterday at time 00:00:000, now all we have to do is add 18 hours to it and we get yesterday at 6pm
If you want to base this on a parameter (called shift in this example) then you can do something like this
Start -
=IIF(Parameters!shift.Value = "AM" ,
DateAdd("H", 6, Today()),
DateAdd("H", 18, DateAdd("D", -1, Today()))
)
End -
=IIF(Parameters!shift.Value = "AM",
DateAdd("H", 18, Today),
DateAdd("H", 6, Today)
)
Related
I have a parameter of type date/time. I am creating a default value as expression. When I put
=IIF(Hour(TODAY()) < 1,
DateAdd("d", -1, FormatDateTime(Today(),DateFormat.ShortDate)),
FormatDateTime(Today(),DateFormat.ShortDate))
it works well and return a value (although always the same, since the hour is always 0 (12AM))
But I want to base this default value on the CURRENT date time attime of report. So when I replace today() with NOW(), it errors out and says that it "doesn't have expected type".
=IIf(Hour(now())<1,
DateAdd("d",-1, FormatDateTime(Today(),DateFormat.ShortDate)),
FormatDateTime(Today(),DateFormat.ShortDate))
Thanks
I tried multiple formulas to convert now() into something, but nothing worked.
It seems as though the expression didn;t evaluate the second part of the IIF because the HOUR(Today) was always 0. The Format would convert the date into a string which would cause the error. Evidently, the DateAdd was converting the string back into a date in order to subtract the day.
I think you can do without all the converting.
=IIF(Hour(NOW()) < 1,
DateAdd("d", -1, Today()),
Today())
I am new to report building with SSRS.
I am currently trying to have an expression in SSRS show a specific date depending on wether today's day is on a weekend or not. This I tried to achieve by utilizing the different date functions Microsoft provides in their documentation. This is what I got so far:
=IIF(WeekdayName(weekday(DateAdd(DateInterval.Day, -1, today()))) = 'Monday', CDate(DateAdd("d", -3, Parameters!DateTo.Value)),
IIF(WeekdayName(weekday(DateAdd(DateInterval.Day, -1, today()))) = 'Sunday', CDate(DateAdd("d", -2, Parameters!DateTo.Value)),
CDate(DateAdd("d", -1, Parameters!DateTo.Value))))
As you can see I am trying to have it so if today's day is Sunday or Monday the date value returned should be the one from last Friday else it should return yesterday's date.
In Report Builder I'm receiving an error message stating that there is an expression expected and I cannot save the file.
What am I missing here? Is there maybe a different approach to achieving this? Thanks for any help.
BR,
Philipp
I would do it like this... There may be more efficient ways but this would be my approach...
=IIF(
Weekday(Today())=7 OR Weekday(Today())=1
,DATEADD(DateInterval.Day, (Weekday(Today())+1) * -1, Today())
, Today()
)
Note This assumes you week starts on a Sunday, if not adjust the first part of the IIF to suit.
So here we are saying...
If today is Saturday (7) or today is Sunday(1) then, subtract the (current day number +1) from the current date, else return the current date.
I have the following expression in a MS-access table:
IIf([End Date/Time]>="12/8/2016 6:00:00",1,0)
12/08/2016 18:15:00 will return a '1', however
12/08/2016 14:23:29 returns a '0'
I'm assuming this is an issue with AM/PM. I tried putting '6:00:00 AM' in my expression but no change.
Also I would like to replace '12/8/2016' with 'yesterday' but date()-1 doesn't seem to be working.
EDIT: I figured out that the time needs to be '06:00:00'. That yield the correct dates. Still don't know how to get this automatically (ie yesterday at 06:00)
Thanks
Your issue is that you threat dates as strings. Use date always, no exceptions.
Further, if your field is not a date value, you must convert it.
Thus, this will work:
IIf(DateValue([End Date/Time]) >= #2016/12/8 6:00:00#, 1, 0)
and this:
IIf(DateValue([End Date/Time]) >= Date() - 1, 1, 0)
and this:
IIf(DateValue([End Date/Time]) >= DateAdd("d", -1, #2016/12/8 6:00:00#), 1, 0)
2 things. First, I believe you need to convert your string to a datetime. I think think you're getting wonky results because it's trying to compare them as a different format. Like a strings or numbers. To format as a date time you use a format fucntion:
Format("12/8/2016 6:00:00AM", "mm/dd/yyyy hh:nn:ss am/pm")
Second, to add a date you need the DateAdd function.
DATEADD('d',-1,"12/8/2016 6:00:00AM")
'd' defines the -1 as a 'day' being added.
So, putting it all together:
DATEADD("d",-1,Format("12/8/2016 6:00:00AM", "mm/dd/yyyy hh:nn:ss am/pm"))
And finally, if you want the want the rolling yesterday, and not perpetually 12/7/2016 (since you would just use that date if it was the case), you need to get today's date with this function:
Date()
So, throwing this into our mix we get:
DATEADD("d",-1,Format(DATE() & " 6:00:00AM", "mm/dd/yyyy hh:nn:ss am/pm"))
I have a report with begin date and end date parameters and is specifically meant for subscription that should serve 2 types of subscriptions:
1, One for previous fiscal week -->Begin date is first day of previous fiscal week and end date is last day of previous fiscal week.
2, One for previous day -->Begin date is previous day and end date is also previous day.
Dataset Datefields:
This is the dataset query results for both date parameters available and default values.
When I create subscription for previous day it only runs that day...after midnight that day, begin date and end date parameter values are blank and the subscription fails with a status message "The subscription contains parameter values that are not valid" values.
Subscription created for previous fiscal week is good for a week until the values previous fiscal week start and end dates change.
You are using a dataset for defaults and values for date parameters, which probably isn’t the best approach.
The way I have handled reports that I want to allow end users to subscribe to, and have the default date values vary depending on whether subscription was a daily, weekly, or monthly one, is to have an extra parameter that makes this possible.
The parameter I add to the report is one I call Period (or Report Period), and it has to be the first parameter in the report, or at least listed before the date parameters. The only values in the dropdown for this parameter are Daily, Weekly, and Monthly (if applicable). These can be whatever you need. Enter these manually as value options in the parameter in the report, since they are not expected to change very often. Based on what the end user chooses for this parameter when creating a subscription, the default date values change. This is done via an expression in the default value for the date parameters that evaluates the value chosen from the Period dropdown.
So, if the end user wants a daily subscription, they choose Daily from the Period parameter dropdown, and the default values for the start and end date parameters change to only include the prior day. If they choose Weekly, the start and end date parameters change to only include the prior week, and so on.
Here is an example for the start date parameter default value expression.
=Switch(Parameters!Period.Value = "Daily" , DateAdd(DateInterval.Day, -1, Today),
Parameters!Period.Value = "Weekly" , DateAdd(DateInterval.WeekOfYear, -1, DateAdd(DateInterval.Day, -(DatePart(DateInterval.Weekday, Today, 0, 0)-1), Today)) ,
Parameters!Period.Value = "Monthly" , DateAdd(DateInterval.Month, -1, DateAdd(DateInterval.Day, -(DatePart(DateInterval.Day, Today, 0, 0)-1), Today)))
For the end date parameter…
=Switch(Parameters!Period.Value = "Daily" , Today,
Parameters!Period.Value = "Weekly" , DateAdd(DateInterval.Day, -(DatePart(DateInterval.Weekday, Today, 0, 0)), Today) ,
Parameters!Period.Value = "Monthly" , DateAdd(DateInterval.Day, -(DatePart(DateInterval.Day, Today, 0, 0)), Today))
Warning!! Changing the Period value either in the report designer (preview), or online, will not cause the date values to automatically change right before your eyes. It will while creating (and thus executing) a subscription, however. I have never looked into why this is. I have other, less painful, things to fill my time.
Give this a try.
I have a form in access when has two textboxes.
The first called txtDateBox which allows the user to select a date.
The second which is called DayBox which displays the days name.
I have a control source in DayBox to get the name of the day.
=WeekdayName(Weekday([txtDateBox])-1)
This works fine until I select a sunday date.
It will then display a #Func! message in the textbox and will not allow me to compile my automatic reports.
The problem is because WeekdayName(0) triggers error #5, "Invalid procedure call or argument."
Avoid the error by using DateAdd to subtract one day from txtDateBox before you give it to Weekday. Here is an Immediate window session to show you what I mean:
txtDateBox = #2014-1-5#
? Format(txtDateBox, "ddd, mmm d, yyyy")
Sun, Jan 5, 2014
? DateAdd("d", -1, txtDateBox)
1/4/2014
? Weekday(DateAdd("d", -1, txtDateBox))
7
? WeekdayName(Weekday(DateAdd("d", -1, txtDateBox)))
Saturday