SSAS - Passing MDX Report Parameter to MDX DataSet Query - ssas

In my MDX report based on cube, the input help for date must be a calendar, therefore Date/Time type of parameter is essential. The field in time dimension by which I filter data is Integer. Example value: 20130827.
My dataset query looks like this:
SELECT NON EMPTY { [Measures].[Hours In Track] } ON COLUMNS, NON EMPTY {
([Dim Date].[Date ID].[Date ID].ALLMEMBERS * [Dim Division].[Hierarchy].[Division ID].ALLMEMBERS ) }
DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS FROM
( SELECT ( STRTOSET(#DimDivisionHierarchy, CONSTRAINED) ) ON COLUMNS FROM
( SELECT ( STRTOMEMBER(#FromDimDateDateID, CONSTRAINED) : STRTOMEMBER(#ToDimDateDateID, CONSTRAINED) ) ON COLUMNS FROM [BicepsArveCube]))
CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS
I used a text field, which showed me, that date/time type parameter value looks as follows:
2013-08-05 00:00:00, while my DateID is Integer type, so I need to do conversion.
When in dataset parameters tab I specify the parameter value as expression:
="[Dim Date].[Date ID].&["
& Replace(Replace("2013-08-05 00:00:00", "-", ""), " 00:00:00", "")
& "]"
I get the data as expected, everything works fine. But when I changed the hardcoded date/time value to parameter value (type date/time ):
="[Dim Date].[Date ID].&["
& Replace(Replace(Parameters!FromDimDateDateID.Value, "-", ""), " 00:00:00", "")
& "]"
I get a constraint violation error. I don't know why, because as written above, the Parameters!FromDimDateDateID.Value looks exactly the same as the hardcoded value I used.
I used text field to check what expression I get after conversion in both cases (hardcoded date and the same date chosen from calendar and passed as parameter value) and it looks exactly the same:
[Dim Date].[Date ID].&[20130805]

Try
STRTOMEMBER(
"[Dim Date].[Date ID].&["
+ Replace(Replace(#FromDimDateDateID, "-", ""), " 00:00:00", "")
+ "]",
CONSTRAINED)
In MDX - which is interpreted by Analysis Services - you cannot access Reporting Services objects like Parameters. Instead, their value is sent to the Analysis Services server along with the MDX statement, which can reference them with the # notation.

Related

How to insert a string literal as a new column in an mdx query

In the following MDX query, I want to set the value of [Measures].[Label] as the string literal "Net Value" instead of NULL (i.e. using a string literal to populate the values in the label column). I'm scratching my head about how to do this in MDX. Tons of background with SQL, but a relative newbie with SSAS.
`WITH
Member [Measures].[Label] AS NULL
Member [Measures].[Value] AS [Measures].[Net Amt]
SELECT NON EMPTY
(
{[Time].[Quarter].[Quarter]},
{[Time].[Month].[Month]},
{[Time].[Work Week].[Work Week]},
{[Customer].[Region Cd].[Region Cd]},
{[Product].[Cd Nm].[Cd Nm]}
) ON Rows,
NON EMPTY
(
{
[Measures].[Value],
[Measures].[Label]
}
) ON Columns
FROM [Reporting]
WHERE
(
{
[Time].[Year].[Year].&[2022]:[Time].[Year].[Year].&[2023]
},
{[Segment].[Segment Nm].[Segment Nm].&[SEG VALUE]}
)`
If I try a value in double quotes, the query just times out without finishing. Just using a null like this only takes 2 seconds to return.
Member [Measures].[Label] AS "Net Amt"
The problem with putting a constant is that the NON EMPTY now returns every combination of Time, Customer and Product.
Instead you want to return a constant but only on rows where the Net Amt measure is not empty.
Member [Measures].[Label] AS IIF(Not(IsEmpty([Measures].[Net Amt])),"Net Amt",Null)
Alternately you could use a constant but use the NonEmpty function instead against the Net Amt measure only:
Member [Measures].[Label] AS "Net Amt"
Member [Measures].[Value] AS [Measures].[Net Amt]
SELECT NonEmpty(
{
{[Time].[Quarter].[Quarter]}*
{[Time].[Month].[Month]}*
{[Time].[Work Week].[Work Week]}*
{[Customer].[Region Cd].[Region Cd]}*
{[Product].[Cd Nm].[Cd Nm]}
},
[Measures].[Net Amt]
) ON Rows,
{
[Measures].[Value],
[Measures].[Label]
}
ON Columns

Return data between two dates from a MDX Query SSAS

I'm trying to filter data between two date ranges. Its data type is datetime.
I have generated the query via the Query designer in SSAS.
Below is sample of the dataset I have:
Sample image of Measure groups and dimensions:
Sample Filter I have used:
Generated MDX Query:
`SELECT NON EMPTY { [Measures].[Status] } ON COLUMNS, NON EMPTY { ([Lobby].[Added Local Time].[Added Local Time].ALLMEMBERS ) } DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_VALUE, MEMBER_UNIQUE_NAME ON ROWS FROM ( SELECT ( [Lobby].[Added Local Time].&[2020-01-02T10:32:37.806667] : [Lobby].[Added Local Time].&[2020-02-19T13:43:13.833333] ) ON COLUMNS FROM ( SELECT ( { [Lobby].[Status].[All] } ) ON COLUMNS FROM [LTS KROI DEMO])) WHERE ( [Lobby].[Status].[All] ) CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS`
Problem:
Issue is that it doesn't filter the data according to the given datetime ranges. Neither gives any error.
If I use the only the Filter - Status a specific value without giving all it all works fine.
Please try the Filter function. It will be slower but should work since the approach you took only works if the exact date time exists
SELECT { [Measures].[Status] } ON COLUMNS, NON EMPTY {
Filter(
[Lobby].[Added Local Time].[Added Local Time].ALLMEMBERS,
[Lobby].[Added Local Time].CurrentMember.MemberValue >= CDate("2020-01-02 10:32:37.806667")
and [Lobby].[Added Local Time].CurrentMember.MemberValue <= CDate("2020-02-19 13:43:13.833333")
)
} DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_VALUE, MEMBER_UNIQUE_NAME ON ROWS
FROM [LTS KROI DEMO]
CELL PROPERTIES VALUE
I was able get the desired result by following the answer of
#GregGalloway by making a small change to the parameter I passed as
the date. When I removed time passing the date to the Cdate function
it work fine.
SELECT { [Measures].[Status] } ON COLUMNS, NON EMPTY {
Filter(
[Lobby].[Added Local Time].[Added Local Time].ALLMEMBERS,
[Lobby].[Added Local Time].CurrentMember.MemberValue >= CDate("2020-01-02")
and [Lobby].[Added Local Time].CurrentMember.MemberValue <= CDate("2020-02-19")
)
} DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_VALUE, MEMBER_UNIQUE_NAME ON ROWS
FROM [LTS KROI DEMO]
CELL PROPERTIES VALUE

MDX Function (Excel PowerPivot) to Exclude Non Numeric Values from DB

I am quite new to MDX and I am trying hard to write a query that allows me to retrieve only numeric values.
My query at present is:
SELECT NON EMPTY {ISNUMERIC([Measures].[Average Booking Window])}
ON COLUMNS, NON EMPTY {
([Stay Date].[Year].[Year].ALLMEMBERS *
[Stay Date].[Month of Year].[Month of Year].ALLMEMBERS )
} DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME
ON ROWS FROM (
SELECT ( [Booking Date].[Calendar].[Date].&[2018-01-01T00:00:00] :
[Booking Date].[Calendar].[Date].&[2018-08-31T00:00:00] )
ON COLUMNS FROM (
SELECT ( { [Hotel].[Market].&[Pisa City, Italy] } )
ON COLUMNS
FROM [MYCUBE]))
WHERE ( [Hotel].[Market].&[Pisa City, Italy])
CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR,
FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE,
FONT_FLAGS
The error I get is:
Query (1, 18) The function expects a tuple set expression for the 1 argument. A string or numeric expression was used.
What I am expecting is to get data only when it's numeric and I want the MDX query to completely exclude the rows with non numeric data.
Thank you in advance

Get Current Date in MDX Query

I am trying to get current date records and I have a query where a particular date is given , how can I put currentdate in that position.
I am new to MDX, if anyone answer that will be really helpful.
Below is the MDX query :
SELECT NON EMPTY { [Measures].[SHC] } ON COLUMNS, NON EMPTY { ([C].[RHC].[rhc].ALLMEMBERS ) } DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS FROM ( SELECT ( { [DHC].[DHC].&[01/01/1992] } ) ON COLUMNS FROM [TABULAR_EAL]) WHERE ( [DHC].[DHC].&[01/01/1992] ) CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS
I want to get records based on CurrentDate.
Something like this:
StrToMember('[DHC].[DHC].&[' + Format(Now(),'dd/MM/yyyy') + ']')
See my blogpost for more details.
I complete the answer :
With Member Measures.CurrentDate As
Format(
Now(),
'yyyyMMdd'
)
Member Measures.GetMemberCurrentDate as
'[Start Date].[Miladi Int Date].&['+ measures.CurrentDate + ']'
member Measures.GetCurrentDateBimehValue as
(StrToMember(Measures.GetMemberCurrentDate) ,[Measures].[BimehValue])
select Measures.GetCurrentDateBimehValue on columns
from [BimehCube]

MDX Date Formatting

Can any one please tell me how to format date in MDX queries? We dont use SSRS to generate report ,we have our own customised reporting tool built on SSAS.Date filter sends date in yyyy/mm/dd format . As of now we dont have a date dimension. My date member looks like:
[CNB_DimSampleInfo].[COAReleasedON].&[2013-01-02T03:20:00].
How can I format date in STRTOmemeber? I have tried doing this. My question is how will the value coming from user suit my member format as below. I know ssrs does it easily but we are not using SSRS. Below is my Code
my code
SELECT
[Measures].[Result] ON COLUMNS
,NON EMPTY
{
[CNB_DimProduct].[ProductUcode].[ProductUcode].ALLMEMBERS*
[CNB_DimProduct].[ProductDesc].[ProductDesc].ALLMEMBERS*
[CNB_DimTest].[TestUcode].[TestUcode].ALLMEMBERS*
[CNB_DimTest].[TestName].[TestName].ALLMEMBERS*
[CNB_DimSampleInfo].[LotNo].[LotNo].ALLMEMBERS*
[CNB_DimSampleInfo].[BatchNo].[BatchNo].ALLMEMBERS*
[CNB_DimSampleInfo].[COAReleasedBy].[COAReleasedBy].ALLMEMBERS*
[CNB_DimSampleInfo].[COAReleasedON].[COAReleasedON].ALLMEMBERS*
[CNB_DimSampleInfo].[SampleReferenceNo].[SampleReferenceNo].ALLMEMBERS*
[CNB_DimSampleInfo].[AnalysedBy].[AnalysedBy].ALLMEMBERS*
[CNB_DimSampleInfo].[AnalysedOn].[AnalysedOn].ALLMEMBERS
} ON ROWS
FROM
(
SELECT
StrToMember
(
"[CNB_DimSampleInfo].[COAReleasedON].[" + Format("2013-01-02","yyyy MM")
+ "]:STRTOMember([CNB_DimSampleInfo].[COAReleasedON].["
+
Format
("2013-01-02"
,"yyyy MM"
)
+ "]"
) ON COLUMNS
FROM Cube001
);
There is also StrToSet which is better in your circumstance as you're using the : operator which returns a set:
...
...
FROM
(
SELECT
StrToSet
(
"[CNB_DimSampleInfo].[COAReleasedON].[" + Format("2013-01-02","yyyy MM")
+ "]:[CNB_DimSampleInfo].[COAReleasedON].["
+
Format
("2013-01-02"
,"yyyy MM"
)
+ "]"
,CONSTRAINED
) ON COLUMNS
FROM Cube001
);
does the following definitely return the date formatted the same as your key values?
Format("2013-01-02","yyyy MM")
Do your key values for dates look like this?...
[CNB_DimSampleInfo].[COAReleasedON].[2013 01]
Your date member
[CNB_DimSampleInfo].[COAReleasedON].&[2013-01-02T03:20:00]
looks like a bad candidate for a user date parameter, as it's precise down to the minute (perhaps the second). Unless the user exactly matches the time, they'll get nothing. But perhaps you're enforcing exact matches by using a LimitToList select list in the UI.
To get this member name, you can format the input string like this:
format([Your Input Parameter],'yyyy-MM-ddThh:mm:ss')
I have tried out using filter as below
SELECT
[Measures].[Result] ON COLUMNS
,NON EMPTY
{
filter([CNB_DimSampleInfo].[COAReleasedON].members,instr([CNB_DimSampleInfo].[COAReleasedON].currentmember.member_caption,"2013-01-02")>0 or instr([CNB_DimSampleInfo].[COAReleasedON].currentmember.member_caption, "2013-04-01")>0)
*[CNB_DimProduct].[ProductUcode].[ProductUcode].ALLMEMBERS*
[CNB_DimProduct].[ProductDesc].[ProductDesc].ALLMEMBERS*
[CNB_DimTest].[TestUcode].[TestUcode].ALLMEMBERS*
[CNB_DimTest].[TestName].[TestName].ALLMEMBERS
} ON ROWS
FROM Cube002