How to sum next periods in MDX - ssas

I've created this calculated member to sum next periods and it's quite slow. Instead if I do the same thing for the past previous periods with LastPeriods calculation, it runs smoothly.
Any ideas why it's happening? Is there any other function for next periods?
WITH
MEMBER [Measures].[Avg Dmd BUM 4Months]
AS
Avg(
{([Date].[Calendar].currentMember,[Measures].[Dmd Fcst BUM])
,([Date].[Calendar].currentMember.lead(1),[Measures].[Dmd Fcst BUM])
,([Date].[Calendar].currentMember.lead(2),[Measures].[Dmd Fcst BUM])
,([Date].[Calendar].currentMember.lead(3),[Measures].[Dmd Fcst BUM])
}
)

Maybe try the more traditional format for Avg - with a second argument:
Avg(
{[Date].[Calendar].currentMember
,[Date].[Calendar].currentMember.lead(1)
,[Date].[Calendar].currentMember.lead(2)
,[Date].[Calendar].currentMember.lead(3)
}
,[Measures].[Dmd Fcst BUM]
)
You could use lag with negative numbers and also the range operator ':'
Avg(
[Date].[Calendar].currentMember: [Date].[Calendar].currentMember.lag(-3)
,[Measures].[Dmd Fcst BUM]
)

Related

Get schedules of particular date from date ranges with MDX query

I already posted this problem many times, but unfortunately nobody could understand, I m sorry for my poor english :(
I reformulate ...
I have following fact table
I want to get records that match with a particular date and day of week (JOUR) in all dates range (DATE_DEB, DATE_FIN)
I can do that in SQL like this:
SELECT DATE_DEB,
DATE_FIN,
ID_HOR,
to_char(HR_DEB,'hh24:mi:ss') as HR_DEB,
to_char(HR_FIN,'hh24:mi:ss') as HR_FIN,
JOUR
FROM GRP_HOR HOR, GRP
WHERE GRP.ID_ACTIV_GRP = HOR.ID_ACTIV_GRP
AND TO_DATE('1998-01-08', 'YYYY-MM-DD') between DATE_DEB and DATE_FIN
AND 1 + TRUNC(TO_DATE('1998-01-08', 'YYYY-MM-DD')) - TRUNC(TO_DATE('1998-01-08', 'YYYY-MM-DD'), 'IW') = JOUR
So, I'll get 29 records (see below) which included in each range and match with the day of week (JOUR ), after that I want to enlarge them by hours (HR_DEB, HR_FIN).
The problem is, what's how the best way to do this ?
Create 2 date dimension and link them with DATE_DEB, DATE_FIN.
Create 2 Time dimension and link them with HR_DEB, HR_FIN.
How can I implement the between SQL clause in MDX ? Or geater than or Less than ?
Thank you in advance.
OUTPUT :
To specify a range of dates in MDX you just use the colon operator :
Here is the documentation on MSDN: https://learn.microsoft.com/en-us/sql/mdx/range-mdx?view=sql-server-2017
This is the example they give:
With Member [Measures].[Freight Per Customer] as
(
[Measures].[Internet Freight Cost]
/
[Measures].[Customer Count]
)
SELECT
{[Ship Date].[Calendar].[Month].&[2004]&[1] : [Ship Date].[Calendar].[Month].&[2004]&[3]} ON 0,
[Product].[Category].[Category].Members ON 1
FROM
[Adventure Works]
WHERE
([Measures].[Freight Per Customer])

MDX - Running Sum over months limited to an interval

I have a query that after some sweating and some swearing works
WITH
MEMBER [Measures].[m_active] AS ([Measures].[CardCount], [Operation].[Code].[ACTIVATION])
MEMBER [Measures].[m_inactive] AS ([Measures].[CardCount], [Operation].[Code].[DEACTIVATION])
MEMBER [Measures].[p_active] AS
SUM(
[Calendar.YMD].[2016].[January]:[Calendar.YMD].CurrentMember,
[Measures].[m_active]
)
MEMBER [Measures].[p_inactive] AS
SUM(
[Calendar.YMD].[2016].[January]:[Calendar.YMD].CurrentMember,
[Measures].[m_inactive]
)
MEMBER [Measures].[tot_active] AS (
SUM({[Calendar.YMD].[2010].Children}.Item(0):[Calendar.YMD].CurrentMember, [Measures].[m_active]) -
SUM({[Calendar.YMD].[2010].Children}.Item(0):[Calendar.YMD].CurrentMember, [Measures].[m_inactive])
)
MEMBER [Measures].[p_tot_active] AS
SUM(
[Calendar.YMD].[2016].[January]:[Calendar.YMD].CurrentMember,
[Measures].[tot_active]
)
SELECT
{[Measures].[m_active], [Measures].[p_active], [Measures].[m_inactive], [Measures].[p_inactive], [Measures].[tot_active], [Measures].[p_tot_active]} ON COLUMNS,
NonEmptyCrossJoin(
{Descendants([Calendar.YMD].[2016].[January]:[Calendar.YMD].[2017].[August], [Calendar.YMD].[Month])},
{Descendants([CardStatus.Description].[All CardStatus.Descriptions], [CardStatus.Description].[Description])}
) on ROWS
FROM [Cube]
What I obtain is a table that for each months show the activation and deactivation relative to that month, the accumulated activations relative to the period considered (starting from 1 January 2016 and ending 1 August 2017) and the total active cards from the beginning of time (january 2010) until the end time interval.
This interval is parametrized and the day are to be considered, with this query all the activations made in august are considered even the ones made after the 1st.
I try to make some modifications like this.
WITH
MEMBER [Measures].[m_active] AS ([Measures].[CardCount], [Operation].[Code].[ACTIVATION])
MEMBER [Measures].[m_inactive] AS ([Measures].[CardCount], [Operation].[Code].[DEACTIVATION])
MEMBER [Measures].[p_active] AS
SUM(
[Calendar.YMD].[2016].[January].[1]:[Calendar.YMD].CurrentMember,
[Measures].[m_active]
)
MEMBER [Measures].[p_inactive] AS
SUM(
[Calendar.YMD].[2016].[January].[1]:[Calendar.YMD].CurrentMember,
[Measures].[m_inactive]
)
MEMBER [Measures].[tot_active] AS (
SUM({[Calendar.YMD].[2010].[January].Children}.Item(0):[Calendar.YMD].CurrentMember, [Measures].[m_active]) -
SUM({[Calendar.YMD].[2010].[January].Children}.Item(0):[Calendar.YMD].CurrentMember, [Measures].[m_inactive])
)
MEMBER [Measures].[p_tot_active] AS
SUM(
[Calendar.YMD].[2016].[January].[1]:[Calendar.YMD].CurrentMember,
[Measures].[tot_active]
)
SELECT
{[Measures].[m_active], [Measures].[p_active], [Measures].[m_inactive], [Measures].[p_inactive], [Measures].[tot_active], [Measures].[p_tot_active]} ON COLUMNS,
NonEmptyCrossJoin(
{Descendants([Calendar.YMD].[2016].[January]:[Calendar.YMD].[2017].[August], [Calendar.YMD].[Month])},
{Descendants([CardStatus.Description].[All CardStatus.Descriptions], [CardStatus.Description].[Description])}
) on ROWS
FROM [Cube]
But I get this error on the relative fields:
#ERR: mondrian.olap.fun.MondrianEvaluationException: Members must belong to the same level
How can i solve this? Thanks.

MDX Running Totals with parameters

I am trying to calculate running totals for a date range which is passed through parameters in SSRS. I know i need to use lag function, but i am not able to get it right. I tried using the following
WITH MEMBER [Measures].[Rolling12Months] AS
SUM(
[Reporting Period].[Fiscal].[Fiscal Month Name].&[1]&[2013] : [Reporting Period].[Fiscal].CURRENTMEMBER,
[Measures].[Amount]
)
Below script will do what #whytheq's script does, only dynamically. It figures out the last 12 months based on the current member, which you can pass on as a slicer, or when you are in a set context, you might not need the slicer axis. I am assuming that's why you thought you needed the LAG function.
WITH SET Last12Months AS
{[Reporting Period].[Fiscal].CURRENTMEMBER.LAG(12) : [Reporting Period].[Fiscal].CURRENTMEMBER}
MEMBER [Measures].[Rolling12Months] AS
SUM(
Last12Months,
[Measures].[Amount]
)
SELECT
[Measures].[Rolling12Months] ON 0
, [Reporting Period].[Fiscal].[Fiscal Month Name].Members ON 1
FROM [YourCubeName]
WHERE [Reporting Period].[Fiscal].[Fiscal Month Name].&[1]&[2014]
//`WHERE` clause can be left out if context is already set.
Your script looks ok - maybe just change to Aggregate:
WITH MEMBER [Measures].[Rolling12Months] AS
Aggregate(
[Reporting Period].[Fiscal].[Fiscal Month Name].&[1]&[2013]:[Reporting Period].[Fiscal].CurrentMember,
[Measures].[Amount]
)
So a complete script would be something like
WITH MEMBER [Measures].[Rolling12Months] AS
Aggregate(
[Reporting Period].[Fiscal].[Fiscal Month Name].&[1]&[2013]:[Reporting Period].[Fiscal].CurrentMember,
[Measures].[Amount]
)
SELECT
{[Measures].[Amount]
, [Measures].[Rolling12Months] } ON 0
, [Reporting Period].[Fiscal].[Fiscal Month Name].Members ON 1
FROM [YourCubeName]

Is there a better way to calculate the moving average in an MDX in iccube?

I'm using the following calculated member to calculate the moving average for my visits for the last 30 days period; is there a shorter way to do this?
WITH
MEMBER [Measures].[Visits Moving Avg] AS
AVG(
[TIME].[Time].Prevmember : [TIME].[Time].Prevmember.Prevmember.Prevmember....
, [Measures].[VISITS]
), SOLVE_ORDER = 0
Instead of using the serie of prevMember.prevMember... calls you can use the Lag MDX function function as following :
WITH
MEMBER [Measures].[Visits Moving Avg] AS
AVG(
[TIME].[Time].prevMember : [TIME].[Time].prevMember.lag(30)
, [Measures].[VISITS]
), SOLVE_ORDER = 0
By the way, it looks like currentMember is missing in your query; you are currently computing the moving average for the defaultMember of the [Time] dimension. The query using the currentMember of the time dimension is as following :
WITH
MEMBER [Measures].[Visits Moving Avg] AS
AVG(
[TIME].[Time].currentMemBer.prevMember
: [TIME].[Time].currentMemBer.prevMember.lag(30)
, [Measures].[VISITS]
), SOLVE_ORDER = 0

MDX -No Sales over 30 days

I'd like to get the product with zero sales over 30 days. E.g. Below is my expected result:
Store,Product,Days
Store1, product1, 33
Store1, product2, 100
Store2, product5, 96
Store34, product14, 78
Store100, product9, 47
So I wrote below query:
WITH
MEMBER [Measures].[Zero Sales Days]
AS
COUNT(
FILTER(
NONEMPTY( [Calendar].[Date].[Day],[Measures].[POS Qty])
, ( [Measures].[POS Qty]=0)
)
)
SELECT
([Store].[Store].[Store],[product].[product].[product]) on 1,
([MEASURES].[Zero Sales Days]) ON 0
FROM [testcube]
The problem is: How to filter the case: days of zero sales<30
Thanks,
Nia
I did some change and then ran against my DB.
I got nothing if I added the where cause. If not, the result is '#Error'.
I need not select any time related dimension. What I want to do for the report is: select store and product dimension, and define a calculated measure to get the count. Boyan, I will be really appreciated it if you can need the detailed the query for it.
The function LastPeriods is what you're looking for:
WITH
MEMBER [Measures].[Zero Sales Days]
AS COUNT(
FILTER([Calendar].[Date].[Day],
SUM( LastPeriods(30, [Calendar].[Date].currentmember),[Measures].[POS Qty])
= 0 )
)
SELECT
([Store].[Store].[Store],[product].[product].[product]) on 1,
([MEASURES].[Zero Sales Days]) ON 0
FROM [testcube]
The following query works against Adventure Works and shows you the products with no sales for over 30 days from the date in the WHERE clause back:
WITH
MEMBER [Measures].[Number of Periods With No Sales] AS
Iif(([Date].[Date].CurrentMember, [Measures].[Internet Sales Amount])=0,
([Date].[Date].PrevMember, [Measures].[Number of Periods With No Sales])+1,
NULL
)
MEMBER [Measures].[Number of > 30 Periods With No Sales] AS
Sum(
Iif([Measures].[Number of Periods With No Sales] > 30,
[Measures].[Number of Periods With No Sales],
NULL
)
)
SELECT
{
[Measures].[Number of > 30 Periods With No Sales]
} ON 0,
NON EMPTY {
[Product].[Product Categories].[Product]
} ON 1
FROM [Adventure Works]
WHERE [Date].[Calendar].[Date].&[860]
You will need to re-work it (change the dimension/measure names) to get it to work against your db. Please let me know if you need a query which can give you all products regardless of the date, which have at least one period with more than 30 days with no sales (e.g. max period with no sales, or an arbitrary such period). This will require a few changes. Also, since the query is using recursion it may be slow - if it is too slow we can see how to improve its performance - something which may require changes to your data model to support this bit of analytics.