Fix several values : mdx query - sql

I have this mdx query :
SELECT
NonEmpty
(
{
[Dim Sales Territory].[Sales Territory Country].[Sales Territory Country]
}
) ON COLUMNS
,NonEmpty
(
{
[Due Date].[Calendar Year].[Calendar Year]
*
[Due Date].[French Month Name].MEMBERS
}
) ON ROWS
FROM [AW Cube]
WHERE
[Measures].[Sales Amount];
This gives me the result: for each month of year my sales amount
What I want to do is to get result only for 2 years ..
For one year I did this
SELECT
NonEmpty
(
{
[Dim Sales Territory].[Sales Territory Country].[Sales Territory Country]
}
) ON COLUMNS
,NonEmpty
(
{
[Due Date].[Calendar Year].&[2008]
*
[Due Date].[French Month Name].MEMBERS
}
) ON ROWS
FROM [AW Cube]
WHERE
[Measures].[Sales Amount];
But for two years i didn't found anything !!

How about :
NONEMPTY(
{ [Due Date].[Calendar Year].&[2008], [Due Date].[Calendar Year].&[2009] }
* { [Due Date].[French Month Name].MEMBERS }
) ON ROWS
This is actually a shortcut of the crossjoin function.

Related

MDX formatting results

I'm new to MDX querying. I am having trouble changing how the MDX output is formatted. I have made a similar example using the "Adventure Works Internet Sales Model". See below:
WITH
MEMBER [Measures].[Calculate YTD] AS
Sum
(
periodstodate([Date].[Calendar].[Year],[Date].[Calendar].CurrentMember)
,[Measures].[Internet Total Sales]
)
SELECT
{[Measures].[Internet Total Sales]
,[Measures].[Calculate YTD]
} ON COLUMNS,
[Date].[Calendar].[Month] ON ROWS
FROM [Adventure Works Internet Sales Model]
WHERE ([Date].[Date].&[2012-01-01T00:00:00]:[Date].[Date].&[2018-01-01T00:00:00])WHERE ([Date].[Date].&[2012-01-01T00:00:00]:[Date].[Date].&[2018-01-01T00:00:00])
the results looks like this:
MDX results
What I would like to see is that the "[Date].[Calendar].[Month]" row to be displayed as the end of month date (e.g. 31-Mar-2019)
Welcome to SO!
You could add in an additional column showing the last day of each month:
WITH
MEMBER [Measures].[Last Day of Month] AS
[Date].[Calendar].CurrentMember.LastChild.MEMBER_CAPTION
MEMBER [Measures].[Last Day of Month v2] AS
TAIL(EXISTING [Date].[Date].[Date].MEMBERS).ITEM(0).ITEM(0).MEMBER_CAPTION
MEMBER [Measures].[Calculate YTD] AS
Sum
(
periodstodate(
[Date].[Calendar].[Year],[Date].[Calendar].CurrentMember
)
,[Measures].[Internet Total Sales]
)
SELECT
{
[Measures].[Last Day of Month]
, [Measures].[Last Day of Month v2]
,[Measures].[Internet Total Sales]
,[Measures].[Calculate YTD]
} ON COLUMNS,
[Date].[Calendar].[Month]
ON ROWS
FROM [Adventure Works Internet Sales Model]
WHERE
(
[Date].[Date].&[2012-01-01T00:00:00]:
[Date].[Date].&[2018-01-01T00:00:00]
);
Output

I'm stuck with MDX query

i need sales amount of first month of each quarter
WITH SET [FIRSTMONTHOFQTR] AS
DESCENDANTS(
DESCENDANTS(
[Date].[Calendar].CURRENTMEMBER,
[Date].[Calendar].[Calendar Quarter]
),
[Date].[Calendar].[Month]
)
SELECT {
[Measures].[Sales Amount]
} ON COLUMNS,
{
[FIRSTMONTHOFQTR]
} ON ROWS
FROM [Adventure Works];
With above im getting each month but i need only first month. how can i filter that?
This is one approach:
WITH
SET [Qtrs] AS
[Date].[Calendar].[Calendar Quarter]
SET [FIRSTMONTHOFQTR] AS
Generate
(
[Qtrs] AS s
,Head(Descendants(s.CurrentMember,[Date].[Calendar].[Month]))
)
SELECT
{[Measures].[Sales Amount]} ON COLUMNS
,[FIRSTMONTHOFQTR] ON ROWS
FROM [Adventure Works];
It returns the following:

How to use avg function in mdx?

I have my mdx query:
SELECT
NON EMPTY
{[Measures].[Amount]} ON COLUMNS
,NON EMPTY
{[Dim Date].[Day Of Week].[Day Of Week].MEMBERS} ON ROWS
FROM
(
SELECT
[Dim Date].[Date Int].&[20140730] : [Dim Date].[Date Int].&[20150730] ON COLUMNS
FROM [Cube]
WHERE
[Dim Client].[Common Client UID].&[{some id}]
);
so i have my a weekday dim, which contain members as numbers from 1-7. Query find returns amount for each weekday, which is summed up, but i want to find out an average, so somehow i need to find out how many items was summed to give me [Measures].[Amount] result. I have tryed with separate member function which didnt worked.
WITH MEMBER [Measures].[Avg] AS
Avg(
( [Dim Date].[Day Of Week].CURRENTMEMBER, [Measures].[Amount] )
)
Avg return exectly the same value. How do i do such a request in mdx?
This will give you an average over 1 member:
WITH MEMBER [Measures].[Avg] AS
Avg(
( [Dim Date].[Day Of Week].CURRENTMEMBER, [Measures].[Amount] )
)
That is because CURRENTMEMBER is returning 1 member.
If you want an average over several members than you need to supply a set as the first argument for the Avg function. Here is an example:
WITH MEMBER [Measures].[Daily Avg] AS
Avg(
Descedants(
[Date].[Date - Calendar Month].CURRENTMEMBER
,[Date].[Date - Calendar Month].[Calendar Day]
,[Measures].[Amount]
)
Although I suspect something like the follwoing should work in your context:
WITH
MEMBER [Measures].[Avg] AS
Avg
(
(EXISTING
[Dim Date].[Date Int].MEMBERS)
,[Measures].[Amount]
)
SELECT
NON EMPTY
{
[Measures].[Amount]
,[Measures].[Avg]
} ON COLUMNS
,NON EMPTY
{[Dim Date].[Day Of Week].[Day Of Week].MEMBERS} ON ROWS
FROM
(
SELECT
[Dim Date].[Date Int].&[20140730] : [Dim Date].[Date Int].&[20150730] ON COLUMNS
FROM [Cube]
WHERE
[Dim Client].[Common Client UID].&[{some id}]
);

MDX How do you create a variance and variance % in a report

Using AdventureWorksDW2008R I have the following DataSet
SELECT NON EMPTY {
[Measures].[Sales Amount], [Measures].[Total Product Cost], [Measures].[Internet Sales Count]
} ON COLUMNS, NON EMPTY
{
([Order Date].[Calendar Year].[Calendar Year].ALLMEMBERS )
} DIMENSION PROPERTIES MEMBER_CAPTION, MEMBER_UNIQUE_NAME ON ROWS
FROM [Adventure Works Cube]
Resutls are:
Sales Amount Total Product Cost Internet Sales Count
2005 4342674.0296 2562584.6235 8949
2008 25016003.1911002 14715208.9522001 51449
Is there a way to calculate the variance of each in the report?
For example the Variance of Internet Sales Count would be:
51449 – 8949 = 42500
And the % variance would be
42500/51449 = 83%
I know I can use the following to get the Sum:
=Sum(Fields!Internet_Sales_Count.Value, "DataSet1")
Is there a way to get the 2008 value and subtract the 2005 value?
Here is one possibility:
WITH
MEMBER [Measures].[Internet Sales diff] AS
(
[Delivery Date].[Calendar Year].CurrentMember
,[Measures].[Internet Sales Amount]
)
-
(
[Delivery Date].[Calendar Year].CurrentMember.Lag(1)
,[Measures].[Internet Sales Amount]
), format_string = '#,###,###,##0.00'
SELECT
NON EMPTY
{
[Measures].[Sales Amount]
,[Measures].[Total Product Cost]
,[Measures].[Internet Sales Amount]
,[Measures].[Internet Sales diff]
} ON COLUMNS
,NON EMPTY
{[Delivery Date].[Calendar Year].[Calendar Year].ALLMEMBERS}
DIMENSION PROPERTIES
MEMBER_CAPTION
,MEMBER_UNIQUE_NAME
ON ROWS
FROM [Adventure Works];
The result of the above is the following:
A percentage measure could then be added like this:
WITH
MEMBER [Measures].[Internet Sales diff] AS
(
[Delivery Date].[Calendar Year].CurrentMember
,[Measures].[Internet Sales Amount]
)
-
(
[Delivery Date].[Calendar Year].CurrentMember.Lag(1)
,[Measures].[Internet Sales Amount]
)
,format_string = '#,###,###,##0.00'
MEMBER [Measures].[Internet Sales diff %] AS
IIF
(
[Measures].[Internet Sales Amount] = 0
,null
,
[Measures].[Internet Sales diff]
/
(
[Delivery Date].[Calendar Year].CurrentMember.Lag(1)
,[Measures].[Internet Sales Amount]
)
)
,format_string = '#,###,###,##0.00%'
SELECT
NON EMPTY
{
[Measures].[Sales Amount]
,[Measures].[Total Product Cost]
,[Measures].[Internet Sales Amount]
,[Measures].[Internet Sales diff]
,[Measures].[Internet Sales diff %]
} ON COLUMNS
,NON EMPTY
{[Delivery Date].[Calendar Year].[Calendar Year].ALLMEMBERS}
DIMENSION PROPERTIES
MEMBER_CAPTION
,MEMBER_UNIQUE_NAME
ON ROWS
FROM [Adventure Works];
Results in this:
Here is a better approach using the parallelperiod function:
WITH
MEMBER [Measures].[Internet Sales PrevYr] AS
IIF
(
[Measures].[Internet Sales Amount] = 0
,null
,(
[Measures].[Internet Sales Amount]
,ParallelPeriod
(
[Delivery Date].[Calendar Year].[Calendar Year]
,1
,[Delivery Date].[Calendar Year].CurrentMember
)
)
)
,format_string = '$#,###,###,##0.00'
MEMBER [Measures].[Internet Sales diff] AS
IIF
(
[Measures].[Internet Sales Amount] = 0
,null
,
[Measures].[Internet Sales Amount] - [Measures].[Internet Sales PrevYr]
)
,format_string = '$#,###,###,##0.00'
MEMBER [Measures].[Internet Sales diff %] AS
IIF
(
[Measures].[Internet Sales PrevYr] = 0
,null
,
[Measures].[Internet Sales diff] / [Measures].[Internet Sales PrevYr]
)
,format_string = '#,###,###,##0.00%'
SELECT
NON EMPTY
{
[Measures].[Internet Sales Amount]
,[Measures].[Internet Sales PrevYr]
,[Measures].[Internet Sales diff]
,[Measures].[Internet Sales diff %]
} ON COLUMNS
,NON EMPTY
{[Delivery Date].[Calendar Year].[Calendar Year].MEMBERS} ON ROWS
FROM [Adventure Works];
Results:

How to get top 2 count record and rest total(others) using two dimension in same mdx

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]