Sql Analysis Services Current Date - mdx

I need to perform a calculation in SSAS which only applies to the current and future months (it can't be applied retrospectively using the available data).
I can do this by using the calendar hierarchy and hard coding today's month as follows...
SCOPE([Measures].[RollingStock]);
([Dim Date].[Calendar].[Month].&[201008]:NULL) =
([Measures].[Quantity On Hand]
- [Measures].[SO Open Quantity]
+ [Measures].[PO Open Quantity]
- [Measures].[Forecasts Quantity]);
END SCOPE;
I want to replace 201008 with the current month (in that format).
Any ideas?

CREATE SET CURRENTCUBE.[Current And Future Months]
AS {
StrToMember("[Dim Date].[Calendar].[Month].&[" + Format(now(), "yyyyMM") + "]"):NULL
}

Related

How to pull next 2 quarter data with a data model that doesn't have fiscal time database and company defined fiscal year

I would like to pull all orders from the current, next and next to next quarter of the time stamp. I am able to pull up current quarter data but I am not able to pull next and next+1 quarter data.
I am currently using MS SQL Server 2013.
The time stamp also appears in a weird format. for eg.20191031_FY20_Q1_Wk1 whereas [Order Close Fiscal Year Quarter Display Code] appears as 2020-Q1. So to pull current quarter data I have used below condition:
(LEFT(Time_Stamp,2)+'20'+'-'+substring(Time_Stamp,15,2)) = [Order Close Fiscal Year Quarter Display Code]
What I logically want to do is this:
(LEFT(Time_Stamp,2)+'20'+'-'+substring(Time_Stamp,15,2)) = [Order Close Fiscal Year Quarter Display Code] + 1
(LEFT(Time_Stamp,2)+'20'+'-'+substring(Time_Stamp,15,2)) = [Order Close Fiscal Year Quarter Display Code] + 2
Obviously, I came across data type conversion error. I even tried using Dateadd() function:
[Opportunity Close Fiscal Year Quarter Display Code] = DATEADD(quarter,1, cast(left(time_stamp,8) as date) )
but still I keep coming across same error.
The MOST IMPORTANT THING I would like to HIGHLIGHT is my org's Fiscal Year is not same as generic Fiscal Year. In my org, Fiscal Year begins from Oct and ends in Sep. So I am not sure how even DateAdd() function will help. I believe having a Fiscal Time table customized as per org's Fiscal Year could be of great help for me but my manager thinks the BI team won't entertain such a request.
Any help in building this query would be really great!!
You would seem to have time_stamp values whose format is not as you describe.
You should find the column values that don't convert to a date:
select time_stamp
from t
where try_cast(left(time_stamp, 8) as date) is null and time_stamp is not null
With this information, you can debug your code or the data.

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

SSAS cube - calculations

I inherited a cube and I don't quite get what it's doing. It uses a dimension called "date tool" which really have members that have nothing to do with the facts table. It's members have calculation scripted. It plays with the time dimension, so you can get variances across time.
So one of the member calculate prior month variance (script below in Visual Studio)
For example - if I select March in the "date field", then [Prior Month Variance] from the "Date Tool" will give me the numbers for (March - February)
([DateTool].[Aggregation].[Prior Month Variance]) =
[DateTool].[Aggregation].[Previous Month] - [DateTool].[Aggregation].DefaultMember;
I want to amend this so that, when I select [month] = January, it display January only and not (January - December)
This is because (they decided to upload YTD data instead of MTD)

Return number of days till nowday

I have [Measures].[Days in the period] which calculates number of days for my Time hierarchy.
I want to create the calculated member that will return number of days until today. For example, if today is 14 of November it must show 14 days for November (not 30). And nulls for upcoming months (December, for example). There is nothing like GetDate() from SQL, so I'm a bit confused about easy way to implement it. My initial code doesn't have much:
member [Measures].[Days in the period passed] as [Measures].[Days in the period]
Resolved it myself. The code below does the job:
WITH
MEMBER [Measures].[Days in the period passed] AS
FILTER
(
Descendants([Time].[Calendar], [Time].[Calendar].[Day]),
[Time].[Day].Properties("Day key") <= FORMAT(NOW(), "yyyyMMdd")
).COUNT
EDIT: more elegant and correct way is the scope:
SCOPE
(
[Measures].[Days in the period passed], [Time].[Day].[Day].Members
);
THIS = IIF([Time].[Day].CurrentMember.Properties( "Key0" ) <= FORMAT(NOW(), "yyyyMMdd"),
[Measures].[Days in the period],
NULL);
END SCOPE;
Checking select:
SELECT
([Time].[Calendar].[Mouth]) ON COLUMNS,
(([Measures].[Days in the period passed])) ON ROWS
FROM [TestCube]

Is it possible to create a dynamically calculated field?

How can I create a dynamically calculated field to determine the age in days based on a date/time stamp?
For example say I have a field that has a date time stamp. What I want to be able to see when browsing the cube is a column showing the number of days since the time stamp. So if the date time stamp is '8/21/13 12:00PM' and I browse the cube on '8/22/13 12:00PM', the calculated column "Number of Days Since" would show 1.
Is this even possible with SSAS?
Yes, you can:
with member Measures.[date string] as
Left(Right([Date].[Date].CurrentMember.UniqueName, 9), 8)
member Measures.[date formatted] as
Left(Measures.[date string], 4) + "-" +
Mid(Measures.[date string], 5, 2) + "-" +
Right(Measures.[date string], 2)
member Measures.[date date] as
CDate( Measures.[date formatted])
member Measures.[to today] as
now() - Measures.[date date]
, format_string = 'yy" years, "m" months, "d" days"'
select {
Measures.[date string],
Measures.[date formatted],
Measures.[date date],
Measures.[to today]
}
on columns,
[Date].[Date].[Date].Members
on rows
from [Adventure Works]
gives what you want. You do of course not need all the intermediate measures, they just show step by step what is calculated.
One remark, however: I formatted the to today measure with a date format using yy. A four digit year would show as "1912 years" etc, as SSAS is internally using the date coding like Excel of number of days since Jan, 1, 1900. I mis-used the date formatting here to format a duration, which is a number and not a real date. This formatting approach is only giving correct results for positive durations less than 100 years. But this is only a formatting issue and the main duration calculation is correct nevertheless. It just shows the duration in days, and the time within the day as fractions.