I have a report on daily base date and I get a table where it display data of today when I run for today date.
Then I have a line chart which shows the graph of that date and the count of hit. Here, I need the graph to be shown for the whole month when I run the report not for only single date.
Please let me know the logic .
Thank you
Create a new Dataset and define variables containing the range of data you need for whole month graph. Then select data using the variables.
Declaring variables:
declare #startOfMonth datetime2 = (SELECT DATEADD(month, DATEDIFF(month, 0, getdate()), 0))
declare #endOfMonth datetime2 = getdate()
Next, use this variables in your query:
select
/*
all data you need
*/
where date_of_data >= #startOfMonth and date_of_data <= #endOfMonth
Then use this Dataset in your new monthly graph.
Related
I'm trying to make a copy activity to copy data from an on-premise SQL database with data factory. I need the to modify dynamiccaly the query, so it takes a range of dates, for example ranges of every minute:
So for example: the first copy would be using dynamical content as
...
AND DateTime > '20230131 13:06'
AND DateTime < '20230131 13:07'
and then, the next run would be:
AND DateTime > '20230131 13:07'
AND DateTime < '20230131 13:08'
I tried to use variables like:
AND DateTime > #{variables('starttime')}
AND DateTime < #{variables('endtime')}
It works when I give the times manually to variables but the actual data is for a year, so I need to read the start and end times from a file or somehow automate it. I tried to use "ForEach" block, but the problem is that I can only set one variable as a loop, either start or end time.
what is the best solution for this?
How can I use "ForEach" block on start time and enter the end time in a way that is one minute after the start time?
You can read the start time file, loop through it and add a minute to each time in for loop.
The following is the output of my look up of csv file containing start_times.
Loop through this data. Inside for each I have 2 set variable activities, one for converting start time to a valid format so minute can be added using addMinutes() function.
#concat(substring(item().start_time,0,4),'-',substring(item().start_time,4,2),'-',substring(item().start_time,6,2),' ',substring(item().start_time,9,5))
Now, for this format, add minutes and replace the - value concatenated above.
#replace(addminutes(variables('tp'),1,'yyyy-MM-dd hh:mm'),'-','')
I'm not sure how this works in Data Factory but in t-sql, you can increase your variables by one minute with the DATEADD function. Fiddle
DATEADD(*interval*, *number of intervals*, *start datetime*)
I checked Books Online and it looks like data factory uses datetime_add. It appears to work the same way though.
DATETIME_ADD(*interval*, *number of intervals*, *start datetime*)
I hope this helps.
DECLARE #startdate datetime = '20230131 13:06'
, #enddate datetime
, #finishdate datetime;
SET #enddate = DATEADD(minute, 1, #startdate);
SET #finishdate = DATEADD(minute, 100, #startdate); --will loop 100 times.
--change that to whatever you want.
WHILE #startdate < #finishdate
BEGIN
SELECT data
INTO newTable
FROM oldTable
WHERE oldTableDate >= #startdate
AND oldTableDate <= #enddate;
SELECT #startdate = DATEADD(minute, 1, #startdate),
#enddate = DATEADD(minute, 1, #enddate)
END
We've been using MS Access, with the following syntax for MTD Data that works for us:
Between DateSerial(Year(Date()),Month(Date()),1)
And DateSerial(Year(Date()),Month(Date())+1,0)
We need to transition the above logic to SQL/SSRS for automatic emailed reports, but I cannot get this DateSerial logic to work with SQL.
In the Filter field of the SQL query, I can successfully use BETWEEN '8/1/2014' AND '8/31/2014' for MTD data, but would like to have a DateSerial logic applied so that reports don't need to be created for every month, quarter, year, etc.
When trying to use the DateSerial function, we get the error "Invalid or missing Expression". I've seen a few topics on this that Parameters are required, but really believe that this is a simple syntax issue for the filter field, since actual dates work with the BETWEEN command.
There are several different ways to get this. Here is just one way.
Get todays date. In this case 8/27/2014
Declare #Today date = cast(getdate() as date)
Get the first of the month, 26 days in the past
Declare #StartDate date = dateadd(d, -1 * (day(#Today) - 1), #Today)
select #Today, #StartDate
You can use the function CONVERT:
http://msdn.microsoft.com/en-us/library/ms187928.aspx
Or the function DATEFROMPARTS if you are using SQL Server 2012:
http://msdn.microsoft.com/en-us/library/hh213228.aspx
Or DATEADD:
select DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0); -- first day of current month
select DATEADD(MONTH, DATEDIFF(MONTH, -1, GETDATE()), -1) -- last day of current month
This last one I took from: https://stackoverflow.com/a/11746042/1274092
See mine at:
http://sqlfiddle.com/#!3/d41d8/38333
This has been resolved. The ODBC driver does not apparently play well with SSRS. The DateSerial command would not work within the query itself. The workaround was to add the filter to the Dataset. This syntax is what works, but again only in the Dataset filter: [expression] Between [first value box] =DateSerial(Year(Now()),1,1) [second value box] =DateSerial(Year(Now()),12,31)
This gives us the YTD reporting data that we require.
Goal: To provide dynamic date calculations for SSRS Parameters to be used in scheduling.
I have a report containing two date parameters, DateRangeBegin and DateRangeEnd. The problem we are running into is that users want to schedule this report for a date range like "Month to Date", "Year to Date", "Last Week", etc. When a user goes to schedule the report, they can only provide static dates for these two parameters.
The idea I had was to create a dataset that will calculate these values and be referenced in another parameter called DynamicDate. The user would then select "Yesterday", from the DynamicDate parameter, and the DateRangeBegin and DateRangeEnd parameters would get updated with the calculated values from the dataset.
The dataset would be something like this:
Select
2 as DateCalcId,
'Yesterday' as DateCalcDescription,
CONVERT(VARCHAR, DATEADD(DAY,-1,GETDATE()), 101) as DateCalcBegin,
CONVERT(VARCHAR, DATEADD(DAY,-1,GETDATE()), 101) as DateCalcEnd
UNION ALL
Select
1 as DateCalcId,
'Today' as DateCalcDescription,
CONVERT(VARCHAR, DATEADD(DAY,0,GETDATE()), 101) as DateCalcBegin,
CONVERT(VARCHAR, DATEADD(DAY,0,GETDATE()), 101) as DateCalcEnd
UNION ALL
Select
3 as DateCalcId,
'Month to Date' as DateCalcDescription,
CONVERT(VARCHAR,(CONVERT(datetime, CONVERT(VARCHAR, Year(GetDate())) + '-' + Convert(Varchar,Month(GetDate())) + '-01')), 101) as DateCalcBegin,
CONVERT(VARCHAR, DATEADD(DAY,0,GETDATE()), 101) as DateCalcEnd
order by
DateCalcId
I think the function I'm wanting to use is the Lookup() function in the date parameters for their default values, but I'm having a little trouble with the syntax. So far I have:
=Lookup(Parameters!DynamicDate.Value, Fields!DateCalcId.Value, Fields!DateCalcBegin.Value, "CalculatedDates")
But I'm getting this error:
A Value expression used for the report parameter 'DateRangeBegin'
refers to a field. Fields cannot be used in report parameter
expressions.
Does anyone know how to get this working? Or is there another way people have done this?
So I think I've come up with a pretty good solution that fits my requirements.
I really like the idea of having these dynamic date range pairs being calculated in a SQL Dataset because they can be reused across multiple reports, don't require a custom dll to be created, and the pair can be calculated in a single record.
So the solution is to have a single stored procedure that takes a nullable parameter, where passing in null returns the entire set, but passing in the DateCalcId will return a single record that can then be attributed to the range beginning and end parameters.
So the entire dataset would populate the Available Values for the first parameter (#DynamicDate), where the Value field is the "DateCalcId" column, and the Label field is the "DateCalcDescription" column.
Then the date range parameters default values would be tied to the single result dataset using the #DynamicDate value as the input to the stored procedure.
This allows the user to select the Dynamic Date and the date range parameters are defaulted to the calculated dates, and the user is able to overwrite these dates if they desire.
If the dates should always be calculated based on the value selected, then the date parameter Available values can be set to the single result dataset as well.
Here is the stored procedure:
Create PROCEDURE [CNF].[RptCalculatedDateRanges]
#DynamicDateId int = null
AS
BEGIN
SET NOCOUNT ON;
Declare #Today datetime = convert(varchar,getdate(),101)
Select
*
from
(
Select
2 as DynamicDateId,
'Yesterday' as DynamicDateDescription,
DATEADD(DAY,-1,#Today) as DynamicDateBegin,
DATEADD(DAY,-1,#Today) as DynamicDateEnd
UNION ALL
Select
1 as DynamicDateId,
'Today' as DynamicDateDescription,
#Today as DynamicDateBegin,
#Today as DynamicDateEnd
UNION ALL
Select
3 as DynamicDateId,
'Month to Date' as DynamicDateDescription,
CONVERT(VARCHAR,(CONVERT(datetime, CONVERT(VARCHAR, Year(#Today)) + '-' + Convert(Varchar,Month(#Today)) + '-01')), 101) as DynamicDateBegin,
#Today as DynamicDateEnd
) D
where
#DynamicDateId = D.DynamicDateId or
#DynamicDateId is null
order by
DynamicDateId
END
You don't to use a dataset, you can just use the Default expression to calculate the parameter date value using VBA. For example, DateRangeEnd for the following dates would be:
Yesterday:
=DateAdd(DateInterval.Day, -1, Today)
End of Last Month:
=DateAdd(DateInterval.Day, -1, DateAdd(DateInterval.Day, 1-Day(Today), Today))
So you have a SWITCH statement based on your DynamicDate parameter:
=Switch(Parameters!DynamicDate.Value = 1, Today, Parameters!DynamicDate.Value = 2, DateAdd(DateInterval.Day, -1, Today))
and so on for all your date range choices.
I'm trying to get a query that will multiply a static number by the amount of days in a date range the problem i'm having is that when a single day is selected it returns a result of 0 instead of 1: Ex:
Declare #Startdate DATE
Declare #enddate DATE
SET #Startdate='9/1/2013'
SET #enddate='9/1/2013'
SELECT 1154*(Select DATEDIFF(DAY, #startdate, #enddate))
This example returns 0 instead of 1. Should I be using something other than DateDiff?
Additional clarification - This will be used as part of a report where the date range will be dynamically entered by person calling the report.
Could just add 1:
Declare #Startdate DATE
Declare #enddate DATE
SET #Startdate='9/1/2013'
SET #enddate='9/1/2013'
SELECT (DATEDIFF(day,#Startdate,#enddate)+1)*1154
Update, as noted don't need the inner SELECT
Ok I am trying to write a query that says get the current date and make it the start date. Then I want to go a month back from that current date for the EndDate. Is it possible to do this? Like if it was 9-15-2010 as the start date can I go a month back from that to 8-15-2010 or is this no possible....what would you do for like 9-20-2010 as the start date since every month has a different amount of days in it? Otherwise if this is not possible how else could I do this? The report will always be run on the 25th of the month so any ideas? I need to go from the 25th back a month....I can get some duplicate records between months if needed but less is obviously better
Right now I am using this:
DECLARE #StartDate DATETIME,
#EndDate DATETIME;
SET #StartDate = DATEADD(m,-1,GETDATE());
SET #EndDate = DATEADD(m, 1, #StartDate);
Does this work?
Also, how would I then say my AuditInsertTimestamp is between #Start adn #EndDate?
Currently I have this:
AND cvn.[AuditInsertTimestamp] BETWEEN #StartDate AND #EndDate ;
This is still giving me dates like 7-26-2010 though....
Thanks!
That should work. Did you try it?
If it doesn't work (and there are only 12 test cases to check if you don't trust the documentation) then you can re-build the date from the date parts.
Here's the problem. It should be like this:
cvn.[Subject] = 'Field Changed (Plate Type)'
AND (
cvn.[Note] LIKE 'Old Type: IRP%New Type: BASE PLATE%'
OR cvn.[Note] LIKE 'Old Type: Base Plate%New Type: IRP%'
)
AND cvn.AuditInsertTimestamp BETWEEN GETDATE() AND DATEADD(MONTH, -1, GETDATE())
AND takes precidence over OR, so you were picking up anything with Old Type:IRP or in the correct date range (with Old Type: Base Plate)
Based on your comment:
Well this is being used to select
records. So if I run it on the 25th I
need 30 days back then my field
AuditInsertTimestamp needs to be
between these 2 dates.
I think you need to do something like this:
SELECT * FROM Table
WHERE AuditInsertTimestamp BETWEEN GETDATE() AND DATEADD(MONTH, -1, GETDATE())