I would like to define a measure that will count dimension members by other dimension and add it as a measure on the cube.
Should I create a new measure group with a count of the attribute of dimension or it's better to create a calculated member?
Example on adventureworks: count products by ProductCategory
WITH
MEMBER [Measures].[Number of Products] AS
Count(Existing
[Product].[Product].[Product].Members
)
SELECT
[Measures].[Number of Products] on 0,
{[Product].[Category].Members} ON ROWS
FROM [Adventure Works]
You haven't given details, but my guess is that your current code will return the count of all products, repeated for each Product Category.
I don't have a working copy of Adventureworks to hand, but if Product is a level one level below Product Category in hierarchy "ProductHierarchy" on the Product dimension, this should work:
WITH MEMBER [Measures].[Number of Products] AS
Count([Product].[ProductHierarchy].CurrentMember.Descendants)
SELECT
[Measures].[Number of Products] ON 0,
{[Product].[Category].Members} ON 1
FROM [Adventure WOrks]
Related
Please consider this scenarios:
I have a cube with one Fact table and one measure called SalesAmount . Now I want to create a measure based on these 2 Selects:
Select 1:
Select [Measures].[SalesAmount]
From MyCube
Where [Product].[Color].[Gray]
and Select 2:
Select [Measures].[SalesAmount]
From MyCube
Where [Dates].[Calendar Year].&[2015]
The problem is in Add Calculate Member there is a box for simple formula. How can I create a measure for Select 1 + Select 2?
Thanks
I am not sure where are you looking at this Add Calculate Member option, but you could try something like this (Adventure Works database).
with member measures.[MyMeasure1]
as
([Measures].[Internet Sales Amount], [Date].[Calendar].[Calendar Year].&[2006])
member measures.[MyMeasure2]
as
([Measures].[Internet Sales Amount], [Product].[Color].&[Grey])
member measures.[MyMeasure12]
as
measures.[MyMeasure1] + measures.[MyMeasure2]
select {[Measures].[Internet Sales Amount], measures.[MyMeasure1], measures.[MyMeasure2] , measures.[MyMeasure12]} on 0
from [Adventure Works]
You can also create those members from Visual Studio Data Tools - in Calculations tab:
I have an MDX query which drills into a hierarchy - I need the count of a dimension for each level. I have tried a count function but it doesnt seem to work against each level:
member [RefIDCount] as distinctCount([Incident Details].[Process Safety Classification].&[Tier 1])
select
{
[RefIDCount]
} on columns,
{
DESCENDANTS(
[Reporting Hierarchies].[Hierarchy].[Reporting Category].&[49]
-- #RepCat#
)
} on rows
FROM [Monthly Stats]
This gives me the same count for each level :
What am I doing wrong? How do I get the count to be per level?
Take a look at the example below
with member measures.t
as
[Product].[Product Categories].currentmember.level.members.count
select
{[Measures].[Internet Sales Amount],measures.t}
on 0,
descendants([Product].[Product Categories].members
)
on 1
from
[Adventure Works]
Result
I am trying to better understand scopes and calculated members better, so in the AdventureWorks database I did the following:
I wrote this simple scope statement:
SCOPE([Customer].[Customer].[All Customers], [Measures].[Average Rate]);
/* This expression sets the value of the Amount measure */
THIS = 999;
END SCOPE;
And equivalent calculated member will be this:
Create Member CurrentCube.[Measures].[My Measure]
AS
iif([Customer].[Customer].currentmember IS [Customer].[Customer].[All Customers], 999, [Measures].[Average Rate]);
But I am not sure how to create calculated member that will be equivalent to this scope assignment:
SCOPE([Customer].[Country].members, [Measures].[Average Rate]);
/* This expression sets the value of the Amount measure */
THIS = 999;
END SCOPE;
Actually I am not sure how to write iff that will check if current member of Customer dimension is a member of [Customer].[Country]
Here you go
///Will Display 999 for any member apart from All
with Member [Measures].[My Measure]
AS
iif([Customer].[Country].currentmember.Properties ("Member_Value",TYPED)='All Customers',[Measures].[Internet Sales Amount] , 999)
select {[Measures].[Internet Sales Amount],[Measures].[My Measure]}
on columns,
[Customer].[Country].members
on rows
from [Adventure Works]
///Will display 999 for any member
with Member [Measures].[My Measure]
AS
iif([Customer].[Country].currentmember.Properties ("Member_Value",TYPED)='WhatEver', 999, 999)
select {[Measures].[Internet Sales Amount],[Measures].[My Measure]}
on columns,
[Customer].[Country].members
on rows
from [Adventure Works]
//Below queries help to check if a member is part of a hierarchy
///////////////////////////////////////////////////////////////////////////
//Query checks if a Member is part of a hierarchy
with MEMBER TestIFAValidMember
as
[Product].[Subcategory].[InvalidMember].UniqueName
MEMBER TestIFAValidMember2
as
[Product].[Subcategory].[Caps].UniqueName
select {TestIFAValidMember,TestIFAValidMember2}
on columns
FROM [Adventure Works]
You can also try
WITH MEMBER MEASURES.NotMember AS
IsSibling([Product].[Category].CURRENTMEMBER, [Product].[Category].[Invalid])
MEMBER MEASURES.ISMember AS
IsSibling([Product].[Category].CURRENTMEMBER, [Product].[Category].[Bikes])
SELECT {MEASURES.NotMember,MEASURES.ISMember} ON 0,
[Product].[Category].MEMBERS ON 1
FROM [Adventure Works]
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])
I am trying to create a dataset for an SSRS report as documented here:
http://sqlblog.com/blogs/stacia_misner/archive/2010/10/08/29249.aspx
The challenge is that I have multiple measures who's data I want to include in the measure column and I want to include the name of the measure in the RowValue column. So where the following query returns only data for measure "Sales Amount":
with
member [Measures].[Measure] as [Measures].[Sales Amount]
member [Measures].[RowValue] as [Product].[Category].CurrentMember.Name
member [Measures].[ColumnValue] as [Date].[Calendar Year].CurrentMember.Name
select {[Measures].[Measure], [Measures].[RowValue], [Measures].[ColumnValue]} on columns,
non empty ([Product].[Category].[Category].Members, [Date].[Calendar Year].[Calendar Year].Members) on rows
from [Adventure Works]
What I want to do is run the following type of query but have the data returned in the structure of the query above which would allow me to plug it into an SSRS report matrix:
WITH
MEMBER measures.SalesAmount AS [Measures].[Sales Amount]
MEMBER measures.CustomerCount AS [Measures].[Customer Count]
MEMBER measures.InternetFreightCost AS [Measures].[Internet Freight Cost]
SELECT [Date].[Calendar Year].[Calendar Year].Members ON COLUMNS,
{measures.SalesAmount,measures.CustomerCount,measures.InternetFreightCost} ON ROWS
FROM [Adventure Works]
Do any of the MDX ninjas know if this is even possible with MDX?
with member [Geography].[City].[Sales Amount] as 1
member [Geography].[City].[Customer Count] as 1
member [Geography].[City].[Freight Cost] as 1
member [Measures].[RowValue] as [Geography].[City].CurrentMember.Name
member [Measures].[ColumnValue] as [Date].[Calendar Year].CurrentMember.Name
member [Measures].[Measure] as
CASE
WHEN [Geography].[City].CurrentMember IS [Geography].[City].[Sales Amount]
THEN ([Measures].[Internet Sales Amount], [Geography].[City].[All Geographies])
WHEN [Geography].[City].CurrentMember IS [Geography].[City].[Customer Count]
THEN ([Measures].[Customer Count], [Geography].[City].[All Geographies])
WHEN [Geography].[City].CurrentMember IS [Geography].[City].[Freight Cost]
THEN ([Measures].[Internet Freight Cost], [Geography].[City].[All Geographies])
END
select {[Measures].[RowValue], [Measures].[ColumnValue], [Measures].[Measure]}
on columns,
{ [Geography].[City].[Sales Amount], [Geography].[City].[Customer Count], [Geography].[City].[Freight Cost]}
*
[Date].[Calendar Year].[Calendar Year].Members
having [Measures].[Measure] <> null
on rows
from [Adventure Works]
should deliver what you want. I used [Geography].[City] as an utility hierarchy. This can be any hierarchy unused in the query. I chose this one, as it is unrelated to both measure groups used in the query, and hence very unlikely to be used in any query. Some Cube designers create one or two one-member dummy dimensions in their cubes that are unrelated to any measure group, and can be used just like here in order to create calculated members on them.
One of the difficulties with ReportingServices queries is that measures must always be in the columns, and no other hierarchy may be in the columns. Hence, if we want to have the measures in the rows, we must move them to another hierarchy. This is done in two steps: First, we create dummy members on the utility hierarchy, and then map these to the measure needed in the CASE construct of the [Measures].[Measure] definition, where we need to use the default member of the utility dimension (in most cases the All member) in order to get something different than the 1 that I used for the dummy value.
Finally: non empty does not work properly with this construct, as [Measures].[RowValue] and [Measures].[ColumnValue] are never null. Hence I replaced it by HAVING, which can look at specific column values within the row.