MDX - Same day of Week Last year - sql

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.

Related

SSAS/MDX : create calculated measure for specific month

I'm not friendly with MDX and I have some difficulties to create calculated measures.
We have some data in a cube and the users would like to have also the measures of the previous month, the measures of december of last year and the measures of december of the current year.
We have our own calendar table in our database with the fields date, month, year, month number...
I have created a Calendar Dimension based on this table with the Time type (with link between date -> Days, month -> Months and year -> Years ).
I have added the measure in the cube.
I try to create the calculated measure like this :
previous month :
CREATE MEMBER CURRENTCUBE.[Measures].[MEASURE_A_PREVIOUS_MONTH]
AS ([Dim_CALENDAR].[MONTH].CURRENTMEMBER.PREVMEMBER, [Measures].[MEASURE_A])
=> OK
december last year :
CREATE MEMBER CURRENTCUBE.[Measures].[MEASURE_A_DECEMBER_PREVIOUS_YEAR]
AS IIF(ISEMPTY([Measures].[MEASURE_A]), NULL, ([Dim_CALENDAR].[MONTH_NUMBER].[12], [Dim_CALENDAR].[YEAR].CURRENTMEMBER.PREVMEMBER, [Measures].[MEASURE_A]))
=> It's good for the first year (2021-M01 to 2020-M12) but after I have a sum of all the month by year
december current year
CREATE MEMBER CURRENTCUBE.[Measures].[MEASURE_A_DECEMBER_PREVIOUS_YEAR]
AS IIF(ISEMPTY([Measures].[MEASURE_A]), NULL, ([Dim_CALENDAR].[MONTH_NUMBER].[12], [Dim_CALENDAR].[YEAR].CURRENTMEMBER, [Measures].[MEASURE_A]))
=> It's good for the first year (2019-M12) but after I have a sum of all the month by year
image of cube results
It seems i can't use the PARALLELPERIOD function because I would like a specific month (december) isn't it ?
What could be the issue?
OK find the solution, it comes from my Calendar dimension, I had a bad relation, I had the relation DATE-> MONTH_NUMBER instead of MONTH -> MONTH_NUMBER. Now it's work.

MDX Syntax to retrieve Specific Dates

I am building some reports in Pyramid Analytics. I created a custom set and tried to write a MDX query which will retrieve the first day of the first month for the first three quarters of the previous year, selected in the slicer. i.e.
If I choose in my slicer 2017, I expect to see the following in the body of the report: Jan 1st 2016, April 1st 2016 and July 1st 2016. These will change according to the selection in the slicer.
I only got as far as the syntax below, which only returns first day of the first month of the first quarter of the previous year i.e. Jan 1st 2016
[Policy - Inception Date].[Calendar Hierarchy].[!#NewPar#!].PREVMEMBER.FIRSTCHILD.FIRSTCHILD.FIRSTCHILD
Note:Newpar = Parameter
Can you please help with the correct syntax?
Thanks.
Without knowing the structure of your Date dimension I'm having to use some guess work but I've made the following in Pyramid and it works fine against our cube:
Generate
(
Head //<<<this will get the first 3 quarters
(
Descendants
(
{[Date].[Date - Calendar Month].[!#aYear#!].PrevMember} //<<<this will get the previous year
,[Date].[Date - Calendar Month].[Calendar Quarter]
)
,3
)
,Head //<<<this will get the first day of each of the quarters found in the first argument of the generate function
(
Descendants
(
[Date].[Date - Calendar Month].CurrentMember
,[Date].[Date - Calendar Month].[Calendar Day]
)
,1
)
)
Here is the result with the year parameter in the top left:

Oracle Week Number from a Date

I am brand new to Oracle. I have figured out most of what I need but one field is driving me absolutely crazy. Seems like it should be simple but I think my brain is fried and I just can't get my head around it. I am trying to produce a Sales report. I am doing all kinds of crazy things based on the Invoice Date. The last thing I need to do is to be able to create a Week Number so I can report on weekly sales year vs year. For purposes of this report my fiscal year starts exactly on December 1 (regardless of day of week it falls on) every year. For example, Dec 1-7 will be week 1, etc. I can get the week number using various functions but all of them are based on either calendar year or ISO weeks. How can I easily generate a field that will give me the number of the week since December 1? Thanks so much for your help.
Forget about the default week number formats as that won't work for this specific requirement. I'd probably subtract the previous 1 December from invoice date and divide that by 7. Round down, add 1 and you should be fine.
select floor(
(
trunc(invoiceDate) -
case
-- if December is current month, than use 1st of this month
when to_char(invoiceDate, 'MM') = '12' then trunc(invoiceDate, 'MM')
-- else, use 1st December of previous year
else add_months(trunc(invoiceDate, 'YYYY'), -1)
end
) / 7
) + 1
from dual;

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

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