SSAS / MDX Query - Sort a CellSet - ssas

I have an MDX query where I am returning a list of products and the total sales value for that item from my SSAS cube, like so:
SELECT
NON EMPTY
{ [Item].Children } ON COLUMNS,
{ [Total Line Value] } ON ROWS
FROM [Sales Analysis]
What I would like to do is sort the results in a descending fashion (ie the part that has sold the most will be at the top, and so forth). Is this possible? Any help would be appreciated, thanks.

Try this
SELECT
NON EMPTY
Order([Item].Children, Measures.[Total Line Value], DESC) ON COLUMNS,
{ [Total Line Value] } ON ROWS
FROM [Sales Analysis]

Related

Simple Calculated Member Running for Forever - MDX

I am facing very strange issue with MDX (SSAS 2014), on which simplest calculated member is taking forever to execute. Could someone please help me to understand why i am facing this issue. If i not use calculated member everything works fast and result comes in seconds. When i remove Producer attribute, query performances well.
Below is the complete query.
WITH
MEMBER Measures.AsOfDate AS ("[Policy Effective Date].[Year-Month].[Date].&[2018-01-04T00:00:00]")
MEMBER Measures.YTDPremium AS AGGREGATE (YTD(STRTOMEMBER(Measures.AsOfDate)), [Measures].[Written Premium])
SELECT NON EMPTY
{
Measures.YTDPremium
} ON COLUMNS, NON EMPTY
{
(
[Program].[Program Name].[Program Name]
,[Insuring Company].[Insuring Company Name].[Insuring Company Name]
,[Line Of Business].[Line Of Business].[Line Of Business]
,[Producer].[Producer Name].[Producer Name]
)
} ON ROWS
FROM [Premium]
Try understand what the following part does in your query
NON EMPTY { ( [Program].[Program Name].[Program Name]
,[Insuring Company].[Insuring Company Name].[Insuring Company Name]
,[Line Of Business].[Line Of Business].[Line Of Business]
,[Producer].[Producer Name].[Producer Name]
) } ON ROWS
In the above MDX you are telling the server to take a cross product of all values of "Programs", "Line Of Business" and "Producer Name". So lets say you have 4 values of programs , 3 values of line of business and 2 values of producer name. The total combinations are 4*3*2=24
Now the "Non empty" removes any combinations that are not present in your dataset. This is done by removing all rows that have "Null" value in column value.
Your measure is returning value irrespective if that combination exists or not. You can modify your Calculatedmeasure to return value only in the case if the combination is valid. This can be achived by checking an actual measure for that combination
Edit: based the below example is based on the comment
In the below example i am trying to get the internet sales amount categories and components
select
{ [Measures].[Internet Sales Amount] }
on columns,
(
[Product].[Category].[Category],
[Customer].[Country].[Country]
)
on rows
from [Adventure Works]
Result
Now add "Non empty" to the query and observe the results.
Results
Now lets add calculted measure that returns "hello". Notice how the non empty clause is ineffective.
Now modify the code make the calculated measure check other measures for null
with member measures.t as
case when [Measures].[Internet Sales Amount] = null then null else "hello" end
select
{ [Measures].[Internet Sales Amount] ,measures.t }
on columns,
non empty
(
[Product].[Category].[Category],
[Customer].[Country].[Country]
)
on rows
from [Adventure Works]
Result
The bottom line: Because of cross product your result is so huge that SSAS is having hard time handling it.

MDX query picking columns on different condition

As shown in the below query,
SELECT
{[Measures].[Internet Sales Amount] } ON COLUMNS
,{([Product].[City].[City].ALLMEMBERS)} ON ROWS
FROM [Adventure Works]
Where ([Promotions].[City].&[Seattle],[Geography].[State-Province].&[WA]&[US])
This will list all the Product City names in row-wise and the column sales amount. Now I have to fetch the same column based on different City and State-Provine Name. So for that, I can repeat the query like this
SELECT
{[Measures].[Internet Sales Amount] } ON COLUMNS
,{([Product].[City].[City].ALLMEMBERS)} ON ROWS
FROM [Adventure Works]
Where ([Promotions].[City].&[LA],[Geography].[State-Province].&[CA]&[US])
Now I have these two queries returning same Product City names, but Two sales amount columns based on different conditions. I want to join these results like below
SELECT
{
[Measures].[Internet Sales Amount] EXISTS ([Promotions].[City].&[Seattle],
[Geography].[State-Province].&[WA]&[US]),
[Measures].[Internet Sales Amount] EXISTS ([Promotions].[City].&[LA],
[Geography].[State-Province].&[CA]&[US])
} ON COLUMNS
,{([Product].[City].[City].ALLMEMBERS)} ON ROWS
FROM [Adventure Works];
Now, this query should produce a single result with Product City names as rows and Col1 and Col2 of Internet Sales amount based on different conditions. But the problem is, the last query is giving me an error
"Parser: The syntax for 'Exists' is incorrect."
I am new to MDX, could someone please help me on how to join these results?
I was able to solve this problem based on the solution described here!
to-join-2-mdx-queries-with-same-hierarchy

MDX Query change column title

These are my first mdx queries. I would like to execute some queries from a C# application and convert the results to a JSON file or a tabular format.
So I need the columns titles, you can see in the picture in attachment the columns title is empty, how can I change them in the mdx query ?
SELECT { [Measures].[Nombre Inscrits] } ON COLUMNS,
{ ([ZONE CLIENT].[ID ZONE].[ID ZONE].ALLMEMBERS * [DimDate].[Year].[Year].ALLMEMBERS)}
ON ROWS FROM [Loisirs cube]
The empty cells are empty because in the result space if a name is given to them, it will not make sense, I would suggest you handle it in your code. However for the sake of learning you can use the below query , and ignore the first two columns of the result
with member
[measures].[ID ZONE] as
[ID ZONE].[ID ZONE].currentmember.name
[measures].[Year] as
[DimDate].[Year].currentmember.name
SELECT
{[measures].[ID ZONE],[measures].[Year], [Measures].[Nombre Inscrits] } ON COLUMNS,
{ ([ZONE CLIENT].[ID ZONE].[ID ZONE].ALLMEMBERS * [DimDate].[Year].[Year].ALLMEMBERS)}
ON ROWS FROM [Loisirs cube]
So here a sample on adventure works
SELECT
{ [Measures].[Internet Sales Amount] } ON COLUMNS,
{ ([Product].[Category].[Category].ALLMEMBERS * [Date].[Calendar Year].[Calendar Year].ALLMEMBERS)}
ON ROWS
FROM [Adventure Works]
Result
Now lets modify the query
with member
[Measures].[Category]
as
[Product].[Category].currentmember.name
member [Measures].[Year]
as
[Date].[Calendar Year].currentmember.name
SELECT
{ [Measures].[Category], [Measures].[Year],[Measures].[Internet Sales Amount] } ON COLUMNS,
{ ([Product].[Category].[Category].ALLMEMBERS * [Date].[Calendar Year].[Calendar Year].ALLMEMBERS)}
ON ROWS
FROM [Adventure Works]
Result

CLASSIC: named set in MDX top 2 by dimension

SSAS 2012 Multidimensional DW has
Measures: Line total,
Dimension with hierarchy: [Division] - [Sales
Person].
I am trying to make set of top 2 salespersons by divisions.
This code sample gives me the correct result in SSMS:
with set [f] as
Generate(
{ [Sales Person].[Hierarchy].[All] . children },
[Sales Person].[Division].CurrentMember *
TopCount(
EXISTING [Sales Person].[Sales Person Id].children
,2
,[Measures].[Line Total]
)
)
SELECT [Measures].[Line Total] ON COLUMNS,
[f]
ON ROWS from [Adventure Works]
it's working fine. It will give me top 2 by division.
However, if i want to make named set in cube based on previous MDX thus:
create dynamic set CURRENTCUBE.[f] as Generate(
{ [Sales Person].[Hierarchy].[All] . children },
[Sales Person].[Division].CurrentMember *
TopCount(
EXISTING [Sales Person].[Sales Person Id].children
,2
,[Measures].[Line Total]
)
)
it will give me no error but this named set will not show in the browser. Validation of the MDX code in the calculations tab passes ok.
You say it won't show in the browser, have you allocated it a display folder? If not, it may not be where you expect to see it.
Can you use it in an MDX query OK?

SSAS -> MDX -> Creating a column percentage within my query based on counts

SELECT
NON EMPTY {[Measures].[Fact Order Count]}
ON COLUMNS,
{ ([Front Manager].[Front Manager Id].[Front Manager Id].ALLMEMBERS * [Order Type].[Order Type].[Order Type].ALLMEMBERS ) }
ON ROWS
FROM
[TEST_DW] CELL PROPERTIES VALUE
So, I have three columns in the output:
Front Manager, Order Type, Order Count
The above query shows me the counts for each manager and order type combination. I need a fourth column which would be a percentage of the types of orders for each front manager.
So, if there are four types of orders (A, B, C, D), and a manager had 25 of each order totaling 100. The fourth column would read 25%.....
I have scoured the web on how to do this, but have really come up short on this one. Any direction on this would be greatly appreciated, I am definitely new to MDX. Thanks.
What you're looking for are MDX Calculated members.
Let's assume the member for order A is called : [Order Type].[Order Type].[Order A] and we want to calculate the percentage from the total.
WITH
MEMBER [Order A] AS ([Order Type].[Order Type].[Order A],[Measures].[Fact Order Count]) / ([Measures].[Fact Order Count]) , FORMAT_STRING = 'Percent'
SELECT
{[Measures].[Fact Order Count],[Measures].[Order A]} on 0
...
What is important in the calculated members is that you can evaluate any MDX tuple (e.g ([Order Type].[Order Type].[Order A],[Measures].[Fact Order Count]) ). This changing if needed the values coming from the pivot axis (defined in on 0 and on 1..). Note you can add calculated members for the measures as well as the other dimensions.