Not sure if this is the right place for MDX question but it seemed to be the most appropriate.
I have a question about MDX and the AVG function.
I would like to compute the average sale amount by day across several month for a year.
So I would like to compute the AVG of the 2010/01/01, 2010/02/01, 2010/03/01, etc... and this for everyday of the month.
Can anyone give me a hint on how I'd be able to do that ?
I would go for something that looks like this
WITH MEMBER [Measures].[Total] AS AVG(DESCENDANTS([Time].[2010], [Day]),[Measure].[Sale])
Thank you,
UPDATE
I have open a new question with a clearer explanation of my problem and study case.
Please find it at : MDX: avg advanced use
You are on the right track. You can compute the average with:
WITH
MEMBER [Measures].[Average Sales] AS
AVG(DESCENDANTS([Time].[Calendar].CurrentMember,
[Time].[Calendar].[Date]),
[Measure].[Sale])
SELECT
{
[Measures].[Average Sales]
} ON 0,
{
[Time].[Calendar].[Month]
} ON 1
FROM [YourCube]
This will give you the average for each member of the Calendar hierarchy of the Time dimension which you select. It will work for Years, Quarters, Months, etc and will average the Sale measure over days under the specified members. In your case you can just select Month on ROWS or COLUMNS as shown in the code sample.
Related
I am a newb when it comes to MDX so please bear with me as I try to explain this.
I have cube with a measure for cost [Measure].[Cost] and the query is setup with a time parameter to obtain the total cost up to that point in time. #ToAcctDate and is used in the FROM statement as such:
FROM (SELECT (STROSET(#ToAcctDate, CONSTRAINED)) ON COLUMNS
but I would like to get the PREVIOUSMEMBER if possible and to something like
WITH
MEMBER [Measures].[PriorPeriod] AS
SUM( (STROSET(STROSET(#ToAcctDate, CONSTRAINED).PREVMEMBER), [Measures].[Cost])
so that I can then have both the YTD costs as of #ToAcctDate and the YTD Costs at the beginning of the period [Measures].[PriorPeriod] in the same query without unions. is this possible? and if so, is this the right approach?
Maybe along the lines of this?
HEAD will find the first member of the set which you can then apply PREVMEMBER to:
SUM(
HEAD(STROSET(#ToAcctDate, CONSTRAINED)).ITEM(0).ITEM(0).PREVMEMBER)
, [Measures].[Cost]
)
Would much appreciate any help on this.
I have a measure called "Sales" populated with values, however i am trying to turn the "Sales" value to 0, whenever the "Sales Flag" is set to 0.
Important Note: The Sales Flag is based on Date (lowest level of detail).
The difficulty that i am really experiencing and cant get a grip on, is how i am trying the display the MDX outcome.
As explained above, i would want to make the "Sales" value 0 whenever we have a 0 in the "Sales Flag" (which is based on the Date), but when I run the MDX Script I would wan't on the ROWS to NOT display the Date, but instead just the Week (higher Level to Date), as below shows:
I really have spent hours on this and can't seem to understand how we can create this needed custom Sales measure based on the Sales Flag on the date level, but having the MDX outcome display ROWS on Week level.
Laz
You need to define the member in the MDX before the select. Something like that:
WITH MEMBER [Measures].[Fixed Sales] as IIF([Sales Flag].currentMember=1,[Sales], 0)
SELECT [Measures].[Fixed Sales] on 0, [Sales Flag] on 1 from [Cube]
I am writing the code without SSAS here so it might not be the 100% correct syntax but you can get the general idea ;)
You can add the iif in the SELECT part but I find creating member to be the cleaner solution.
SELECT IIF([Sales Flag].currentMember=1,[Sales], 0) on 0, [Sales Flag] on 1 from [Cube]
If you have a control over the cube in SSAS you can create a calculated member there and you can access it easier.
Glad to hear if Veselin's answer works for you, but if not...
Several approaches are also possible.
Use Measure expression for Sales measure:
Use SCOPE command for Day level (if it's Key level of Date dimension). If it's not a key level you have to aggregate on EVERY level (week, year etc) to emulate AggregateFunction of Sales measure but with updated behavior for one flag:
SCOPE([Date].[Your Date Hierarchy].[Day].members,[Measures].[Sales]);
THIS=IIF([Sales Flag].CurrentMember = 1,[Measures].[Sales],0);
END SCOPE;
Update logic in DSV to multiply Sales column by SalesFlag. This is the easiest way from T-SQL perspective.
I want to calculate sales for promotion using it's date. I need 3 measures, avg sales from 21 days before promotion start date, sales in between of promotion's start and end date, and sales from 21 days after promotion's end date.
Why Visual Studio highlights avg in code below?
CREATE MEMBER CURRENTCUBE.[Measures].[Sales in promotion]
AS Avg(Existing([Promotion].[Promotion name].[Promotion name]),[Measures].[Sales]), ...
Same in here:
CREATE MEMBER CURRENTCUBE.[Measures].[Sales before promotion]
AS (EXISTING([Promotion].[Promotion name].[Promotion name]), AVG(strtomember("[Date].[Date].&["+ [Promotion].[Date].currentmember.member_key+"]").lag(21) : strtomember("[Date].[Date].&["+ [Promotion].[Date From].currentmember.member_key+"]"),
[Measures].[Sales])) ...
If I do sum(existing()) in first measure, the sum is calculated correctly, but it doesn't allow me to get average.
EXISTING will only help if [Promotion] is part of your query in either the WHERE or SELECT clause. If it is not included in either of these clause then EXISTING will be finding 1 member - the All member.
You could try NonEmpty and maybe move the period logic into a custom set?
WITH
SET [PERIOD] AS
STRTOSET(
"[Date].[Date].&["+ [Promotion].[Date].currentmember.member_key+"].lag(21)
:
[Date].[Date].&["+ [Promotion].[Date From].currentmember.member_key+"]"
)
From the code you posted I cannot tell if you want a daily average or and average per promotion ? Say there were 2 promotions over the 21 days does this mean you want (Total/2/21) ?
I am a complete newbie to MDX and have been asked to work out a calculation that I am finding difficult. I've read up the basics on the language and looked at online articles but struggling to find something which can help me.
I have a Measure of TotalLogins on a day to day basis.
I have a Dimension of Date, that has Date/Month/Year/DayOfWeek attributes.
What I need to do is get the average TotalLogins over a particular month, but with the caveat of excluding Saturdays/Sundays.
I've been playing with the AVG function, the EXCEPT function and the IIF function, but cant seem to hit the nail on the head.
Here's an example of what I've been attempting:
WITH
MEMBER [Measures].[MyAvg] AS
(
AVG(Descendants ([Dim Date].[Date].CurrentMember,
[Dim Date].[Date]),
[Measures].[UniqueVisitsDay])
)
SELECT
{[Measures].[MyAvg]} ON COLUMNS,
{NONEMPTY([Dim Date].[Year Month].Members)} ON ROWS
FROM
[MyCube];
This gives me an average per month, but doesnt allow me to specify a range of months to fall between and doesnt allow me to filter the totals so that Sat/Sun arent included.
Can someone point me in the right direction?
Thanks in advance!!
Try this:
WITH MEMBER [Measures].[MyAvg] AS
AVG(
EXCEPT([Dim Date].[Day Of Week].Members,
{[Dim Date].[Day Of Week].[Saturday],[Dim Date].[Day Of Week].[Sunday]}
), [Measures].[UniqueVisitsDay]
)
SELECT { [Measures].[MyAvg] } ON COLUMNS,
NON EMPTY { [Dim Date].[Year Month].Members } ON ROWS
FROM
[MyCube];
If you want to limit your results to a specific date range, replace the 3rd row from the bottom with something like this:
NON EMPTY { [Dim Date].[Year Month].[2013 November]:[Dim Date].[Year Month].[2014 January] } ON ROWS
(of course you might need to replace the member names with their actual names, or better yet, use the keys, for example: [Dim Date].[Year Month].&[201311])
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!