AverageOfChildren in MDX - ssas

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:

Related

Create Calculate Measures in SSAS

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:

calculate the cumulative % of a hierarchy member in icCube | TopPercent but than different

With the function TopPerent, you can get the set of members - ordered top down - that comply to the % value provided.
I would like to switch this function and get the cumulative % given the member.
Description of the image:
TopPercent in icCube, all countries in Excel ordered top down on Amount. Cum % caluclated. The BLUE highlighted values indicate the value I would like to obtain
I hope it will be enough to understand the point:
With
Set [OrderedCity] as
Order([Customer].[City].[City].Members,[Measures].[Internet Sales Amount],DESC)
Member [Measures].[Cum] as
Sum(
Head([OrderedCity],Rank([Customer].[City].CurrentMember,[OrderedCity])),
[Measures].[Internet Sales Amount]
)
Member [Measures].[Cum %] as
[Measures].[Cum] / ([Customer].[City].[All],[Measures].[Internet Sales Amount]),
Format_String = "Percent"
Select
Non Empty [OrderedCity] on 1,
{[Measures].[Internet Sales Amount],[Measures].[Cum],[Measures].[Cum %]} on 0
From [Adventure Works]

MDX Record Count

Can any one tell me how to get the record count that is a result of a MDX query?I have tried various methods and I haven't really got a solution for that.I am a beginner in MDX queries.
WITH
MEMBER [Measures].[amount] as
COUNT(
[your_dimension].[your_dimension_property].Members
)
SELECT {[Measures].[amount]} ON COLUMNS
FROM [your_awesome_cube]
A code like this will return to you the amount of members in your dimension, the COUNT method have this syntax
Count(Set_Expression [ , ( EXCLUDEEMPTY | INCLUDEEMPTY ) ] )
so you can do a lot of things, like filter your search
Create a measure in the cube that is a count or distinct count.
1) open the cube
2) Right click on the fact table on which that measure sits
3) Select New Measure...
4) Dropdown the list and select the aggregation
5) In the source column section, select the column you want the aggregation on (if u cant find it, click on show all columns on the bottom- this depends on what you are aggregating)
with member
Measures.Counts as
[your_dimension].[your_dimension_property].Children.COUNT
select Measures.Counts on 0
FROM [your_awesome_cube]
When you say record count, you basicly are saying the valid combinations of your row axis.
Lets take a basic example, the query returns 3637 rows, of which 1 row is parctically the column name row.
select [Measures].[Sales Amount] on columns,
(
[Customer].[Country].[Country],
[Product].[Product].[Product]
) on rows
from [Adventure Works]
Now to get the row count without running the query, lets put the combinations in count function and put the count function in a runtime measure
This Returns 3636 row.
with member [Measures].[rowCount]
as
count(([Customer].[Country].[Country],[Product].[Product].[Product]))
select [Measures].[rowCount] on columns from [Adventure Works]
Notice I have not eleminated the null combinations on rows. Lets do that next
The query returns 2101 rows , again one row from column headers.
select [Measures].[Sales Amount] on columns,
non empty
(
[Customer].[Country].[Country],
[Product].[Product].[Product]
) on rows
from [Adventure Works]
Now lets count the rows
This returns 2100 rows.
with member [Measures].[rowCount]
as
count(nonempty(( [Customer].[Country].[Country],[Product].[Product].[Product])
,{[Measures].[Sales Amount]}
))
select [Measures].[rowCount]
on columns from [Adventure Works]
Till now we had measure from just one measure group, now lets try with multiple measure groups.
select {[Measures].[Sales Amount],[Measures].[Internet Sales Amount]} on columns,
non empty
(
[Customer].[Country].[Country],
[Product].[Product].[Product]
) on rows
from [Adventure Works]
//Cell set consists of 2101 rows and 3 columns.
//Wrong way
with member [Measures].[rowCount]
as
count(nonempty(( [Customer].[Country].[Country],[Product].[Product].[Product])
,{[Measures].[Internet Sales Amount]}
))
select [Measures].[rowCount] on columns from [Adventure Works]
//935
//Right way
with member [Measures].[rowCount]
as
count(nonempty(( [Customer].[Country].[Country],[Product].[Product].[Product])
,{[Measures].[Sales Amount],[Measures].[Internet Sales Amount]}
))
select [Measures].[rowCount]
on columns from [Adventure Works]
///2100
Notice when we use just a single measure the result may not be correct . If the measure we use has a null value then the combination would be removed. Where as in our rows the other measure will ensure that the combination appears.
Now Lets add a filter to the picture.
select {[Measures].[Sales Amount],[Measures].[Internet Sales Amount]} on columns,
non empty
filter(
(
[Customer].[Country].[Country],
[Product].[Product].[Product]
)
,[Measures].[Internet Sales Amount]>5000) on rows
from [Adventure Works]
//Cell set consists of 586 rows and 3 columns.
//Wrong way
with member [Measures].[rowCount]
as
count(nonempty(( [Customer].[Country].[Country],[Product].[Product].[Product])
,{[Measures].[Sales Amount],[Measures].[Internet Sales Amount]}
))
select [Measures].[rowCount]
on columns from [Adventure Works]
//2100
//Right way
with member [Measures].[rowCount]
as
count(nonempty(
filter(([Customer].[Country].[Country],[Product].[Product].[Product]),[Measures].[Internet Sales Amount]>5000)
,{[Measures].[Sales Amount],[Measures].[Internet Sales Amount]}
))
select [Measures].[rowCount]
on columns from [Adventure Works]
///585
Again till i gave the RowCount measure the exact senario that I have on my row axis it fails.

List all previous Years and measure till today such that all 12 Month data is available for year.Else exclude the year

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.

MDX Scope Vs WIth member

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.