Converting mdx parameter to ssrs between dates - reporting-services

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

Related

Implementing a date range in SSRS with the data coming from a cube

I have been trying to implement a date range parameter in SSRS 2015. The data is from a cube. I dragged my date into the dimension box of the query designer and went into the parameter tab to change the fromDate and toDate to date/time format and this resulted in the following MDX query.
SELECT ( STRTOMEMBER(#FromDate, CONSTRAINED) :
STRTOMEMBER(#ToDate, CONSTRAINED) )
My date format is dd/mm/yyyy. I have looked on several forums and tried a few method but continued to get the error:
The restrictions imposed by the CONSTRAINED flag in the STRTOMEMBER function
were violated
Thank you in advance
Your parameters will need to be formed in full address mdx. e.g. #FromDate could be this - I've used the DateKey:
'[Date Dim].[Date Hier].&[20180301]'
So some concatenation, within ssrs, would be required before getting passed:
"[Date Dim].[Date Hier].&[" + 20180301 + "]" //<<you'll need to change to the names used in your cube
I'd also be tempted to use strToSet as you want a set. So mine might be something like:
( STRTOSET( "[Date Dim].[Date Hier].&[" + #FromDate + "]:" +
"[Date Dim].[Date Hier].&[" + #ToDate + "]" , CONSTRAINED) )
Here is a good reference: Range parameter on the MDX-query
The error message you received means that the parameter values do not exist in the cube in that format. To solve this you need to create a field that can be formatted and displayed to users without changing the value that the query uses.
When you create parameters in the Query Designer, it automatically creates hidden datasets which populate the Available Values for the parameter. To see this, right-click on the Datasets folder and select Show Hidden Datasets.
Now, go to the dataset properties and add a calculated field in the Fields tab. This will be your nicely formatted date.
In the parameter properties for each parameter, go to the Available Values tab. Leave the Value field the way it is, but change the Label field to the new column you created.
Now the dropdown will show the friendly label, but use the real value behind the scenes!

MDX Query in SSRS Shared DataSet giving wrong Datatype

I have a shared dataset for a Mobile Report on SSRS 2016. The dataset sits on a SSAS data source, and has an MDX query to pull from the cube.
The primary key pulled back from the query is a date, and is formatted as such in SSAS; however when I use the dataset in Mobile Report Publisher it doesn't acknowledge it as a date datatype, it thinks it's a string. This means I can't use the dataset as a main series for a time chart, as it doesn't think there's anything formatted as a date.
The below is the section of MDX that I think is causing the issue.
SELECT
{
[Unresolved], [Assigned to Provider], [Unresolved past due date], [Other past due date], [Provider past due date],
[Company past due date], [Provider PDD Daily Change], [Company PDD Daily Change], [Other PDD Daily Change], [Company pre due date], [Company PRDD Daily Change]
} ON COLUMNS,
NONEMPTY(
{
[Date Snapshot].[Date].[Date]
}) ON ROWS
FROM [Source]
WHERE
( FILTER([Date Snapshot].[YQMD].[Date], [Date Snapshot].[YQMD].CURRENTMEMBER.MEMBER_KEY >= '2015-08-15') )
Update: since SQL Server 2016 SP1 the workaround explained below is no longer necessary and dates should be properly recognized without doing anything special:
To try this new functionality out, create a simple MDX query using the Query Designer that includes a date field from your data model. When you consume that shared dataset with Mobile Report Publisher, it will properly recognize the date field from your query without an additional calculated measure being required.
The original text of the answer follows:
The reason as to why this happens is explained in the official Microsoft documentation:
The default return type for Analysis Services queries is a string.
When you build a dataset in Reporting Services Report Builder, the
string type is respected and gets saved to the server.
However, when the JSON table renderer processes the dataset, it reads
the value of the column as a string and renders strings. Then when SQL
Server Mobile Report Publisher fetches the table, it also only sees
strings.
The official workaround is also explained there:
The workaround for this is to add a calculated member when you're
creating a shared dataset in Report Builder.
An example of such a calculated member (taken from here):
cdate(format([Date].[Date].CURRENTMEMBER.MEMBER_VALUE, "yyyy-MM-dd"))
But this workaround (as pointed out by Victor_Rocca in a comment to the official Microsoft documentation) has a big problem:
The downside to this is that it will return all the dates that have
been selected in your query, not just the ones that have a value
associated with it in your cube (essentially removing the NON EMPTY
behavior from the MDX query). When looking at a lot of records across
time, this could significantly impact your report performance.
So I think there's a better workaround which is creating a calculated field in the dataset:
with an expression such as this one (assuming the name of your field is 'Date' and that it returns unique names such as [Date Snapshot].[Date].[Date].&[2010-01-01T00:00:00] and [Date Snapshot].[Date].[Date].&[2010-12-31T00:00:00]:
=CDate(mid(Fields!Date("UniqueName"),33,10))
This way you don't have to manually edit your MDX and NON EMPTY behavior is preserved. This new field gets correctly identified as date datatype in Mobile Report Publisher:
In any case, help is on the way, according to this post based on information obtained at last Pass Summit (October 2016), the following is being developed:
Better support for generated MDX. The current date workaround will no
longer be needed and make everyone’s life easier.
As far as I understand - MDX query language does not contain the data type "Date", hence it will be converted to "String".
IBM support - MDX Data types
Wikipedia - MDX Data types
I had the same problem. You can go around this by creating a Member on the MDX query level using VBA functions:
WITH
MEMBER [Date] AS Cdate([Date Snapshot].[Date].CURRENTMEMBER.Name)
SELECT ...
Your snapshot date needs to have one of the suggested formats. If it does cdate() function should do the trick. SSRS Mobile Publisher will recognize it as a date instead of string.
I've encountered this before and in the tablix cell where I use one of these dates I used the following expression:
=format(cdate(Fields!Calendar_Day.Value),"dd MMM yy")
I ended up wrapping the MDX in a T-SQL query, using OPENQUERY. This is clunky as hell, especially when I needed to pass a parameter, but it allowed me to explicitly convert the result set to whatever datatype I wanted. Urgh.
The only way I managed to get MRP to recognize my date as a real date to filter trough time navigator was adding a calculated measure based on the MemberValue of the Date Dimension.
E.g.:
WITH MEMBER [Measures].[Date2] AS ([_Reference Period].[Date].CurrentMember.MemberValue)
And after that, adding the field [Measures].[Date2] to the measures of the query:
SELECT NON EMPTY { [Measures].[Date2] } ON COLUMNS, NON EMPTY { ([_ReferencePeriod].[Date].[Date].ALLMEMBERS * [Companies].[Company].[Company].ALLMEMBERS * [Contract].[Type].[Type].ALLMEMBERS * [Employee].[Gender].[Gender].ALLMEMBERS * [Situation Type].[Situation Type].[Situation Type].ALLMEMBERS ) } DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_VALUE, MEMBER_UNIQUE_NAME ON ROWS FROM [SOURCE] CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS

what is correct format to pass Today as work item createdDate parameter

I have created data set (as TFS drill through report) using TFSOlap data source which is displaying all bugs based on created date. Now, I want to pass date range as parameter from main report so drill through report displays data for the bugs created between given date range. I am getting error "string.. can not be converted to date type" I am using following formatting expression at drillthrough report parameter:
="[Work Item].[System_CreatedDate].&[" + FORMAT(Parameters!FromWorkItemSystemCreatedDate, "yyyy-MM-dd") + "T00:00:00]"
Please let me know what type of expression should be used to avoid error. I tried with using CDate() instead of FORMAT but still getting problem.

SSRS Espression for String to Date Time format

Hi I have a SQL Stored Procedure to for my SSRS Report.
Below is the query to fetch Date i used.
(CONVERT(CHAR(10), ENTER_DT, 103), '''')
I am not in a position to change this query. The Dat Format currently displayed is "DD/MM/YYYY".
I want to display the date format as Below using SSRS Expressions.
MM-DD-YYYY
Please guide me to do that using SSRS Expresions. PLease help.
Just change the Display Format instead of converting into TSQL (Stored Procedure), Follow the steps go to textBox Properties, Select Number & Category Custom then Custom Format whatever you want

SSRS Report Dataset Parameter format discovery when querying Tfs 2010 SSAS Cube

I'm very new to SSRS and Report Builder, and I'm trying to throw together a simple report that shows data for a single changeset. I've created a different report already that takes date paramaters, and thanks to some lucky googling, I learned that instead of feeding date strings to the report parameters, it was necessary to use expressions such as:
="[DATE].[Date].&[" & Format(CDate(Parameters!FromDateDate.Value),"yyyy-MM-dd") + "T00:00:00]"
Finding this post was like winning the lottery because I would have never been able to figure that out for myself. Hence, for my changeset report, I figured I could use the following expression for my changeset parameter.
="[Version Control Changeset].[Changeset ID].&[" & Parameters!VersionControlChangesetChangesetID.Value + "]"
For my report, the VersionControlChangesetChangesetID parameter is just an integer. I got the dimension names by using the "Copy" context menu item in the query designer, assuming that these would be the correct identifiers.
However, I get the following error when running the reoprt:
The Value expression for the query parameter ‘VersionControlChangesetChangesetID’ contains an error: Input string was not in a correct format. (rsRuntimeErrorInExpression)
I have two questions about this.
Why isn't the expression I wrote working?
How can I learn better how to format these values, and how they're formatted inside the cube, so that I'm not just guessing when I run into these formatting errors?
Thanks!