Return Employee Count > 1 MDX SQL Server 2012 - sql-server-2012

I am trying to write a simple MDX query to get employee counts that are more than one. I am able to use filter in the rows but how can I get the query to only return values whose count is more than one?
select NON EMPTY {[Measures].[Employee Count]} ON COLUMNS,
[Employee].[Employee ID].[Employee ID] ON ROWS
from [Human Capital]

Try this:
WITH MEMBER [Measures].[Employee Count 2+] as
IIf(
[Measures].[Employee Count]>1,
[Measures].[Employee Count],
Null
)
select {[Measures].[Employee Count 2+]} ON COLUMNS,
NON EMPTY [Employee].[Employee ID].[Employee ID].Members ON ROWS
from [Human Capital]

Related

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.

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.

Getting a count of users each day in Mondrian MDX

I'm trying to write a query to give me the total number of users for each customer per day.
Here is what I have so far, which for each customer/day combination is giving the total number of user dimension entries without splitting them up by customer/day.
WITH MEMBER [Measures].[MyUserCount]
AS COUNT(Descendants([User].CurrentMember, [User].[User Name]), INCLUDEEMPTY)
SELECT
NON EMPTY CrossJoin([Date].[Date].Members, [Customer].[Customer Name].Members) ON ROWS,
{[Measures].[MyUserCount]} on COLUMNS
FROM
[Users]
The problem with your calculated member is that [User].CurrentMember is set to the All member for every row tuple, and thus the count is the total. What you need is a way for the [Customer].CurrentMember and [Date].CurrentMember to effectively filter the [User] dimension.
You need to use a measure that makes sense, i.e. that will have a non-empty value for meaningful joins of the dimension members that you're interested in.
To find this out, you could start by running a query like this:
SELECT
NON EMPTY CrossJoin(
[User].[User Name].Members,
[Measures].[Some measuse]
) ON COLUMNS,
NON EMPTY CrossJoin(
[Date].[Date].Members,
[Customer].[Customer Name].Members
) ON ROWS
FROM [Project]
You would have selected Some measure adequately. The results of that query will be a lot of empty cells, but in a given row, the columns that do have a value correspond to the Users that are related to a given Customer x Date tuple (on the row). You want to count those columns for every row. COUNT and FILTER are what you need, then the query with the calculated member will be
WITH MEMBER [Measures].[User count] AS
COUNT(
FILTER(
[User].[User Name].Members,
NOT ISEMPTY([Measures].[Some measure])
)
)
SELECT
NON EMPTY {[Measures].[User count]} ON COLUMNS,
NON EMPTY CrossJoin(
[Date].[Date].Members,
[Customer].[Customer Name].Members
) ON ROWS
FROM [Users]
I am assuming a fair bit here, but with some experimentation you should be able to work it out.

Can I reformulate this MDX query to use sets instead of an "And"?

with member [Measures].[BoughtDispenser] as
Sum(Descendants([Customer].[Customer].CurrentMember, [Customer].[Customer]),
Iif(
(IsEmpty(([Item].[ItemNumber].&[011074], [Measures].[Sale Amount]))
And IsEmpty(([Item].[ItemNumber].&[011069], [Measures].[Sale Amount]))
)
Or IsEmpty([Measures].[Sale Amount]),
0 , 1
)
)
select
{[Measures].[Sale Amount]} on columns,
non empty filter([Customer].[Customer].children, [Measures].[BoughtDispenser])
* {[Item].[ItemNumber].members}
on rows
from [Sales]
where [EnteredDate].[Quarter].&[2010-01-01T00:00:00]
;
The object is to show all the items purchased by customers who also bought either of the two dispensers (011069 and 011074).
I based the calculated member on a query I found to do basket analysis. I feel like there should be a way to write it with the set {[Item].[ItemNumber].&[011074], [Item].[ItemNumber].&[011069]} instead of the two IsEmpty tests. Everything I've tried ended up having every Customer in the result.
My environment is SQL Server Analysis Services 2005.
Yes I can! It just required a slightly different approach to the calculated member:
with member [Measures].[BoughtDispenser] as
Sum(Descendants([Customer].[Customer].CurrentMember, [Customer].[Customer])
* {[Item].[ItemNumber].&[011069], [Item].[ItemNumber].&[011074]},
[Measures].[Quantity Shipped]
)
select
{[Measures].[Sale Amount]} on columns,
non empty filter([Customer].[Customer].children, [Measures].[BoughtDispenser])
* {[Item].[ItemNumber].members}
on rows
from [Sales]
where [EnteredDate].[Quarter].&[2010-01-01T00:00:00]
;

MDX TOP 1 - Return TOP 1 from a CUBE

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]