How do we get the intersection of the following three sets?
//A
SELECT
{} ON 0,
EXISTS(
[Customer].[Customer].[Customer].MEMBERS
, {[Product].[Product Categories].[Category].&[1]}
, "Internet Sales")
ON 1
FROM [Adventure Works]
//B
SELECT
{} ON 0,
EXISTS(
[Customer].[Customer].[Customer].MEMBERS
, {[Product].[Product Categories].[Subcategory].&[2]}
, "Internet Sales")
ON 1
FROM [Adventure Works]
//C
SELECT
{} ON 0,
EXISTS(
[Customer].[Customer].[Customer].MEMBERS
, {[Product].[Product Categories].[Product].&[477]}
, "Internet Sales")
ON 1
FROM [Adventure Works]
Can the three members from the Product dimension get included one EXISTS function?
I could nest the use nested INTERSECTION around three EXISTS like the following but it seems very messy!...
SELECT
{} ON 0,
INTERSECT(
EXISTS(
[Customer].[Customer].[Customer].MEMBERS
, {[Product].[Product Categories].[Product].&[477]}
, "Internet Sales"),
INTERSECT(
EXISTS(
[Customer].[Customer].[Customer].MEMBERS
, {[Product].[Product Categories].[Category].&[1]}
, "Internet Sales"),
EXISTS(
[Customer].[Customer].[Customer].MEMBERS
, {[Product].[Product Categories].[Subcategory].&[2]}
, "Internet Sales")
)
)
ON 1
FROM [Adventure Works]
SELECT
{} ON 0
,Exists
(
Exists
(
Exists
(
[Customer].[Customer].[Customer].MEMBERS
,{[Product].[Product Categories].[Subcategory].&[2]}
,"Internet Sales"
)
,{[Product].[Product Categories].[Category].&[1]}
,"Internet Sales"
)
,{[Product].[Product Categories].[Product].&[477]}
,"Internet Sales"
) ON 1
FROM [Adventure Works];
Philip,
Related
This is my sql code - I want to convert it into an mdx query.
Also those results set use into power bi report.
I am unable to write this sql query into mdx query. Can anybody help me?
Select * From(
Select
dense_RANK()over(partition by t.MonthName order by t.amount desc)rank
,t.*
From(
Select DSP.SalesPointShortName ,dd.MonthName
,SUM(fs.salesAmount)Amount
From FactSales FS
INNER JOIN DimDate dd on fs.DateKey=dd.DateKey
INNER JOIN DimSalesPoint DSP on DSP.SalesPointID=FS.SalesPointID
group by dsp.SalesPointShortName ,dd.MonthName
)as t
)as f where f.rank=1
My expected output is:
Lots of google searching i have the answer from below link
After following this link desire results comes.
https://bipassion.wordpress.com/2013/06/22/mdx-dense-rank/
`
WITH SET [Sorted Models] AS
// For each month get Top 25 records, choosed a Top 25 from business case
Generate (
[Dim Date].[Month Name].[Month Name].Members,
TopCount
( nonempty(
{
[Dim Date].[Month Name].CurrentMember
* [Dim Sales Point].[SalesPoint].[Sales Point Short Name].MEMBERS
}
,[Measures].[Sales Amount]
)
, 1
,[Measures].[Sales Amount]
)
)
//Select
//[Measures].[Sales Amount] on 0,[Sorted Models] on 1
//From [MdCubeTest]
//where [Dim Product Group Item].[Sub Category Name].&[Bag]
MEMBER [Measures].[Rank] AS
// Get the Rank of current member Tuple to Current Month Set
// Do not use Sorted Models set due to dynamic for each set
Rank (
(
[Dim Date].[Month Name].CurrentMember
, [Dim Sales Point].[SalesPoint].CurrentMember
)
, Generate ( [Dim Date].[Month Name].CurrentMember,
TopCount
( nonempty(
{ [Dim Date].[Month Name].CurrentMember
* [Dim Sales Point].[SalesPoint].[Sales Point Short Name]
}
,[Measures].[Sales Amount]
)
, 25
,[Measures].[Sales Amount]
)
)
, [Measures].[Sales Amount]
)
MEMBER [Measures].[Previous Set Index] AS
// Get the Set Index using Rank
(
[Measures].[Rank] - 2
)
MEMBER [Measures].[Dense Rank] AS
// Get the Dense Rank using the Index position value
CASE
WHEN [Measures].[Rank] = 1
THEN 1
ELSE
(
[Sorted Models].Item([Measures].[Previous Set Index]),
[Measures].[Dense Rank]
)
+
Iif
(
(
[Sorted Models].Item([Measures].[Previous Set Index]),
[Measures].[Sales Amount]
)
=
[Measures].[Sales Amount]
,0
,1
)
End
SELECT
{
[Measures].[Sales Amount]
, [Measures].[Rank]
, [Measures].[Dense Rank]
//, [Measures].[Previous Set Index]
} ON rows
,NON EMPTY
{
// FILTER Can be used to get the TOP 3 using DENSE RANK
FILTER (
[Sorted Models]
, [Measures].[Dense Rank] <=1
)
} ON columns
FROM [MdCubeTest]
where [Dim Product Group Item].[Sub Category Name].&[Bag]`
I am trying to find out which year had the highest reseller sales across each of the State-Province. I am using Adventure Works DW 2008 R2 database.
Here is the query that I have got -
SELECT { [Reseller Sales Amount] } ON COLUMNS,
{
Generate (
[Geography].[Geography].[Country].Members,
TopCount (
Order (
Descendants (
[Geography].[Geography].CurrentMember
, [Geography].[Geography].[State-Province]
)
*[Date].[Calendar].[Calendar Year].Members,
[Reseller Sales Amount],
DESC
),
1
)
)
} ON ROWS
FROM [Adventure Works]
When it comes to France it's displaying (null) as result set. Also why is it not listing all the State-Provinces but only select few of them? Is it also possible to get the Country alongside the results. When I tried another cross-join I get error saying Geography hierarchy already exists.
To get rid of the null you can use BDESC rather than DESC. The extra B means that all natural hierarchy order will be Broken
SELECT { [Reseller Sales Amount] } ON COLUMNS,
{
Generate (
[Geography].[Geography].[Country].Members,
TopCount (
Order (
Descendants (
[Geography].[Geography].CurrentMember
, [Geography].[Geography].[State-Province]
)
*[Date].[Calendar].[Calendar Year].Members,
[Reseller Sales Amount],
BDESC //<<<
),
1
)
)
} ON ROWS
FROM [Adventure Works];
To add in country and avoid the error that you are seeing use the unused attribute hierarchy [Geography].[Country] rather than the user hierarchy [Geography].[Geography] which you already have on the 1 axis:
SELECT
{[Reseller Sales Amount]} ON COLUMNS
,{
Generate
(
[Geography].[Geography].[Country].MEMBERS
,TopCount
(
Order
(
[Geography].[Country].[Country]*
Descendants
(
[Geography].[Geography].CurrentMember
,[Geography].[Geography].[State-Province]
)*
[Date].[Calendar].[Calendar Year].MEMBERS
,[Reseller Sales Amount]
,bDESC
)
,1
)
)
} ON ROWS
FROM [Adventure Works];
Results are the following:
We will explore the universal option realization of TOP-filters on the levels.
TOP-rules can be set for every level.
It is used for detailed of analysis of data (Expand, Drilldown).
Aggregates are formed based on TOP.
Setting the Sort order will do, as long as the data is sorted in the
most Pivot Table.
SELECT
{ [Measures].[Reseller Sales Amount] }
DIMENSION PROPERTIES PARENT_UNIQUE_NAME , HIERARCHY_UNIQUE_NAME , CUSTOM_ROLLUP , UNARY_OPERATOR , KEY0 ON 0
, ORDER
( HIERARCHIZE
( HIERARCHIZE ( [Geography].[Geography].Levels ( 0 ).Members )
)
, ([Measures].[Reseller Sales Amount])
, BDESC
)
DIMENSION PROPERTIES PARENT_UNIQUE_NAME , HIERARCHY_UNIQUE_NAME , CUSTOM_ROLLUP , UNARY_OPERATOR , KEY0 ON 1
FROM
( SELECT
{ Generate
( [Geography].[Geography].Levels ( 3 ).Members
, TopCount
( Filter
( [Geography].[Geography].CurrentMember.Children
, NOT IsEmpty ( [Measures].[Reseller Sales Amount] )
)
, 3
, [Measures].[Reseller Sales Amount]
)
)
} ON COLUMNS
FROM
( SELECT
{ Generate
( [Geography].[Geography].Levels ( 2 ).Members
, TopCount
( Filter
( [Geography].[Geography].CurrentMember.Children
, NOT IsEmpty ( [Measures].[Reseller Sales Amount] )
)
, 3
, [Measures].[Reseller Sales Amount]
)
)
} ON COLUMNS
FROM
( SELECT
{ Generate
( [Geography].[Geography].Levels ( 1 ).Members
, TopCount
( Filter
( [Geography].[Geography].CurrentMember.Children
, NOT IsEmpty ( [Measures].[Reseller Sales Amount] )
)
, 3
, [Measures].[Reseller Sales Amount]
)
)
} ON COLUMNS
FROM
( SELECT
{ Generate
( [Geography].[Geography].Levels ( 0 ).Members
, TopCount
( Filter
( [Geography].[Geography].CurrentMember.Children
, NOT IsEmpty ( [Measures].[Reseller Sales Amount] )
)
, 3
, [Measures].[Reseller Sales Amount]
)
)
} ON COLUMNS
FROM
( SELECT
{ [Geography].[Geography].Levels ( 0 ).Members } ON COLUMNS
FROM [Adventure Works]
)
)
)
)
)
CELL PROPERTIES BACK_COLOR , CELL_ORDINAL , FORE_COLOR , FONT_NAME , FONT_SIZE , FONT_FLAGS , FORMAT_STRING , VALUE , FORMATTED_VALUE , UPDATEABLE , ACTION_TYPE
I'm using the following:
WITH
SET [myset] AS
Order
(
{
[Customer].[Country].[Country].MEMBERS
*
[Customer].[Customer].[Customer].MEMBERS
}
,[Measures].[Internet Sales Amount]
,ASC
)
MEMBER [Measures].[rank] AS
Rank
(
(
[Customer].[Country].CurrentMember
,[Customer].[Customer].CurrentMember
)
,[myset]
)
MEMBER [Measures].[newrank] AS
IIF
(
[myset].Item(
[Measures].[rank] - 1).Item(0).Name
<>
[myset].Item(
[Measures].[rank] - 2).Item(0).Name
,1
,
([myset].Item([Measures].[rank] - 2),[Measures].[newrank]) + 1
)
SELECT
{
[Measures].[Internet Sales Amount]
,[Measures].[rank]
,[Measures].[newrank]
} ON 0
,[myset] ON 1
FROM [Adventure Works];
It is the work of Amish Shah: http://blog.sqltechie.com/2010/03/rank-with-partitioning-mdx.html
Currently it does not give tuples that have the same value the same rank e.g. if x has an Internet Sales Amount of 10 and y also has 10, in the above they will not have the same rank.
Can the above be changed so that if they have the same value then they have the same value for [Measures].[rank] - and also the same value for [Measures].[newrank]?
(adding a third argument into the definition of [Measures].[rank] doesn't seem like a possible route to proceed as the Rank function then goes into cel-by-cell calculation mode)
How about using recursive calculated members like so -
WITH
SET [myset] AS
Order
(
{
[Customer].[Country].[Country].MEMBERS
*
[Customer].[Customer].[Customer].MEMBERS
}
,[Measures].[Internet Sales Amount]
,ASC
)
MEMBER [Measures].[myrank] AS
Rank
(
(
[Customer].[Country].CurrentMember
,[Customer].[Customer].CurrentMember
)
,[myset]
)
member prevmembervalue as
(myset.item(myRank - 2), [Measures].[Internet Sales Amount])
member currentmembervalue as
(myset.item(myRank - 1), [Measures].[Internet Sales Amount])
MEMBER greaterthanprev as
iif(currentmembervalue > prevmembervalue, 1, 0)
member rankActual as
iif
(
prevmembervalue = null, 1,
iif(currentmembervalue > prevmembervalue,
(myset.item(myRank - 2), rankActual) + 1,
(myset.item(myRank - 2), rankActual)
)
)
select myset on 1,
{[Measures].[Internet Sales Amount], rankActual, myRank}
on 0
from
[Adventure Works]
The measures greaterthanprev, currentmembervalue and prevmembervalue are actually not needed. Added them here just for some added clarification on the process.
How do I get the sales for the last product of a cross join of each product group and brand? I had a look at the Tail function but I can't get it to work properly.
This is the MDX I have so far:
SELECT {[Measures].[Sales Amount]} ON COLUMNS,
{
[Dim Brands].[Brand Name].[Brand Name].ALLMEMBERS *
[Dim Product Groups].[Product Group Name].[Product Group Name].ALLMEMBERS *
Tail ([Dim Products].[Product Name].[Product Name].ALLMEMBERS)
}
It only returns the last product for the whole cube rather than the last product for each brand and product group.
You can use the GENERATE function to generate the product for every combo or Brand and product group. The EXISTING takes care of the scope.
WITH SET LastProductForEachBrandAndProductGroup AS
GENERATE
(
EXISTING
NonEmpty
(
[Dim Brands].[Brand Name].[Brand Name].ALLMEMBERS, [Dim Product Groups].[Product Group Name].[Product Group Name].ALLMEMBERS
)
,
Tail (
[Dim Products].[Product Name].[Product Name].ALLMEMBERS
)
)
SELECT {[Measures].[Sales Amount]} ON COLUMNS,
NonEmpty({
[Dim Brands].[Brand Name].[Brand Name].ALLMEMBERS *
[Dim Product Groups].[Product Group Name].[Product Group Name].ALLMEMBERS *
LastProductForEachBrandAndProductGroup
}) ON ROWS
FROM YourCube
If by last, you imply the last member in any sorted(by some measure) list, the above would need some more work. Let me know how it works out for you.
Separating out the sets should speed this script up. Like the following:
WITH
SET [allBrands] AS
[Dim Brands].[Brand Name].[Brand Name].ALLMEMBERS
SET [allGroups] AS
[Dim Product Groups].[Product Group Name].[Product Group Name].ALLMEMBERS
SET [A] AS
Generate
(
{[allBrands] * [allGroups]} AS s
,
s.Current
*
Tail
(
NonEmpty
(
[Dim Products].[Product Name].[Product Name].ALLMEMBERS
,[Measures].[Sales Amount]
)
,1
)
)
SELECT
NON EMPTY
{[Measures].[Sales Amount]} ON 0
,NON EMPTY
[A] ON 1
FROM [YourCube];
Against Microsoft's AdvWrks a similar, and maybe more useful, variant of the above would be to use TopCount rather than the slightly artitrary Tail:
WITH
SET [allYears] AS
[Date].[Calendar].[Calendar Year].MEMBERS
SET [allCountries] AS
[Customer].[Customer Geography].[Country].MEMBERS
SET [A] AS
Generate
(
{[allYears] * [allCountries]} AS s
,
s.Current
*
TopCount
(
[Product].[Product Categories].[Product].ALLMEMBERS
,2
,[Measures].[Internet Sales Amount]
)
)
SELECT
{[Measures].[Internet Sales Amount]} ON 0
,[A] ON 1
FROM [Adventure Works];
This results in the following:
i have this query, i need to implement for two dimension
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]
I try but it is not working for two dimension
WITH
SET FIPS as
[Geography].[State-Province].[State-Province]
//TopCount([Product].[Subcategory].[Subcategory],10,[Measures].[Sales Amount])
SET [TCat] AS
Generate( {FIPS}, CrossJoin( {[Geography].[State-Province].CurrentMember}, topsum( ([Product].[Subcategory].[Subcategory]), 2, [Measures].[Sales Amount] ) ))
MEMBER [Product].[Subcategory].[Other] AS
Aggregate(Except( [Product].[Subcategory].[Subcategory].Members, TCat))
SELECT
{ [Measures].[Sales Amount] } ON COLUMNS,
Union(
TCat , {[Geography].[State-Province].[State-Province],[Product].[Subcategory].[Other]} ) ON ROWS
FROM [Adventure Works];
Try using Except.
WITH
SET [TCat] AS
TopCount([Product].[Subcategory].[Subcategory],10,[Measures].[Sales Amount])
MEMBER [Product].[Subcategory].[Other] AS
Aggregate(Except( [Product].[Subcategory].[Subcategory].Members, TCat)
SELECT { [Measures].[Sales Amount] } ON COLUMNS,
Union( TCat , {[Product].[Subcategory].[Other]} ) ON ROWS
FROM [Adventure Works]