Interesting SSRS parameter discovery when using subscriptions - reporting-services

I have recently been putting together an SSRS report that will run every 15 minutes for the previous 15 minute 'chunk' of time. In essence a very straight forward and simple report that will run via an automated subscription.
I was using Microsoft SQL Server 12 Report Builder Version 3.
I was alerted to an issue with the output csv when my recipient reported being sent blank files, most odd considering the report generated as expected when run manually.
Long story short, it was the expressions I was using to generate the From and To dates. Manual runs produced data, subscription runs did not.
Original parameters
FromDate
dateadd(DateInterval.Second, (second(now()) + 900) * -1, dateadd(Dateinterval.Minute, (minute(now()) mod 15) * -1, now()))
ToDate
dateadd(DateInterval.Second, (second(now()) + 1) * -1, dateadd(Dateinterval.Minute, (minute(now()) mod 15) * -1, now()))
New Parameters
FromDate
dateadd(DateInterval.Minute, -15, dateadd(DateInterval.Minute, cint(datediff(DateInterval.Minute,today(),now()) / 15) * 15, today()))
ToDate
dateadd(DateInterval.Second, 899,Parameters!FromDate.Value)
Thought I would post this here for two reasons
Theories as to why
It might help someone in the future

Your original parameters take Now and subtract the minutes to arrive at a 15-minutes time, then they take the seconds of another Now (which is later and could be in the next second or even minute) and subtract that value to arrive at a 0-second time (or a 59-second time). This could already cause a problem when there is a change of seconds between the first and the second Now, which isn't very unikely, as on my test system there were 0.59 seconds between the two evaluations of Now in the FromDate parameter. Also, the Now value is more accurate than just a second, and your formula does not respect that. Therefore, if the records you are trying to process in your report happen to have a time of exactly a quarter of an hour, the first parameter is for sure greater (by maybe 0.01 second) and so the record is ignored.
Your formula for the "new parameters" does not depend on the seconds of Now() and will always return a time with no fraction of a second, so I guess that that's what makes the difference.
The expression for the FromDate could be simplified a little:
=Today.AddSeconds(900*(DateDiff(DateInterval.Minute, Today, Now)\15-1))
If you do not plan to run the report very short before midnight, there should not be a problem caused by a change of the day during the evaluation of Today and Now, and you could calculate the second parameter in a similar way, independently from the first one:
=Today.AddSeconds(900*(DateDiff(DateInterval.Minute, Today, Now)\15)-1)

The original parameter values were being calculated individually which means they would each have slightly different values for Now(). I know this is a long shot, but it's a theory. If the subscription job fired off a fraction of a second before a 15 minute interval, it's possible that the ToDate returned just before the FromDate. This would result in an invalid date range.
With the new expressions, the ToDate is referencing the FromDate which forces them to be calculated in sequence, not in parallel. Not to mention you're adding to the FromDate which also forces the date range to have a consistent length. However, you may still run into a case where you get the same report twice if the FromDate is calculated on the wrong side of a 15 minute cutoff.
One way to test/avoid this issue would be to offset the subscription time so that it doesn't actually try to fire at the exact 15 minute cutoffs. For example, you could have it scheduled to go off 1 minute afterwards.

Related

Adding two date stamps into a single SSRS expression/cell

This is all done in Microsoft SQL Server Report Builder
So I have a large data set that contains Work orders and then their 'type/craft'. Each craft is then broken down into each row so that you can see how many work orders are still open in the certain craft. At the top of the page, It list, WO's 1-2 Past Due, WO's 3-5 Past Due, WO's 6-10 Past Due... etc, till you reach 30 days+
I then have an expression inserted that will tell you what date the 1-2 days is... However, I am having trouble making the expression value be 2 dates or an in between date.... For example, I have =DateAdd("d", -1, Now) inserted which will give me the date from 1 day back, however, I would ALSO like it to show 2 days back.... So instead of ONLY saying 6/13/2018... it would say 6/12/2018 to 6/13/2018
I guess I could go back and edit my SQL code to automatically do the dates, however I thought it would be easier to use the report system.
You would want your expression to be something like this:
=DateAdd("d", -2, Today()) & " to " & DateAdd("d", -1, Today())
If you need the time as well you would want to use Now instead of Today, but based on your question it seems you are only interested in the dates, so this should return exactly what you are looking for.

Time bands in a column in Report Builder 3.0

Take Datetime and give the half hour band it belongs to, irrespective of the date, just interested in the time.
There is as far as I can see, no time periods in SSRS, I need half hour time periods, that's 48 blocks in a day.
I would like to plot my data in bar chart,the interval for the bar width is along the x axis, the height is the occurrences on the y axis. I need an interval of half hour(the width of the histogram),I don't want to run some IIF/CASE statement, I would rather, convert the DateTime i have to a band on each row of data, what's the best way to output so I get half hour intervals along the X axis.
I don't want to run some IIF/CASE statement
By this I assume you mean you don't want 48 layers of IIF/CASE. Here's an SSRS expression that'll band a datetime just using a single IIF:
=DateAdd(DateInterval.Minute, 0-
IIF(Minute(Fields!DATETIMEFIELD.Value) <= 29, Minute(Fields!DATETIMEFIELD.Value), Minute(Fields!DATETIMEFIELD.Value) -30),
DateAdd(DateInterval.Second, 0-Second(Fields!DATETIMEFIELD.Value) ,Fields!DATETIMEFIELD.Value))
This removes the seconds from the time, and removes as many minutes necessary to get to the previous half-hour. You could likely use the same logic with different syntax as a column in your SQL query, if you prefer.
Use this calculated value for all the relevant parts of your SSRS chart.

How do I retry until a given date matches current date within For Loop Container?

Scenario:
I have a lookup table which has a date column, I need to look at this date column and check if its today's date, if not then wait for 5 mins and check the same thing again, and if the date is current send an email and exit the loop, and if after 6 retries if the date is not current execute a SQL task.
I have a ForLoop Container with the following settings:
InitExpression : #[User::Counter] = 0
EvalExpression : #[User::Counter] < 6
AssignExpression : #[User::Counter] = #[User::Counter] + 1
How / Where do I check the date :
SELECT ControlTimeStamp from LOOKUPTABLE
WHERE ControlTimeStamp = DATEADD(dd, 0, DATEDIFF(dd, 0, GETDATE()))
Note:
I'm using Business Intelligence Development Studio (BIDS) 2008 for SSIS package development.
I think you'll want an approach like this. Execute your SQL Task to determine whether today is your date. From there you'll need to either sleep for N minutes or you'll want to send an email. The trick is to use an Expression on the Precedence Constraint between the Execute SQL Task the children.
My implementation differs slightly from what yours but the concept remains the same. I created two variables, #ActualDate and #ReferenceDate. #ActualDate is today and #ReferenceDate gets set from the Execute SQL Task. I then see whether they are equivalent. For what you have, if you get a result, then you know the send mail condition has been met so change your Expressions to meet that.
What isn't shown is how to terminate the loop early as I'm not quite certain how to do that.

Past 5 week calculation in WEBI (BO 4.0)?

I am facing issue in calculating past 5 week data based on current date (excluding currrent week)
For e.g:
Suppose we are in 40th week of this year, I need to get the sum of all the transactions for the previous 5 weeks (39, 38, 37, 36 & 35).
Currently calculating based on the calendar day but as Calendar day is giving the granular level of data, inorder to increase the performance I need to use the calendar week (sample data like (2012/40).
Is there a way to do this?
I'd construct a flag field (either in the back-end or in the universe) using datediff (in SQL Server terms).
Failing that, you could construct a variable in Web Intelligence using the Week function.
Pseudo-code but something like:
=IF(Transaction_Date < Week(CurrentDate()) AND Transaction_Date >= (Week(CurrentDate())-5); "TRUE"; "FALSE")

Get the next xx:30 time (the next half-past hour) after a specified time

I'm trying to find a MySQL query which will obtain the time that is the next half-past-the-hour from a specified datetime. I explicitly mean the next half-past-the-hour, not the next half-hourly point.
So for instance:
If the datetime was "2009-10-27
08:15:24", the answer would be
"2009-10-27 08:30:00"
If the
datetime was "2009-10-27 08:49:02",
the answer would be "2009-10-27
09:30:00".
I came across this page which refers to SQL Server, and towards the end of that thread there is a similar sort of problem. But it's not quite the same, and it relies on a function that MySQL doesn't have.
Here is a fuller list of examples and expected return values:
2009-10-27 08:15:24 should return 2009-10-27 08:30:00
2009-10-27 08:49:02 should return 2009-10-27 09:30:00
2009-10-27 23:49:10 should return 2009-10-28 00:30:00
2009-10-27 10:30:00(.000001) should return 2009-10-27 11:30:00
(Note how, in the fourth example, because the exact half-past (10:30:00.0000000) has already gone, the next half-past-the-hour point is found.)
I tried using this kind of thing:
SELECT IF( (MINUTE(NOW()) < 30), HOUR(NOW()), (HOUR(NOW()) + 1) )
(after which addition of a CONCATed string would take place), but it would fail because of the changeover to another day, and it feels inherently 'hacky'.
Can anyone suggest a suitable sort of algorithm? I wouldn't expect a full answer (though that would be nice!), but suggestions as to the kind of algorithm would be helpful. I've been drawing over bits of paper for two hours now! I have a hunch that using modulo might be useful but I'm not sufficiently familiar with using it.
The answer will be fed to a PHP class later, but I'd rather implement this at SQL level if possible, as the rest of query also performs other date comparison functions efficiently.
This is a little messy, but works:
select from_unixtime( floor((unix_timestamp(MY_DATE)+(30*60))/(60*60))*(60*60) + (30*60) )
It pushes the time forward 30 minutes, then truncates to the top of the hour, then adds 30 minutes to it. Because it's working unix timestamps (seconds since 1970), you don't have to worry about the boundaries of days, months, years, etc.
I can't help but notice that this would be much easier at the PHP level :-) That said, here's what you can do:
Add 30 minutes to your datetime using DATE_ADD(); this will move to the next hour if it's already past half-hour
Create a new datetime value by extracting date / hour and hard coding minutes / seconds. CONVERT(), ADDTIME() and MAKETIME() all help.
The end result is:
select ADDTIME(
CONVERT(DATE(DATE_ADD(yourDateTime, INTERVAL 30 MINUTE)), DATETIME), # date part
MAKETIME(HOUR(DATE_ADD(yourDateTime, INTERVAL 30 MINUTE)), 30, 0) # hour + :30:00
) from ...
Use the MAKETIME(hour,minute,second) function to construct the desired value.