Reputation: 1222
I'm creating a data set where I eventually want to compare a specific two week date range a specific year (2014) and the same weeks previous year (i.e. two weeks up to the last date of the specified date range).
What I have done thus far is to create the Query to find the relevant data for the specified two week date range as this:
SELECT NON EMPTY {
[Measures].[Quantity], [Measures].[Total Price]
,[Measures].[Contribution Margin], [Measures].[Profit Margin]
} ON COLUMNS,
NON EMPTY {
( [Dim Date].[Calender].[Date].[2014-11-20]:[Dim Date].[Calender].[Date].[2014-11-30]
,[Dim Store2].[Store Key].&[1024] )
} on rows
from [DSV_FactStoreSales 1]
I don't understand how I get the same period the previous year. I have looked at ParallelPeriod
, but the examples I've seen has included currentmember
, but in this example I have a specified range, spanning two weeks.
Does anyone have any idea how to implement this? I need this to be in the same Query, in order to be able to utilize the result set as a data set in SSRS.
EDIT
I ran the Query:
SELECT NON EMPTY {
[Measures].[Quantity]
,[Measures].[Total Price]
,[Measures].[Contribution Margin]
,[Measures].[Profit Margin]
} ON COLUMNS,
NON EMPTY
{
(
parallelperiod([Dim Date].[Calender].[Year],1,[Dim Date].[Calender].[Date].[2014-11-17])
:
parallelperiod([Dim Date].[Calender].[Year],1,[Dim Date].[Calender].[Date].[2014-11-30]),[Dim Store2].[Store Key].&[1024]
)
} on rows
from [DSV_FactStoreSales 1]
but it gave me the result set:
Quantity Total Price Contribution Margin Profit Margin
2014-11-07 1024 1 119.2 47.04 39.46%
2014-11-10 1024 1 31.6 21.1 66.77%
2014-11-25 1024 1 55.2 25.45 46.11%
2014-11-26 1024 2 110.8 65.78 59.37%
2014-11-28 1024 166 20903.62 9101.82 43.54%
2014-11-29 1024 117 11870.75 5535 46.63%
2014-11-30 1024 50 9148.8 4008.13 43.81%
2014-12-01 1024 91 11574.39 4933.01 42.62%
2014-12-02 1024 21 2776.8 1254.12 45.16%
where the dates in particular seems off compared to the sought-after effect.
EDIT 2
I've tried the query
Select Non empty {[Measures].[Total Price]} on 0,
([Dim Date].[Calender].[Date].members) on 1
FROM (Select
{
(parallelperiod([Dim Date].[Calender].[Month],4,[Dim Date].[Calender].[Date].[2014-11-22])
:parallelperiod([Dim Date].[Calender].[mONTH],4,[Dim Date].[Calender].[Date].[2014-14-28]), [Dim Store2].[Store Key].&[1041])
} on columns
from [DSV_FactStoreSales 1])
but that resulted in showing sales from 2014-07-26
through 2018-05-02
. May there be some underlying hierarchy error, or have I misinterpreted ParallelPeriod()
? Please note
that I did some testing on Month
in this last example Query.
EDIT 3 "Using the With MEMBER approach"
I've also tried the suggested solution to invoke WITH Member AS
like this:
With
Member [Measures].[QuantityParallelPeriod] as
(
ParallelPeriod([Dim Date].[Calender].[Year],1,[Dim Date].[Calender].CurrentMember),
[Measures].[Quantity]
)
Select Non empty {[Measures].[QuantityParallelPeriod],[Measures].[Total Price]} on 0,
non empty{([Dim Date].[Calender].[Date].members)} on 1
FROM (Select
{
(parallelperiod([Dim Date].[Calender].[MOnth],4,[Dim Date].[Calender].[Date].[2014-11-22])
:parallelperiod([Dim Date].[Calender].[MOnth],4,[Dim Date].[Calender].[Date].[2014-11-28]), [Dim Store2].[Store Key].&[1024])
} on columns
from [DSV_FactStoreSales 1])
but that resulted in
Quantity
Parallel
Period Total Price
2014-11-07 (null) 119.2
2014-11-10 (null) 31.6
2014-11-25 (null) 55.2
2014-11-26 (null) 110.8
2014-11-28 (null) 20903.62
2014-11-29 (null) 11870.75
2014-11-30 (null) 9148.8
2014-12-01 (null) 11574.39
2014-12-02 (null) 2776.8
2015-11-13 1 (null)
2015-11-16 1 (null)
2015-12-01 91 (null)
2015-12-02 21 (null)
I don't understand how only dates Outside
the specified parallelperiod range
returns a value?
EDIT Expected Output
What I'd expect as an output would be:
Quantity TotalPrice Contribution margin ProfitMargin
2013-11-17 4 5 13 23 %
2013-11-18 4 5 15 25 %
. . . . .
. . . . .
. . . . .
2013-11-30 1 100 80 80 %
2014-11-17 1 100 80 80 %
2014-11-18 1 100 80 80 %
. . . . .
. . . . .
. . . . .
2014-11-30 1 100 80 80 %
(where the numeric values are dummy values and may no add up "logically")
Upvotes: 0
Views: 349
Reputation: 11625
Here is a shot at a working solution. If this isn't the shape of the final working query you want please clarify as I didn't catch that in your question.
WITH
MEMBER [Dim Date].[Calender].[All].[Prior Year] as
Aggregate(
{
parallelperiod([Dim Date].[Calender].[Year],1,[Dim Date].[Calender].[Date].[2014-11-17])
:
parallelperiod([Dim Date].[Calender].[Year],1,[Dim Date].[Calender].[Date].[2014-11-30])
}
)
MEMBER [Dim Date].[Calender].[All].[Current Year] as
Aggregate(
{
[Dim Date].[Calender].[Date].[2014-11-17]
:
[Dim Date].[Calender].[Date].[2014-11-30]
}
)
SELECT {
[Measures].[Quantity], [Measures].[Total Price]
,[Measures].[Contribution Margin], [Measures].[Profit Margin]
} ON COLUMNS,
{[Dim Date].[Calender].[All].[Prior Year],
[Dim Date].[Calender].[All].[Current Year]}
on rows
from [DSV_FactStoreSales 1]
Where [Dim Store2].[Store Key].&[1024]
Per your clarifying expected results, here is another shape:
SELECT {
[Measures].[Quantity], [Measures].[Total Price]
,[Measures].[Contribution Margin], [Measures].[Profit Margin]
} ON COLUMNS,
{
{
parallelperiod([Dim Date].[Calender].[Year],1,[Dim Date].[Calender].[Date].[2014-11-17])
:
parallelperiod([Dim Date].[Calender].[Year],1,[Dim Date].[Calender].[Date].[2014-11-30])
},
{
[Dim Date].[Calender].[Date].[2014-11-17]
:
[Dim Date].[Calender].[Date].[2014-11-30]
}
}
on rows
from [DSV_FactStoreSales 1]
Where [Dim Store2].[Store Key].&[1024]
Upvotes: 0
Reputation: 1484
It will return you the same period set 1 year before:
{
ParallelPeriod([Dim Date].[Calender].[Year],1,[Dim Date].[Calender].[Date].[2014-11-20])
:
ParallelPeriod([Dim Date].[Calender].[Year],1,[Dim Date].[Calender].[Date].[2014-11-30])
}
In order to add calculated measure with last year:
With
Member [Measures].[QuantityParallelPeriod] as
(
ParallelPeriod([Dim Date].[Calender].[Year],1,[Dim Date].CurrentMember),
[Measures].[Quantity]
)
The full code must look like the following:
With
Member [Measures].[QuantityParallelPeriod] as
(
ParallelPeriod([Dim Date].[Calender].[Year],1,[Dim Date].[Calender].CurrentMember),
[Measures].[Quantity]
)
Select
Non empty {[Measures].[Quantity],[Measures].[QuantityParallelPeriod]} on 0,
Non empty {[Dim Date].[Calender].[Date].[2014-11-20]:[Dim Date].[Calender].[Date].[2014-11-30]} on 1
From [DSV_FactStoreSales 1]
Where ([Dim Store2].[Store Key].&[1024])
Upvotes: 2