I hope someone can share their experience with me. I've used the following ssrs expression to default a SSRS report parameter to the last day of the current month for months without issue:
=DateAdd(ādā, -1, DateSerial(Year(Now()), Month(Now()), 1))
This morning instead of returnng 3/31/2016, the expression is stuck on 3/29/2016. Can anyone help me understand why this happened?
You are substracting one day to the first day of the current month.
You have to get the first day of the next month and substract one day.
=DateAdd("d", -1, DateSerial(2016, Month(Now.AddMonths(1)), 1))
It returns 3/31/2016
Let me know if this helps.
The expression you show will give the last day of the previous month, not the last day of this month.
You want to do date arithmetic rather than build dates from scratch. Think about what will happen in December if you are using the current year but adding a month to find the month you need.
The safest way to get the last day of the current month would be to calculate the first day of this month, add one month then subtract a day:
=DateAdd(DateInterval.Day, -1, DateAdd(DateInterval.Month, 1, DateAdd(DateInterval.Day, 1-Day(Today), Today)))
If what you actually want is the last day of the previous month, then that is simple:
=DateAdd(DateInterval.Day, 0-Day(Today), Today)
Related
I have an earnings table in my Google Data Studio report from January 21 - YTD.
But what i want is a table that shows me YTM (so this year until last month) Jan-21 til Sep-21 and that dynamicly on 1st Nov, Okt-21 is added to the table, and on 1st dec Nov-21
is added etc.
Because the Total YTD value is other wise too high, due to cost being booked at the end of the month and earnings through the month.
Filters wont let you do it dynamicly,
date range filters same,
always possible to do it manualy with date range but this i cannot ask from my client.
any suggestions??
Kind regards
I found the answer,
i left the question for when someone else wants to do it,
it is actualy simpel fixed by using advanced date filter and then leaving the start date on 1-jan, and end date to today minus 1 month.
this gave me the exact result i needed yt last month
I'm having a Report where I need to display the dates of last 7 days.
As shown below
Sun, Mon etc... are hard coded and the Dates are written in Expression
For example,
If today is Wednesday I need to show the Dates till last Tuesday.
If it is Thursday I need to show the Dates till last Wednesday.
How to retrieve the dates information and display below the corresponding days.
To get last seven days date you can do something like below in each expression,
To get Days in Header
=WeekdayName(weekday(Parameters!TodaysDate.Value)) --Tuesday
=WeekdayName(weekday(DateAdd("d",1,Parameters!TodaysDate.Value))) --Wedneday
Same for others too... Just by increment/decrement by 1.
To get Days Date in Data
=Format(Parameters!TodaysDate.Value,"dd-MMM-yyyy")
=Format(DateAdd("d", 1, Parameters!TodaysDate.Value),"dd-MMM-yyyy")
Same for others too... Just by increment/decrement by 1.
Here TodaysDate would be parameter date or Now() Date.
I have given demo of incremental one, you can change it as per your condition. I think you need to use decrement here. So Instead of 1 you need to use -1.
This will give you output like below,
I have a stored procedure that runs to pull data for sales in a given month.
It does not return 31 days on months that have 31 days.
I need some help understanding the breakdown of the following string
(dateadd(dd,-(datepart(dd,getdate())),CONVERT(CHAR(8),GetDate(),112)))+'23:59:59')
I understand that CONVERT(CHAR(8),GetDate(),112) is taking the system date and converting to YYYYMMDD and that datepart(dd,getdate()) takes the system date and takes just the day part, but I cannot decipher the entire string.
There is a issue with the expression you have given. The bracket after 23:59:59 does not have a opening brace.
However what the expression intends to do is:
(datepart(dd,getdate())) is getting the the current date's day part
CONVERT(CHAR(8),GetDate(),112)) is getting current date in YYYMMDD
(dateadd(dd,-(datepart(dd,getdate())),CONVERT(CHAR(8),GetDate(),112))) is subtracting the day part from today's date (see the negative sign).
Thus it is trying to get the first day of the current month. In case the result of the above expression crosses the last day of the previous month, it adds 23 hours 59 minutes.
The logic was probably intended to get the last day of the previous month. This adding of ~24 hours is creating confusion.
This will help you out:
declare #First datetime = dateadd(month,datediff(month,0,getdate()),0)
declare #Last datetime = dateadd(second,-1,dateadd(month,datediff(month,0,getdate())+1,0))
#First checks for the number of months from ZERO to NOW, then adds that number of months to ZERO to give you the first moment of this month.
#Last works similarly, but adds one month to the number of months added to ZERO to give you the first moment of next month, then it subtracts 1 second from that date to give you the last second of the last day of this month.
If you need to go to MS, you can change the last bit to subtract 3 miliseconds instead of 1 second.
Following are two ways of adding days and months to a given date. Both seem to be logically correct but returns different values.
Column number 1: Add months and then days,
Column number 2: Add days and then months
DECLARE #d DATE = '20140128'
SELECT DATEADD(DAY, 3, DATEADD(MONTH, 1, #d)) Add_Months_Days,
DATEADD(MONTH, 1, DATEADD(DAY, 3, #d)) Add_Days_Months
Results and fiddle
Add_Months_Days Add_Days_Months
---------------- ----------------
2014-03-03 2014-02-28
I understand why it is happening and both are logical too. But in a situation where we need to add months and days to a given date at the same time, is there a standard way to do this?
They are both logical but return different results as implicit in your question is the truncation of the add-month result to month-end, should it take you over a month-boundary. You have this in the second query, but not the first.
I believe they are both correct, but they do different things.
MSDN states:
If datepart is month and the date month has more days than the return
month and the date day does not exist in the return month, the last
day of the return month is returned.
In the first example you first add 1 month to 20140128 making it 20140228 (a valid date) and then add 3 days, ending up with 20140303.
In the second example however you add 3 days, getting 20140131 and then add 1 month, but since February 2014 only has 28 days you'll get 20140228 as the result as the dateadd operation returns the last day of the month as stated above.
I don't believe there is a standard way of doing this, I would think it comes down to the specific business requirements, however I personally think doing month-day and getting the latter end date might be "more correct" as it seem to follow from the intent (the day-month method seem to lose a few days).
Adding MONTH (1) to a date ("20140128"), will not add total days of the month (Jan - 31, Feb - 28 or 29, etc.). It will add the given MONTH value (1) to the input date and result will be "20140228".
Please refer this Question and Answer
How do we get the first day of the month and last day of the month. i am using it on a apex class.
i can get the todays date by using
date.today()
but how would i get the first and last day of the month.
Thanks
You should get familiar with the Apex Code Developers Guide as it has very good documentation for questions like this. Here's the Date methods page that you would find helpful.
With respect to your specific question, you get the first date of the month (I'll use today's month in my example) using the following
Date firstDayOfMonth = System.today().toStartOfMonth();
To get the last day of the month, use the following:
Date lastDayOfMonth = firstDayOfMonth.addDays(Date.daysInMonth(firstDayOfMonth.year(), firstDayOfMonth.month()) - 1);
I think there is a better way to solve the second problem, that is, accept the last day of the month.
firstDayOfMonth.addMonths(1).addDays(-1);