How to get Next available Date in MDX - mdx

I would like to get next available date for next month based on working day.
I am using Businessday = 1 as a working day.
Next available date for September is 09/03/2019 for running MDX on 08/31/2019.
I tried:
[Time].[Date].NextMember.MEMBER_VALUE
without any filter and it returns 09/1/2019.
If I do
[Time].[Date].LEAD(2).MEMBER_VALUE
then it give 09/02/2019 but this date is not marked as a business day according to my Businessday = 1
The result should be 09/03/2019

Here is the example that I made on Adventure Works cube - just instead of Businessday measure I am using [Measures].[Internet Order Count] measure.
So, in this example, for each month, calculated member [Measures].[NextWorkingDay] is showing first date in the next month that has Internet Order Count = 8 (you will just say Businessday = 1)
WITH MEMBER [Measures].[NextWorkingDay] AS
FILTER(Descendants([Date].[Calendar].Currentmember.Lead(1),,leaves)
, [Measures].[Internet Order Count]= 8).ITEM(0).NAME
SELECT { [Measures].[NextWorkingDay]
} ON 0,
[Date].[Calendar].[Month].MEMBERS ON 1
FROM [Adventure Works]

Related

Calculated member in SSAS based on custom Last Year Date (Retail calendar)

I have spent a lot of time solving this issue, but no success yet.
I have a Date dimension called [Dim Date] and in this table I have a [Date Key] which contains all the dates from '2013-07-01' to '2016-12-31'. I also have a measure called [Retail Revenue] which is a decimal number for that date. So far so easy!
We are a retail Calendar and all our calculations/comparisons are based on a retail calendar (which is a customized table in DW).
The date hierarchy in this calendar is as below (please see the screenshot):
-- retail year (e.g. 2017)
---- retail half year (e.g. 2017-H1)
------ retail quarter year (e.g. 2017-Q1)
-------- retail month (e.g. 201702) (months from 201701 to 201712)
---------- retail week (e.g. 201708) (weeks from 201701 to 201752)
------------ date key (e.g. 2016-08-22)
SCREENSHOT
We also have an attribute called "Retail Last Year Date" which shows the equivalent date of last calendar date (e.g. 2015-08-24 in the screenshot).
I need to have a calculated member showing the "Retail Revenue" for last year (based on the attribute "Retail Last Year Date"), next to the regular "Retail Revenue" for [date key].
I tried to use ParallelPeriod and Scope, and could not get the numbers properly. Probably an easy task but I am not a hero in mdx unfortunately!
Will be more than thankful if anyone please can help me out with this.
Thanks
Rez
Hopefully this helps.
This is against AdvWrks. I've created a query scoped custom measure that gets the internet sales from the year before:
WITH
MEMBER [Measures].[Internet Sales Amount LastYear] AS
(
ParallelPeriod
(
[Date].[Calendar].[Calendar Year]
,1
,[Date].[Calendar].CurrentMember
)
,[Measures].[Internet Sales Amount]
)
SELECT
{
[Measures].[Internet Sales Amount]
,[Measures].[Internet Sales Amount LastYear]
} ON 0
,
[Date].[Calendar].[Date].&[20070121]
:
[Date].[Calendar].[Date].&[20070127] ON 1
FROM [Adventure Works];
Here are the results:
Thanks for your response, but I didn't get how it uses the dimension property? because my calendar is customized and I cannot assume the last year calendar date is the same as this year. I have to read it from the dimension property "Retail Last Year Date").
Thanks

MDX - Calculated MTD measure returning null value

I have a Cube with a Date dimension hierarchy (Year,Semester,Quarter,Month,Day).
There are 2 measures available in the datawarehouse.
[AUD DLY] : Daily numbers
[AUD YTD] : Daily incremental YTD numbers
Hierarchy is as follows
Date - Dimension
Financial - Hierarchy
Year - [Date].[Financial].[Year]
Semester - [Date].[Financial].[Semester]
Quarter - [Date].[Financial].[Quarter]
Month - [Date].[Financial].[Month]
Day - [Date].[Financial].[Day]
Measures [AUD DLY]
Measures [AUD YTD]
I need to add a MTD field in the measures such that when the business select a particular date on their slicer in excel for e.g., 3 March 2016, MTD should be calculated as either of the following ways :
1) [AUD MTD] should be calculated by subtracting [AUD YTD] on
last day of previous month from the current selected date.
So if we select 3 March 2016 then
[AUD MTD] = [AUD YTD] on 3 March 2016 - [AUD YTD] on 29 Feb 2016
OR
2) [AUD MTD] should be calculated by adding the [AUD DLY] from first day
of the current month until the selected date in that month.
So if we select 3 March 2016 then
[AUD MTD] = SUM ([AUD DLY] from 1 March 2016 to 3 March 2016)
I created a New Calculated Member from Calculations tab in the Cube designer in the BIDS 2010. The MDX query is below. However when I try to browse the cube the [AUD MTD] values are only returning nulls.
Can someone please help what am I doing wrong ?
CREATE MEMBER CURRENTCUBE.[Measures].[AUD MTD]
AS Aggregate
(
PeriodsToDate
(
[Date].[Financial].[Month]
,[Date].[Financial].CurrentMember
)
,[Measures].[AUD DLY]
),
FORMAT_STRING = "Currency",
NON_EMPTY_BEHAVIOR = { [AUD DLY] },
VISIBLE = 1 , DISPLAY_FOLDER = 'AUD Values' , ASSOCIATED_MEASURE_GROUP = 'Measures' ;
Also the business would be using the new calculated measure in excel using the Slicer, they want to select any date and be able to view the MTD value for that month.
Also can someone please help with MDX query for both methods (1) and (2) ?
Your help much appreciated.
I think the problem with your quoted code (method 2) is that [Date].[Financial].CurrentMember must be at a level at or below the [Date].[Financial].[Month] level. Otherwise PeriodsToDate returns an empty set.
So the problem is not in your calculated member definition, but somewhere in the query in which it's being used. In that query, [Date].[Financial].CurrentMember may be returning a member at a level above Months. Hard to see without seeing the query itself.
Method (1) is more fiddly. You can get the last day of the previous month with
Ancestor([Date].[Financial].CurrentMember,[Date].[Financial].[Month]).PrevMember.LastChild
but you'd have to build in some logic for days in the first month of the financial year, which would otherwise subtract the value for a previous fin year from this year's YTD value. So I'd recommend method (2).
As far as I know selecting a date (I mean a date, not a month) in the Excel slicer will make that date the .CurrentMember. I'm a bit hesitant because Excel does generate some deeply bizarre MDX sometimes.
EDIT: Another possible problem is a hierarchy mismatch. You can select a perfectly good Day in a date hierarchy, but if it isn't in exactly the hierarchy you specify in your calculated member definition, you can get weird results. IMHO more recent versions of SSAS encourage a proliferation of attribute hierarchies and multiple "real" hierarchies, making this a real problem.
As a test can you please add this very simple measure to make sure that currentmember is behaving as expected:
CREATE MEMBER CURRENTCUBE.[Measures].[AUD MTD]
AS [Date].[Financial].CurrentMember.member_caption
If when you use the above all it returns is the All member then you know something is wrong.
You need to double-check your relationships and datatypes used within your date hierarchies as this is often the reason for time calculation problems.
This is an alternative to your measure but I suspect if the original script is not working then neither will this...
CREATE MEMBER CURRENTCUBE.[Measures].[AUD MTD]
AS SUM
(
MTD([Date].[Financial].CurrentMember)
, [Measures].[AUD DLY]
)

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.

mdx first value of a period

I have a cube with a 'sales' measure, where we have the amount of sold units each day. We have a time dimension with 'Year > week > day'.
I need to have the sales for the first and the last day in a period with non empty sales for a calculated field. I tried with OPENINGPERIOD and CLOSINGPERIOD but they return for a week period just Monday and Sunday... the problem is that sometimes Monday is holiday and Sunday is always holiday and I get empty data (we have no sales on holidays)... Here is my example for last day of a period:
WITH
MEMBER
[Measures].[sales end] AS
'((CLOSINGPERIOD([Time.Weeks].[Day],[Time.Weeks].CurrentMember)
, [Measures].[sales]))'
SELECT
{[Measures].[sales], [Measures].[sales end]} ON COLUMNS,
NON EMPTY
({[Time.Weeks].[Week].Members}) ON ROWS
FROM [cubSales]
I need to get the last day with non empty sales in that period... it should be Friday, but if Friday is holiday, it should be Thursday... I mean the last day for the week with a value for the measure sales..
An easy solution could be to just omit holiday days from your time dimension.
Mondrian has no concept of holidays. I would implement my own MDX function with a built-in list of holidays. It's a flexible approach which can be extended to support different holidays from different countries.
Tested on our cube and seems to work ok (just doesn't say anything about holidays!)
SELECT
{
[Measures].[sales]
} ON COLUMNS
,NON EMPTY
Generate
(
[Time.Weeks].MEMBERS
,Tail
(
NonEmpty
(
[Time.Weeks].CurrentMember * [Time.Weeks].[Day].MEMBERS //<<I think .members is generally default but should do the same thing if we are explicit
,[Measures].[sales]
)
)
) ON ROWS
FROM [cubSales];

MDX query to display this year's sale along with the last year's sale

Here's my query. I want to display this year's data from week 12 to week 24 along with last year's data from week 12 to week 24. But I got the same data for both years even I had the "prevMember". Could you help me?
WITH MEMBER [Measures].[Net Sales This Year] AS ([Measures].[Ticket Net Sales],[Date].[Fiscal].[Fiscal Year].currentmember.prevmember)
SELECT {[Measures].[Ticket Net Sales],[Measures].[Net Sales Last Year]} on 0,
NON EMPTY {([Concepts].[Concept Name].[Concept Name],[Locations].[Department Name].[Department Name],
[Date].[Fiscal].[Fiscal Year].Members,[Date].[Week Of Year].&[12]:[Date].[Week Of Year].&[24])} on 1
FROM spbi
You can use the PARALLELPERIOD() function to achieve this.
See http://technet.microsoft.com/en-us/library/ms145500
Could you try this query:
WITH MEMBER [Measures].[Net Sales This Year] AS ([Measures].[Ticket Net Sales],[Date].[Fiscal].CurrentMember.PrevMember)
SELECT {[Measures].[Ticket Net Sales],[Measures].[Net Sales Last Year]} ON 0,
NON EMPTY [Date].[Fiscal].[Fiscal Year].Members * [Date].[Week Of Year].&[12]:[Date].[Week Of Year].&[24]) ON 1
FROM spbi