SSRS expression, iif statement with date conditions - date

So my default values for startDate and endDate in SSRS were set up with the following ssrs expressions.
first day of previous month ssrs expression
=DateAdd(DateInterval.Month, -1, DateSerial(Year(Date.Now), Month(Date.Now), 1))
last day of previous month ssrs expression
=DateAdd(DateInterval.Day, -1, DateSerial(Year(Date.Now), Month(Date.Now), 1))
But that won't work alone in my case unless I want to go in on the 16th of every month and generate this report for the people requesting it for the first 15 days of the current month.
So in my default value expression for start date i am trying this iif statement...
= iif(
DatePart(DateInterval.Day, Today() <> "20",
DateInterval.Month, -1, DateSerial(Year(Date.Now), Month(Date.Now), 1),
DateInterval.Month, 1, DateSerial(Year(Date.Now), Month(Date.Now), 1)
)
Not working out so well. So what i'm trying to do is.....
Change the default start and end date based on what day of the current month it is, So if current day of the current month equals 16, make start date 1 of current month and end date 15 of current month, if current day of the month isn’t 16 make start date first of previous month and end date last day of previous month. So then the only thing needed is to get subscription emails and what day to send them out on.

Untested, but what if you try this? (for your start date parameter):
= iif(
DatePart(DateInterval.Day, Today()) <> "16",
DateAdd(DateInterval.Month, -1, DateSerial(Year(Date.Now), Month(Date.Now), 1)),
DateSerial(Year(Date.Now), Month(Date.Now), 1)
)

Related

Rewrite dynamic T-SQL date variables in DAX

We're currently rebuilding basic emailed reports built using T-SQL to be paginated reports published on Power BI.
We're muddling through by creating the tables we need with the appropriate filters in Power BI Desktop to reconcile the numbers, then taking the DAX code from them using the Performance Analyser.
The one I'm working at the minute has a simple bit of SQL code to get data for a previous calendar month. I have no idea how or if it's possible for this to exist in DAX?
-- Validation to get previous month
IF (MONTH(GETDATE()) - 1) > 0
SET #MONTH = MONTH(GETDATE()) - 1
ELSE
SET #MONTH = '12'
-- Validation to get year of previous month
IF (#MONTH < 12)
SET #YEAR = YEAR(GETDATE())
ELSE
SET #YEAR = YEAR(GETDATE()) - 1
-- Set start date and finish date for extract
SET #PERIOD = #YEAR + RIGHT('00' + #MONTH, 2)
It needs to become a hidden SSRS parameter or just inline code to be used with this DAX variable:
VAR __DS0FilterTable =
TREATAS({"202212"}, 'Org View_VaultexCalendar'[Calendar Month No])
So the "202212" would become #period or the equivalent if doable without a parameter.
SSRS Parameter:
=IIF(
Month(Today()) > 1,
Year(Today()) & RIGHT("00" & Month(Today()) - 1, 2),
Year(Today())-1 & "12"
)
DAX expression:
IF (
MONTH ( TODAY () ) > 1,
YEAR ( TODAY () ) & FORMAT ( MONTH ( TODAY () ) - 1, "00" ),
YEAR ( TODAY () ) - 1 & "12"
)
In both cases we look at today's month and check if it's after January. If it is, take the current year and concatenate it with the current month less one and padded with a leading zero when needed. In the other case we know that the month is January so take the current year less one and concatenate it with "12"

Subtracting 1 ISO 8601 year from a date in BigQuery

I'm trying to manipulate a date value to go back in time exactly 1 ISO-8601 year.
The following does not work, but best describes what I want to accomplish:
date_add(date '2018-01-03', interval -1 isoyear)
I tried string conversion as an intermediate step, but that doesn't work either:
select parse_date('%G%V%u',safe_cast(safe_cast(format_date('%G%V%u',date '2018-01-03') as int64)-1000 as string))
The error provided for the last one is "Failed to parse input string "2017013"". I don't understand why, this should always resolve to a unique date value.
Is there another way in which I can subtract an ISO year from a date?
This gives the corresponding day of the previous ISO year by subtracting the appropriate number of weeks from the date. I based the calculation on the description of weeks per year from the Wikipedia page:
CREATE TEMP FUNCTION IsLongYear(d DATE) AS (
-- Year starting on Thursday
EXTRACT(DAYOFWEEK FROM DATE_TRUNC(d, YEAR)) = 5 OR
-- Leap year starting on Wednesday
(EXTRACT(DAY FROM DATE_ADD(DATE(EXTRACT(YEAR FROM d), 2, 28), INTERVAL 1 DAY)) = 29
AND EXTRACT(DAYOFWEEK FROM DATE_TRUNC(d, YEAR)) = 4)
);
CREATE TEMP FUNCTION PreviousIsoYear(d DATE) AS (
DATE_SUB(d, INTERVAL IF(IsLongYear(d), 53, 52) WEEK)
);
SELECT PreviousIsoYear('2018-01-03');
This returns 2017-01-04, which is the third day of the 2017 ISO year. 2018-01-03 is the third day of the 2018 ISO year.

Check if the difference between dates is exactly 'n' months in expression SSRS

In my quarterly report Im trying to validate the two parameters StartDate and EndDate.
I first check if the difference between the dates is 2 months:
Switch(DateDiff(
DateInterval.Month, Parameters!StartDate.Value, Parameters!EndDate.Value) <> 2,
"Error message")
Then I try to add whether the StartDate is the first day of month AND EndDate is last day of month:
And (Day(Parameters!StartDate.Value) <> 1
And Day(DATEADD(DateInterval.Day,1,Parameters!EndDate.Value)))
So the whole expression looks like this:
Switch(DateDiff(DateInterval.Month, Parameters!StartDate.Value, Parameters!EndDate.Value) <> 2
And
Parameters!IsQuarterly.Value = true
And
Day(Parameters!StartDate.Value) <> 1
And
Day(DATEADD(DateInterval.Day,1,Parameters!EndDate.Value))<>1),
"Error: Quarterly report must include 3 months")
But It works wrong when the difference between dates is still 2 months, but StartDate and EndDate are not first and last day of the whole period.
I'd appreciate any help :)
I would say just change the implementation Add another two Parameter With Quarter and Year
Quarter like Q1,Q2,Q3 & Q4 with Value 1,2,3 & 4 respectively and year 2012,2013,2014 & so on
Now based on the parameter selected Qtr & Year set Default value of start & End Date
=DateSerial(Parameters!Year.Value), (3*Parameters!Qtr.Value)-2, 1) --First day of Quarter
=DateAdd("d",-1,DateAdd("q",1,Parameters!Year.Value, (3*Parameters!Qtr.Value)-2, 1))) --Last day of quarter
Doing this no need to do any validation bcz its always get the correct Date Difference.
Other Reference
First day of current quarter
=DateSerial(Year(Now()), (3*DatePart("q",Now()))-2, 1)
Last day of current quarter
=DateAdd("d",-1,DateAdd("q",1,DateSerial(Year(Now()), (3*DatePart("q",Now()))-2, 1)))

Previous month in parameter

I need to set a Hidden Default Parameter in SSRS.
In my query I need to not show last months total but the month before that, ie in Feb I need to show Dec totals.
I usually use this for last month but cannot tweak it for the month prior.
Set first date of last month:
=DateAdd("m", -1, DateSerial(Year(Now()), Month(Now()), 1))
Set last date of last month:
=DateAdd("d", -1, DateSerial(Year(Now()), Month(Now()), 1))
If i understand you right you want to:
Get the first day of two months from now
=DateAdd("m", -2, DateSerial(Year(Now()), Month(Now()), 1))
And you want to get the last day on month two months from now
=DateAdd("d" , -1 , DateAdd("m", -1, DateSerial(Year(Now()), Month(Now()), 1)))
Depending on exactly what you need, use something like:
DateAdd
(
DateInterval.Month
, -1
, DateAdd(DateInterval.Day, -1, DateSerial(Year(Parameters!Date.Value), Month(Parameters!Date.Value), 1))
)
Used in a report:

current date minus one month crystal report

How to calculate current date(month) minus one month in crystal report?
To subtract one month from the current date use
DateAdd ("m", -1, CurrentDate)
If you just want the month number use this
Month(DateAdd ("m", -1, CurrentDate))
If you are trying to get the month name use this
MonthName(Month(DateAdd ("m", -1, CurrentDate)))