Add another measure in MDX - ssas

MDX Query:
SELECT NON EMPTY
{ [Time].[Date].[Date].AllMembers } ON COLUMNS,
NON EMPTY
{ [Person].[Alias].[Alias].AllMembers } ON ROWS
FROM [Cube]
WHERE ( [Measures].[Count Person] )
Result Format:
2014-04-01 2014-04-02 2014-04-04
XYZ 14 36 68
What if I want to include another measure [Measures].[Person%] as a column and get result in following format:
Person% 2014-04-01 2014-04-02 2014-04-04
XYZ 10% 14 36 68
How can I arrange query?

You can use
SELECT NON EMPTY
{ ( [Measures].[Person%], [Time].[Date].[All]) }
+
{[Measures].[Count Person]} * [Time].[Date].[Date].AllMembers
ON COLUMNS,
NON EMPTY
{ [Person].[Alias].[Alias].AllMembers } ON ROWS
FROM [Cube]
just using two hierarchies (measures and time) in the columns.

Related

MDX: Distinct Count of members summarized by another dimension

From my cube, I am trying to get a distinct count of all non-empty [ID].[FullID]s but summarized by [Underlying].
I know that, for example, there are two IDs for [Underlying].[Underlying1] at this particular WHERE slice and I can see this by running the below MDX query, which clearly gives me a row for each (but a zero count?):
Results:
Underlying | FullID | CountOf
------------------------------
Underlying1 | ID1 | 0
Underlying1 | ID2 | 0
...
Code:
WITH
MEMBER CountOf AS
DistinctCount([ID].[FullID].Children)
SELECT
NON EMPTY {[Underlying].Children * [ID].[FullID].Children
} ON ROWS,
NON EMPTY {CountOf
} ON COLUMNS
FROM [MyCube]
WHERE ([Time].&[2018-11-27T00:00:00],
[Factor].[FactorName].[FACTOR1],
[Factor].[FactorType].[FACTORTYPE1]
[Location].[Location1]
)
However when I remove the * [ID].[FullID].Children I don't get what would like:
What I want:
Underlying | CountOf
---------------------
Underlying1 | 2
...
What I get:
Underlying | CountOf
---------------------
Underlying1 | 24
...
There is clearly something else going on here to give me a 24 count, but I cannot figure it out...
You are getting 24 because you measure is counting the members in [ID].[FullID].Children. What i understand is that you want to count the number of [ID].[FullID] who have a fact value availabe against them for [Underlying].Children. So your code should be like this
WITH
MEMBER CountOf AS
Count(
nonempty(([Underlying].currentmember,[ID].[FullID].Children),
[Measures].[ConnectingMeasure])
)
SELECT NON EMPTY {[Underlying].Children } ON ROWS,
NON EMPTY {CountOf} ON COLUMNS
FROM [MyCube]
WHERE ([Time].&[2018-11-27T00:00:00],[Factor].[FactorName].[FACTOR1],
[Factor].[FactorType].[FACTORTYPE1],[Location].[Location1]
)
Here is a sample of what you want to do in adventureworks. I am trying to count all Promotion, that are present for a product based on the internet sales data.
WITH
MEMBER CountOf AS
count(nonempty( ([Product].[Product].currentmember, [Promotion].[Promotion].children) ,[Measures].[Internet Sales Amount]))
SELECT
NON EMPTY {CountOf} ON COLUMNS,
NON EMPTY {
([Product].[Product].Children )
} ON ROWS
FROM [Adventure Works]
//Base query to understand what is counted
WITH
MEMBER CountOf AS
Count(nonempty( ([Product].[Product].currentmember, [Promotion].[Promotion].children) ,[Measures].[Internet Sales Amount]))
SELECT
NON EMPTY [Measures].[Internet Sales Amount] ON COLUMNS,
NON EMPTY {
([Product].[Product].Children,[Promotion].[Promotion].children )
} ON ROWS
FROM [Adventure Works]

How to merge two MDX query results having different measures and filters?

Let's start by saying that I'm a total newbie on MDX, I need to merge two (or more) query results into one pivot.
The queries will have the same dimensions on ROWS and COLUMNS, but different measures and filters (normally a time period).
Here is an example
Query 1:
SELECT
NON EMPTY {{[stores].[storecountry].[storecountry].Members}} ON COLUMNS,
NON EMPTY {{[SalesTypes].[Description].[Description].Members}} *
{[Measures].[TransactionValue], [Measures].[TransQty]} ON ROWS
FROM [Model]
WHERE ({[dDates].[Date].[Date].&[2016-01-05T00:00:00] : [dDates].[Date].[Date].&[2016-01-12T00:00:00]})
Result of query 1:
CA US
Regular Sale TransactionValue 761 16
Regular Sale TransQty 8 233
Return TransactionValue 156 4
Return TransQty 1 45
Query 2:
SELECT
NON EMPTY {{[stores].[storecountry].[storecountry].Members}} ON COLUMNS,
NON EMPTY {{[SalesTypes].[Description].[Description].Members}} *
{[Measures].[DiscountPerc]} ON ROWS
FROM [Model]
WHERE ({[dDates].[Date].[Date].&[2015-03-12T00:00:00] : [dDates].[Date].[Date].&[2015-06-02T00:00:00]})
Result of query 2:
CA US
Regular Sale DiscountPerc 40 % 59 %
Return DiscountPerc 32 % 43 %
Expected result after merging
CA US
Regular Sale TransactionValue 761 16
Regular Sale TransQty 8 233
Regular Sale DiscountPerc 40 % 59 %
Return TransactionValue 156 4
Return TransQty 1 45
Return DiscountPerc 32 % 43 %
Is it achievable without manually merging the AdomdClient.CellSet from the calling application?
Thank you!
I'd use calculated members:
with
Member [Measures].[TransactionValueReport] as
Aggregate(
{[dDates].[Date].[Date].&[2016-01-05T00:00:00]:[dDates].[Date].[Date].&[2016-01-12T00:00:00]},
[Measures].[TransactionValue]
)
Member [Measures].[TransQtyReport] as
Aggregate(
{[dDates].[Date].[Date].&[2016-01-05T00:00:00]:[dDates].[Date].[Date].&[2016-01-12T00:00:00]},
[Measures].[TransQty]
)
Member [Measures].[DiscountPercReport] as
Aggregate(
{[dDates].[Date].[Date].&[2015-03-12T00:00:00]:[dDates].[Date].[Date].&[2015-06-02T00:00:00]},
[Measures].[DiscountPerc]
)
Select
Non Empty [stores].[storecountry].[storecountry].Members on 0,
Non Empty [SalesTypes].[Description].[Description].Members * {[Measures].[TransactionValueReport],[Measures].[TransQtyReport],[Measures].[DiscountPercReport]} on 1
From [Model]

select data from olap MDX query

Please help to understand how MDX query works.
I have connected to cube using excel and construct a mdx query. In short finally I need to get the table like this:
12.01.2015
+-------+-------+-------+-----+-------+------
| 00:00 | 01:00 | 02:00 | ... | 23:00 | TOTAL
--------+-------+-------+-------+-----+-------+------
Ivan | null | 3 | null | ... | 12 | 38
Pert | 3 | 8 | null | ... | null | 125
Sidor |
We see the Date, Time (hour), FIO (Ivan, Petr etc) and values.
The cube has dimensions: Dim Date, Dim Hour, Dim Users
I trying to get MDX query from excel but I cant understand how to modify it to get result I need. Here is the request (formatted):
SELECT NON EMPTY
CrossJoin(
Hierarchize(
DrilldownMember(
{
{
DrilldownMember(
{
{
DrilldownLevel
(
{[Dim Date].[Даты YMD].[All]}
)
}
},
{[Dim Date].[Даты YMD].[Year Name].&[2015]}
)
}
},
{[Dim Date].[Даты YMD].[Year Name].&[2015].&[5]}
)
),
Hierarchize(
{
DrilldownLevel(
{[Dim Hour].[Hour Key].[All]}
)
}
))
DIMENSION PROPERTIES PARENT_UNIQUE_NAME,
[Dim Date].[Даты YMD].[Date Key].[Month Name],[Dim Date].[Даты YMD].[Date Key].[Year Name] ON COLUMNS ,
NON EMPTY
Hierarchize(
{
DrilldownLevel(
{[Dim Users].[FIO].[All]}
)
}
)
DIMENSION PROPERTIES PARENT_UNIQUE_NAME ON ROWS FROM
(
SELECT ({[Dim Date].[Даты YMD].[Year Name].&[2015].&[5]}
)
ON COLUMNS
FROM [Dwh Sorting])
WHERE ([Measures].[Fact Table Count]) CELL PROPERTIES VALUE,
FORMAT_STRING,
LANGUAGE,
BACK_COLOR,
FORE_COLOR,
FONT_FLAGS
It gets data for May 2015. My goal is to get a long table (with a lot of columns) like I showed. With one or more months.
Excel code that is autogenerated adds a lot of extra "bits".
Getting rid of the extra bits I'm guessing at something like the below. This script should give you lots of columns - assuming data for every hour in 2015 you should end up with 24 columns for every day in 2015!
The reason I say "guessing" as we do not know the hiearchical structure of your cube:
SELECT
NON EMPTY
Descendants
(
[Dim Date].[Даты YMD].[Year Name].&[2015]
,[Dim Date].[Даты YMD].[Date] //<<this assumes there is a level in Dim date called Date
)
*
{[Dim Hour].[Hour Key].MEMBERS} ON COLUMNS
,NON EMPTY
[Dim Users].[FIO].MEMBERS ON ROWS
FROM [Dwh Sorting]
WHERE
[Measures].[Fact Table Count];
If you need just the hours for a range of dates then try the : operator. You will not need to apply the Descendants function in this case:
SELECT
NON EMPTY
(
[Dim Date].[Даты YMD].[Date Key].[2014-01-02]
:
[Dim Date].[Даты YMD].[Date Key].[2015-02-10]
)
*
{[Dim Hour].[Hour Key].MEMBERS} ON COLUMNS
,NON EMPTY
[Dim Users].[FIO].MEMBERS ON ROWS
FROM [Dwh Sorting]
WHERE
[Measures].[Fact Table Count];

How can I select top 25 customers ordered by amount descending, for each dept?

I want top 25 customers ordered by amount descending, for each dept. like:
Dept|Customer|Amt
1 cust_1 5000
cust_2 2000
.
.
.
2 cust_26 6000
cust_27 3000
.
.
3
.
.
7
What I have tried so far is:
SELECT
NON EMPTY { [Measures].[Amount] } ON COLUMNS ,
NON EMPTY {
[Customer].[DEPT].[DEPT]*
ORDER(
TOPCOUNT([Customer].[Customer].[Customer],25,[Measures].[Amount]),
[Measures].[Amount], DESC)
}
FROM [cube]
I am getting result but not like 25 for each dept, though I have proper data.
Your second term of the cross join for the rows (ORDER(TOPCOUNT(...))) is not aware of the first ([Customer].[DEPT].[DEPT]). You should use Generate for this, as it loops over the departments, and thus allows you to put the current department into the first argument of the TOPCOUNT:
SELECT
NON EMPTY { [Measures].[Amount] } ON COLUMNS ,
NON EMPTY
Generate([Customer].[DEPT].[DEPT],
TOPCOUNT({[Customer].[DEPT].CURRENTMEMBER}
*
[Customer].[Customer].[Customer],
25,
[Measures].[Amount]
)
)
FROM [cube]
Also note that you do not need to order the result of TOPCOUNT descending, it is already ordered this way.

Filter non empty members on row

I have the following MDX query:
select
NON EMPTY [Measures].Members ON COLUMNS,
NON EMPTY {[MY_DIMENSION.MY_HIERARCHY].[VALUE].Members} ON ROWS
from
[MY_CUBE]
It gives the following result:
[Measures].[COUNT_TICKET]
-------------------------------------------------------------
[MY_DIMENSION.MY_HIERARCHY].[#null] 14 333 458 (<-- not needed)
[MY_DIMENSION.MY_HIERARCHY].[VAL1] 4 864
[MY_DIMENSION.MY_HIERARCHY].[VAL2] 5 588
[MY_DIMENSION.MY_HIERARCHY].[VAL3] 2 567
[MY_DIMENSION.MY_HIERARCHY].[VAL3] 4 500
Which takes a long time because there are a big number of null values out there.
Is it possible to filter the #null Members of my hierarchy?
Try the following
select
NON EMPTY {[Measures].Members} ON COLUMNS,
NON EMPTY {[MY_DIMENSION.MY_HIERARCHY].[VALUE].Members} ON ROWS
from
[MY_CUBE]