Sort measures where row and column axis are cross-joined in MDX - mdx

I've a table where there's two dimensions in the row axis crossjoined and two dimensions plus a measure crossjoined in the column axis. Eg:
I would like to sort the rows of a specific column, taking the image as an example I would like to sort all rows so the ones under France -> apples -> avg_sales are sorted. Eg:
I've been able to find many examples on how to do this when there's just a bunch of measures as columns (with no crossjoin) and many different dimensions crossjoined in the row axis but I'm not able to find anywhere how to solve this particular situation.
I tried something like the following:
SELECT CrossJoin(
[Country].[Country].members,
CrossJoin(
[Product Category].[Product].members,
{[avg_sales]}
)
) on columns,
Order(
Filter(
CrossJoin(
[Date].[Year].Members,
[Date].[Month].Members
),
NOT IsEmpty(avg_sales)
),
CrossJoin(
[Country].[Country].[France],
CrossJoin(
[Product Category].[Product].[apples],
{[avg_sales]}
)
),
BDESC
) ON ROWS
FROM [Main Cube]
But MDX Order() function signature doesn't allow it, it complains by saying:
Mondrian Error: No function matches signature 'Order(<Set>, <Set>, <Symbol>)'

This MDX expression on the row axis would do the trick:
ORDER(
CrossJoin([Date].[Year].Members, [Date].[Month].Members),
([Measures].[avg_sales], [Product Category].[Product].[apples]),
BASC
)
If you wanted to sort the months by sales but still keep the years together, then you could do this:
ORDER(
CrossJoin([Date].[Year].Members, [Date].[Month].Members),
[Date].[Year].CurrentMember.ORDERKEY,
BASC,
([Measures].[avg_sales], [Product Category].[Product].[apples]),
BASC
)
You could even sort the rows by the average sales across the selected countries and fruits on the column axis.
There's a free trial version of Pentaho EE which includes an OLAP client (Pentaho Analyzer) on Mondrian that generates these types of MDX expressions. You can use their Data Source Wizard to load your data, build a few reports and study the MDX to see how the ORDER MDX function is used.
https://www.hitachivantara.com/en-us/products/data-management-analytics/lumada-dataops/data-integration-analytics/download-pentaho.html

By the doc https://mondrian.pentaho.com/documentation/mdx.php
there is only 2 functions:
<Set> Order(<Set>, <Value>, <Symbol>)
<Set> Order(<Set>, <Value>)
The Order function must be some like this
Order(
Filter(
CrossJoin(
[Date].[Year].Members,
[Date].[Month].Members
),
NOT IsEmpty(avg_sales)
) *
CrossJoin(
[Country].[Country].[France],
CrossJoin(
[Product Category].[Product].[apples],
{[avg_sales]}
)
),
BDESC
)

Related

Number or Results and WITH MEMBER Calculation interacting weird

In our MDX Queries, we use a template like the following WITH MEMBER . SELECT SUBSET . FROM (SELECT . FROM .) a lot.
What this particular query should do (and does!) is list all product variants matching a filter, and give the sales amount for it for a month in time. On the same row, we have the sales total of all variants of this product.
We need only a subset of the result (for paging), and the results need to ordered to allow paging of course. Then there is a non empty, so only product variants that were sold in the time period qualify.
This works just fine. Paging also works: putting 30, 60, 90 for Start in SUBSET is gives the correct results.
However, if I put a 1 as Count (SUBSET(..., x, 1)) the result of the "Price all" goes to (null) for each and any row I select with x. If I return two or more rows it works just fine, and all rows have their correct values.
Two results and the correct value in the rightmost column (in case you are wondering how the total of all variants, 67, can be lower than the sales of one particular variant (280): I picked a bad example - there was a return (so a negative sale) for another variant of this product, for 213. The 67 is correct and desired!)
One Row and a (null) result
I do not understand how or why the SUBSET count would interfere with the Tuple I select in the WITH MEMBER. If it does it for 30 rows at a time, why not for one?
*edit
I just tried, what would happen if I filter in a way that only one result matches (and SUBSET paging is set to "working values" of 0, 30): The same as in the 2nd screenshot, for one result row the WITH MEMBER evaluates to (null).
So it is not that SUBSET is the problem, but more general: When there is only one result row!
Could this be a bug in our SQL Server Version? It is SQL 2012 with SP4 and all latest security patches.
WITH
MEMBER [DIM Date].[HI Year_Month_Day].[CURRENT_MONTH] AS
[DIM Date].[HI Year_Month_Day].&[20201215].Parent /* Select Month of December */
MEMBER [MEASURES].[Price all] AS (
(
[DIM Products].[HI Products].Currentmember.Parent
, [DIM Products].[HI Subproduct].[Total]
)
, [MEASURES].[Price]
), FORMAT_STRING = '#,0' /* Get the parent product total next to every subproduct */
SELECT {
([DIM Datum].[HI Year_Month_Day].[CURRENT_MONTH], [MEASURES].[Price])
, ([DIM Datum].[HI Year_Month_Day].[CURRENT_MONTH], [MEASURES].[Price All])
} ON COLUMNS
, {
SUBSET(
ORDER(
NONEMPTY(
([DIM Products].[HI Produt Subproduct].[Subproduct])
, {([DIM Date].[HI Year_Month_Day].[CURRENT_MONTH], [MEASURES].[Price])}
)
, ([DIM Date].[HI Year_Month_Day].[CURRENT_MONTH], [MEASURES].[Price])
, BDESC
)
, 0
, 30
)
} ON ROWS
FROM (
SELECT
{[DIM StoreLocation].[HI Country].[Country].&[US]}
* {[DIM Productgroup].[HI Productgroup].[Productgroup].&[Bikes]}
ON COLUMNS
FROM [Cube]
)

MDX sort order in Crossjoin

I want to show a list of callers we have missed calls from within a daterange. I want te result to be ordered by date. But I can't figure out how to do this.
My MDX statement:
With
Member [Measures].[Gemist] AS
sum(
except({[CM resultaat].[Resultaat].[CM resultaat].allmembers},
{[CM resultaat].[Resultaat].[CM resultaat].[answer],[CM resultaat].[Resultaat].[CM resultaat].[answer overflow]}),
[Measures].[SN Gesprekken]
)
Select
order([Measures].[Gemist],[Datum].[Datum].currentMember.value, ASC) on 0,
nonempty(crossjoin(Hierarchize([ServiceNummer ANI].[Ani]),[Datum].[Datum].[Dag]),[Measures].[Gemist]) on 1
FROM (SELECT {[datum].[datum].[dag].[2020-04-01]:[datum].[datum].[dag].[2020-04-28]} ON 0 FROM [Cube])
After some google searches I tried this to order the measure by date but also tried to order the crossjoin. Output stays te same, no order on date:
Anyone has a solution for this?
You need to order the vertical axis (i.e., the axis 1):
order(
nonempty(
crossjoin( [ServiceNummer ANI].[Ani], [Datum].[Datum].[Dag] )
),
[Datum].[Datum].currentMember.key,
BASC
) on 1
using the key (or name) of the current member of the [Datum] dimension.
Hope that helps.

MDX query to order (and topfilter) results after/with a crossjoin

I would like to order a set of results in an MDX query which also includes a crossjoin.
I have the following measures and dimensions:
[Measures].[Starts]
[Framework].[Framework Name]
[Framework].[Pathway Name]
I would like to create a list of the (corresponding) Framework and Pathway names that correspond to the top 25 numbers of [Measures].[Starts].
I have managed to output a FULL list of results using:
select [Measures].[Starts] on COLUMNS,
NON EMPTY CrossJoin(
Hierarchize({DrilldownLevel({[Framework].[Pathway Name].Children})}),
Hierarchize({DrilldownLevel({[Framework].[Framework Name].Children})})
) on ROWS
from [DataCube]
to create the following example output:
However, I need it to be sorted by the starts in descending order (and preferably only keep the top 25 results). I have tried almost everything and have failed. A google search didn't find any results.
Did you stumble across the TopCount function?
select [Measures].[Starts] on COLUMNS,
NON EMPTY
TopCount
(
CrossJoin
(
Hierarchize({DrilldownLevel({[Framework].[Pathway Name].Children})}),
Hierarchize({DrilldownLevel({[Framework].[Framework Name].Children})})
),
25,
[Measures].[Starts]
) on ROWS
from [DataCube]
Here's the msdn link.
H2H
For efficiency it is better to order the set before using the TopCount function:
WITH
SET [SetOrdered] AS
ORDER(
{DrilldownLevel([Framework].[Pathway Name].Children)}
*{DrilldownLevel([Framework].[Framework Name].Children)}
,[Measures].[Starts]
,BDESC
)
SET [Set25] AS
TOPCOUNT(
[SetOrdered]
,25
)
SELECT
[Measures].[Starts] on 0,
NON EMPTY
[Set25] on 1
FROM [DataCube];

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]
;