SSRS Expression First Day of of First Week of Current Year - reporting-services

Everyone,
I have a question that has stumped me for a day and can't figure out. What I am looking for is a formula in SSRS Expression that will tell me what the date is for the first day of the first ISO week of the current year.
For Example:
2014 would yield: 12/30/2013. The reason for this would be that the first ISO week of the 2014 year is from (12/30/2013) - (01/05/2014).
2013 would yield: 12/31/2012
I would appreciate any help anyone?
Thanks,

You can use this function:
Public Function dtFirstDayOfISOYear(ByVal intYear As Integer) as Datetime
'the first week of a ISO year is the week that contains the first Thursday of the year (and, hence, 4 January)
Dim intDayOfWeek As Integer = CInt(New DateTime(intYear, 1, 4).DayOfWeek)
'ISO weeks start with Monday
If intDayOfWeek < DayOfWeek.Monday Then intDayOfWeek = intDayOfWeek + 7
Return DateAdd(DateInterval.Day, -intDayOfWeek + 1, New DateTime(intYear, 1, 4))
End Function
And call it using an Expression like this:
=Code.dtFirstDayOfISOYear(2014)
You can also use a standalone Expression like this:
=DateAdd("d", (-1) * (CInt(New DateTime(2014, 1, 4).DayOfWeek) + IIf(CInt(New DateTime(2014, 1, 4).DayOfWeek) < DayOfWeek.Monday, 7, 0)) + 1, New DateTime(2014, 1, 4))

Related

How to get the every week by google spread sheet

I want to have date of every weeks.
I put 07/02/2022 in F5 cell and put =EDATE(F5 + 7) in G5 cell
But N/A comes in G5
I guess maybe I put 07/02/2022 in F5 in a wrong way..
What should I do?
read the error message:
Wrong number of arguments to EDATE. Expected 2 arguments, but got 1 arguments.
correct G5 to =EDATE(F5,7)
In case you refer to tomorrow's date, that is 2/7/2022.
You are, if you read the help, adding 7 months with the command above.
The date of the next week starting on any day
function nextWeekDateStartingOnThis(day = 1) {//Sun(0) - Sat(6)
let dt = new Date();
let nd = new Date(dt.getFullYear(), dt.getMonth(), dt.getDate() + 7 - dt.getDay() + day);
Logger.log(nd);
return nd;//this returns the date of the next monday because day = 1
}
If you want the date of the next tuesday then make day = 2

SSRS tweak to time expression

I am currently using this expression to show the previous working day in an SSRS report:
=DateAdd("d"
, Switch(DatePart("w", Today) = 2, -3
,DatePart("w", Today) = 1, -2
,True, -1)
, Today)
which works fine.
However I would like the output to be,if I ran the query today for example,:
24/04/2020 23:59:59
Instead of the current 24/04/2020
Please can you advise on how I could add hours, minutes and seconds- 23:59:59 - to the above expression?
Thank you
There may be a more elegant way of doing this but I based this on you current expression. I've text it and it seems to work OK.
=DateAdd("s"
, Switch(
DatePart("w", Today) = 2, (-3 * 86400) -1,
DatePart("w", Today) = 1, (-2 * 86400) -1,
True, -85401
)
, Today)
This simply does a datediff in seconds rather than days and then adjusts the amount of seconds to remove by 1

Output First of Month Following 60 Days From a Stored Date in an Access qry

I have a date field in access where I have to calculate the first of the month following 60 days from that date. For example my date is 12/12/2017. I need to output 3/1/2018, First of the month following 60 days from 12/12/2017. I know how to use the DateAdd function to get my 60 days, but am confused as to how to output the first of the month following those 60 days.
Thanks,
Mark
There are multiple ways to do this. One way using DATEADD():
SELECT DATEADD("d", -"d"(DATEADD(MONTH, 1, DATEADD("d", 60, '2017/12/12')))+1,
DATEADD("m", 1, DATEADD("d", 60, '2017/12/12')))
If you have Access 2013+ you can use EOMONTH()
SELECT DATEADD("d", 1, EOMONTH(DATEADD("d", 60, '2017/12/12')))
Use this expression:
DateThreeFirst: DateAdd("m", 3, DateSerial(Year([YourDateField]), Month([YourDateField]), 1))
To first deduct one day:
DateThreeFirst: DateAdd("m", 3, DateSerial(Year(DateAdd("d", -1, [YourDateField])), Month(DateAdd("d", -1, [YourDateField])), 1))
To use days' count and not months:
DateThreeFirst: DateSerial(Year(DateAdd("d", 60, [YourDateField])), Month(DateAdd("d", 60, [YourDateField])) + 1, 1)

Datediff in MsAccess

I am stuck in one place.
I am using DateDiff in Ms Access it is giving me proper output, like
StartDate is 10-Sep-2016
EndDate is 15-Oct-2016
Total Days which I will get is 35
& months will i get is 1 Month
DateDiff('d',StartDate,EndDate)
**But I want output as 2 months if it is exeeded the 30 days.
if it is 61 days then 3 months & so on.
**IIFFF days diffrence is
29 Days then output should be 1 months
30 Days then output should be 1 months
32 Days then output should be 2 months
60 Days then output should be 2 months
62 Days then output should be 3 months**
Will that be possible in the DateDiff in MsAccess
or is there any other function available so that i can achieve the same output.**
You can do this using conditional logic. Perhaps something like this:
select iif(DateDiff('d', StartDate, EndDate) > 30,
DateDiff('d',StartDate,EndDate) & " days",
"2 months"
)
Your logic that anything exceeding 30 days is "2 months" seems strange. Normally, I think the logic would look like this:
select iif(DateDiff('d', StartDate, EndDate) > 30,
DateDiff('d', StartDate, EndDate) & " days",
DateDiff('m', StartDate, EndDate) & " months"
)
will this logic suffice to modify your SQL function?
Public Function FN_GET_MONTH(iDays As Long, Optional iDaysInMonth As Long = 30)
If (iDays / iDaysInMonth) > iDays \ iDaysInMonth Then
FN_GET_MONTH = (iDays \ iDaysInMonth) + 1
Else
FN_GET_MONTH = (iDays \ iDaysInMonth)
End If
End Function
?FN_GET_MONTH(29) = 1
?FN_GET_MONTH(31) = 2
?FN_GET_MONTH(60) = 2
?FN_GET_MONTH(80) = 3
?FN_GET_MONTH(91) = 4
you can have this public function and use it in your SQL code like
FN_GET_MONTH(DateDiff("d", StartDate, EndDate))
This query seems to give the results you seek:
SELECT
StartDate,
EndDate
numDays,
((numDays - 1) \ 30) + 1 AS numMonths
FROM
(
SELECT
StartDate,
EndDate,
DateDiff("d", StartDate, EndDate) AS numDays
FROM YourTable
)
It gives me
numDays numMonths
------- ---------
...
29 1
30 1
31 2
32 2
...
59 2
60 2
61 3
62 3
...
It seems like your minimum count of months for a positive count of days is 1, thus:
MonthCount = Sgn(DateDiff("d",StartDate,EndDate)) + DateDiff("m",StartDate,EndDate)
Edit
For a 30-day cut that will produce your example output, use this simple formula in your query:
MonthCount: (DateDiff("d",[StartDate],[EndDate])-1)\30+1

Linq to SQL - Start Of month

I have an sql statement which returns me the 1st day of the month:
DATEADD(month, DATEDIFF(month, 0, #input), 0)
How would I recreate this call using SQLFunctions.DateAdd and SQLFunctions.DateDiff?
Thanks in advance
If you are really using LINQ-to-SQL:
var zero = new DateTime(2000, 1, 1);
var resxxxx = (from p in ctx.Addresses
select new {
month = zero.AddMonths(SqlMethods.DateDiffMonth(zero, p.Date))
}
).FirstOrDefault();
The important part is month = zero.AddMonths(SqlMethods.DateDiffMonth(zero, p.Date)).
Note that I've used the zero = new DateTime(2000, 1, 1);, but any 1st of the month would be ok (1st jan 1980, 1st jul 1983, 1st dec 2100...). It's the two 0s in your SQL command. p.Date is the date of which you want to calculate the first of the month (so #input).