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.
Related
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]
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:
I newbie in olap using AdventureWorksDW2008R2 db and Adventure Works 2008R2 olap with Adventure Works cube.
I want to write an mdx query as per the result using T-SQL: -
select
FactResellerSales.OrderDateKey,
SUM(FactResellerSales.SalesAmount) as 'Reseller Sales Amount'
from FactResellerSales with (nolock)
inner join DimEmployee on DimEmployee.EmployeeKey = FactResellerSales.EmployeeKey
where
FactResellerSales.OrderDateKey=20070901
and DimEmployee.FirstName='Jae' and DimEmployee.LastName='Pak'
group by FactResellerSales.OrderDateKey
The result:
OrderDateKey Reseller Sales Amount
20070901 415046.9295
Using the mdx query, I can only execute the following: -
SELECT [Measures].[Reseller Sales Amount] ON COLUMNS,
NON EMPTY [Date].[Date].members ON ROWS
FROM [Adventure Works]
WHERE [Employee].[Employees].&[291]
The result is from July 1, 2006 to June 1, 2008 with the respective Reseller Sales Amount summary for each day.
My questions are, within the mdx query WHERE clause how do I:
a) Filter by date which is September 1, 2007? It doesn't accept the date dimension.
b) Filter by Employee using the name 'Jae B. Pak' which is in a multi-level hierarchy instead of using the EmployeeKey which is 291.
You need to crossjoin the employee dimension with the date you are interested in.
The following will give you just the value;
SELECT { [Measures].[Reseller Sales Amount] } ON COLUMNS
FROM [Adventure Works]
WHERE ({ [Employee].[Employee Department].[Employee].[Jae B. Pak]},
{[Date].[Date].&[20070901] })
The where clause in MDX is a slicer which behaves differently from the SQL where clause (worth reading about). The following cross join on rows would provide the same result without using the slicer, but will include the dimension attributes within the result.
SELECT { [Measures].[Reseller Sales Amount] } ON COLUMNS,
CROSSJOIN({ [Employee].[Employee Department].[Employee].[Jae B. Pak]}, {[Date].[Date].&[20070901] }) ON Rows
FROM [Adventure Works]
and finally, if you want to return based on a 'SQL Like', you can use instr (the following gives all employees where the name include ja)
SELECT [Measures].[Reseller Sales Amount] ON COLUMNS,
{FILTER([Employee].[Employees].allmembers,
instr([Employee].[Employees].currentmember.member_caption,'Ja')>0) * [Date].[Date].&[20070901]} ON ROWS
FROM [Adventure Works]
hopefully that will get you going
How do I use percentile (let's say 99% percentile) as a measure in SSAS cube? I'd like to be able to report on it using many different dimensions and filters so pre-calculating in sql is not an option.
If you want to evaluate percentiles over a dimension, then you can do it without having to pre-calculate anything in SQL. This can be done using a combination of the TopPercent and Tail MDX functions.
For example, say you want to find the 25th percentile over a [Date].[Calendar Month] dimension attribute by the [Measures].[Avg Daily Census] (I work in health care) measure. You could use the following query to do this.
WITH
MEMBER [Measures].[25th Percentile] AS
(
Tail(
TopPercent(
[Date].[Calendar Month].[Calendar Month],
100 - 25,
[Measures].[Avg Daily Census]
)
).Item(0),
[Measures].[Avg Daily Census]
)
SELECT
[Measures].[25th Percentile] ON 0
FROM [Census]
This query uses TopPercent to find the top 75 percent of values along the [Calendar Month] dimension attribute, finds the member with the lowest value along that set, and then evaluates the measure at that member.
Now, if you want to generate the entire set of percentile values, you can use a sequence utility dimension (just a dimension containing numbers), or you could create a custom percentile dimension containing 0-99. The following query would generate the values of the measure over all percentages.
WITH
MEMBER [Measures].[Percentile] AS
(
Tail(
TopPercent(
[Date].[Calendar Month].[Calendar Month],
100 - [Sequence].[Ones].CurrentMember.MemberValue,
[Measures].[Avg Daily Census]
)
).Item(0),
[Measures].[Avg Daily Census]
)
SELECT
[Measures].[Percentile] ON 0,
[Sequence].[Ones].&[0] : [Sequence].[Ones].&[99] ON 1
FROM [Census]
However, if you are trying to evaluate percentiles at the grain of the fact table, then I believe you will either need to pre-calculate the percentile values for each fact row or add a degenerate dimension and use the method above.
I want to create a barchart with a bar for each month and some measure.
But i also want to filter on a range of day which might not completly overlap some of the month.
When that happen I would like the aggregate count for those month to only aggregat over the days that fall in my date range not get the aggregate for the whole month.
Is that possible with MDX and if it is how should the request look like?
Create a second time dimension, using a virtual dimension of the original dimension. Use one dimension in the WHERE and another in the SELECT.
This often happens anyway if some people want 'Business Time' of quarters and periods, and others prefer months. Or if you have a financial year which runs April-April.
You can use subselect. You can find more information on this page and this page:
When a member is specified in the axis clause then that member with
its ascendants and descendants are included in the sub cube space for
the subselect; all non mentioned sibling members, in the axis or
slicer clause, and their descendants are filtered from the subspace.
This way, the space of the outer select has been limited to the
existing members in the axis clause or slicer clause, with their
ascendants and descendants as mentioned before.
Here is an example:
SELECT { [Customer].[Customer Geography].[Country].&[Australia]
, [Customer].[Customer Geography].[Country].&[United States]
} ON 1
, {[Measures].[Internet Sales Amount], [Measures].[Reseller Sales Amount]} ON 0
FROM ( SELECT {[Customer].[Customer Geography].[Country].&[Australia]
, [Customer].[State-Province].&[WA]&[US]} ON 0
FROM [Adventure Works]
)
The result will contain one row for Autralia and another one for the United States. With the subselect, I restricted the value of United Stated to the Washington state.
One way I found to do it with Mondrian is as follow
WITH MEMBER [Measures].[Units Shipped2] AS
SUM
(
{
exists([Store].[Store Country].currentmember.children,{[Store].[USA].[WA],[Store].[USA].[OR]})
},[Measures].[Units Shipped]
)
MEMBER [Measures].[Warehouse Sales2] AS
SUM
(
{
exists([Store].[Store Country].currentmember.children,{[Store].[USA].[WA],[Store].[USA].[OR]})
},[Measures].[Warehouse Sales]
)
SELECT
{[Measures].[Units Shipped2],[Measures].[Warehouse Sales2]} ON 0,
NON EMPTY [Store].[Store Country].Members on 1
FROM [Warehouse]
I am not sure if the filtering will be done in SQL like below and give good performance or be run locally
select Country, sum(unit_shipped)
where state in ('WA','OR' )
group by Country