SSRS dataset filter - reporting-services

Hi I'm trying to build a report in ssrs which gives the output based on current weekending date where end day is saturday. I'm pulling my data from SSAS Cube and I have a weekending date field so I applied a filter in the query designer to get the current weekending date using a parameter I used this expression:
DateAdd("d",7-DatePart(DateInterval.Weekday,Today,FirstDayOfWeek.Sunday),Today)
When I'm executing my query, it throws an error saying the restriction imposed by the constrained flag in the STRTOSET function were violated.
Please let me know how can I fix this.

The error is thrown because you are passing a string that doesn't correspond with a valid member in your cube.
Note the Query Designer builds a MDX query based on the dimensions and members you select, this query uses the STRTOSET() function to convert your string parameter in a valid member.
...
STRTOSET(#CurrentWeekendDate,CONSTRAINED)
...
If you pass the current weekend date to your parameter it produces:
STRTOSET('2016-01-10',CONSTRAINED)
As 2016-01-10 is not a valid member in your Date dimension it throws the error.
You have to pass something like this depending on your cube:
[Date].[Date Key].&[2005-01-01T00:00:00]
So in SSRS you have to set your parameter to Text and use this expression:
CORRECT EXPRESSION:
="[200 Date].[Week Ending Date].&[" &
Format(
DateAdd("d",7-DatePart(DateInterval.Weekday,Today,FirstDayOfWeek.Sunday),Today),
"yyyy-MM-ddThh:mm:ss"
)
& "]"
UPDATE: If [Week Ending date] level doesn't include time:
="[Date].[Date Key].&[" &
Format(
DateAdd("d",7-DatePart(DateInterval.Weekday,Today,FirstDayOfWeek.Sunday),Today),
"yyyy-MM-dd"
)
& "]"
Go through this tutorial if you get stuck.
Let me know if this helps.

Related

Argument data type nvarchar is invalid for argument 3 of convert function in SSRS

I am having an issue with a parameter and the convert function when executing my query in Report Builder.
I am having the following in my code:
CONVERT(VARCHAR(11), COALESCE(Status.POBDate, Status.[Sched Collection Date]),(#DateFormat)) AS [Collection Date]
,CONVERT(VARCHAR(11), Status.[Act Del Date],(#DateFormat)) AS [Delivery Date]
The (#DateFormat) parametner has data type Integer and available values as per the bild below.
The funny thing is that I can run the query in SSMS without any problem, but when trying to apply some adjustments in Report Builder, and save the report, it is complaining about the invalind argument even though, the parament (#DateFormat) was not edited anyhow. The report worked perfect online and only after opening it in Report Builder it started to complain also when I do not apply any new adjustments.
Any idea what can be wrong and how I could solve it?
I have checked some ideas here on stackoverflow, but nothing worked out so far.
Tones of thanks in advance!
Your parameter type is text not integer and that causes the error.
You can verify it by casting the DateFormat parameter to INTEGER in your SQL code
CONVERT(VARCHAR(11), COALESCE(Status.POBDate, Status.[Sched Collection Date]),CAST(#DateFormat AS INTEGER)) AS [Collection Date]
As a suggestion use the parameter to format the dates inside the report.
How to do it:
Remove conversion from your SQL code
In your parameter set it to text and as values set the date format string
For US ="dd/MM/yyyy"
For UK ="MM/dd/yyyy"
For your date fields set the format expression to = Parameters!DateFormat.Value

Converting mdx parameter to ssrs between dates

I have several section of similar MDX code that has been created in Microsoft SQl Server Management Studio using cube data and I now need to use the code in a SSRS paginated report. I'm getting the following error:
"Query (4, 2) The restrictions imposed by the CONSTRAINED flag in the
STRTOMEMBER function were violated."
This code works fine in Management Studio and SSRS using a date but as soon as I change the date to a parameter I get the error.
MEMBER [Measures].[Sales in Period2] AS
AGGREGATE (
{STRTOMEMBER("[Paid Date].[Date].&[2020-11-01]", CONSTRAINED) : STRTOMEMBER("
[Paid Date].[Date].&[2020-11-30]", CONSTRAINED) }
, [Measures].[Paid Amount]
),FORMAT_STRING = "#.00;(#.00);0;0"
I've tried changing:
[2020-11-01] to [#StartDate1],
[2020-11-01] to [" + #ParameterName + "],
STRTOMEMBER to STRTOSET, and
remove CONSTRAINED.
One possibly way to approach this:
This error says that your #ParameterName is not in the correct format. In your example you have the date as 2020-11-30, i.e., yyyy-MM-dd format. So, your parameter should also have the same format. If the format is same you can use it &[#ParameterName] in your StrToMember.
To achieve this, you can change the Dataset of your parameter to format it in the required format.
Since you are using MDX to get the data, you can look at the option of getting the parameter values directly from your main data set or the date dimension, Paid Date in your example.
In my sample below using the AdventureWorks sample, you can see the date format is different. You may want to show the date format in a different format to the users, but internally you would want it in the same format as your Cube wants it.
I would suggest you look at this link as well to see an example end to end flow with MDX parameters:
https://blog.pragmaticworks.com/writing-parametrized-mdx-for-reporting-services
Good luck

MS Access Date/Time format issue?

This might be a possible duplicate, but I have a strange issue with MS Access DB.
When I update a Date/Time value for my column req_date, I'm facing a problem.
10-03-2017
is getting updated as below
10-03-2017
where as
10-18-2017
is getting updated as below
18-10-2017
I'm using the following code in c# to Execute the query:
query = "update gb_jobs set req_delivery=myDate"
myCMD = new OleDbCommand(query, con, trans);
int tempCnt = myCMD.ExecuteNonQuery();
where as myDate is already converted to string from date time,
As per the solution by Albert, I concatenated my myDate to #myDate# but it is throwing following error:
query = "update gb_jobs set req_delivery=#myDate#"
Error : Data type mismatch in criteria expression.
You don’t mention where/when/how you are updating that column in question.
As a general rule, if the VBA variable type is an actual DATE type variable then you can assign such values directly to a control or recordset in code.
However if ANY of your code uses a string result, then you MUST format the string as USA format regardless of your computer's regional settings. Your regional settings will thus transform the date display to whatever floats your boat.
So any of your date formats have to be of mm/dd/yyyy. Given your examples, it looks like you are following that format. This suggests that you have your DISPLAY set to DD/MM/yyyy. So in theory what you have given so far is correct behaviour.
What this suggests is that your result of 10-03-2017 ACTUALLY means 03/10/2017. So it is in fact March and not October.
Thus in VBA code to update (or query) some data, you have to go:
dtStart as date
dtEnd as date
If you set the value of above two dates, then to query the data, you MUST go:
strWhere = "InvoiceDate >= #" & format(dtStart,'mm/dd/yyyy') & "#" & _
" and InvoiceDate <= #" & format(dtEnd,"mm/dd/yyyy") & "#"
Docmd.OpenReport "rptInvoice",acViewPreview,,strWhere
So any code that will query, or update values with SQL has to re-format the data to USA format (mm/dd/yyyy). What the control and forms will display is as noted whatever you have in windows regional panel, but ALL internal code must format strings as mm/dd/yyyy.
So it not clear how you are making the change, but from what you have given so far, your DISPLAY of info is dd/mm/yyyy, but you are entering the data as mm/dd/yyyy which is correct. If you are entering this data on a form and not using code, then from what you have given your date format as set by windows is dd/mm/yyyy.

initialize a parameter in MS Access query

In MS Access 2010 I am trying to declare and initialize a variable in a query, then display the contents of that variable. The reason for doing this is to use the parameter in a more complicated query as part of a filter. Please note that for this particular case this task must be done in a query object, not in VBA. Here is the code thus far:
PARAMETERS #Date DATE;
SELECT TOP 1 FORMAT(LastUpdated, "yyyy-mm-dd") AS #Date FROM Table1 GROUP BY FORMAT(LastUpdated, "yyyy-mm-dd") ORDER BY FORMAT(LastUpdated, "yyyy-mm-dd") DESC;
SELECT #Date;
This results in an error message: "Characters found after end of SQL statement."
If this can be modified to work the last line of code will be replaced with the more complex query that needs to use #Date in a filter. The other requirement is that it has to be contained within one query object.
In Access you don't need to use the # to prefix parameters. Parameters are inferred as any column which cannot be resolved. So if your table does not have a date column then using date is sufficient for "declaring" a parameter. However maybe something like p_date can separate it from the data type DATETIME/DATE
I don't know what exactly you are expecting with this
FORMAT(LastUpdated, "yyyy-mm-dd") AS #Date
It seems to be trying to assign an alias to FORMAT(LastUpdated, "yyyy-mm-dd") as the parameter value of #Date?
An Access query cannot return multiple result sets so your SELECT #Date; is ultimately what is causing the "Characters found after end of SQL statement." error message.

How to convert separate date and time parameters to a valid datetime?

I have a report with date and time (only an hour and minutes) parameters which are separate. The Date parameter is of type Date/Time, and the Time parameter is set as Text. I want to merge those two values into one because I want to pass a minimum number of arguments to the stored procedure. I tried to achieve that goal in many ways but SSRS returns an error for every attempt.
If I try to use expression like this:
=Format(FormatDateTime(Parameters!startDate.Value, DateFormat.ShortDate).ToString() + Parameters!startTime.Value, "dd/MM/yyyy HH:mm")
SSRS returns this error:
Error conterting data type nvarchar to datetime.
And when I tried to use Datetime.Parse like this:
=DateTime.Parse(Format(FormatDateTime(Parameters!startDate.Value, DateFormat.ShortDate).ToString() + Parameters!startTime.Value, "dd/MM/yyyy HH:mm"))
SSRS said:
The Value expression for the query parameter '#startDate' contains an error: The string was not recognized as a valid DateTime. There is a unknown word starting at index 0.
When I removed the FormatDateTime function I get yet another error:
The Value expression for the query parameter '#startDate' contains an error: Input string was not in a correct format.
Do you have any ideas how to write this expression correctly?
PS. I use SSRS 2008 R2.
This works for me:
=CDate(Format(Parameters!Date.Value, "yyyy-MM-dd") + " " + Parameters!Time.Value)
Didn't try and troubleshoot your specific examples, but they may be running into issues where you're not including the space between the date and time.
The above expression may be suitable for your report; don't know robust it would be in different locales.
You could also consider doing the concatenation/conversion in custom code if you need more flexibility.