SSAS MDX : Appending columns from different result sets - ssas

I've started working with SSAS cubes recently. I have a requirement where I need results from different date ranges to be shown side-by-side, for the required measures. Something like :
Last 7 Days Last 6 Months Last Full Year
Internet Sales Amount X Y Z
Reseller Sales Amount X Y Z
Any pointers towards a solution would be greatly appreciated.
Thanks,
Venu

I'm only guessing (and maybe my syntax is wrong), but perhaps something like this:
WITH
MEMBER [MYTIME][Last 7 Days] AS
'Aggregate(LastPeriods(7, StrToMember(Format(CDate(),"YYYY-MM-DD"))))'
[MYTIME][Last 6 Months] AS
'Aggregate(LastPeriods(6, StrToMember(Format(CDate(), "YYYY-MM"))))'
[MYTIME][Last Full Year] AS
'Aggregate(LastPeriods(12, StrToMember(Format(CDate(), "YYYY-MM"))))'
SELECT
{[MYTIME].[Last 7 Days], [MYTIME].[Last 6 Months], [MYTIME][.Last Full Year]} ON COLUMNS
{[Internet Sales Amount], [Reseller Sales Amount]} on ROWS
FROM MYCUBE
That one gets 12 months for the the full year, but if you mean calendar year, you'll want to do something different.

Related

Expression for - Row by row variance on grouped datasets

I am having troubles with creating an expression in SSRS.
I'd like to calculate the difference between two figures. The columns are in separate datasets and are grouped. They also show a total at the end of each group.
Eg
Dataset 1 Dataset 2
Month Workshops which Ran Month Workshops which Ran Variance
Apr 40 Apr 30 10
May 50 May 40 10
Jun 45 Jun 35 10
Q1 Total 135 Q1 Total 105 30
The quarters then carry on but, you get the picture.
Is there a way to make an expression to calculate the variance column even though the data is grouped and in different datasets?
Any help would really be appreciated :)
Will
If we assume:
There could be voids in either data set, we could use a full outer join and coalesce.
You want the absolute difference for variance (no negatives)
You want to display the month and workshops which ran in all cases.
Neither dataset would span more than 1 year's period. (if they did we would need the aggregate datasets to contain year along with month and include it on the join)
The Q1 total value (or others) exists in both data sets and is spelled the same.
.
SELECT DS1.Month as [DS1 Month]
, DS1.[Workshops which Ran] as [DS1 Workshops which Ran]
, DS2.Month as [DS2 Month]
, DS2.[Workshops which Ran] as [DS2 Workshops which Ran]
, abs(coalesce(DS1.[Workshops which Ran],0) - coalesce(DS2.[Workshops which Ran],0)) as [Variance]
FROM Dataset1 DS1
FULL OUTER JOIN Dataset2 DS2
on DS1.Month = DS2.Month
The best way is to create a dataset with all your data in one place. If you can't do this for whatever reason, and the data in the datasets is more details than the aggregated data you are showing in your example, then check this post.
http://salvoz.com/blog/2013/05/27/sum-result-of-ssrs-lookupset-function/

AVG(EXISTING()) syntax error

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) ?

MDX Top 10 based on a Column

I'm currently working with MDX in pentaho.
Currently I'm facing issue is that Unable to get top 10 count based on a Measure.
i.e., I want to write MDX query for find top 10 customer's based on Current year Sales volume and also needed Previous year sales amount for that customers.
My Problem is that MDX query Sum the Previous and Current year sales volume and listed out the top 10.
How can i get top 10 based on Current year column?
For Example,
Customer Current Year Previous Year
A 100 200
B 150 125
C 200 -
Expected Result:
Customer Current Year Previous Year
C 200 -
B 150 125
A 100 200
Please help me out.
The Topcount function takes 3 arguments, a Set, the number of items to return and a numeric expression.
Supposing you have [Customer].Members on Rows, two [Time] dimension members already defined and [Measures].[Sales] as your measure, you can use
Select
TopCount( [Customer].Members, 3, ( [Measures].[Sales], [Time].[Current Year]) ) on Rows,
{ [Time].[Current Year], [Time].[Previous Year] } on Columns
From [My Cube]
Where [Measures].[Sales]
The TopCount will return the top 3 customers, based on their rank by
([Measures].[Sales], [Time].[Current Year])
If you don't specify this tuple as the numeric expression you get the rank by the overall [Measures].[Sales] value.
Documentation: http://technet.microsoft.com/en-us/library/ms144792.aspx

Count maximum sequel of null values - mdx query

I want to create a member based on this problem
I have a Product A being sold
I want to find the largest range of consecutive days without sale
example:
days 1,2,3 the product not sale, after that,it sold for 15 consecutive days, at 19th day it didnt sell for 2 days and after that it sold every day until the end of the month
so my maximum days without sale was 3
The following query delivers in the Microsoft sample cube Adventure Works what you want:
WITH Member Measures.[days without sales] AS
IIf( [Measures].[Internet Sales Amount] > 0
, 0
,(Measures.[days without sales], [Date].[Calendar].CurrentMember.PrevMember) + 1
)
Member Measures.[Max days without sales] AS
Max( [Date].[Calendar].[Date].Members
,Measures.[days without sales]
)
SELECT { [Measures].[Max days without sales] }
ON COLUMNS
FROM [Adventure Works]
WHERE [Product].[Product].&[486]
The measure days without sales is defined recursively, and returns how many days up to and including the current member of the [Date].[Calendar] hierarchy there was no sales. You may need to adapt the criteria for "without sale", bearing in mind that in MDX, numerical comparisons treat NULL as 0 - which is different from SQL.
This measure only works correctly if there is a member in this hierarchy for each day, i. e. there are no gaps in this hierarchy. And actually, the definition is more general than just working for days: If you use months for the [Date].[Calendar].CurrentMember, it would give you the number of months without sales, etc. It works with each level of the hierarchy.
The measure Max days without sales does not contain the product in its definition, it delivers the maximum days for whatever is in context (in this case the product in the WHERE clause).
Please note that - as actually there is a loop over all days in the [Date].[Calendar] hierarchy when calculating Measures.[Max days without sales], and within that the recursion again iterates along the previous days, and all this for each cell in the result set - this may be slow for large reports.

MDX named set with non empty last child

I have a "Trend" dimension. The hierarchy is, Trend->Week->Day. The fact is linked to the "Day" key. There is no guarantee that measure value will exist for all days in a given week.
When the user wants to see the measure at "Week" level, I need to show only the last empty day value for that Week. I have multiple measures and hence I ain't interested in creating a new calculated measure for each one of them (like How to display the total of a level as the value of its last child in MDX)
Instead, is there any way to create a named set with which I can achieve the functionality as below?
Example
Week Day Measure
1 1 4
1 2 5
2 3 7
2 7 9
3 5 10
Should get at "Week" level as
Week Day Measure
1 2 5
2 7 9
3 5 10
Thanks! :)
If you want to create a calculated member that can be reused for several measures, you can create a utility dimension that will contain a bunch of calculated member only. Dunno how to do that in SSAS (I'm familiar with icCube). Then you can use this hierarchy in your requests to apply the calculated member.
Let's take your example. I've called the utility dimension [Stats]. Its default member is a calculated member returning the value of the current measure. And it contains the [Last Day] calc. member.
WITH MEMBER [Stats].[Stats].[Last Day] AS (
NonEmpty(
Order( [Trend].[Trend].currentMember.children,
[Trend].[Trend].currentMember.properties( 'key', TYPED ),
BDESC
),
[Stats].[Stats].defaultMember
).item(0),
[Stats].[Stats].defaultMember )
SELECT
[Measures].members on 0,
[Trend].[Trend].[Week].members on 1
FROM [your-cube]
WHERE [Stats].[Stats].[Last Month]
You can see the trick with [Last Month] in the slicer that is applied to each [MEasures] of the SELECT. Then its formula is using a NonEmpty of the [Days] (reversed with the order() based on the key - you might need to adjust) for the current [MEasures].