MDX query not working - ssas

I am trying to perform the between two dates (inclusive) in mdx. I have two fact tables and one mapping table.
Fact_TableA
TableAId,
ValueA,
Date_FK
Fact_TableB
TableBId,
ValueB,
Date_FK
Fact_MappingTable
TableAId,
TableBId
Fact_MappingTable has many to many relationship with Fact_TableA and Fact_TableB. I have written below query
SELECT
NON EMPTY
{
[Measures].[ValueA],
[Measures].[ValueB]
} ON COLUMNS,
NON EMPTY
{
(
[Fact Table A].[Column AID].[Column AID].ALLMEMBERS *
[Fact Table B].[Date FK].[Date FK].ALLMEMBERS
)
} DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS FROM
(
select ([Fact Mapping Table].[Date FK].&[2014-12-10T00:00:00] : [Fact Mapping Table].[Date FK].&[2015-01-01T00:00:00]) ON COLUMNS FROM [DW Testing]
)
This query is performing the
Select
Fact_TableA.ValueA
,Fact_TableB.ValueB
from
Fact_TableA
left outer join Fact_MappingTable on
Fact_TableA.TableAId = Fact_MappingTable.TableAId
left outer join Fact_TableB on
Fact_TableB.TableBId = Fact_MappingTable.TableBId
where Date_FK between '2015-01-01' and '2015-01-01'
But my requirement is
Select
Fact_TableA.ValueA
,Fact_TableB.ValueB
from
Fact_TableA
left outer join Fact_MappingTable on
Fact_TableA.TableAId = Fact_MappingTable.TableAId and
Fact_MappingTable.DateM_FK between '2015-01-01' and '2015-01-10'
left outer join Fact_TableB on
Fact_TableB.TableBId = Fact_MappingTable.TableBId
where Date_FK between '2015-01-01' and '2015-01-10'
I have tried performing the Subselect and Where slicer but is not working correctly. Any suggestion how this could be done in MDX? How dimension usage should have relationships among dimensions and fact table to perform the filtering.
Attached the screenshots

Maybe this:
SELECT
NON EMPTY
{
[Measures].[ValueA],
[Measures].[ValueB]
} ON COLUMNS,
NON EMPTY
{
EXISTS(
[Fact Table A].[Column AID].[Column AID].ALLMEMBERS
,{[Fact Mapping Table].[Date FK].&[2014-12-10T00:00:00]
:
[Fact Mapping Table].[Date FK].&[2015-01-01T00:00:00])}
)
* [Fact Table B].[Date FK].[Date FK].ALLMEMBERS
} DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS FROM
(
select ([Fact Mapping Table].[Date FK].&[2014-12-10T00:00:00] : [Fact Mapping Table].[Date FK].&[2015-01-01T00:00:00]) ON COLUMNS FROM [DW Testing]
)
Try swapping the Exists function for the NonEmpty function:
SELECT
NON EMPTY
{
[Measures].[ValueA],
[Measures].[ValueB]
} ON COLUMNS,
NON EMPTY
{
NonEmpty(
[Fact Table A].[Column AID].[Column AID].ALLMEMBERS
,{[Fact Mapping Table].[Date FK].&[2014-12-10T00:00:00]
:
[Fact Mapping Table].[Date FK].&[2015-01-01T00:00:00])}
)
* [Fact Table B].[Date FK].[Date FK].ALLMEMBERS
} DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS FROM
(
select ([Fact Mapping Table].[Date FK].&[2014-12-10T00:00:00] : [Fact Mapping Table].[Date FK].&[2015-01-01T00:00:00]) ON COLUMNS FROM [DW Testing]
)

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 showing values of only the best salesmen

Let's say I have two simple dimensions:
Products - with id and name
Salesmen - with id and name
My fact table is named SALES and contains the ids of the abovementioned.
Let's say product X has been sold by salesmen A, B and C.
Product Y has been sold by salesmen B, C, and D
I want to produce a MDX query which would tell me the names of the salesmen who sold both those products. In this case the result would be B and C
My attempt:
select {null} on 0,
DESCENDANTS (
[Salesmen].[Name].children
) on 1
FROM [Test]
where (
{
(
[Products].[Name].&[X]
)
,
(
[Products].[Name].&[Y]
)
}
)
Please try nesting exist function like this:
SELECT
{} on 0,
EXISTS(
EXISTS(
{[Salesmen].[Name].MEMBERS}, //<<TRY THIS INSTEAD
{[Products].[Name].&[X]}
)
,{[Products].[Name].&[Y]}
)
ON 1
FROM [Test];
Strictly speaking EXISTS requires the name of a measures group as it's third argument like this:
SELECT
{} on 0,
EXISTS(
EXISTS(
{[Salesmen].[Name].MEMBERS} //<<TRY THIS INSTEAD
,{[Products].[Name].&[X]}
,"Reseller Sales" //<<replace with group name from your cube
)
,{[Products].[Name].&[Y]}
,"Reseller Sales" //<<replace with group name from your cube
)
ON 1
FROM [Test];
An alternative approach is to use a member from the hierarchy [Measures] and the functions NonEmpty and Intersect:
SELECT
{} on 0,
INTERSECT(
NONEMPTY(
{[Salesmen].[Name].MEMBERS}
,([Products].[Name].&[X],[Measures].[SomeMeasureInYourCube])
)
,NONEMPTY(
{[Salesmen].[Name].MEMBERS}
,([Products].[Name].&[Y],[Measures].[SomeMeasureInYourCube])
)
)
ON 1
FROM [Test];
The above may well work with a simple tuple of the members of Products only
SELECT
{} on 0,
INTERSECT(
NONEMPTY(
{[Salesmen].[Name].MEMBERS}
,([Products].[Name].&[X])
)
,NONEMPTY(
{[Salesmen].[Name].MEMBERS}
,([Products].[Name].&[Y])
)
)
ON 1
FROM [Test];

How to build MDX query for two periods of time?

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.

MDX Query Column name for Measures

have following query
select
non empty
(
[Dimension1].[Description].children,
[Dimension1].[GCode].members,
[Measures].[GScore Sum]
)
on columns,
non empty
(
[Dimension2].[DCode].[DCode] *
[Dimension2].[DName].[DName] *
[Dimension2.[Barcode].[Barcode] *
[Dimension2].[LN].[LName] *
[Dimension2].[FN].[FName]
)
on rows
from
[MCube]
where
(
{[Dimension2].[HARC].[DCode].&[0000]}
)
In the results [GSCORE SUM] column name repeats is eachtheir way to avoid the repeation of column name or give the unique name for EACH GLCE Code member instead of just repetting [GSCore Sum] in results or make the [Gscore Sum] invisible
http://i.stack.imgur.com/yte59.jpg
Not 100% sure what you require but have you tried adding the measure to the WHERE clause?:
select
non empty
(
[Dimension1].[Description].children,
[Dimension1].[GCode].members
)
on columns,
non empty
(
[Dimension2].[DCode].[DCode] *
[Dimension2].[DName].[DName] *
[Dimension2.[Barcode].[Barcode] *
[Dimension2].[LN].[LName] *
[Dimension2].[FN].[FName]
)
on rows
from
[MCube]
where
(
[Measures].[GScore Sum],
{[Dimension2].[HARC].[DCode].&[0000]}
)

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.