CURRENTMEMBER function gives an error in MDX - mdx

I have my dimension as below:
I want to get one market at a time.
When I use this query, I am getting the list of all the members as I am using .MEMBERS function:
SELECT [MARKET BASE].[Market Base].[Market Base].MEMBERS ON 1,
[Measures].[% OTC Sales] ON 0
FROM [PharmaTrend Monthly Ext];
But when I use the following query to get only the current member then I get an error saying: The CURRENTMEMBER function expects a hierarchy expression for the 1 argument. A member expression was used.
SELECT [MARKET BASE].[Market Base].[Market Base].CURRENTMEMBER ON 1,
[Measures].[% OTC Sales] ON 0
FROM [PharmaTrend Monthly Ext];
UPDATE:
When I use the below query, I get the result with All member:
WITH
MEMBER [Market] AS
[MARKET BASE].[Market Base].[Market Base].CURRENTMEMBER
SELECT [Measures].[% OTC Sales] ON 0,
[Market] ON 1
FROM [PharmaTrend Monthly Ext];
How can I go about resolving this?

CURRENTMEMBER is implicitly picked if you have any member from the hierarchy in scope and laid out on axis. By default it is the ALL member.
WITH MEMBER [Measures].[Market] AS
[MARKET BASE].[Market Base].CURRENTMEMBER.NAME
SELECT [MARKET BASE].[Market Base].[Market Base].MEMBERS ON 1,
{[Measures].[% OTC Sales], [Measures].[Market]} ON 0
FROM [PharmaTrend Monthly Ext];
This will return the name of the current market selected or on axis(in scope).

Just to be precise chaps - currentmember does not iterate over anything in mdx. None of the mdx I see in the original post is using any sort of iteration.
This function is as close as you get to a loop in mdx:
GENERATE
Also Filter can be thought of as an iterator.
CURRENTMEMBER is generally used in a WITH clause for claculations. What it does is pick up the current member for each member that is in context - this basically means what you see in the result table.

Related

MDX Scope to get denominator

I have a SSAS multidimensional cube and i am trying to calculate a denominator for a query by using a scope statement.
I am trying to assign the measure "Total SalesCumulative" the total value of all the sales for all the stores. I want this to stay being the total value and not get sliced when in a pivot table. The measure called Total Sales sums up all the sales for each store. I have gotten as far as the below but this just returns the value for each store and shows the same value as Total Sales.
SCOPE ([Measures].[Total SalesCumulative],[Dim Store].[Store Name].members);
THIS = ([Dim Store].[Store Name].[All],[Measures].[Total Sales]);
END SCOPE;
Has anyone got any suggestions how I can amend this?
I have checked your formula on Adventure Works database, and it seems correct. Just to be sure, you can check what you get with the query like the one below in SSMS (without making scope assignment):
with member measures.[All Order Count] as
([Measures].[Internet Order Count],[Product].[Model Name].[All Products] )
select {[Measures].[Internet Order Count] ,measures.[All Order Count] } on 0,
[Product].[Model Name].members on 1
from [Adventure Works]
Have you tried to clear cash before running the query after you made the change?
Secondly, maybe there is some other scope assignment in the script, that affects the same measure and dimension and overrides your formula, because the last scope assignment wins.
Try to add FREEZE(THIS) to your scope statement just to check if it will change the numbers:
SCOPE ([Measures].[Internet Order Count],[Product].[Model Name].members);
THIS = ([Product].[Model Name].[All Products] , [Measures].[Internet Order Count]) ;
FREEZE(THIS);
END SCOPE;

MDX Covariance per location

I'm trying to see how useful the Covariance function is in MDX. I have two measures, GP and order age. I'd like to compare the correlation between these two variables for every sales location. My current query returns the same value for all locations:
with
member [Measures].[r] as correlation( [Location].[Location Name].[Location Name].members, [Measures].[Order Age], [measures].[gp])
select [Measures].[r] on 0,
[Location].[Location Name].[Location Name].members on 1
from [DM OIP]
Where am I going wrong?
Your MDX is calculating the covariance of those two measures across all locations for every row. How about calculating the covariance of those two measures across months for the current location (the location on that row)?
with
member [Measures].[r] as correlation( [Date].[Month].[Month].members, [Measures].[Order Age], [measures].[gp])
select [Measures].[r] on 0,
[Location].[Location Name].[Location Name].members on 1
from [DM OIP]
Looks like you're missing a relationship.
So location is not related to one, or both of the measures used.

MDX calculation with children and parents (contribution to total)

I got a cube with cadastral information of some neighborhoods of a city. One of the measures is 'cadastral value': the value of the land of a neighborhood.
Now, the city is divided into districts and a district, into neighborhoods (hierarchy with two levels).
The question is: I need to calculate the percentage that the value of each neighborhood represents compared with the parent (the district) and with the total (city), and also the same about the district compared with the city.
How can I do it?
I tried:
WITH MEMBER
[Measures].[Percentage] AS
([Nbh].[Nbh].[Nbh],[Measures].[Cad value].CurrentMember
/[Nbh].[Nbh].[Nbh],[Measures].[Cad value].CurrentMember.Parent)
SELECT
{[Measures].[Percentage]} ON 0,
[Nbh].[Nbh].[Nbh].AllMembers ON 1
FROM Cadastre
Where the dimensions are:
Nbh : Neighborhood
Cad value: Cadastral value
What I get is "error" in every result value, and the explanation is "Current member () : argument (0) type mismatch: expected:'level|hierarchy|dimension', got:'measure'"
What am I doing wrong?
WITH MEMBER [Measures].[Percentage] AS
([Nbh].[Nbh].[Nbh],[Measures].[Cad value].CurrentMember
/[Nbh].[Nbh].[Nbh],[Measures].[Cad value].CurrentMember.Parent)
There are two problems with this:
One, you are using a static set to compute the value of calculated member namely:
[Nbh].[Nbh].[Nbh]
This is NOT [Nbh].[Nbh].CURRENTMEMBER
Second and the bigger mistake: Using the currentmember on a measure. You can't do that. You can use Measures.CURRENTMEMBER but that's about it. It's best used on hierarchies which are used on axes.
That said, your approach is almost legit. You need to fetch the measure value for the parent.
This should work:
WITH MEMBER
[Measures].[Percentage] AS
DIVIDE
(
[Measures].[Cad value],
([Nbh].[Nbh].CURRENTMEMBER.PARENT,[Measures].[Cad value]) //Cad value for parent
)
SELECT
{[Measures].[Percentage]} ON 0,
[Nbh].[Nbh].[Nbh].AllMembers ON 1
FROM Cadastre
Try the following:
WITH MEMBER [Measures].[Parent %] AS
IIF(
[Nbh].[Nbh].CURRENTMEMBER IS [Nbh].[Nbh].[All]
,1
,[Measures].[Cad value] /
(
[Measures].[Cad value],
[Nbh].[Nbh].CurrentMember.Parent
)
), FORMAT_STRING = 'Percent'
SELECT
{[Measures].[Parent %]} ON 0,
[Nbh].[Nbh].[Nbh].AllMembers ON 1
FROM Cadastre;
Including the IIF is a defensive measure. If you switch to showing AllMembers of the complete hierarchy then the All member will be included. Here is an example using AdvWrks:
WITH
MEMBER [Measures].[Parent %] AS
IIF
(
[Product].[Subcategory].CurrentMember
IS
[Product].[Subcategory].[All Products]
,1
,
[Measures].[Internet Sales Amount]
/
(
[Measures].[Internet Sales Amount]
,[Product].[Subcategory].CurrentMember.Parent
)
)
,FORMAT_STRING = 'Percent'
SELECT
NON EMPTY
{[Measures].[Parent %]} ON 0
,NON EMPTY
[Product].[Subcategory].ALLMEMBERS ON 1
FROM [Adventure Works];
The above returns the following:
Without the IIF the expression is the following:
WITH
MEMBER [Measures].[Parent %] AS
[Measures].[Internet Sales Amount]
/
(
[Measures].[Internet Sales Amount]
,[Product].[Subcategory].CurrentMember.Parent
)
,FORMAT_STRING = 'Percent'
SELECT
NON EMPTY
{[Measures].[Parent %]} ON 0
,NON EMPTY
[Product].[Subcategory].ALLMEMBERS ON 1
FROM [Adventure Works];
We get the following because of lack of foresight:
The error is certainly because of this statement is incorrect:
WITH MEMBER
[Measures].[Percentage]
AS ([Nbh].[Nbh].[Nbh],[Measures].[Cad value].CurrentMember/[Nbh].[Nbh].[Nbh],[Measures].[Cad value].CurrentMember.Parent)
Try to make it like this
WITH MEMBER
[Measures].[Percentage]
AS ([Nbh].[Nbh].[Nbh],[Measures].[Cad value])/([Nbh].[Nbh].[Nbh],[Measures].[Cad value])

MDX different expressions, same result

The following two statements return the same results in the cube, is one more optimized than the other or is there some compiler magic happening in the background?
CREATE MEMBER
CURRENTCUBE.[Measures].[Total Interest Expense]
AS
AGGREGATE(
EXISTING({([GL Income Statement Account].[Account Type].&[INTEREST EXPENSE])}),
[Measures].[Amount]
)
CREATE MEMBER
CURRENTCUBE.[Measures].[Total Interest Expense]
AS
(
[GL Income Statement Account].[Account Type].&[INTEREST EXPENSE],
[Measures].[Amount]
)
These are actually pretty much the same expressions. In the first expression, the Measures.[Amount] is aggregated over the set [GL Income Statement Account].[Account Type].&[INTEREST EXPENSE](which is actually a member). The EXISTING clause does not have any significance as you are making the set(first parameter to the AGGREGATE function) context independent by defining it as a member. In the second expression, the value of the measure is evaluated over the set(which) is again just a member. (Tuples comprising a measure are actually numeric in nature).

SSAS: Percent of Total not working in hierarchy

I have a simple olap cube - one set of measures and some unexciting dimensions.
I've add one calculation to get the "percent of total" sales against the gross sales measure. The code for this calculation is:
([Dim Stores].[Store Name].CurrentMember, [Measures].[Gross Sales])
/
([Dim Stores].[Store Name].Parent, [Measures].[Gross Sales])
This works.
Within the store dimension, there is a hierarchy called 'By State' where the stores are contained within.
Two questions please:
1. Any idea why the calculation would not work when I use the the 'By state' hierarchy i.e. the same calculation grouped by the next level up?
The state problem aside, any idea why my grand total shows an error even when I just use the Store Name?
TIA!
In poking around, I found a template within the "calculation tools" called "Percentage of Total". Using it, I translated my calculation to this:
Case
// Test to avoid division by zero.
When IsEmpty
(
[Measures].[Gross Sales]
)
Then Null
Else ( [Dim Stores].[By State].CurrentMember, [Measures].[Gross Sales] )
/
(
// The Root function returns the (All) value for the target dimension.
Root
(
[Dim Stores]
),
[Measures].[Gross Sales]
)
End
It worked!