mdx first value of a period - mdx

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

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;

DAX Counting Values in previous period(s)

I have a Month Column with the Month Field populated for each line for the 100K of lines of data I have.
I need to count the amount of times the Month Field is populated in the Previous Month (Period).
I also need to count the total amount of times the Month Field is populated in the Previous 11 months as well.
This is a rolling count for each months reporting that I do..
table name: 'ws pds' and field name [Month Tagged]
You can utilize the powerful time intelligence functions in DAX such as PARRALLELPERIOD to look at values from previous months. But in order to make use of these functions you need to create a calendar/date entity. Mark that entity as a Date table. And join to it by date from your "ws pds" table. The Date dimension should span the timeframe of your date with a continuous list of dates, one row per day.
Then your measure could look like this:
PreviousMonthCount=
CALCULATE (
COUNTROWS ( 'ws pds' ),
'ws pds'[Month Tagged] <> BLANK (),
PARALLELPERIOD ( Calendar[Date], -1, MONTH )
)

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

Clients growth over time MDX

I want to calculate the clients growth over the time.
So every day i have the total clients per state and per product subscription, and i can calculate the total for every day.
If i want to calculate the growth every day i don't have problems because i use a calculated member with
[Date].CurrentMember-[Date].PrevMember
This works pretty fine, but now i want to calculate the growth on month. So i have to sum all day growths of the month to calculate the month growth, right?
But my problem is that i'm too newbie to MDX and i can't find a way to produce that result (I want to know how many clients i have more or less over the year).
My intuition says that i need to sum all day's growth in the agregate date.
Could you help me?
If your date hierarchy has a month level above the date level (eg. Year-Month-Day), your cube will already have pre-processed this value. I would use ANCESTOR and LAG to get the data for a given day's month:
WITH MEMBER [Date].[YMD].[Current Month] AS
ANCESTOR(
[Date].[YMD].CurrentMember,
[Date].[YMD].[Month Level]
)
MEMBER [Date].[YMD].[Growth this month] AS
(
[Date].[YMD].[Current Month]
-
[Date].[YMD].[Current Month].LAG(1)
)
This will, however, only get the data from a whole-month period.
If what you're after is all the data between a particular day and the same day in the previous month, then PARALLELPERIOD is your go-to function (sidenote: not a goto statement). PARALLELPERIOD(Level, N, Member) will look at the position of Member amongst its siblings, then go to its ancestor at Level, go N members prior to that, and traverse back down to a member in the same relative position as Member.
In other words, it looks up your date in a prior month, year or whatev'.
WITH MEMBER [Date].[YMD].[One Month Ago Today] AS
PARALLELPERIOD(
[Date].[YMD].[Calendar Month],
1,
[Date].[YMD].CurrentMember
)
MEMBER [Date].[YMD].[All data since today last month] AS
(
/* The [Member]:[Member] syntax here is a range */
[Date].[YMD].[One Month Ago Today] : [Date].[YMD].CurrentMember
)
MEMBER [Date].[YMD].[Two Months Ago Today] AS
PARALLELPERIOD(
[Date].[YMD].[Calendar Month],
2,
[Date].[YMD].CurrentMember
)
MEMBER [Date].[YMD].[All data between today last month and today in the previous month] AS
(
[Date].[YMD].[Two Months Ago Today] : [Date].[YMD].[One Month Ago Today]
)
MEMBER [Date].[YMD].[Growth in the last month since the previous month] AS
(
[Date].[YMD].[All data between today last month and today in the previous month]
-
[Date].[YMD].[All data since today last month]
)
Hope this helps.
<3