MDX TOP 1 - Return TOP 1 from a CUBE - mdx

Is there a way I limit the values returned by an MDX query in a way similar to a SQL TOP 1 query.
SELECT TOP 1 Names
From tbl_Names
Order by names DESC
e.g. is the a MDX statement that could do the same?

You can use the TOPCOUNT and BOTTOMCOUNT functions. Basically you specify which dimension or set you want the TOP or BOTTOM n from and then specify some measure or numeric expression with which to order the resultset.

Try something like:
From the web..
WITH
SET [TCat] AS
TopCount([Product].[Subcategory].[Subcategory],10,[Measures].[Sales Amount])
MEMBER [Product].[Subcategory].[Other] AS
Aggregate([Product].[Subcategory].[Subcategory] - TCat)
SELECT {
[Measures].[Sales Amount]
} ON COLUMNS,
TCat + [Other] ON ROWS
FROM [Adventure Works]

Related

Getting results for even years only

MDX has a nice feature whereby I can specify a range of members:
SELECT ([Canada],[2006]:[2011]) on Rows,
[Measures].members on Columns
FROM [Sales]
Is there a way to calculate the set of even years: {[2006], [2008], [2010]}? I am looking for a way that would work for large sets, so that listing the years manually is not an option.
You can filter you function using a filter function, a declared function and MOD function (MOD returns the remainder from the division - like % in Java ) :
WITH
FUNCTION isEven(Value _number) as Mod( Int(_number) , 2 ) = 0
SELECT
FILTER( [Date].[Date].[Year] as t, isEven( Year( t.current.key) ) ) on 0
FROM [Cube]
If you are using this filter often you could create a FilterEven declared function once in the script (same for isEven() )
Try this. I used adventure works for the query.For the mod logic i took help from
Mod Logic
WITH
MEMBER [Measures].[Data Type] AS
[Date].[Day of Year].CurrentMember.Properties ("Member_Value",TYPED)
MEMBER [Measures].[IsEven] as
[Measures].[Data Type]-Int([Measures].[Data Type]/2)*2
select {[Measures].[Internet Order Count] }
on columns,
filter (
[Date].[Day of Year].[Day of Year],
[Measures].[IsEven]=0)
on rows
from [Adventure Works]
Plus you can have a column in the date dimension have 1,0 to indicate if the year is even or odd. Then simply use that column in the MDX query , no need to do all the above manipulations

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.

MDX Query - Select Columns From Same Dimensions

I have a requirement displaying data from same dimension in more than 1 column. For eg. I want to show data Year and Month wise. In my dimension structure, Year and Month belongs to same hierarchy. When I run below query I get error. PFB the query.
Select NON EMPTY {[Measures].[Target Actual Value]} ON 0,
NON EMPTY {[Realization Date].[Hierarchy].[Year Name].Members *
[Realization Date].[Hierarchy].[Month Year]} ON 1
From [Cube_BCG_OLAP]
The error I get is Query (2, 12) The Hierarchy hierarchy is used more than once in the Crossjoin function. I am new to MDX queries. Please help in this regard. Thanks in advance.
Select NON EMPTY {[Measures].[Target Actual Value]} ON 0,
NON EMPTY {[Realization Date].[Hierarchy].[Year Name].Members ,
[Realization Date].[Hierarchy].[Month Year]} ON 1
From [Cube_BCG_OLAP]
Instead of CROSSJOIN have a set as above. In a set, you can put members from same hierarchy
I like Sourav's answer - but it will put the results in one column which is slightly different than the question.
In AdvWorks this is in one column:
SELECT
[State-Province].MEMBERS ON COLUMNS
,{
[Date].[Calendar].[Calendar Year].MEMBERS
,[Date].[Calendar].[Month].MEMBERS
} ON ROWS
FROM [Adventure Works];
It is possible to switch to two columns and use a cross join but you need to find out the details of your Date dimensions Attribute hierarchies (as opposed to User hierarchies):
SELECT
[State-Province].MEMBERS ON COLUMNS
,
[Calendar Year].[All Periods].Children
* [Month].MEMBERS ON ROWS
FROM [Adventure Works];
In your cube maybe something like this:
SELECT
NON EMPTY
{[Measures].[Target Actual Value]} ON 0
,NON EMPTY
[Year Name].MEMBERS
*
[Month Year].MEMBERS ON 1
FROM [Cube_BCG_OLAP];

Select kpi with month on columns

I want to get two plant's oee per month in a specific year (2013), probably it's pretty trivial but I don't know how to do it:
with member [Measures].[OEE] as 'OEE'
select
{
[Equipment].[Plant Hierarchy].[Group].[DEMO_PLANT],
[Equipment].[Plant Hierarchy].[Group].[DEMO_PLANT2]
} on columns,
{
[Time].[Month]
} on rows
from ExpertPlan
where [Time].[Year].&[2013-01-01T00:00:00]
the select is ok (plants on columns and months on rows), but i'm not sure about the "with" usage to get the values be the [Measures].[OEE]
With is for custom calculated members and custom sets.
Add your measure to the slicer (WHERE clause)
SELECT
{
[Equipment].[Plant Hierarchy].[Group].[DEMO_PLANT]
,[Equipment].[Plant Hierarchy].[Group].[DEMO_PLANT2]
} ON COLUMNS
,{[Time].[Month]} ON ROWS
FROM ExpertPlan
WHERE
(
[Time].[Year].&[2013-01-01T00:00:00]
,[Measures].[OEE]
);
Here are a couple of prototyped ways of approaching this in AdvWrks cube:
SELECT
[Customer].[Country].&[United States] ON COLUMNS
,[Date].[Calendar].[Month] ON ROWS
FROM [Adventure Works]
WHERE
(
[Date].[Calendar Year].&[2007]
,[Measures].[Internet Sales Amount]
);
SELECT
[Customer].[Country].&[United States] ON COLUMNS
,Descendants
(
[Date].[Calendar].[Calendar Year].&[2007]
,[Date].[Calendar].[Month]
) ON ROWS
FROM [Adventure Works]
WHERE
[Measures].[Internet Sales Amount];
Both of the above result in this result cell set:
Adding on to the other answer, you can also employ a subselect.
SELECT
{
[Equipment].[Plant Hierarchy].[Group].[DEMO_PLANT]
,[Equipment].[Plant Hierarchy].[Group].[DEMO_PLANT2]
} ON COLUMNS
,{[Time].[Month]} ON ROWS
FROM
(
SELECT [Time].[Year].&[2013-01-01T00:00:00] ON 0
FROM [ExpertPlan]
)
WHERE
{[Measures].[OEE]}
In MDX, WHERE is the 3rd axis, although it is not too apparent. Think it to be like excel, where you have the rows, columns and values. You can put the hierarchies(attribute) on any of the axes([Measures] also behaves like any regular hierarchy). It will just change the way your final output will look like.

Change Dates to numbers

This is the script:
SELECT
{[Measures].[Internet Order Count]} ON COLUMNS
,Descendants
(
[Date].[Calendar].[Month].[August 2006]
,[Date].[Calendar].[Date]
,self
) ON ROWS
FROM [Adventure Works];
It returns this:
Can I change the script so that instead of dates it returns integers starting at either 0 or 1 i.e. the first would be 1 the second would be 2 etc.
This adds a counter but I'd like to get rid of the date column:
WITH
MEMBER [Measures].[r] AS
Rank
(
[Date].[Calendar].CurrentMember
,[Date].[Calendar].[Month].[August 2006].Children
)
SELECT
{
[Measures].[r]
,[Measures].[Internet Order Count]
} ON COLUMNS
,[Date].[Calendar].[Month].[August 2006].Children ON ROWS
FROM [Adventure Works];
This will be difficult, as what you have on the rows and columns are sets of tuples of members, either physical or calculated ones.
What you could do of course is this:
WITH Member [Date].[Calendar].[1] AS [Date].[Calendar].[Date].&[20060801]
Member [Date].[Calendar].[2] AS [Date].[Calendar].[Date].&[20060802]
...
Member [Date].[Calendar].[31] AS [Date].[Calendar].[Date].&[20060831]
SELECT
{
[Measures].[Internet Order Count]
} ON COLUMNS
,
{
[Date].[Calendar].[1],
[Date].[Calendar].[2],
...
[Date].[Calendar].[31]
}
ON ROWS
FROM [Adventure Works]
This may be feasible in case you generate the query with a tool.
However, in cases like this, I normally would keep the query like your second query, and just ignore the row headers in the client tool.