AdventureWorks, Cubes and Prev.Member - mdx

I am working through the Adventure Works tutorial on the MSDN site and making good progress.
I was trying to test myself and go off the guide to see if I was understanding the lessons and I've gotten myself a bit confused.
My intention was to use the Prev.Member MDX command so that in the Pivot Table when looking at sales, I can see the sales totals for the same period, the year before.
( [Measures].[Internet Sales Count]
, [Order Date].[Calendar Date].PrevMember
)
This is the expression I thought would work. Sadly, this just produces blank fields on the Pivot Table. The [Order Date].[Calendar Date] is taken from the date hierarchy that the guide advised to make.
Trying the expression [Order Date].[Date Key].PrevMember also returns blank cells.
The other code tried was using [Order Date].PrevMember but that just returns #VALUE!.

Try adding in Currentmember so that your expression knows what to apply PrevMemeber to:
(
[Measures].[Internet Sales Count]
, [Order Date].[Calendar Date].CurrentMember.PrevMember
)

Related

How to get measure value for certain date range in MDX?

I am new to MDX and for now it looks like some hell to me.
I have a measure called Sales KG. It calculate sales amount based on table AData where I have column named Data_Date.
I need to get Sales KG value for specified range of dates.
The problem is I can't understand how to specify that range. It doesn't look like simple < and > are works here.
I totally lost and don't have much to show, but this is what I tried:
select
[Sales KG] on Columns
from [Model]
where ([Format_TT].[Супермаркет], [Data_Date].&[20160101] : [Data_Date].&[20170101])
But it tells me that can't convert string "20160101" into date type. And probably this is not what I want. I want it to be single value for date range in single cell.
What to do?..
Take a look at the below sample they will help .
Please note that "Date" is a dimesnion in the cube, which has an attribute "[Date]" in it. "[Measures].[Internet Sales Amount]" in the cube. It is necessary to have them defined in the SSAS project, If one of them is not defined in the project but exists in the base tables of star schema it will not work. MDX can only see objects defined in the SSAS project
//First way
select
[Measures].[Internet Sales Amount]
on columns
from [Adventure Works]
where {[Date].[Date].&[20130101]:[Date].[Date].&[20130131]}
Second way
select
[Measures].[Internet Sales Amount]
on columns,
{[Date].[Date].&[20130101]:[Date].[Date].&[20130131]}
on rows
from [Adventure Works]

How to filter measure multiple times in cube

I need to product a report from my cube that looks something like the following.
(dummy data)
Where it lists sales and gross profit for today, this week, the period and year to date across the products category.
My cube is setup as follows
A date dimension
And the cube itself
Currently I have not implemented the product category pieces.
I'm struggling with how to write an MDX query that can return the sales/gross profit for a single day and then week and so on.
I can return it by itself like so
SELECT {[Measures].[Gross Profit],[Measures].[Price]} ON COLUMNS
From [Cube]
WHERE [Date].[Date Key].[2015-04-22];
and so on for the other various types (week etc), but I'm unsure as how to apply the where filter to the columnn itself rather than the overall query, or if this is even the correct way to do it and I should be making multiple MDX calls that I then compose in my app that will use this.
Can anyone give me a pointer in the right direction here?
EDIT: Seems to mostly work using the approach #Akshay Rane described however I cannot get one of my measures to work
MEMBER [This Week] as
(PeriodsToDate([Date].[Fiscal Week Date].[Fiscal Week],StrToMember('[Date].[Fiscal Week Date].[Date Key].&[' + '20140401' + ']'))
,[Measures].[Merchandise Gross Profit])
Gives me an error:
The function expects a string or numeric expression for the argument. A tuple set expression was used.
Any pointers here?
You will have to create separate Calculated Members for each time interval you want to aggregate the data upon.
This can be done in [Adventure Works] cube as follows.
WITH
MEMBER [Today] as
([Measures].[Internet Sales Amount], [Date].[Date].CURRENTMEMBER)
MEMBER [Last Week] as
AGGREGATE (
{ [Date].[Date].CURRENTMEMBER.lag(6) : [Date].[Date].CURRENTMEMBER }
, [Measures].[Internet Sales Amount]
)
SELECT
{ [Today], [Last Week] } ON 0,
{ ([Product].[Product Categories].[Category], [Date].[Date].[Date]) } ON 1
FROM
(
/*FILTERING ON SPECIFIC DATE USING SUBCUBE*/
SELECT [Date].[Date].&[20070715] ON 0
FROM [Adventure Works]
)
If you can take the different levels of date from the same user hierarchy then something like this is possible:
SELECT
{
[Date].[Calendar].[Month].&[2006]&[7]
,[Date].[Calendar].[Date].&[20050220]
}
*
{
[Measures].[Order Quantity]
,[Measures].[Internet Sales Amount]
} ON COLUMNS
,{[Product].[Category].Children} ON ROWS
FROM [Adventure Works];
The above produces results like this:

Result is the same for all columns

The cube used in this question is available as 'MDX Step-by-Step', matching the default Adventureworks cube.
I'm learning to work with MDX, so my apologies in case the answer to this question is pretty obvious. The following query results in the following result, how is it that the amount of sales isn't split out across the different countries?
SELECT { [Customer].[Customer Geography].[All Customers]
, [Customer].[Customer Geography].[Country].MEMBERS } ON COLUMNS,
Product.Category.Members ON ROWS
FROM [Step-by-Step]
this MDX query don't specify a Measure, so it will take the default measure for the cube. The cube seems to me to be AdventureWorks: I've executed the same query on my test machine and it's giving me the same results.
The default measure for the AdventureWorks Cube is [Measures].[Reseller Sales Amount].
If you open the cube definition in Visual Studio BIDS you will notice under the Dimension Usage tab that the [Measures].[Reseller Sales Amount] will not use the [Customer] dimension, so it the cube can't split the amount through that dimension.
That said, you can try to specify a measure in your query that run against the Customer dimension, such as [Internet Sales Amount], like:
SELECT
[Measures].[Reseller Sales Amount] * {[Customer].[Customer Geography].[All Customers],
[Customer].[Customer Geography].[Country].MEMBERS } ON COLUMNS,
Product.Category.Members ON ROWS
FROM [Step-by-Step]
or you can edit the cube definition to get the default measure to interact with the Customer dimension, elaborate the cube, deploy it and re run your original query. If it's for learning reasons, I think that adding the measure will be enough.

MDX: Limit parent aggregate by child value

My MDX is fledgling at best, and it is a realistic possibility that I don't even know how to phrase my question correctly to search for an answer. Sorry if this is a duplicate.
I have a date/time hierarchy [Dates]:
[Work Date].[Dates].[Year].[Quarter].[Month].[Day]
What I want to do is return the previous 5 years worth of data, for the month of X (let's assume January):
Year Sub Total
2008 645712.11
2009 848075.43
2010 765802.60
However, I'm having trouble restricting the "Year" data, based on the specific month. I have tried this MDX code, but it yields no results at all:
SELECT [Measures].[Sub Total] ON 0,
[Work Date].[Dates].[Year] ON 1
FROM (
SELECT [Work Date].[Dates].[Month].&[01] ON 0
FROM [Data Warehouse])
If I edit the sub-select I can get a specific year, quarter and month... but I only want to restrict the month portion and not the year. I've looked into using an EXCEPT clause, but I run into the same issue. Does that make any sense? I appreciate any help, and am not opposed to reading long articles if it will further my learning / understanding. Thanks!
After your explanation in comments: To get the same child of each hierarchy level you can use the MDX function COUSIN (here the reference on msdn). As documentation said:
Returns the child member with the same relative position under a
parent member as the specified child member.
so in your case your Date dimension has to be complete without missing months or days, so you can write a query like this (I've tested on AdventureWorks)
with member [Measures].[Sub Total]
as (Cousin([Date].[Calendar].[Month].&[2004]&[1], [Date].[Calendar].CurrentMember), [Measures].[Internet Sales Amount]) , Format_string = 'Currency'
Select {[Measures].[Internet Sales Amount],[Measures].[Sub Total] } on 0
,{
[Date].[Calendar].[Calendar Year].&[2002]
,[Date].[Calendar].[Calendar Year].&[2003]
,[Date].[Calendar].[Calendar Year].&[2004]
} on 1
from [Adventure Works]
I've put side by side the total and the sub total for the years. I am considering only these 3 years because they are complete from jan to dec and I can apply cousin operator without problem.
Hope this help!

How to pass parameter in mdx query

How to pass parameter in MDX query.
Below is my mdx qyery----
SELECT
NON EMPTY FILTER({ ( [Tbl Electricity].[Transaction Id].[Transaction Id] ) }, [Measures].[From Year]=2011 and [Measures].[To Month]=[Measures].[To Month].&['+#month+' ]) ON ROWS,
NON EMPTY { [Measures].[From Year], [Measures].[To Year], [Measures].[To Month], [Measures].[From Month], [Measures].[Total Cost], [Measures].[Quantity] } ON COLUMNS
FROM [ECERM Dev1]
In above query, I am comparing [From Year] by 2011 and I would like to compare [To Month] dimension with my parameter #month.
By running above query I am getting below error
This depends on the client or reporting tool you're using. XMLA protocol support MDX parameters ( #month ).
However you need a client / library supporting this feature. What tool are you using ?