Conditional formatting a date range - formatting

I am trying to conditional format in SSRS.
in my report I have a planned start field and an actual start field.
I want the actual start field to change to red if the actual start date is more than an hour or less than an hour of the planned start date.
So if the planned start date is 25/07/2021 21:00 but the actual start date is 25/07/2021 22:00 then I want the actual start date to turn red.
Also if the planned start date is 25/07/2021 21:00 but the actual start date is 25/07/2021 20:00 then I want the actual start date to turn red.
can this be done?
This is what I have tried so far
=iif(DateDiff("H", Fields!ACTUAL_START_DATE.Value, Fields!PLANNED_START_DATE.Value) >= 1, "red", "black")
any help would be much appreciated

There are a variety of ways to handle this. Your method is fine, just need to add ABS() to your expression and it should work.
=iif(ABS(DateDiff("H", Parameters!actualstart.Value, Parameters!plannedstart.Value)) >= 1, "red", "black")

Related

SQL ORACLE Get week numbers from multiple datetime rows

I have 70.000 rows of data, including a date time column (YYYY-MM-DD HH24-MM-SS.).
I want to split this data into 3 separate columns; Hour, day and Week number.
The date time column name is 'REGISTRATIONDATE' from the table 'CONTRACTS'.
This is what I have so far for the day and hour columns:
SELECT substr(REGISTRATIONDATE, 0, 10) AS "Date",
substr(REGISTRATIONDATE, 11, 9) AS "Hour"
FROM CONTRACTS;
I have seen the options to get a week number for specific dates, this assignment concerns 70.000 dates so this is not an option.
You (the OP) still have to explain what week number to assign to the first few days in a year, until the first Monday of the year. Do you assign a week number for the prior calendar year? In a Comment I asked about January 1, 2017, as an example; that was a Sunday. The week from January 2 to January 8 of 2017 is "week 1" according to your definition; what week number do you assign to Sunday, January 1, 2017?
The straightforward calculation below assigns to it week number 0. Other than that, the computation is trivial.
Notes: To find the Monday of the week for any given date dt, we can use trunc(dt, 'iw'). iw stands for ISO Week, standard week which starts on Monday and ends on Sunday.
Then: To find the first Monday of the year, we can start with the date January 7 and ask for the Monday of the week in which January 7 falls. (I won't explain that one - it's easy logic and it has nothing to do with programming.)
To input a fixed date, the best way is with the date literal syntax: date '2017-01-07' for January 7. Please check the Oracle documentation for "date literals" if you are not familiar with it.
So: to find the week number for any date dt, compute
1 + ( trunc(dt, 'iw') - trunc(date '2017-01-07', 'iw') ) / 7
This formula finds the Monday of the ISO Week of dt and subtracts the first Monday of the year - using Oracle date arithmetic, where the difference between two dates is the number of days between them. So to find the number of weeks we divide by 7; and to have the first Monday be assigned the number 1, instead of 0, we need to add 1 to the result of dividing by 7.
The other issue you will have to address is to convert your strings into dates. The best solution would be to fix the data model itself (change the data type of the column so that it is DATE instead of VARCHAR2); then all the bits of data you need could be extracted more easily, you would make sure you don't have dates like '2017-02-29 12:30:00' in your data (currently, if you do, you will have a very hard time making any date calculations work), queries will be a lot faster, etc. Anyway, that's an entirely different issue so I'll leave it out of this discussion.
Assuming your REGISTRATIONDATE if formatted as 'MM/DD/YYYY'
the simples (and the faster ) query is based ond to to_char(to_date(REGISTRATIONDATE,'MM/DD/YYYY'),'WW')
(otherwise convert you column in a proper date and perform the conversio to week number)
SELECT substr(REGISTRATIONDATE, 0, 10) AS "Date",
substr(REGISTRATIONDATE, 11, 9) AS "Hour",
to_char(to_date(REGISTRATIONDATE,'MM/DD/YYYY'),'WW') as "Week"
FROM CONTRACTS;
This is messy, but it looks like it works:
to_char(
to_date(RegistrationDate,'YYYY-MM-DD HH24-MI-SS') +
to_number(to_char(trunc(to_date(RegistrationDate,'YYYY-MM-DD HH24-MI-SS'),'YEAR'),'D'))
- 2,
'WW')
On the outside you have the solution previous given by others but using the correct date format. In the middle there is an adjustment of a certain number of days to adjust for where the 1st Jan falls. The trunc part gets the first of Jan from the date, the 'D' gets the weekday of 1st Jan. Since 1 represents Sunday, we have to use -2 to get what we need.
EDIT: I may delete this answer later, but it looks to me that the one from #mathguy is the best. See also the comments on that answer for how to extend to a general solution.
But first you need to:
Decide what to do dates in Jan before the first Monday, and
Resolve the underlying problems in the date which prevent it being converted to dates.
On point 1, if assigning week 0 is not acceptable (you want week 52/53) it gets a bit more complicated, but we'll still be able to help.
As I see it, on point 2, either there is something systematically wrong (perhaps they are timestamps and include fractions of a second) or there are isolated cases of invalid data.
Either the length, or the format, or the specific values don't compute. The error message you got suggests that at least some of the data is "too long", and the code in my comment should help you locate that.

Excel: Insert blank row if non consecutive date

We have a process at work that runs every 31 hours and would like to have a column in an Excel 2010 spreadsheet that reflects the time and dates the process is due to run each day over the coming year e.g.
start date (Cell:A1):
10/31/2016 1:00
11/1/2016 8:00
11/2/2016 15:00
11/3/2016 22:00
11/5/2016 5:00
11/6/2016 12:00
11/7/2016 19:00
11/9/2016 2:00
11/10/2016 9:00
11/11/2016 16:00
11/12/2016 23:00
11/14/2016 6:00
I am currently using the formula: A2 =A1 + (31/24) to populate the dates and times in the column which appears to work ok. I need to insert a blank row between days that are not consecutive (in order to highlight that fact, to make it easier for the operators to read and not instigate the process at the wrong time!) which is where the difficulty lies. I am assuming that I will need a separate VBA function to step through each cell and compare the day date (ignoring time value.) in the previous cell, if day date is greater than 1 (as in not the following day.) insert a blank row.
I am looking for a solution to the following scenario but am struggling to get my head round it and would appreciate any guidance/ help anyone is able to provide.
Many thanks
You do not need a VBA Function for that, it can be accomplished with the function
A3 = IF(A2="",A1+31/24, IF(INT(A2+31/24)=INT(A2)+1,A2+31/24,""))
*For that to work, you will have to populate cells A1 and A2 manually, though.
You can use the INT() function to get the date part of a DateTime field, so it becomes simple to check if adding 31 hours will leave a 1 day gap in the sequence by checking INT(A2+31/24) = INT(A2)+1.
So, to explain the function, the part
IF(INT(A2+31/24)=INT(A2)+1,A2+31/24,"")
will check if the days are consecutive, and if so, it will fill the DateTime. If not, it will leave it blank.
The outer IF checks if the cell above is blank. If it is, the function will use the one above that.

VB - Fill cell background based on date comparing today to cell value

I have a column that returns dates in this form:
"2016-06-01 23:29:34.283"
I am wondering how I can fill the cell background green if the day matches today, and red if its not today (hour and minute doesn't matter).
I tried this but no luck:
=Switch(Fields!Last_Upload.value = Today(), "Green", Fields!Last_Upload.value != Today(), "Red").
Edit: This is using VS Data Tools
Of course the problem is comparing the date without the timestamp. You can use the DateValue function for this like so:
=IIf(DateValue(Fields!Last_Upload.value) = Today, "Green", "Red")

Check if the current date string is within or outside the range of two other dates

I have 2 date pickers. One is for selecting a start date and the other is for selecting an expiry date. When a selection has been made I convert the date to string format and store the results in a two text fields.
This is how I save to the database:
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "MMMM d" // e.g. September 15
let sDate = dateFormatter.dateFromString(startDateField.text) as NSDate!
let eDate = dateFormatter.dateFromString(expiryDateField.text) as NSDate!
activity["startDate"] = sDate
activity["expiryDate"] = eDate
activity.saveInBackgroundWithBlock({ (success: Bool, error: NSError!) -> Void in
What I intend to do is have a table view display 2 sections: one showing activities that are active, and the other showing activities that are expired/not in date range.
I figured I could do this by taking the current date and checking it was within the start date and expiry date range.
My saved date from above is showing up in parse like this:
The thing is, I don't want to take the year or time into account. The only thing that matters is the month and day. So for example an activity could run once a year between September 15th and December 20th.
I need a way to check that the current date is within those two dates taking the day (e.g. 15th) of the month into account. So today's date is October 19 and an activity would be in range if its start date was September 15 and its expiry date December 20, but would be out of range/operation if the expiry date was October 18 or start date was October 25.
So to summarise:
I will be displaying two sections in a table view. One with active activities and another with non-active out of operation activities.
How do take my stored start date and expiry date and easily check the current date is within their range?
For the occasions when users pick either a start date or expiry date I need to check if the current date is greater/less than the start date (to be able to decide if the activity should show up or not) or if the current date is greater/less than the expiry date (to be able to decide if the activity has passed or not passed its expiry date.
It would be nice if I could roll 1 and 2 into one function and use it in the table view.
Would be interested to learn how the more experienced programmer would achieve this.
Instead of the date format "MMMM d" (e.g. "September 15"), you can convert the dates
to a string with the date format "MMdd" (e.g. "0915"). Then you can do a simple
string comparison between the current date, the start date and the expiry date.
For example,
"0915" < "1019" < "1220"
so October 19 is in the range from September 15 to December 20.

Format date in Values for a Range (Gant) Chart SSRS 2008 R2

I have a date column in which the dates are re-curring every year. I'm only interested in the month really, but I can't just put the month number as the high and low value for the gant chart. Is there any way I can format the column? Any suggestions would be awesome.
The gant chart currently is taking the whole date and is working fine, but like I said it goes out to the next year.
Figured it out. So the DATECOLUMN I had was formated as such:
Example: 2013-01-01 00:00:0.000
In order to put in the GANT(RANGE) CHART in the series property, under values, I formated the Top Value and Low Value as =MONTH(DATECOLUMN) using the expression option.
Then for the Horizontal Axis Properties I set the Interval to 1 and Interval Type to Number. And There you have it Monthly time spans with only the Months 1 - 12 showing.
I must also note that I only had one time recorded. So I just did Top Value as: DATEADD(MM,1,DATECOLUMN) In Order to get the right time span in my dataset Script.
Which date format would u like to display in your chart.
If (month with year) like 'Sep 2013'
use this date format for the same.
SUBSTRING(CONVERT(VARCHAR(11),getutcdate(), 113),4,8)