How to build MDX query for two periods of time? - sql

I have the following MDX:
SELECT
NON EMPTY
{
[Measures].[Returns]
,[Measures].[Returns]
} ON COLUMNS
,NON EMPTY
{[Employees].[Company].[Company].ALLMEMBERS}
DIMENSION PROPERTIES
MEMBER_CAPTION
,MEMBER_UNIQUE_NAME
ON ROWS
FROM
(
SELECT
StrToSet
("[Exec Date].[Hierarchy].[Month Num].&[2014]&[1]"
,CONSTRAINED
) ON COLUMNS
FROM [cbSales]
)
WHERE
IIF
(
StrToSet
("[Exec Date].[Hierarchy].[Month Num].&[2014]&[1]"
,CONSTRAINED
).Count
= 1
,StrToSet
("[Exec Date].[Hierarchy].[Month Num].&[2014]&[1]"
,CONSTRAINED
)
,[Exec Date].[Hierarchy].CurrentMember
);
Both columns shows returned data for month 2014/01
I want to have data for the next month (2014/02) in the second column.
How can I change the query to achieve my goal.

I'll try to replicate with AdvWks cube, so currently not tested:
WITH
MEMBER [Measures].[ReturnsNextMth] AS
(
[Measures].[Returns]
,[Exec Date].[Hierarchy].CurrentMember.NextMember
)
SELECT
NON EMPTY
{
[Measures].[Returns]
,[Measures].[ReturnsNextMth]
} ON COLUMNS
,NON EMPTY
{
[Employees].[Company].[Company].ALLMEMBERS
*
[Exec Date].[Hierarchy].[Month Num].ALLMEMBERS
} ON ROWS
FROM
(
SELECT
StrToSet
("[Exec Date].[Hierarchy].[Month Num].&[2014]&[1]"
,CONSTRAINED
) ON COLUMNS
FROM [cbSales]
);
Something similar in AdvWks:
WITH
MEMBER [Measures].[ReturnsNextMth] AS
(
[Measures].[Internet Order Count]
,[Date].[Calendar].CurrentMember.NextMember
)
SELECT
NON EMPTY
{
[Measures].[Internet Order Count]
,[Measures].[ReturnsNextMth]
} ON COLUMNS
,NON EMPTY
{
[Sales Territory].[Sales Territory].[Country].MEMBERS
*
[Date].[Calendar].[Month].ALLMEMBERS
} ON ROWS
FROM
(
SELECT
[Date].[Calendar].[Month].&[2008]&[1] ON COLUMNS
FROM [Adventure Works]
);
This gives these results. I found this quite surprising as the sub-select seems to suggest we only have Jan-2008 available, but sub-select is a bit of a strange creature. This article explains: http://bisherryli.com/2013/02/08/mdx-25-slicer-or-sub-cube/. So our measure [ReturnsNextMth] still functions ok.

Related

MDX query to except specific date from date range

I am need to delete all 2 month from set. This code return all date range without excepting 2 month.
SELECT
{[Measures].[In]} ON COLUMNS,
EXCEPT([Date].[2014].[1] : [Date].[2016].[2], [Date].[Month].[2]) ON ROWS
FROM [Shop hourly]
Print screen for whytheq
My decision based on whytheq answear. I create a dimension for all kind of dates, and except them. Example:
SELECT {[Measures].[In sum]} ON COLUMNS,
NON EMPTY
{[Shop].[11], [Shop].[22], [Shop].[33]} *
Except([Quarter].Children, [Quarter].[2]) *
[Month].Children ON ROWS
FROM [Shop hourly]
WHERE
([Date].[2013].[1].[1] : [Date].[2016].[1].[1]) *
Except([Year].Children, [Year].[2014])
In AdventureWorks I can do the following:
SELECT
[Measures].[Internet Sales Amount] ON 0
,NON EMPTY
Except
(
[Date].[Calendar].[Month].&[2005]&[7]
:
[Date].[Calendar].[Month].&[2008]&[7]
,Exists
(
[Date].[Calendar].[Month].MEMBERS
,[Date].[Calendar Quarter of Year].&[CY Q3]
)
) ON 1
FROM [Adventure Works];
So adapting the above to your cube maybe looks like the following:
SELECT
{[Measures].[In]} ON COLUMNS
,Except
(
[Date].[2014].[1] : [Date].[2016].[2]
,Exists
(
[Date].MEMBERS
,[Date].[Month].[2]
)
) ON ROWS
FROM [Shop hourly];

MDX Sort By Month

I am trying to put together my first query for a pentaho CDE dashboard chart.
Starting Query
WITH
SET [~COLUMNS] AS
{[DimProgram.Name].[Name].MEMBERS}
SET [~ROWS] AS
{[DimTime.CalendarYearMonth].[CalendarYearMonth].MEMBERS}
SELECT
NON EMPTY
CrossJoin
(
[~COLUMNS]
,{[Measures].[SubmissionCount]}
) ON COLUMNS
,NON EMPTY
[~ROWS] ON ROWS
FROM [PSE_FactSubmission];
This query returns the data I want but needs to be tweaked a bit to be ready for actual use. I want to sort by date descending and limit to only the past 12 months.
I've read several webpages on sorting in MDX, but haven't been able to put together a query that will run. When the query doesn't run just an "Error" prompt.
Ordering Attempt
WITH
SET [~COLUMNS] AS
{[DimProgram.Name].[Name].MEMBERS}
SELECT
NON EMPTY
CrossJoin
(
[~COLUMNS]
,{[Measures].[SubmissionCount]}
) ON COLUMNS
,NON EMPTY
Order
(
[DimTime.CalendarYearMonth].[CalendarYearMonth].MEMBERS
,[DimTime.CalendarYearMonth].CurrentMember.Member_Key
,DESC
) ON ROWS
FROM [PSE_FactSubmission];
Any tips on sorting or how to limit to the past X months would be very appreciated.
Usually a Date/Time dimension is ordered naturally in the cube design so there is no need to use Order. I don't need to with the cubes I use.
If it is in a strange order in the cube then you need to break (B) this hierarchical order by using BASC or BDESC:
WITH
SET [~COLUMNS] AS
{[DimProgram.Name].[Name].MEMBERS}
MEMBER [Measures].[orderMeas] AS
[DimTime.CalendarYearMonth].CurrentMember.Member_Key
SET [~ROWS] AS
Order
(
[DimTime.CalendarYearMonth].[CalendarYearMonth].MEMBERS
,[Measures].[orderMeas]
,BASC
)
SELECT
NON EMPTY
CrossJoin
(
[~COLUMNS]
,{[Measures].[SubmissionCount]}
) ON COLUMNS
,NON EMPTY
[~ROWS] ON ROWS
FROM [PSE_FactSubmission];
To get the most recent 12 months you can use the Tail function - better to use it against NonEmpty months:
WITH
SET [~COLUMNS] AS
{[DimProgram.Name].[Name].MEMBERS}
MEMBER [Measures].[orderMeas] AS
[DimTime.CalendarYearMonth].CurrentMember.Member_Key
SET [~ROWS] AS
Order
(
NonEmpty
(
[DimTime.CalendarYearMonth].[CalendarYearMonth].MEMBERS
,[Measures].[SubmissionCount]
)
,[Measures].[orderMeas]
,BASC
)
SELECT
NON EMPTY
CrossJoin
(
[~COLUMNS]
,{[Measures].[SubmissionCount]}
) ON COLUMNS
,NON EMPTY
Tail
(
[~ROWS]
,12
) ON ROWS
FROM [PSE_FactSubmission];
Hi Andrew - against AdvWrks I've got the following running without any errors. I needed to change Member_Key to MemberValue:
WITH
SET [~COLUMNS] AS
[Product].[Product Categories].[Product]
MEMBER [Measures].[orderMeas] AS
[Date].[Calendar].CurrentMember.MemberValue
SET [~ROWS] AS
Order
(
NonEmpty
(
[Date].[Calendar].[Month].MEMBERS
,[Measures].[Internet Sales Amount]
)
,[Measures].[orderMeas]
,ASC
)
SELECT
NON EMPTY
CrossJoin
(
[~COLUMNS]
,{[Measures].[Internet Sales Amount]}
) ON COLUMNS
,NON EMPTY
Tail
(
[~ROWS]
,12
) ON ROWS
FROM [Adventure Works];

MDX filter data

WITH
MEMBER [Measures].[ParameterCaption] AS [Estate].[Week].CURRENTMEMBER.MEMBER_CAPTION
MEMBER [Measures].[ParameterValue] AS [Estate].[Week].CURRENTMEMBER.UNIQUENAME
MEMBER [Measures].[ParameterLevel] AS [Estate].[Week].CURRENTMEMBER.LEVEL.ORDINAL
SELECT
{[Measures].[ParameterCaption], [Measures].[ParameterValue], [Measures].[ParameterLevel]} ON COLUMNS
,
NON EMPTY (
ORDER (
EXCEPT( [Estate].[Week].[ALL].CHILDREN
, { [Estate].[Week]})
, ( [Estate].[Week].MEMBERVALUE)
, ASC
)
) ON ROWS
From [EstateRpt]
WHERE Filter([V Estate Weekly Rpt].[Week].CHILDREN, [V Estate Weekly Rpt].[Week].MEMBERVALUE = 'NONE')
Hi, i am new to the MDX. I want to filter the week which is not equal to "NONE"? by default, week is set the "NONE", so it will appear the NONE data in cube. I want to filter this NONE.
I do try the WHERE clause but it show the error to me which i do not figure out what is the problem
If you want to filter out 'NONE' then you need to use this in your filter:
<> 'NONE'
Also you need to use CurrentMember within your filter like this example:
SELECT
{
[Date].[Calendar].[Month].&[2006]&[7]
,[Date].[Calendar].[Date].&[20050220]
}
*
{[Measures].[Reseller Sales Amount]} ON COLUMNS
,
{[Product].[Category].Children}
*
{[Geography].[Geography].[Country].MEMBERS} ON ROWS
FROM [Adventure Works]
WHERE
Filter
(
[Geography].[State-Province].MEMBERS
,
[Geography].[State-Province].CurrentMember.Member_Caption <> 'California'
);

How can I get top 3 items sold in particular three days in MDX queries?

This is my MDX query. This will the items that is sold on particular three days. This query returns all items sold, but I wish to get only top 3 items on that particular dates.
How do I can get this? Here's my MDX query:
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])
Use the TopCount function as below:
select
{
[Measures].[Quantity],
[Measures].[Net Sales]
}
on columns,
NON EMPTY
TopCount([Products].[Item Description].children * [Calendar].[Date].[Date],
3,
[Measures].[Quantity])
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])
To get the top 3 records for each date, you can user the GENERATE function :
select
{
[Measures].[Quantity],
[Measures].[Net Sales]
}
on columns,
NON EMPTY
GENERATE([Calendar].[Date].[Date],
TopCount([Products].[Item Description].children,
3,
[Measures].[Quantity])
)
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])

MDX and combine two similar

i am fairly new to MDX and SSAS. Recently i have been asked to produce a report which requires a Starting value for each monthly period and then the movement within each period ending up at the starting value for the next month.
I can produce this in two separate MDX see below, but i am flummoxed on how to bring these results together in one dataset in SSRS without basically forming the data physically in a database and pulling trough into the warehouse. The queries are slightly different since they both use a different Measure and then one uses a couple more Dimensional filters.
any help would be most appreciated and i hope someone here can help or offer some useful advice on other ways to possibly get to what i need to:
WITH MEMBER [Member1] AS AGGREGATE( ({[SRD Date].[Base Report Date].& [1]},{[SRD Date].[Current Year].& [Yes]}), [Measures].[Amount])
SELECT NON EMPTY { [member1] } ON COLUMNS
, NON EMPTY {([SRD Date].[Year].[Year].ALLMEMBERS
* [SRD Date].[Month Key].[Month Key].ALLMEMBERS
* [SRD Date].[Month Of Year].[Month Of Year].ALLMEMBERS) } DIMENSION PROPERTIES MEMBER_CAPTION
, MEMBER_UNIQUE_NAME ON ROWS FROM (
SELECT (
- { [Resource].[Category].& [Support]
, [Resource].[Category].& [Bolt-on] }
) ON COLUMNS
FROM (
SELECT ([Exclude Test Accounts]) ON COLUMNS
FROM (
SELECT ([OnlyUnitsAndItems]) ON COLUMNS
FROM (
SELECT ([ExcludeNonReportableMonths]) ON COLUMNS
FROM [Cube1]
) ) ) )
WITH MEMBER [Member2] AS AGGREGATE( ( {[SRD Date].[Current Year].& [Yes]}), [Measures].[Amount Of Movement])
SELECT NON EMPTY { [Member2] } ON COLUMNS
, NON EMPTY {([SRD Date].[Year].[Year].ALLMEMBERS
* [SRD Date].[Month Key].[Month Key].ALLMEMBERS
* [SRD Date].[Month Of Year].[Month Of Year].ALLMEMBERS
* [SRD].[Movement Type].[Movement Type].ALLMEMBERS) } DIMENSION PROPERTIES MEMBER_CAPTION
, MEMBER_UNIQUE_NAME ON ROWS FROM (
SELECT (
- { [Resource].[Category].& [Support]
, [Resource].[Category].& [Bolt-on] }
) ON COLUMNS
FROM (
SELECT ([Exclude Test Accounts]) ON COLUMNS
FROM (
SELECT ([OnlyUnitsAndItems]) ON COLUMNS
FROM (
SELECT ([ExcludeNonReportableMonths]) ON COLUMNS
FROM [Cube1]
) ) ) )
Just combine both WITH clauses (without a comma or anything in between!), and list both members in the columns axis:
WITH MEMBER MEMBER [Member1] AS AGGREGATE( ({[SRD Date].[Base Report Date].& [1]},{[SRD Date].[Current Year].& [Yes]}), [Measures].[Amount])
MEMBER [Member2] AS AGGREGATE( ( {[SRD Date].[Current Year].& [Yes]}), [Measures].[Amount Of Movement])
SELECT NON EMPTY { [Member1], [Member2] } ON COLUMNS
, NON EMPTY {([SRD Date].[Year].[Year].ALLMEMBERS
* [SRD Date].[Month Key].[Month Key].ALLMEMBERS
* [SRD Date].[Month Of Year].[Month Of Year].ALLMEMBERS
* [SRD].[Movement Type].[Movement Type].ALLMEMBERS) } DIMENSION PROPERTIES MEMBER_CAPTION
, MEMBER_UNIQUE_NAME ON ROWS FROM (
SELECT (
- { [Resource].[Category].& [Support]
, [Resource].[Category].& [Bolt-on] }
) ON COLUMNS
FROM (
SELECT ([Exclude Test Accounts]) ON COLUMNS
FROM (
SELECT ([OnlyUnitsAndItems]) ON COLUMNS
FROM (
SELECT ([ExcludeNonReportableMonths]) ON COLUMNS
FROM [Cube1]
) ) ) )
I hope I did not miss any important other difference between both queries.