How to pass parameter in mdx query - mdx

How to pass parameter in MDX query.
Below is my mdx qyery----
SELECT
NON EMPTY FILTER({ ( [Tbl Electricity].[Transaction Id].[Transaction Id] ) }, [Measures].[From Year]=2011 and [Measures].[To Month]=[Measures].[To Month].&['+#month+' ]) ON ROWS,
NON EMPTY { [Measures].[From Year], [Measures].[To Year], [Measures].[To Month], [Measures].[From Month], [Measures].[Total Cost], [Measures].[Quantity] } ON COLUMNS
FROM [ECERM Dev1]
In above query, I am comparing [From Year] by 2011 and I would like to compare [To Month] dimension with my parameter #month.
By running above query I am getting below error

This depends on the client or reporting tool you're using. XMLA protocol support MDX parameters ( #month ).
However you need a client / library supporting this feature. What tool are you using ?

Related

Parser: The syntax for 'SCOPE' is incorrect

I'm learning MDX and having trouble with SCOPE.
As a test I'm trying to create a new measure only for Fiscal Year 2011 but
have a syntax error.
It isn't a Create Calculated Measure in SSAS Script but a query in SSMS.
Any help or pointers would be great.
WITH MEMBER [Measures].[Test Scope]
AS 1
(
SCOPE ([Measures].[Test Scope]);
SCOPE ([Date].[Fiscal Year].&[2011]);
THIS = [Measures].[Reseller Order Quantity] + 10000;
END SCOPE;
END SCOPE;
)
SELECT
{[Measures].[Reseller Order Quantity],[Measures].[Test Scope]} ON COLUMNS
,[Date].[Fiscal Year].[Fiscal Year].MEMBERS ON ROWS
FROM [Adventure Works]
Query (4, 2) Parser: The syntax for 'SCOPE' is incorrect.
You can’t use a scope statement in a query. It has to be defined in the MDX script of the cube or run in the context of a session. The most common way to handle this is the following.
WITH MEMBER [Measures].[Test Scope] AS
IIF(
[Date].[Fiscal Year].CurrentMember is [Date].[Fiscal Year].&[2011],
[Measures].[Reseller Order Quantity] + 10000,
NULL
)
SELECT
{[Measures].[Reseller Order Quantity],[Measures].[Test Scope]} ON COLUMNS
,[Date].[Fiscal Year].[Fiscal Year].MEMBERS ON ROWS
FROM [Adventure Works]
There is also very old MDX syntax CELL CALCULATION in an MDX query which I would recommend not using without through testing since it is infrequently used. Off the top of my head it would look like this. It could “scope” over any measure or calculated measure or tuple. Here we have scoped over a null calculated measure.
WITH MEMBER [Measures].[Test Scope] AS
NULL
CELL CALCULATION [Test Scope Calc]
FOR '([Measures].[Test Scope],[Date].[Fiscal Year].&[2011])'
AS '[Measures].[Reseller Order Quantity] + 10000'
SELECT
{[Measures].[Reseller Order Quantity],[Measures].[Test Scope]} ON COLUMNS
,[Date].[Fiscal Year].[Fiscal Year].MEMBERS ON ROWS
FROM [Adventure Works]

How to filter measure multiple times in cube

I need to product a report from my cube that looks something like the following.
(dummy data)
Where it lists sales and gross profit for today, this week, the period and year to date across the products category.
My cube is setup as follows
A date dimension
And the cube itself
Currently I have not implemented the product category pieces.
I'm struggling with how to write an MDX query that can return the sales/gross profit for a single day and then week and so on.
I can return it by itself like so
SELECT {[Measures].[Gross Profit],[Measures].[Price]} ON COLUMNS
From [Cube]
WHERE [Date].[Date Key].[2015-04-22];
and so on for the other various types (week etc), but I'm unsure as how to apply the where filter to the columnn itself rather than the overall query, or if this is even the correct way to do it and I should be making multiple MDX calls that I then compose in my app that will use this.
Can anyone give me a pointer in the right direction here?
EDIT: Seems to mostly work using the approach #Akshay Rane described however I cannot get one of my measures to work
MEMBER [This Week] as
(PeriodsToDate([Date].[Fiscal Week Date].[Fiscal Week],StrToMember('[Date].[Fiscal Week Date].[Date Key].&[' + '20140401' + ']'))
,[Measures].[Merchandise Gross Profit])
Gives me an error:
The function expects a string or numeric expression for the argument. A tuple set expression was used.
Any pointers here?
You will have to create separate Calculated Members for each time interval you want to aggregate the data upon.
This can be done in [Adventure Works] cube as follows.
WITH
MEMBER [Today] as
([Measures].[Internet Sales Amount], [Date].[Date].CURRENTMEMBER)
MEMBER [Last Week] as
AGGREGATE (
{ [Date].[Date].CURRENTMEMBER.lag(6) : [Date].[Date].CURRENTMEMBER }
, [Measures].[Internet Sales Amount]
)
SELECT
{ [Today], [Last Week] } ON 0,
{ ([Product].[Product Categories].[Category], [Date].[Date].[Date]) } ON 1
FROM
(
/*FILTERING ON SPECIFIC DATE USING SUBCUBE*/
SELECT [Date].[Date].&[20070715] ON 0
FROM [Adventure Works]
)
If you can take the different levels of date from the same user hierarchy then something like this is possible:
SELECT
{
[Date].[Calendar].[Month].&[2006]&[7]
,[Date].[Calendar].[Date].&[20050220]
}
*
{
[Measures].[Order Quantity]
,[Measures].[Internet Sales Amount]
} ON COLUMNS
,{[Product].[Category].Children} ON ROWS
FROM [Adventure Works];
The above produces results like this:

AdventureWorks, Cubes and Prev.Member

I am working through the Adventure Works tutorial on the MSDN site and making good progress.
I was trying to test myself and go off the guide to see if I was understanding the lessons and I've gotten myself a bit confused.
My intention was to use the Prev.Member MDX command so that in the Pivot Table when looking at sales, I can see the sales totals for the same period, the year before.
( [Measures].[Internet Sales Count]
, [Order Date].[Calendar Date].PrevMember
)
This is the expression I thought would work. Sadly, this just produces blank fields on the Pivot Table. The [Order Date].[Calendar Date] is taken from the date hierarchy that the guide advised to make.
Trying the expression [Order Date].[Date Key].PrevMember also returns blank cells.
The other code tried was using [Order Date].PrevMember but that just returns #VALUE!.
Try adding in Currentmember so that your expression knows what to apply PrevMemeber to:
(
[Measures].[Internet Sales Count]
, [Order Date].[Calendar Date].CurrentMember.PrevMember
)

How can I use and SSAS dimension to build a generic Year To Date measure?

I'm trying to emulate the method laid out by Russo, Ferrari and Webb in Expert Cube Development to build a dimension that reports (among other things) a Year To Date version of every measure in the cube, and I can't seem to make it work. I'm hoping someone here will have experience with this technique (or can point me to a better one) and can tell me where I'm going wrong.
I'm trying to translate this piece of MDX code that appears in the calculation section of their sample cube into my cube:
SCOPE (
[Date].[Calendar].MEMBERS,
[Date].[Date].MEMBERS );
( [DateTool].[Aggregation].[Year To Date] )
= Aggregate(
{ [DateTool].[Aggregation].DefaultMember } *
PeriodsToDate(
[Date].[Calendar].[Year],
[Date].[Calendar].CurrentMember
) );
END SCOPE;
Here is my version:
SCOPE (
[Time Order Date].[Fiscal Date].MEMBERS,
[Time Order Date].[Date].MEMBERS
);
( [Time Calculations].[Calculation].[Year to Date] )
= Aggregate(
{ [Time Calculations].DefaultMember } *
PeriodsToDate(
[Time Order Date].[Fiscal Date].[Year],
[Time Order Date].[Fiscal Date].CurrentMember
) );
END SCOPE;
This differs from the Russo et al version only by substituting our time dimension names for theirs.
FWIW, I have built a Time Calculations dimension following Russo et al's DateTool dimension.
The particular formulation that you see above returns "VALUE!" when I try to view it, and I don't quite see what might be wrong with it.
FWIW, if instead of trying to perform the aggregation, I simply assign the value of 10 in the above (i.e. I define the measure like this:
( [Time Calculations].[Calculation].[Year to Date] )
= 10
;
), just to see the substitution being made, I get the value "20". I have no idea what is going on double the static value.
Any help would be appreciated. --sw

A different twist on the aggregation of calculated measures in SSAS

I have to count something in a strange way, and it works in most cases -- both at leaves and at higher levels of aggregation across multiple dimensions. But it doesn't give correctly aggregated values for one specific dimension.
What I have at the moment is ...
CREATE MEMBER CURRENTCUBE.[Measures].[Active Commitments]
AS NULL,
FORMAT_STRING = '#,#',
VISIBLE = 1;
SCOPE (DESCENDANTS([Date Dimension].[Fiscal Year Hierarchy],,AFTER));
[Measures].[Active Commitments] =
iif([Constituent Activity].[Type].currentMember.Properties("Name")="Correspondence",
sum(([Commitment Dates].[Start Date], NULL: [Date Dimension].[Fiscal Year Hierarchy]), [Measures].[Commitment Count]),
sum(([Commitment Dates].[First Funded Date], NULL: [Date Dimension].[Fiscal Year Hierarchy]), [Measures].[Commitment Count]))
- sum(([Commitment Dates].[Stop Date],[Commitment].[Was Commitment Ever Active].[Sometime Active], NULL: [Date Dimension].[Fiscal Year Hierarchy]), [Measures].[Commitment Count]);
END SCOPE;
SCOPE (DESCENDANTS([Date Dimension].[Fiscal Year Hierarchy],,AFTER));
<Similar to above>
As you can see, the complexity is that one type of "commitment" commences at the [Start Date] and others commence at the [First Funded Date].
This fails whenever multiple members of [Constituent Activity] are selected because in such cases the use of currentMember in the SCOPE statement is invalid. For example, the following MDX executes successfully but outputs #Error --
select
[Measures].[Active Commitments] on columns
,[Date Dimension].[Fiscal Year Hierarchy].[Fiscal Year].&[2011\12] on rows
from Compass3
where
{[Constituent Activity].[Description].[XYZ]
,[Constituent Activity].[Description].[ABC]}
I think what I need to encode within the SCOPE statement is the recursive ...
if a single member of [Constituent Activity] is current
then use the calc as defined above
else use [Measures].[Active Commitments] = sum(all selected members of [Constituent Activity], [Measures].[Active Commitments])
... but how would I write that?
Do you have control all the way back to the data warehouse/data source view? Can you add a calculated field to your fact table, so you have [Commitment Active Date] at the cell level? Then you could do much simpler counts in the cube.
I have found that enforcing business rules and doing business calculations in the data warehouse is more efficient and easier in the long run.