How to compare year to date figures from the current year with the previous year? - ssas

With the bellow MDX query im attempting to return a comparasion of sales, but its not working
For example i want to compare the values from current year until today (29/september/2022) with the values until today's date last year (29/september/2021).
WITH
MEMBER [Measures].[Sales YTD] AS
'SUM(YTD(closingperiod([Date].[Calendar].[Date])),[Measures].[Sales])'
MEMBER [Measures].[Sales LYTD] AS
'SUM(YTD(PARALLELPERIOD([Date].[Calendar].[Calendar Year],1,closingperiod([Date].[Calendar].[Date]))),[Measures].[Sales])'
SELECT
{[Measures].[Sales YTD],[Measures].[Sales LYTD]} ON COLUMNS
...
Can please someone help ?

Related

MDX - Same day of Week Last year

Facing some trouble when comes to MDX, trying to get the Revenue PY.
Had a date dimension set up with a Year Week Date Hierarchy
when I run the below MDX, it is pointing me to 5th Sep, instead of 12th Sep.
SELECT
ParallelPeriod
(
[Date].[Date YWD].[Year]
,1
,[Date].[Date YWD].[Date].&[2017-09-11T00:00:00]
) ON 0
FROM [TestCube];
Does this mean, I have my dimension set up wrong, but the hierarchy still gives me correct dates in a week.
Above is the name column, key columns are Year Number, Week Number, Date
Any help would be much appreciated
This function is looking for same relative member a year back. First week of 2017 is W01-17 starting sunday 01-01-2017. So W37-17 is the 37th member within that year.
The first week in 2016 is W53-16 starting sunday 27-12-2015. So week 1 is the 2th member and week 36 is the 37th member.
In this hierarchy you should use the week-year instead of the date-year.

How can I show a last year value through a dimension in MDX?

We have a relative date dimension in our cube that has member values This Year and Last Year as an example.
The user uses this set on columns against sales so they can look at Sales for this year and the same period last year.
The problem comes when they are using the Calendar Date filter to only select values for this month. If the user selects just this month, then the Last Year member disappears.
Is there a way (perhaps with scope statements) that I can tell SSAS: If the user is using these attributes and they select a specific month (or other level), then use ParallelPeriod to implicitly include the same members for the previous year so that they can see the last year sales?
If not, without using calculated members (I have so many measures that I don't want to have to duplicate them), is there a way using dimensions to show a last year value even if the user selects this year in the date dimension?
There are a few options here...
I would just add a new Calculated Member to an existing dimension,i'll add it to a Pseudo-Dimension [Time Period] dimension with something like this:
(i'm pretty sure you need to add it to an existing Hierarchy. I'll assume [Relative Time])
CREATE MEMBER [Time Period].[Relative Time].[Last Year]
AS NULL
, VISIBLE=1;
SCOPE(
DESCENDTS([Time].[YearMonthDate].[Year].MEMBERS,,AFTER)
,[Time Period].[Relative Time].[Last Year]
);
THIS = AGGREGATE(
PARALLELPERIOD(
[Time].[YearMonthDate].[Year]
,1
,[Time].[YearMonthDate].CURRENTMEMBER
)
,[Measures].CURRENTMEMBER
);
END SCOPE;

MDX to calculate difference between sales amount between 2 dates

I have a Cube with a Date dimension hierarchy (Calendar,Year,Month,Week,Day) and Measure as SalesData, would like to add a calculated measure which would give me the SalesData for the selected date and the last date of the Previous Month.
Calendar Hierarchy is as follows
Date - Dimension
Calendar - Hierarchy
Year - [Date].[Calendar].[Year]
Quarter - [Date].[Calendar].[Quarter]
Month - [Date].[Calendar].[Month]
Week - [Date].[Calendar].[Week]
Day - [Date].[Calendar].[Day]
Measures
[Sales]
New Calculated Member need to be created - say [SalesMTD]
Requirement is when a user select any date say 3 March 2016, the calculated member should give Sales as follows
[SalesMTD] = [Sales] on 3 March 2016 - [Sales] on 29 Feb 2016
Can someone please help me write an MDX query for the Calculated Measure ?
Your help much appreciated.
Please try the below code.
CREATE MEMBER CURRENTCUBE.[Measures].[Sales Last Month]
AS ([Date].[Months].CURRENTMEMBER.PREVMEMBER, [Measures].[sales])
, VISIBLE = 1 ;
Then write another calculation using the above calculations
CREATE MEMBER CURRENTCUBE.[Measures].[Sales Difference]
AS ([Measures].[Revenue]-[Measures].[Sales Last Month])
, VISIBLE = 1 ;
with member measures.SalesDataLastDaylasMonth
as
[Date].[Calendar].currentmember.firstsibling.lag(1)
//or [Date].[Calendar].currentmember.firstsibling.prevmember
select {measures.SalesData, measures.SalesDataLastDaylasMonth} on 0,
[Date].[Calendar].[Day].members on 1
from [Some Cube]
Here currentmember.firstsibling.prevmember fetches the member prior to the first day in the list of days in the current month.
Also, you can obviously create this member in a cube instead of having it query scoped like this. The syntax would be similar to the above answer.
Based upon your edit
There are multiple ways of getting to it. Below are some:
with member Measures.[SalesMTD]
as
Measures.[Sales]
-
(Measures.[Sales],Ancestor([Date].[Calendar].currentmember, [Date].[Calendar].[Month]).firstchild.firstchild)
//(Measures.[Sales],[Date].[Calendar].currentmember.parent.parent.firstchild.firstchild.lag(1))
//(Measures.[Sales],[Date].[Calendar].currentmember.parent.parent.firstchild.firstchild.prevmember)
select Measures.[SalesMTD] on 0,
[Date].[Calendar].[Day].members on 1
from [Some Cube]
The first approach is the neatest.

SUM of a YTD-value by Year for several selected years

I am trying to put together an SQL statement that returns the SUM of a value by Year for several selected years, but on a year to date basis. In other words, running the report by selecting years I need the value for the selected years on a year to date basis. This could be something like this: By selecting the years 2015, 2014 until 2010 the Report should give 2015 value ytd of 25.02.2015, 2014 ytd of 25.02.2014 and so on.
Yet I only figured out how to select the Dates for each year to date value manually., but clearly the I was thinking. However, I was hoping to have some of this work handled with my SQL Statement using the selected years Parameter and add month and day somehow.
Has anyone ever tackled this type of problem with an SQL statement, and if so, what is the trick that I am missing?
My current sql statement for ytd data is similar to the following:
SELECT NON EMPTY { [Measures].[Sales Turnover YTD], [Measures].[Order Intake YTD] } ON COLUMNS FROM ( SELECT ( STRTOSET(#DateDate, CONSTRAINED)
This works perfectly for the one year, but still I am seeking the solution for several years.
As described above my desired result would be: after choosing several fiscal years (e.g. 2010-2015) I will get following values:
2015 Sales Turnover YTD Order Intake YTD on date today
2014 Sales Turnover YTD Order Intake YTD on date today one year ago
and so on..
Here is a script against AdvWrks that calculates the equivalent YTD totals for each of the years ON ROWS:
WITH
SET [NonEmptyDates] AS
NonEmpty([Date].[Calendar].[Date].MEMBERS)
SET [LastNonEmptyDate] AS
Tail([NonEmptyDates])
SET [SetDaysInCurrentYear] AS
Descendants
(
Exists
(
[Date].[Calendar].[Calendar Year]
,[LastNonEmptyDate].Item(0).Item(0)
).Item(0)
,[Date].[Calendar].[Date]
)
MEMBER [Measures].[NumDaysInCurrentYear] AS
Rank
(
[LastNonEmptyDate].Item(0)
,[SetDaysInCurrentYear]
)
MEMBER [Measures].[EquivYTD] AS
Aggregate
(
Head
(
Descendants
(
[Date].[Calendar].CurrentMember
,[Date].[Calendar].[Date]
)
,[Measures].[NumDaysInCurrentYear]
)
,[Measures].[Internet Sales Amount]
)
SELECT
{
[Measures].[Internet Sales Amount]
,[Measures].[EquivYTD]
} ON 0
,[Date].[Calendar].[Calendar Year] ON 1
FROM [Adventure Works];

MDX query producing #Error as output

In my SSAS Cube, I have a measure called [Sales Total]. What I want to do is to create another measure that would give me the lowest sales figure in the last 6 months. I want this to be a moving minimum, calculated as the min of sales of every time period from the present month to 6 months back.
I wrote my MDX statement but it produces an error and I have hard time trying to figure out why. It is something to do with aggregating Date dimension members into a filter aggregate.
When i choose a single month member from the Date hierarchy, it returns the correct value. When I select multiple members from the hierarchy, as seen below, it errors out.
Any kind of help is appreciated.
WITH
MEMBER [Measures].[Min Sales Total Rolling 6 months] as '(MIN([Date].[Fiscal Month Hierarchy].currentmember.lag(6):[Date].[Fiscal Month Hierarchy].currentmember,[Measures].[Sales Total]))'
MEMBER [Date].[Fiscal Month Hierarchy].[FilterAggregate] as
'AGGREGATE({
[Date].[Fiscal Month Hierarchy].[Quarter].&[20141].&[201310],
[Date].[Fiscal Month Hierarchy].[Quarter].&[20141].&[201311],
[Date].[Fiscal Month Hierarchy].[Quarter].&[20141].&[201312],
[Date].[Fiscal Month Hierarchy].[Quarter].&[20142].&[201401],
[Date].[Fiscal Month Hierarchy].[Quarter].&[20142].&[201402],
[Date].[Fiscal Month Hierarchy].[Quarter].&[20142].&[201403],
[Date].[Fiscal Month Hierarchy].[Quarter].&[20143].&[201404],
[Date].[Fiscal Month Hierarchy].[Quarter].&[20143].&[201405],
[Date].[Fiscal Month Hierarchy].[Quarter].&[20143].&[201406],
[Date].[Fiscal Month Hierarchy].[Quarter].&[20144].&[201407]
})'
SELECT {
[Measures].[Min Sales Total Rolling 6 months]} ON AXIS(0)
FROM [My Cube]
WHERE ([Date].[Fiscal Month Hierarchy].[FilterAggregate])
As explained in this blog by one of the SSAS developers already many years ago, multi select does not work with CurrentMember. You should use
MEMBER [Measures].[Min Sales Total Rolling 6 months] as
MIN(Tail(EXISTING [Date].[Fiscal Month Hierarchy].[Fiscal Month Hierarchy].Members).Item(0).Item(0).lag(6)
:
Tail(EXISTING [Date].[Fiscal Month Hierarchy].[Fiscal Month Hierarchy].Members).Item(0).Item(0),
[Measures].[Sales Total])
instead.
EXISTING gets the set of all selected members. This is needed as there is no single CurrentMember. Then Tail gets the set consisting of the last of these members, Item(0).Item(0) converts that single element set to a member.