Comparing Dates in If Statement - date

I am trying to compare dates in an expression. If the Closed_Date matches today's date (I am using Today()), then it would output 1 in the box, otherwise output 0. So far I have this but it doesn't seem to work:
=IIF(Fields!Closed_Date = Mid(Today(),1,9), "1", "0")
The reason I am using Mid is to just get the month, day, and year. I don't want the time included. Is there a way you can compare dates using this or another method?

Today() actually returns today's date at midnight, so to compare your date to today you'll need to strip the time from Closed_Date instead. I'd recommend the DateValue function, since it returns date information with time set to midnight which makes for an easy comparison:
=IIF(DateValue(Fields!Closed_Date.Value) = Today(), "1", "0")

Try something like this:
=IIF(
Format(CDate(Fields!Closed_Date), "MM/dd/yyyy") = Today()
, "1", "0"
)
OR
=IIF(
FormatDateTime(Fields!Closed_Date, DateFormat.ShortDate) = Today()
, "1", "0"
)
Avoid using string functions like Mid with the dates. There are lot of date related functions available in SSRS.

Related

Tableau: How do I convert date to month-week #?

I have a date column. For example, the date is 8/2/2022, I'm hoping to see that this will convert to Aug Week 1; and if let's say the date is 7/15/2022, then the result should say July Week 3.
Please help, thanks!
You need to use LOD to do so, as default, you can only do week numbers by year in tableau, you need to create two calculated field to do so:
1.Create a [Min-Week] calculation field as shown below:
{ FIXED MONTH([Date]):MIN(DATEPART('week',[Date]))}
Note the calculation above for [Min-Week] assumes that there is at least one record for the first week of each month. If your data does not allow you to rely on that assumption, you could instead use DATEPART('week', DATETRUNC('month', [Date]))
2.Create a [Week-By-Mth] calculation field as shown below :
CASE DATEPART('week',[Date]) WHEN [Min-Week] THEN "Week 1" WHEN [Min-Week]+1 THEN "Week 2" WHEN [Min-Week]+2 THEN "Week 3" WHEN [Min-Week]+3 THEN "Week 4" WHEN [Min-Week]+4 THEN "Week 5" WHEN [Min-Week]+5 THEN "Week 6" END
EDIT good answer - here's just a shorter equivalent expression for [Week-By-Mth]
"Week " + STR(DATEPART('week, [Date]) - [Min-Week] + 1)
If you prefer an integer to a string, you could simplify further by getting rid of the Week prefix and the STR() call.
The [Week-By-Mth] calculation field should be what you need.

How do you find the first date of the week in SAS from a date?

I have a variable in a SAS dataset that has a number of dates (e.g. 01APR21). What I'm looking to do is create a new variable that shows the date of the first Monday of that week. So using the above example of 01APR21, the output would be 29/03/2021 as that what was when the Monday in that week was. I'm assuming it's using intnx, but I can't get my head around it.
data test;
format date date8.;
format first_day date10.;
date = '01APR21'd;
first_day = ?;
run;
INTNX Parameters:
Interval : WEEK
Increment: 0 (same week)
Alignment: Beginning
(Sunday)
Then add 1 to get to Monday instead of Sunday. You could probably play with the SHIFT INDEX parameter as well.
Monday = intnx('week', dateVariable, 0, 'B') + 1

Calculate a Reference Date in DAX

Trying to nail down the syntax for a pretty straightforward problem.
I have a table called Events and a full-feature DATES table with a relationship between the Dates[Date] field.
Using the event name as a slicer, I trying to create a [First Monday] measure that will return the date of the first Monday of the month.
So for example, if my event date was 2/14/19, [First Monday] would return 2/4/19.
Your Date table needs to contain 2 columns:
Year-Month: for example, "2018-01"
Weekday Number: for example, 1 (for Monday); or Weekday Name (i.e, "Monday")
Then:
First Monday =
CALCULATE( MIN('Date'[Date]),
ALL('Date'),
VALUES('Date'[Year-Month]),
'Date'[Weekday Name] = "Monday")
How it works:
First, we need to access all dates in the Date table, so we use ALL()
Second, we need to see only dates for the current context year and month, for which we can use VALUES()
Third, for each month we only want Mondays, hence Date[Weekday] = "Monday"
All this unfiltering/filtering generates a set of Mondays for the Year-Month visible in the current filter context. All we need to do now is to find the earliest of the Mondays using Min(Date).

DateDiff not available on Access 2013

I am trying to use the so called DateDiff function to subtract the End Date from the Start Date and obtain the numbers of days apart.For example:
10/11/1995 - 7/11/1995 = 3 (extract the 'dd' from DD/MM/YYYY format)
As date values are double with the integer part counting for a day, you can use this simple expression:
[Due Date]-[Start Date]
or, for integer days only:
Fix([Due Date]-[Start Date])
That said, you should a query for tasks like this.

How to convert relative dates like "Today" and "Yesterday" to an XQuery date object?

XQuery has a set of useful functions for date conversion. But how to convert relative dates like "Today" and "Yesterday" into the actual date?
For example "Today, 17:33" should be converted to "2012-05-30", and "Yesterday, 22:13" to "2012-05-29".
1. Parse the Date string
Parse the date string. I provided a small function which splits the word indicating the date off and parses it. I added some more convenient names for dates, but you can easily add more if neccessary, notice I convert to lower-case! It uses XQuery date and time functions for calculating the matching date.
declare function local:from-relative-date($string as xs:string) as xs:date {
switch (lower-case(substring-before($string, ",")))
case "today" return current-date()
case "yesterday" return current-date() - xs:dayTimeDuration('P1D')
case "day before yesterday" return current-date() - 2 * xs:dayTimeDuration('P1D')
case "tomorrow" return current-date() + xs:dayTimeDuration('P1D')
case "day after tomorrow" return current-date() + 2 * xs:dayTimeDuration('P1D')
default return error(xs:QName("XPTY0004"), "Unknown Date")
};
2. Format the date
Now use the XQuery 3.0 function format-date(...) (I hope your XQuery engine supports it, BaseX which I used for this example does) to format the date string like you need it:
format-date(local:from-relative-date("Yesterday, 22:13"), "[Y]-[M00]-[D00]")
If it doesn't, you will have to use year-from-date(...) and according functions for month and day, use some number formatting and concatenate yourself.