Using the Adventure works Cube, if I run the following code (from MS example):
with
Member [Measures].[Internet Sales Amount - Range]
AS aggregate ( [Date].[Fiscal].[Date].&[20080430]:[Date].[Fiscal].[Date].&[20080502] , [Measures].[Internet Sales Amount] )
SELECT
{ [Measures].[Internet Sales Amount - Range] } ON COLUMNS,
{ [Product].[Category].Members } ON ROWS
FROM
[Adventure Works]
You get the proper results.
However, when I implement similar functionality via the SCOPE command in a calculated tab:
scope ([Product].[Category], [Measures].[Internet Sales Amount]);
this = aggregate ( [Date].[Fiscal].[Date].&[20080430]:[Date].[Fiscal].[Date].&[20080502] , [Measures].[Internet Sales Amount] );
end scope;
and then run the following mdx query:
SELECT
{ [Measures].[Internet Sales Amount] } ON COLUMNS,
{ [Product].[Category].Members } ON ROWS
FROM
[Adventure Works]
I get vastly different results. The scope appears to be ignoring the date range or ignoring the category members, i'm not sure what. I am having a similar issue on my cube when creating a new scope with a date range.
Anyone know what's going on with the scope?
Use
CREATE MEMBER CurrentCube.[Measures].[Internet Sales Amount - Range2] AS NULL;
scope ([Measures].[Internet Sales Amount - Range2]);
this = aggregate ( [Date].[Fiscal].[Date].&[20080430]:[Date].[Fiscal].[Date].&[20080502] , [Measures].[Internet Sales Amount] );
end scope;
Otherwise, there might be an infinite recursion as you reference the measure inside the scope which you use in the SCOPE itself as well.
Related
We have a typical Date Dimension.
I am struggling to write an MDX to list the Dim.Year dimension member and measure contain data strictly for all month.(from Jan-Dec) otherwise exclude the year?.
This:
SELECT
[Measures].[Internet Sales Amount] ON 0
,[Date].[Calendar].[Calendar Year].MEMBERS ON 1
FROM [Adventure Works];
Returns this:
If I just now add in NON EMPTY I should get rid of 2009 and 2010:
SELECT
[Measures].[Internet Sales Amount] ON 0
,NON EMPTY
[Date].[Calendar].[Calendar Year].MEMBERS ON 1
FROM [Adventure Works];
Now returns:
The above is a very simple example.
Other scenarios might require us to get rid of nulls using other functions such as FILTER / NonEmpty / HAVING / IIF.
Edit
To just return years that have 12 months with data I've written the following (a wee bit more complicated than the above!). I will try to simplify later although you will definitely require an additional measure to achieve your goal,
WITH
MEMBER [Measures].[cntMths] AS
Count
(
NonEmpty
(
Descendants
(
[Date].[Calendar].CurrentMember
,[Date].[Calendar].[Month]
,SELF
)
,[Measures].[Internet Sales Amount]
)
)
SELECT
{
[Measures].[Internet Sales Amount]
} ON 0
,NON EMPTY
[Date].[Calendar].[Calendar Year].MEMBERS HAVING
[Measures].[cntMths] = 12 ON 1
FROM [Adventure Works];
Edit (for #sourav_agasti)
This is a solution against Adventure Works which only uses attribute hierarchies i.e. for a cube without a well formed Date dimension:
WITH
MEMBER [Measures].[cntMths] AS
Count
(
NonEmpty
(
[Date].[Calendar Year].CurrentMember
*
[Date].[Month of Year].[Month of Year].MEMBERS
,[Measures].[Internet Sales Amount]
)
)
SELECT
{} ON 0
,[Date].[Calendar Year].[Calendar Year].MEMBERS HAVING
[Measures].[cntMths] = 12 ON 1
FROM [Adventure Works];
This is the equivalent to Sourav's script using the above method:
WITH
MEMBER [Measures].[cntMths] AS
Count
(
NonEmpty
(
[Dim Year].[Year].CurrentMember
*
[Dim Month].[Month].MEMBERS
,[Measures].[Foo]
)
)
SELECT
{} ON 0
,[Dim Year].[Year].[Year].MEMBERS HAVING
[Measures].[cntMths] = 12 ON 1
FROM [Your Cube];
Further Edit
I chanced on an article last night which gives a more efficient method. Apparently Count - NonEmpty which I've done above is not tuned to work in block calculation mode whereas Sum -IIF is. So here is a quicker alternative using the later method:
WITH
MEMBER [Measures].[cntMths - S] AS
Sum
(
[Date].[Month of Year].[Month of Year].MEMBERS
,IIF
(
IsEmpty([Measures].[Internet Sales Amount])
,null
,1
)
)
SELECT
{[Measures].[cntMths - S]} ON 0
,[Date].[Calendar Year].[Calendar Year].MEMBERS HAVING
[Measures].[cntMths - S] = 12 ON 1
FROM [Adventure Works];
I took it as a challenge to write a code which gets you the result without there being any hierarchies anywhere. So here goes my attempt(after few hours of madness!).
You must be having a [Dim Month] dimension too. I am assuming the data there is stored like Dec-2014 or Dec 2014. Basically assuming that the last 4 digits give the year.
Now, one way you can figure out the years having data is to first list out all the months which don't have data and then figuring out the year for these months and then NOT displaying these years in the final output.
The below code does that for you:
WITH SET MonthsWithNoData AS
FILTER([Dim Month].[Month].CHILDREN, [Measures].[foo] = 0)
MEMBER [Measures].Year as
RIGHT([Dim Month].[Month].CURRENTMEMBER.member_value, 4)
SET SetOfYearsWithNoData AS
GENERATE(MonthsWithNoData, STRTOSET("[Dim Year].[Year].&[" + RIGHT(MonthsWithNoData.currentmember.member_value, 4) + "]"))
SET SetOfYearsWithData AS
([Dim Year].[Year].MEMBERS - SetOfYearsWithNoData)
member yearValues as
[Dim Year].[Year].currentmember.member_value
member wellFormedyearValues as
[Dim Year].[Year].currentmember.member_unique_name
SELECT {yearvalues, wellFormedyearValues} on 0,
SetOfYearsWithData on 1
from [Your cube]
I agree that the final output does not look too beautiful though. Having said that, a hierarchy would make the code much less complicated and the MDX would be much simpler. A [Calender] dimension with requisite hierarchies in place(as assumed in #whytheq's answer) would be the better approach in terms of elegance as well as performance.
This is the expression:
WITH
MEMBER [Date].[Calendar].[LastDate] AS
Tail(
nonempty(
[Date].[Calendar].MEMBERS,
[Measures].[Internet Sales Amount]
)
).Item(0)
MEMBER [Measures].[MeasureX] AS
[Date].[Calendar].[LastDate].member_value
SELECT
{
[Measures].[MeasureX]
} ON COLUMNS
FROM [Adventure Works];
The result is this:
How do I configure this so MeasureX returns the last date in the [Date].[Calendar] dimension?
The formula of a calculated member cannot return a member; you could use an icCube function for that ;-) but for SSAS perhaps creating a set of one member might do the trick for you. Then accessing the member could be done using the notation: mySet(0).
Hope that helps.
One of the functions being used in a code sample is averageofchilren.
What exactly the AverageOfChildren aggregate function actually does?
How can we compare this with Avg() function in MDX ?
Maybe a custom measure is created in your cube.
Using AdventureWorks try this:
Script 1
SELECT
{[Measures].[Reseller Sales Amount]} ON 0,
NON EMPTY
{[Geography].[Geography].[Country].&[Australia].CHILDREN} ON 1
FROM [Adventure Works]
WHERE ([Date].[Calendar Year].&[2007])
That results in this:
Say I wanted to create a measure that returned the average reseller sales amount per region of each country then I could do the following:
Script 2
WITH
MEMBER [Measures].[AvgOfChildren] AS
AVG(
[Geography].[Geography].CURRENTMEMBER.CHILDREN,
[Measures].[Reseller Sales Amount]
)
SELECT
{ [Measures].[Reseller Sales Amount],
[Measures].[AvgOfChildren]
} ON 0,
{[Geography].[Geography].[Country].MEMBERS} ON 1
FROM [Adventure Works]
WHERE ([Date].[Calendar Year].&[2007])
You can see from the results for Australia (211,857.58) that the AverageOfChildren is the average of the numbers returned by script 1:
If I run the following there’s a whole section of Customers that are (null) internet sales amount for both 2007 and 2008 – why is this? How do I use EXISTS to filter to just customers who have results for the years in the select
note: The choice of years could vary so InitialSet needs to be context aware hence I've used [Date].[Calendar Weeks].CURRENTMEMBER withing the EXISTS function.
WITH
SET [InitialSet] AS
EXISTS(
{[Customer].[Customer].[Customer].MEMBERS},
[Date].[Calendar Weeks].CURRENTMEMBER,
'Internet Sales'
)
SET [OrderedSet] AS
NONEMPTY
(
ORDER
(
[InitialSet],
[Measures].[Internet Sales Amount],
BDESC
)
,([Measures].[Internet Sales Amount],[Date].[Calendar Weeks].CURRENTMEMBER)
)
SELECT
{
[Date].[Calendar Weeks].[Calendar Year].&[2007],
[Date].[Calendar Weeks].[Calendar Year].&[2008]
} ON 0,
[OrderedSet] ON 1
FROM [Adventure Works]
WHERE (
[Measures].[Internet Sales Amount]
);
Sets are evaluated by Analysis Services by taking the WHERE clause into account, but not using any row or column settings. If you would like to evaluate the sets only for 2007 and 2008, you have to state that.
Actually, the [Date].[Calendar Weeks].CURRENTMEMBER in your first set will refer to the default member of the [Date].[Calendar Weeks] hierarchy, which is the All member.
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.