OrderBy MDX Queries? - ssas

This is my MDX Query.
select
{
[Measures].[Quantity],
[Measures].[Net Sales]
}
on columns,
NON EMPTY
{(
[Products].[Item Description].children,
[Calendar].[Date].[Date]
)}
on rows
from
(
select
{
[Calendar].[Date].&[2015-03-23T00:00:00],
[Calendar].[Date].&[2015-03-22T00:00:00],
[Calendar].[Date].&[2015-03-21T00:00:00]
}
ON columns
FROM [SalesReport])
This gives the sales records in quantity and net sales as expected. We need now to sort it to get the item which has the highest quantity in first. In other words, we're looking to apply 'descending' sort here.
I tried all order method which gives me error. Can you please help me to get my wish done?

Have you tried the below ?
select
{
[Measures].[Quantity],
[Measures].[Net Sales]
}
on columns,
NON EMPTY
ORDER((
[Products].[Item Description].children,
[Calendar].[Date].[Date]
), [Measures].[Quantity], DESC)
on rows
from
(
select
{
[Calendar].[Date].&[2015-03-23T00:00:00],
[Calendar].[Date].&[2015-03-22T00:00:00],
[Calendar].[Date].&[2015-03-21T00:00:00]
}
ON columns
FROM [SalesReport])
Second attempt [WORKING SOLUTION]
select
{
[Measures].[Quantity],
[Measures].[Net Sales]
}
on columns,
NON EMPTY
ORDER((
[Products].[Item Description].children
), [Measures].[Quantity], DESC)
*[Calendar].[Date].[Date]
on rows
from
(
select
{
[Calendar].[Date].&[2015-03-23T00:00:00],
[Calendar].[Date].&[2015-03-22T00:00:00],
[Calendar].[Date].&[2015-03-21T00:00:00]
}
ON columns
FROM [SalesReport])

If you wanted to further order the inner group you could do this:
SELECT
{
[Measures].[Quantity],
[Measures].[Net Sales]
}
ON 0,
NON EMPTY
GENERATE(
ORDER(
[Products].[Item Description].children
,[Measures].[Quantity], BDESC)
,
ORDER(
[Products].[Item Description].CURRENTMEMBER //<< possibly [Products].CURRENTMEMBER
*
{[Calendar].[Date].[Date].Members}
,[Measures].[Quantity], BDESC))
ON 1
FROM
(
SELECT
{
[Calendar].[Date].&[2015-03-23T00:00:00],
[Calendar].[Date].&[2015-03-22T00:00:00],
[Calendar].[Date].&[2015-03-21T00:00:00]
}
ON 0
FROM [SalesReport]
);

Related

MDX Combine two queries

I need two join two queries, I tried with members but it didn't work. I am new to MDX, please let me know if there a way of doing this. Error is due to list in the calculated member.
SELECT NON EMPTY { [RR TYPE].[Item].[Item].ALLMEMBERS * [Measures].[Unit Value] } ON COLUMNS,
NON EMPTY { ([FFS].[FFSCD].[FFSCD].ALLMEMBERS) } ON ROWS
FROM [GLCube]
WHERE ( [Location].[Site Name].&[Sandbox]
, [LT DT].[DATE HIERARCHY].[YEAR].&[2010]
,-{ [Ledger].[ID].&[A1],[Ledger].[ID].&[A2] ,[Ledger].[ID].&[A3]} )
SELECT NON EMPTY { [RT TYPE].[Item].[Item].ALLMEMBERS * [Measures].[Price Value] } ON COLUMNS,
NON EMPTY { ([FFS].[FFSCD].[FFSCD].ALLMEMBERS) } ON ROWS
FROM (SELECT [CCTYPE].[Desc].&[FCD] ON COLUMNS
FROM [GLCube])
WHERE ( [Location].[Site Name].&[Sandbox]
, [LT DT].[DATE HIERARCHY].[YEAR].&[2010])
Combined query
WITH MEMBER [Measures].[Unit Value]] AS
(-{[Ledger].[ID].&[A1],[Ledger].[ID].&[A2] ,[Ledger].[ID].&[A3]},[Measures].[Unit Value])
MEMBER [Measures].[Price Value] AS
( [CCTYPE].[Desc].&[FCD],[Measures].[Price Value] )
SELECT NON EMPTY {[Measures].[Unit Value],[Measures].[Price Value]} ON COLUMNS,
NON EMPTY { ([FFS].[FFSCD].[FFSCD].ALLMEMBERS} ON ROWS
FROM [GLCube]
WHERE ( [Location].[Site Name].&[Sandbox]
, [LT DT].[DATE HIERARCHY].[YEAR].&[2010])
Expected Output
WITH
MEMBER [Measures].[Filtered Unit Value] as
Aggregate(
-{ [Ledger].[ID].&[A1],[Ledger].[ID].&[A2] ,[Ledger].[ID].&[A3]},
[Measures].[Unit Value]
)
SELECT NON EMPTY
{
[RR TYPE].[Item].[Item].ALLMEMBERS
* {[RT TYPE].[Item].[All]}
* { [CCTYPE].[Desc].[All]}
* [Measures].[Filtered Unit Value]
}
+
{
{[RR TYPE].[Item].[All]}
* [RT TYPE].[Item].[Item].ALLMEMBERS
* { [CCTYPE].[Desc].&[FCD]}
* [Measures].[Price Value]
}
ON COLUMNS,
NON EMPTY { ([FFS].[FFSCD].[FFSCD].ALLMEMBERS) } ON ROWS
FROM [GLCube]
WHERE ( [Location].[Site Name].&[Sandbox]
, [LT DT].[DATE HIERARCHY].[YEAR].&[2010]
)

Filter MDX, SQL Server and Analysis Services

I have query like this one:
SELECT NON EMPTY {
[DIM CUSTOMER].[Country Region Code].[Country Region Code].ALLMEMBERS
}
ON COLUMNS,
NON EMPTY CROSSJOIN( {
[DIM PRODUCT].[Category Name].CHILDREN
},
{
[DIM PRODUCT].[Sub Category Name].CHILDREN
})
ON ROWS FROM [Foo]
WHERE (
[Measures].[Order Qty]
);
now I would like to add filtering, basically include only categories and subcategories where at least 10 different products were sold.
I end up with sth like below, but it returns nothing...
SELECT NON EMPTY {
[DIM CUSTOMER].[Country Region Code].[Country Region Code].ALLMEMBERS
}
ON COLUMNS,
FILTER (
CROSSJOIN( {
[DIM PRODUCT].[Category Name].CHILDREN
},
{
[DIM PRODUCT].[Sub Category Name].CHILDREN
},
{ [DIM PRODUCT].[Name].CHILDREN
}),
[DIM PRODUCT].[Name].CHILDREN.COUNT > 0
)
ON ROWS FROM [Foo]
WHERE (
[Measures].[Order Qty]
);
If someone is so proficient that it wouldn't take him 40 minutes to spot mistake like me, please help & thanks in advance :)
Instead of .CHILDREN you may need .members
Maybe swap this:
[DIM PRODUCT].[Name].CHILDREN.COUNT > 0
For this:
[DIM PRODUCT].[Name].CURRENTMEMBER.CHILDREN.COUNT > 0

Need to optimize MDX Query

I have built this query through BIDS Query Designer. But I want to optimize this query by removing SELECTS inside outermost FROM Clause and having only cube in from clause and rest filter on where or filter clause:
SELECT { [Measures].[User ID Distinct Count], [Measures].[Post Count], [Measures].[Like Count], [Measures].[Comment Count], [Measures].[Impressions Master Count] } ON COLUMNS,
NON EMPTY { ([Section Master].[Section Name].[Section Name].ALLMEMBERS * [Post Master].[Post Name].[Post Name].ALLMEMBERS * [Post Master].[SP Insert Date].[SP Insert Date].ALLMEMBERS ) } DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS
FROM ( SELECT ( STRTOSET(#SectionMasterSectionName) ) ON COLUMNS
FROM ( SELECT ( STRTOMEMBER(#FromDimDateDate) : STRTOMEMBER(#ToDimDateDate) ) ON COLUMNS
FROM [AnalyticsCube]))
CELL PROPERTIES VALUE, BACK_COLOR, FORE_COLOR, FORMATTED_VALUE, FORMAT_STRING, FONT_NAME, FONT_SIZE, FONT_FLAGS
I am quite new in MDX and don't know much in regards to MDX query optimization. And Because of that I also don't know that this is the Optimized version of query or there is still scope for optimization in this. My cube structure is shown in Image.
I have Update my old query by doing some changes in Row and Column Part, but still stuck with From Clause part
SELECT { [Measures].[User ID Distinct Count],
[Measures].[Post Count],
[Measures].[Like Count],
[Measures].[Comment Count],
[Measures].[Impressions Master Count] } ON COLUMNS,
NON EMPTY { ([Section Master].[Section Name].CHILDREN) *
([Post Master].[Post Name].CHILDREN, [Post Master].[SP Insert Date].CHILDREN ) } ON ROWS
FROM ( SELECT ( STRTOSET(#SectionMasterSectionName) ) ON COLUMNS
FROM ( SELECT ( STRTOMEMBER(#FromDimDateDate) : STRTOMEMBER(#ToDimDateDate) ) ON COLUMNS
FROM [AnalyticsCube]))
This would be the initial simplification:
SELECT
{
[Measures].[User ID Distinct Count]
,[Measures].[Post Count]
,[Measures].[Like Count]
,[Measures].[Comment Count]
,[Measures].[Impressions Master Count]
} ON COLUMNS
,NON EMPTY
{
[Section Master].[Section Name].Children
*
(
[Post Master].[Post Name].Children
,[Post Master].[SP Insert Date].Children
)
} ON ROWS
FROM
(
SELECT
StrToSet(#SectionMasterSectionName) ON 0,
StrToMember(#FromDimDateDate) : StrToMember(#ToDimDateDate) ON 1
FROM [AnalyticsCube]
);
Then it seems odd to me that you don't move StrToSet(#SectionMasterSectionName) into the main SELECT clause:
SELECT
{
[Measures].[User ID Distinct Count]
,[Measures].[Post Count]
,[Measures].[Like Count]
,[Measures].[Comment Count]
,[Measures].[Impressions Master Count]
} ON COLUMNS
,NON EMPTY
{
StrToSet(#SectionMasterSectionName)
*
(
[Post Master].[Post Name].Children
,[Post Master].[SP Insert Date].Children
)
} ON ROWS
FROM
(
SELECT
StrToMember(#FromDimDateDate) : StrToMember(#ToDimDateDate) ON 0
FROM [AnalyticsCube]
);
Logically it is slightly different but you could then think about using a WHERE clause rather than a SUB-SELECT:
SELECT
{
[Measures].[User ID Distinct Count]
,[Measures].[Post Count]
,[Measures].[Like Count]
,[Measures].[Comment Count]
,[Measures].[Impressions Master Count]
} ON COLUMNS
,NON EMPTY
{
StrToSet(#SectionMasterSectionName)
*
(
[Post Master].[Post Name].Children
,[Post Master].[SP Insert Date].Children
)
} ON ROWS
FROM [AnalyticsCube]
WHERE
StrToMember(#FromDimDateDate) : StrToMember(#ToDimDateDate);

How to get top 5 in mdx

How to get top 5 from mdx query?
I have a this query:
WITH SET [Geography].[City] AS
TopCount(
[Geography].[City]
,5
,[Measures].[Reseller Freight Cost]
)
SELECT
NON EMPTY {
CROSSJOIN(
{
[Date].[Calendar]
},
{
[Product].[Category]
}
),
CROSSJOIN(
{
[Date].[Calendar].children
},
{
[Product].[Category]
}
)
} DIMENSION PROPERTIES children_cardinality, parent_unique_name ON COLUMNS,
NON EMPTY {
[Geography].[City],
[Geography].[City].children
} DIMENSION PROPERTIES children_cardinality, parent_unique_name ON ROWS
FROM [Adventure Works]
WHERE (
[Measures].[Reseller Freight Cost]
)
But is not working.
Now i have error
error: {"faultstring":"Query (1, 21) Parser: The syntax for '.' is incorrect.","faultcode":"XMLAnalysisError.0xc10e0002"}
The best if I not must change a code after SELECT word .
When you create a set just declare it with no associated hierarchy so this is wrong [Geography].[City].
Also there is a much more readable syntax for cross-join - just use an asterisk *
Try this:
WITH SET [CitySet] AS
TopCount(
[Geography].[City]
,5
,[Measures].[Reseller Freight Cost]
)
SELECT
NON EMPTY {
[Date].[Calendar] * [Product].[Category]
,[Date].[Calendar].children * [Product].[Category]
} DIMENSION PROPERTIES children_cardinality, parent_unique_name ON COLUMNS,
NON EMPTY {
[CitySet], //<<changed here [Geography].[City],
[Geography].[City].children
}
DIMENSION PROPERTIES children_cardinality, parent_unique_name ON ROWS
FROM [Adventure Works]
WHERE (
[Measures].[Reseller Freight Cost]
)

ssas 2005 mdx get total

I have this MDX query
select
{
[Measures].[Sold value]
,[Measures].[Units]
,[Measures].[Sales baskets]
,[Measures].[ATV]
,[Measures].[AUT]
} on columns
, filter(
nonempty(
{[Branch].[Branch].&[5] *[Receipt No - Sales].[Receipt No].[Receipt No]}
),
[Measures].[Sold value] >= 50
) on rows
from Rmis
where [Time].[Day].&[20131218]
Which generates following result:
How can I get the total of these measures of the above result set? The total should use the aggregation defined in the cube.
with set [rows] as
filter(
nonempty(
{[Branch].[Branch].&[5] *[Receipt No - Sales].[Receipt No].[Receipt No]}
),
[Measures].[Sold value] >= 50
)
member [Receipt No - Sales].[Receipt No].[Total] as
aggregate([rows])
select
{
[Measures].[Sold value]
,[Measures].[Units]
,[Measures].[Sales baskets]
,[Measures].[ATV]
,[Measures].[AUT]
} on columns
,
{ ([Branch].[Branch].&[5], [Receipt No - Sales].[Receipt No].[Total]) }
+
[rows]
on rows
from Rmis
where [Time].[Day].&[20131218]