SSRS 2008 week number calculation of current month - reporting-services

I need to construct a data parameter in SSRS 2008 where if the current week number is 1 then I use the first day of the previous month and if not then I use the current day.
I.e. today is week 4 therefore would utilize today's date
If today was march 2, then the week would be week 1 and I would utilize the first of the previous month, feb 1
note: weeks must follow calendar weeks.
Thanks in advance for your assistance

You can set the parameter default using the following expression:
=IIf(Day(Today()) <=7
and DatePart(DateInterval.WeekDay, Today(), FirstDayOfWeek.Monday)
>= DatePart(DateInterval.WeekDay, DateSerial(Year(Today()), Month(Today()), 1), FirstDayOfWeek.Monday)
, DateAdd(DateInterval.Month, -1, DateSerial(Year(Today()), Month(Today()), 1))
, Today())
So:
If today is one of the first seven days of the month, and the day of the week number is >= the day of the week of the first of the month
=> First week, so use the first day of the previous month
=> Else use the current date.

Related

SSRS Default Date previous Year first day and last Day

How do I get the first and last day of the previous year in SSRS for default dates
tired wth DATEADD function but getting an error
You can use DATESERIAL() to do this easily
DateSerial just take 3 integer values (year, month, day) to return a date.
First day
=DateSerial( year(today()) - 1, 1, 1)
Last Day
=DateSerial( year(today()) - 1, 12, 31)
You don't show what you tried so we can't explain what you are doing wrong.
If I needed to get the 1st day of the previous year, I would take the current year, subtract the number of days from 1 and then subtract a year.
With DATEADD, the first day of last would be:
=DATEADD("yy", 1, DATEADD("d", 1 - DATEPART("dy", TODAY), TODAY))
While the last day is actually simpler:
=DATEADD("d", 0 - DATEPART("dy", TODAY), TODAY)

How to do YTD, MTD calculations based on the Today()

I am very new to SSRS, I have below scenario.
I have to calculate measures for YTD, MTD, Last 7 Days and Previous day based on the today(). I need to use these YTD, MTD, Last 7 days and Previous day variables in the calculations while writing the IIF syntax
For example:
sum(IIF(Year="this year is Year to Date", value, sales)
sum(IIF(CreatedDate="the last 30days values ",sales)
Can any one please tell me how to achieve this
Thanks
You can use the YEAR function to get the year of a date field. An IIF statement is used to evaluate an expression (in your case if the date is within the current year) then the desired result if the expression is true then the result if false. So your YTD formula could look like:
=SUM(IIF(YEAR(Fields!YourDateField.Value) = Year(TODAY()), Fields!Sales.Value, 0)
This reads: The sum of (if the year = current year then Sales else 0).
The last 30 days is similar but you would use the DATEADD function to figure what the date was 30 days ago:
=SUM(IIF(Fields!YourDateField.Value >= DATEADD("d", -30, TODAY()), Fields!Sales.Value, 0)
https://technet.microsoft.com/en-us/library/aa337194(v=sql.100).aspx

MySQL compare by week

I have a select statement of which needs to base its WHERE on a timestamp but for all dates within that week beginning monday.
The MySQL
SELECT DISTINCT(unique_reference) AS unique_reference, date(datetime) AS datetime
FROM sales_tickets
WHERE Date(datetime)='$datetime'
This is based on the fact that $datetime can be any date but the select statement needs to get all records from that week, example: if its the Monday 12th May 2014, it will fetch all results from that week, instead of the one day.
Currently, its fetching only one day of results.
I have no idea how to rectify this issue. Any advise would be awesome thanks.
You can compare using the WEEK function :
WHERE WEEK(DATE(datetime)) = WEEK('$datetime')
If you have multiples years for entries, you can use instead the YEARWEEK function.
Edit for first day of week:
WEEK and YEARWEEK functions have both a second optional argument which tells when a week start. Try to consider mode 1 or 5.
Mode First day of week Range Week 1 is the first week …
0 Sunday 0-53 with a Sunday in this year
1 Monday 0-53 with 4 or more days this year
2 Sunday 1-53 with a Sunday in this year
3 Monday 1-53 with 4 or more days this year
4 Sunday 0-53 with 4 or more days this year
5 Monday 0-53 with a Monday in this year
6 Sunday 1-53 with 4 or more days this year
7 Monday 1-53 with a Monday in this year
A sargable solution would explicitly calculate the start and end points of your desired range:
WHERE datetime >= DATE('$datetime') + INTERVAL 0 - WEEKDAY('$datetime') DAY
AND datetime < DATE('$datetime') + INTERVAL 7 - WEEKDAY('$datetime') DAY
The easiest method might be to have your WHERE statement check against a range of values, which you can calculate beforehand.
I'll assume you're using php.
So your SQL statement will be:
SELECT DISTINCT(unique_reference) AS unique_reference, date(datetime) AS datetime
FROM sales_tickets
WHERE (Date(datetime) > '$startDate')
AND (Date(datetime) < '$endDate');
You'll first have to figure out what $startDate and $endDate are:
$endDate = strtotime('Monday', time()); // might need to adjust this depending on when your week starts
$startDate = $endDate - 7*24*60*60; //one week before.
Be careful with converting times between unix timestamps and datetimes used in SQL, but you get the idea.
Try WEEK():
WHERE WEEK(datetime)=WEEK('$datetime')
Read more: WEEK()

MDX to get last Sunday and Saturday

I built a SSRS 2005 report on a SSAS 2005 cube. The report has start date and end date parameters from Time dimension. I need to set the default values to be last Sunday and last Saturday separately. (financial week is from Sunday to Saturday)
E.g.
Start date: [Time].[Day].&[20140309]
End date: [Time].[Day].&[20140315]
How can I do it dynamically? I mean for this week is above dates, but for next week, it should be 16 March, 22 March. I know how to do it in T-SQL, which will involve some calculation with system date, but MDX?
You can use some VBA Date functions available in MDX:
StrToMember('[Time].[Day].&['
+ Format(DateAdd('d', - DatePart('w', Now(), 1), Now()), 'yyyyMMdd')
+ ']'
)
should give you the last Saturday before today, and
StrToMember('[Time].[Day].&['
+ Format(DateAdd('d', - DatePart('w', Now(), 1) - 6, Now()), 'yyyyMMdd')
+ ']'
)
the last Sunday before that.
The second argument of DateAdd('d', ...) is the number of days to add. And as this is negative here, we go back in time that many days. DatePart('w', ...) returns the weekday number (Sunday = 1, Monday = 2, ...). Thus, if you subtract "weekday number" days from today, luckily you are already at last Saturday. And subtracting six more days, you arrive at the last Sunday before that.

First and Last day of current month

In SQL Server Reporting Services; How can I calculate the first and last day of the previous month?
I know I can use the expression below to get the last day of the current month, but I'm stuck when trying to find the first and last of the previous month.
=DateSerial(Year(Now()), Month(Now()), "1").AddMonths(1).AddDays(-1)
Just a guess based on your code working.
--previous month last
=DateSerial(Year(Now()), Month(Now()), "1").AddDays(-1)
--previous month first
=DateSerial(Year(Now()), Month(Now()), "1").AddMonths(-1)
First day of this month:
=dateadd("m",0,dateserial(year(Today),month(Today),1))
Last day of this month:
=dateadd("m",1,dateserial(year(Today),month(Today),0))
First day of last month:
=dateadd("m",-1,dateserial(year(Today),month(Today),1))
First day of next month:
=dateadd("m",1,dateserial(year(Today),month(Today),1))
Last day of last month:
=dateadd("m",0,dateserial(year(Today),month(Today),0))
Last day of next month:
=dateadd("m",2,dateserial(year(Today),month(Today),0))
For current month
=DateSerial(Year(Parameters!ParameterName.Value), Month(Parameters!ParameterName.Value), "1").AddMonths(1).AddDays(-1)
Previous month last day:
=DateSerial(Year(Now()), Month(Now()), "1").AddDays(-1)
Previous month first day:
=DateSerial(Year(Parameters!ParameterName.Value), Month(Parameters!ParameterName.Value), "1").AddMonths(-1)
Previous month last date:
=DateAdd("d",-(Day(today)), Today)
First of previous month:
=DateAdd("m",-1,DateAdd("d",1-(Day(Today)), Today))
=DateSerial(Year(Now), Month(Now), 1) for first day of the month
and
=DateSerial(Year(Now), Month(Now)+1, 0) for the last day of the month.
http://www.answermysearches.com/ssrs-how-to-set-a-default-parameter-to-the-first-day-of-the-month/2167/
Using Native VB Functions
First Day of Previous Month
=DateAdd("m", -1, DateSerial(Year(Today()), Month(Today()), 1))
Last Day of Previous Month
=DateAdd("d", -1, DateSerial(Year(Today()), Month(Today()), 1))
I was able to accomplish this, and show it in mm/dd format by using:
=Format(DateAdd("D", -1, (DateAdd("M", 1, yourdate.value))), "MM/dd")
=DateSerial(Year(Now), Month(Now), 1)