Wednesday, March 7, 2012
Previous Value Calculation driving me nuts
I've posted my question in the OLAP sql group, but so far no one has
responded, so im hoping you guys can help me out...
My problem is that i am trying to create a value added calculation in a
named query. The calculation i am trying looks like this in algebra
form: (current cost-previous cost)-(current profits-previous profits)
Now the previous cost or previous profits data is based on dates found
in another table (called:tbo.Dates) if that makes any difference
Costs & Profits in table called tbo.Values
If more info is needed, please let me know.
Thanks & Cheers,
Can you provide the DDL (Create Table.. etc) and some sample Data and
what the expected results are?
Barry
daveoram24@.hotmail.com wrote:
> Hey all,
> I've posted my question in the OLAP sql group, but so far no one has
> responded, so im hoping you guys can help me out...
> My problem is that i am trying to create a value added calculation in a
> named query. The calculation i am trying looks like this in algebra
> form: (current cost-previous cost)-(current profits-previous profits)
> Now the previous cost or previous profits data is based on dates found
> in another table (called:tbo.Dates) if that makes any difference
> Costs & Profits in table called tbo.Values
> If more info is needed, please let me know.
> Thanks & Cheers,
Previous Value Calculation driving me nuts
I've posted my question in the OLAP sql group, but so far no one has
responded, so im hoping you guys can help me out...
My problem is that i am trying to create a value added calculation in a
named query. The calculation i am trying looks like this in algebra
form: (current cost-previous cost)-(current profits-previous profits)
Now the previous cost or previous profits data is based on dates found
in another table (called:tbo.Dates) if that makes any difference
Costs & Profits in table called tbo.Values
If more info is needed, please let me know.
Thanks & Cheers,Can you provide the DDL (Create Table.. etc) and some sample Data and
what the expected results are?
Barry
daveoram24@.hotmail.com wrote:
> Hey all,
> I've posted my question in the OLAP sql group, but so far no one has
> responded, so im hoping you guys can help me out...
> My problem is that i am trying to create a value added calculation in a
> named query. The calculation i am trying looks like this in algebra
> form: (current cost-previous cost)-(current profits-previous profits)
> Now the previous cost or previous profits data is based on dates found
> in another table (called:tbo.Dates) if that makes any difference
> Costs & Profits in table called tbo.Values
> If more info is needed, please let me know.
> Thanks & Cheers,
Previous Row Calculations (sql server 2000)
for example:
expr1=PreviousRow.Expr1/30 + expr2 - expr3 =100
so the next row is
expr1=100/30 + expr2 - expr3 =300
so the third row is
expr1=300/30 + expr2 - expr3 =100
or tell me if it not possible please
and special thanks to Umachandar Jayachandran - MS for help.
Hi,
Im not quite sure if this is possible in a view. But you can do this in a stored proc. You can use a cursor and manually traverse your records...
cheers,
Paul June A. Domag
|||Thank you my frind for your help but I need give me some examples of the crusors to understand that...if you can.....
|||
Hi,
Here's a sample in transact sql:
-- Declare the variables to store the values returned by FETCH.
DECLARE @.au_lname varchar(40), @.au_fname varchar(20)
DECLARE authors_cursor CURSOR FOR
SELECT au_lname, au_fname FROM authors
WHERE au_lname LIKE 'B%'
ORDER BY au_lname, au_fname
OPEN authors_cursor
-- Perform the first fetch and store the values in variables.
-- Note: The variables are in the same order as the columns
-- in the SELECT statement.
FETCH NEXT FROM authors_cursor
INTO @.au_lname, @.au_fname
-- Check @.@.FETCH_STATUS to see if there are any more rows to fetch.
WHILE @.@.FETCH_STATUS = 0
BEGIN
-- here is where you would do your calculations
-- Concatenate and display the current values in the variables.
PRINT 'Author: ' + @.au_fname + ' ' + @.au_lname
-- This is executed as long as the previous fetch succeeds.
FETCH NEXT FROM authors_cursor
INTO @.au_lname, @.au_fname
END
CLOSE authors_cursor
DEALLOCATE authors_cursor
GO
BTW, this would be placed inside a stored proc...
cheers,
Paul June A. Domag
|||Bear with me here, as the info is worth it!!!
do you really need a table for this?
Ahhh, memories of my analysis classes!!!
This is what is called a Discrete Dynamic Equation (also called a Difference equation as oppposed to a differential equation) ; specifically, a Discrete first-order Affine Dynamical System
The form is:
An = R An-1 + BThe general solution for which is:
Ak=CRk + B/(1-R)The particular solution for n=0 is
A0=CR0+ B/(1-R)=C+B/(1-R)Solve for C
C=A0-B(1-R)Therefore, the particular solution for the dynamical system is:
Ak=Rk(A0-B/(1-R))+B/(1-R)
in sql:
Create function AffineDynamic(@.A0 real, @.R decimal(16,16), @.B real, @.k int) returns decimal(32,16)
as
begin
declare @.result decimal(32,16)
if @.k = 0
set @.result = @.A0
else
set @.result Power(@.R,@.k) * (@.A0 - (@.B * Power(1-@.R, -1))) + (@.B * Power(1-@.R, -1))
return @.result
end
usage:
-- a random expr1 for row 1
DECLARE @.initial real
set @.initial = 25
-- the rate: 1/30
DECLARE @.rate decimal(16,16)
set @.rate = 0.03333333
-- a random expr2
DECLARE @.expr2 real
set @.expr2 = 23
-- a random expr3
DECLARE @.expr3 real
set @.expr3 = 7
-- First row
select 0 Iter, dbo.AffineDynamic(@.initial, @.rate, @.expr2 - @.expr3,0) Expr1
union
-- Second row
select 1, dbo.AffineDynamic(@.initial, @.rate, @.expr2 - @.expr3,1)
union
-- Third row
select 2, dbo.AffineDynamic(@.initial, @.rate, @.expr2 - @.expr3,2)
union
-- Fourth row
select 3, dbo.AffineDynamic(@.initial, @.rate, @.expr2 - @.expr3,3)
union
-- Fifth row
select 4, dbo.AffineDynamic(@.initial, @.rate, @.expr2 - @.expr3,4)
union
-- Sixth row
select 5, dbo.AffineDynamic(@.initial, @.rate, @.expr2 - @.expr3,5)
union
-- Sixth row
select 6, dbo.AffineDynamic(@.initial, @.rate, @.expr2 - @.expr3,6)
Yields the following:
Iter Expr1
0 25.0000000000000000
1 16.8333320617675780
2 16.5611095428466800
3 16.5520362854003910
4 16.5517330169677730
5 16.5517234802246090
6 16.5517234802246090
Now, note the affinity towards 16.5517234802246090. This is called a point of stability. Any iteration after 5 for A0=25, R=1/30, B= (23-7) = 16 yeilds this number.
To wit:
select 1000 Iter, dbo.AffineDynamic(25, 0.03333333, 16,1000) Expr1
Yields (as expected):
Iter Expr1
1000 16.5517234802246090
Reference:
Discrete Dynamical Modeling, Sandefur, Oxford University Press, 1993, ISBN 0-19-508438-1
Another plain and practical alternative to cursors, and/or esoteric alternates that concentrate on remembering the previous values by storing them on a temporary table that is later listed as show in sample code in the "PreviousRowCalculation" SP.
create table nums(a int, b int)
insert into nums values (0,1)
insert into nums values (1,2)
insert into nums values (2,3)
insert into nums values (3,4)
insert into nums values (4,5)
go
-- drop procedure PreviousRowCalculation
create procedure PreviousRowCalculation
as
begin
set nocount on
create table #tmp (recno int identity, a int, b int, pv int)
insert into #tmp (a,b,pv)
select a,b,0 from nums order by a
declare @.t int,@.c int
set @.t = (select count(*) from #tmp)
set @.c = 1
while @.c <= @.t
begin
update #tmp
set pv = isnull((select pv from #tmp x where x.recno=@.c-1),0) -- expr1
+ (11 + b) - (10 + a) -- + expr2 - expr3
where recno=@.c
set @.c = @.c + 1
end
select * from #tmp
drop table #tmp
end
go
PreviousRowCalculation
go
An = R An-1 + B
This is called a First-Order Homogeneous Dynamical Equation
Which is what your request is:
========================
B = expr2 - expr3
expr1n= [(1/30) * expr1n-1] + B
========================
This assumes that expr2 - expr3 are constant expressions and not variable values, such as columns in the table.
There are different solutions for other forms - First-Order Non-Homogenous Dynamical Equations, N-Order Homogeneous/Non-Homogenous Dynamical Equations and Systems of Dynamical Equations, just to name a few.
Google:
Logistic Equation
Markov Chains
The point I am trying to make, if there is a distinct causal relationship between values of sequential iterations of a formula, that is:
F(x)n = G(F(x)n-1) There is often an dual formula H(x , k) such that
Fk(x) = H(F(x)0 , k)
where k is the iteration you want to calculate and F(x)0 is the intial value, thereby eliminating the need to iterate all the values.
The trick is being able to recognize the form of the system (is it solvable?) and then applying the proper dual.
Ah, the difference between a degree and a certificate!|||
or, using ed's nums table and exprnext = exprPrev +(11 +b) - (10+a)
select cast(null as int) pv, identity(int) id, * into #temp from nums
update #temp set pv = 0 where id = 1
loophere:
update #temp
set pv = calc
from #temp, ( select top 1 curr.ID, prev.pv + (11 + prev.b) - (10+prev.a) calc
from #temp curr inner join #temp prev on curr.id = prev.ID+1 where curr.pv is null ) thecalc
where #temp.ID = thecalc.ID
if @.@.ROWCOUNT <> 0 goto loophere
select * from #temp
drop table #temp
Just a last note, on Blair last alternate solution is the need to evaluate the expression for the first row alone (if actually needed) since the loop does require to start in "recno 1" + 1 therefore missing its evaluation at row 1.|||the second line is the initial expr1:
update #temp set pv = 0 where id = 1
Previous Row Calculations
I am wondering if anyone know a way that you can look up a value from the previous row to do a calculation on. I have a field that I need to subtract the same field from the previous row to validate. Is this possible in a query?
Thanks
KenzieOk, i just composed this script and i hope it could help you
WITH MyTable AS
(SELECT
*,
Num = ROW_NUMBER() OVER(ORDER BY <YourColumn>)
FROM [dbo].<YourTable>)
SELECT
tb.<YourColumn>,
tb.Num,
prev. <YourColumn>
FROM MyTable tb
LEFT JOIN
(SELECT
<YourColumn>,
Num = (ROW_NUMBER() OVER(ORDER BY <YourColumn>) -1)
FROM [dbo].<YourTable>) prev
ON tb.Num = prev.Num
Mind to change <YourColumn> and <YourTable>
let me know if it works (for me it's working)
|||Slight simplification, you can use only the CTE in the self-join like:
WITH t_seq
AS
(
select ROW_NUMBER() OVER(ORDER BY <column_list>) AS seq
from <table>
)
select ...
from t_seq AS t1
left join t_seq AS t2
on t2.seq = t1.seq -1;
Previous Month To Date Calculation
Previous Year To Date:
([Time Calculations].[YTD Pr Yr]=
Aggregate(
Crossjoin({[Calendar Year].[Current Period]},
PeriodsToDate(
[Time].[Calendar Year].[Year],
ParallelPeriod(
[Time].[Calendar Year].[Year],1,
[Time].[Calendar Year].CurrentMember)))
)
);
Adam
Dear Friend,
Check this example:
MTD:
Code Snippet
SUM(PeriodsToDate([DimTime].[Hierarquia].[Month],
[DimTime].[Hierarquia].CurrentMember),
[Measures].[NC_ValorCarteira])
Gets Previous Member
Code Snippet
IIF(IsEMPTY(([Measures].[CM_ResFinAcum],[DimTime].[Hierarquia].PrevMember))
,0,([Measures].[CM_ResFinAcum],[DimTime].[Hierarquia].PrevMember))
Helped?
Regards!
|||That is not quite what I am looking for. I can calculate current month to date just fine, but what I am attempting to calcuate is month to date for the previous year. So from Aug 1, 2006 - Aug 21 2006. Given the nature of our business, know growth from this time last year is essential. This is what I am attempting to use and it returns the entire aggregate of the previous years period. Any help would be greatly appreciated.Aggregate
(
PeriodsToDate(
[Time].[Year - Month - Week - Day of Week].[Month],
ParallelPeriod(
[Time].[Year - Month - Week - Day of Week].[Month],12,
[Time].[Year - Month - Week - Day of Week].CurrentMember)
),
[Measures].[Orders]
)
Adam|||
Hi Adam,
I'm not sure about the structure of your [Time] dimension, so here's a sample Adventure Works query:
Code Snippet
With
Member [Measures].[MTDSales] as
Aggregate(PeriodsToDate([Date].[Calendar].[Month]),
[Measures].[Sales Amount]),
FORMAT_STRING = 'Currency'
Member [Measures].[MTDSales-PY] as
([Measures].[MTDSales],
ParallelPeriod([Date].[Calendar].[Calendar Year])),
FORMAT_STRING = 'Currency'
select
{[Measures].[Sales Amount], [Measures].[MTDSales],
[Measures].[MTDSales-PY]} on 0,
Non Empty
{[Date].[Calendar].[Month].&[2003]&[7].Children,
[Date].[Calendar].[Month].&[2004]&[7].Children} on 1
from [Adventure Works]
|||Using that structure, I am still getting all of last years numbers. The ideal goal is to compare the current months MTD with the corresponding MTD of last year to evaluate growth. It is misleading to compare the current MTD with the entirety of the correspond prior years MTD. What I need is someone to exlude days from the prior year MTD calculation.|||
AdamAtAirNWater wrote:
It is misleading to compare the current MTD with the entirety of the correspond prior years MTD. What I need is someone to exlude days from the prior year MTD calculation.
But that's how I thought the sample Adventure Works query worked. For example, compare these 2 result rows:
...
July 15, 2003 $30,792.07 $3,103,364.27 $2,642,983.51
...
July 15, 2004 $1,379.50 $23,234.19 $3,103,364.27
The [MTDSales-PY] for July 15, 2004 is $3,103,364.27, which is identical to [MTDSales] for July 15, 2003 (the total for all days of July, 2003 is $3,552,319.38). To better understand your issue, could you point out specific examples in the sample query results?
|||Perhaps I made an error in the way I adapted it into my calculation. I am attempting to put this into a calculation in a cube.How would I translate that into an expression for a cube?
Adam
|||This is what I am currently using in my cube to calculate MTD and the Prior MTD. As I said, it is returning the entire value for the previous MTD
Code Snippet
CREATE MEMBER CURRENTCUBE.[MEASURES].[MTD]
AS Aggregate(PeriodsToDate([Time].[Year - Month - Dayof Month].[Month]),
[Measures].[Orders]),
FORMAT_STRING = "Standard",
VISIBLE = 1;
CREATE MEMBER CURRENTCUBE.[MEASURES].[MTD-Prior]
AS ([Measures].[MTD],
ParallelPeriod([Time].[Year - Month - Dayof Month].[Year])),
FORMAT_STRING = "#",
VISIBLE = 1;
|||
Well, the expressions look similar to the Adventure Works sample, so I'm wondering whether there's an issue with the [Time].[Year - Month - Dayof Month] hierarchy not being natural. In any case, it's worth trying the full forms of PeriodsToDate() and ParallelPeriod(), like:
Code Snippet
CREATE MEMBER CURRENTCUBE.[MEASURES].[MTD]
AS Aggregate(PeriodsToDate([Time].[Year - Month - Dayof Month].[Month],
[Time].[Year - Month - Dayof Month].CurrentMember),
[Measures].[Orders]),
FORMAT_STRING = "Standard",
VISIBLE = 1;
CREATE MEMBER CURRENTCUBE.[MEASURES].[MTD-Prior]
AS ([Measures].[MTD],
ParallelPeriod([Time].[Year - Month - Dayof Month].[Year], 1,
[Time].[Year - Month - Dayof Month].CurrentMember)),
FORMAT_STRING = "#",
VISIBLE = 1;
Adam
|||
AdamAtAirNWater wrote:
I do have a yellow triangle on the hierarchy with the message that states "Attribute relationships do not exist between one or more levels in this hierarchy. The following hierarchies do not have a direct or indirect relationship defined to their parent."
This indicates that the [Year - Month - Dayof Month] hierarchy is not natural:
SQL Server 2005 Books Online
Attribute Relationships
...
Natural Hierarchy Relationships
A hierarchy is a natural hierarchy when each attribute included in the user-defined hierarchy has a one to many relationship with the attribute immediately below it.
...Relationships representing natural hierarchies are enforced by creating an attribute relationship between the attribute for a level and the attribute for the level below it.
...
My guess is that the "Month" and/or 'DayOfMonth" attributes in the hierarchy are not unique across higher levels of the hierarchy. For example, if 'DayOfMonth" was like 1, 2, etc, the same member could appear under multiple months. So it should be qualified (could be by month and year) to make it unique. If you study how the [Date] dimension and Fiscal hierarchy in Adventure Works are designed, it will become clearer.
|||I have fixed the hierarchy issues and have even have the calculation almost exactly where I want it. If I use:
Code Snippet
([Measures].[MTD],
ParallelPeriod([Time].[Year - Month - Date].[Simple Date], 365,
[Time].[Year - Month - Date].CurrentMember))
then its gets me the correct information. The only question I now have is there anyway that I can see this number at the month level of the hierarchy. When view my data in the cube, I have to drill down to the day and it give me the correct month to date on that sepecific day, but ideally I don't want to have to go that deep in order to see it. Is that possible?
Thanks again for all the help. It is greatly apprieciated and I have learned a lot.
Adam
|||But which date should be selected for browsing MTD at the month level - is it the last day of the month with data? I'm guessing that most months earlier than the current one have data for all days, so in those cases it will be the total for the month.|||I would want to show the last date with data of the current month for the previous years month.
Adam
|||
One way to do that would be to add a dedicated measure - which would work at any level. So if [MTD-PY] is defined as you indicated above:
Code Snippet
([Measures].[MTD],
ParallelPeriod([Time].[Year - Month - Date].[Simple Date], 365,
[Time].[Year - Month - Date].CurrentMember))
then [LatestMTD-PY] could be like:
Code Snippet
([Measures].[MTD-PY],
Tail(NonEmpty([Time].[Year - Month - Date].[Simple Date],
{[Measures].[Orders]})).Item(0))
Previous Month To Date Calculation
Previous Year To Date:
([Time Calculations].[YTD Pr Yr]=
Aggregate(
Crossjoin({[Calendar Year].[Current Period]},
PeriodsToDate(
[Time].[Calendar Year].[Year],
ParallelPeriod(
[Time].[Calendar Year].[Year],1,
[Time].[Calendar Year].CurrentMember)))
)
);
Adam
Dear Friend,
Check this example:
MTD:
Code Snippet
SUM(PeriodsToDate([DimTime].[Hierarquia].[Month],
[DimTime].[Hierarquia].CurrentMember),
[Measures].[NC_ValorCarteira])
Gets Previous Member
Code Snippet
IIF(IsEMPTY(([Measures].[CM_ResFinAcum],[DimTime].[Hierarquia].PrevMember))
,0,([Measures].[CM_ResFinAcum],[DimTime].[Hierarquia].PrevMember))
Helped?
Regards!
|||That is not quite what I am looking for. I can calculate current month to date just fine, but what I am attempting to calcuate is month to date for the previous year. So from Aug 1, 2006 - Aug 21 2006. Given the nature of our business, know growth from this time last year is essential. This is what I am attempting to use and it returns the entire aggregate of the previous years period. Any help would be greatly appreciated.Aggregate
(
PeriodsToDate(
[Time].[Year - Month - Week - Day of Week].[Month],
ParallelPeriod(
[Time].[Year - Month - Week - Day of Week].[Month],12,
[Time].[Year - Month - Week - Day of Week].CurrentMember)
),
[Measures].[Orders]
)
Adam|||
Hi Adam,
I'm not sure about the structure of your [Time] dimension, so here's a sample Adventure Works query:
Code Snippet
With
Member [Measures].[MTDSales] as
Aggregate(PeriodsToDate([Date].[Calendar].[Month]),
[Measures].[Sales Amount]),
FORMAT_STRING = 'Currency'
Member [Measures].[MTDSales-PY] as
([Measures].[MTDSales],
ParallelPeriod([Date].[Calendar].[Calendar Year])),
FORMAT_STRING = 'Currency'
select
{[Measures].[Sales Amount], [Measures].[MTDSales],
[Measures].[MTDSales-PY]} on 0,
Non Empty
{[Date].[Calendar].[Month].&[2003]&[7].Children,
[Date].[Calendar].[Month].&[2004]&[7].Children} on 1
from [Adventure Works]
|||Using that structure, I am still getting all of last years numbers. The ideal goal is to compare the current months MTD with the corresponding MTD of last year to evaluate growth. It is misleading to compare the current MTD with the entirety of the correspond prior years MTD. What I need is someone to exlude days from the prior year MTD calculation.|||
AdamAtAirNWater wrote:
It is misleading to compare the current MTD with the entirety of the correspond prior years MTD. What I need is someone to exlude days from the prior year MTD calculation.
But that's how I thought the sample Adventure Works query worked. For example, compare these 2 result rows:
...
July 15, 2003 $30,792.07 $3,103,364.27 $2,642,983.51
...
July 15, 2004 $1,379.50 $23,234.19 $3,103,364.27
The [MTDSales-PY] for July 15, 2004 is $3,103,364.27, which is identical to [MTDSales] for July 15, 2003 (the total for all days of July, 2003 is $3,552,319.38). To better understand your issue, could you point out specific examples in the sample query results?
|||Perhaps I made an error in the way I adapted it into my calculation. I am attempting to put this into a calculation in a cube.How would I translate that into an expression for a cube?
Adam
|||This is what I am currently using in my cube to calculate MTD and the Prior MTD. As I said, it is returning the entire value for the previous MTD
Code Snippet
CREATE MEMBER CURRENTCUBE.[MEASURES].[MTD]
AS Aggregate(PeriodsToDate([Time].[Year - Month - Dayof Month].[Month]),
[Measures].[Orders]),
FORMAT_STRING = "Standard",
VISIBLE = 1;
CREATE MEMBER CURRENTCUBE.[MEASURES].[MTD-Prior]
AS ([Measures].[MTD],
ParallelPeriod([Time].[Year - Month - Dayof Month].[Year])),
FORMAT_STRING = "#",
VISIBLE = 1;
|||
Well, the expressions look similar to the Adventure Works sample, so I'm wondering whether there's an issue with the [Time].[Year - Month - Dayof Month] hierarchy not being natural. In any case, it's worth trying the full forms of PeriodsToDate() and ParallelPeriod(), like:
Code Snippet
CREATE MEMBER CURRENTCUBE.[MEASURES].[MTD]
AS Aggregate(PeriodsToDate([Time].[Year - Month - Dayof Month].[Month],
[Time].[Year - Month - Dayof Month].CurrentMember),
[Measures].[Orders]),
FORMAT_STRING = "Standard",
VISIBLE = 1;
CREATE MEMBER CURRENTCUBE.[MEASURES].[MTD-Prior]
AS ([Measures].[MTD],
ParallelPeriod([Time].[Year - Month - Dayof Month].[Year], 1,
[Time].[Year - Month - Dayof Month].CurrentMember)),
FORMAT_STRING = "#",
VISIBLE = 1;
Adam
|||
AdamAtAirNWater wrote:
I do have a yellow triangle on the hierarchy with the message that states "Attribute relationships do not exist between one or more levels in this hierarchy. The following hierarchies do not have a direct or indirect relationship defined to their parent."
This indicates that the [Year - Month - Dayof Month] hierarchy is not natural:
SQL Server 2005 Books Online
Attribute Relationships
...
Natural Hierarchy Relationships
A hierarchy is a natural hierarchy when each attribute included in the user-defined hierarchy has a one to many relationship with the attribute immediately below it.
...Relationships representing natural hierarchies are enforced by creating an attribute relationship between the attribute for a level and the attribute for the level below it.
...
My guess is that the "Month" and/or 'DayOfMonth" attributes in the hierarchy are not unique across higher levels of the hierarchy. For example, if 'DayOfMonth" was like 1, 2, etc, the same member could appear under multiple months. So it should be qualified (could be by month and year) to make it unique. If you study how the [Date] dimension and Fiscal hierarchy in Adventure Works are designed, it will become clearer.
|||I have fixed the hierarchy issues and have even have the calculation almost exactly where I want it. If I use:
Code Snippet
([Measures].[MTD],
ParallelPeriod([Time].[Year - Month - Date].[Simple Date], 365,
[Time].[Year - Month - Date].CurrentMember))
then its gets me the correct information. The only question I now have is there anyway that I can see this number at the month level of the hierarchy. When view my data in the cube, I have to drill down to the day and it give me the correct month to date on that sepecific day, but ideally I don't want to have to go that deep in order to see it. Is that possible?
Thanks again for all the help. It is greatly apprieciated and I have learned a lot.
Adam
|||But which date should be selected for browsing MTD at the month level - is it the last day of the month with data? I'm guessing that most months earlier than the current one have data for all days, so in those cases it will be the total for the month.|||I would want to show the last date with data of the current month for the previous years month.
Adam
|||
One way to do that would be to add a dedicated measure - which would work at any level. So if [MTD-PY] is defined as you indicated above:
Code Snippet
([Measures].[MTD],
ParallelPeriod([Time].[Year - Month - Date].[Simple Date], 365,
[Time].[Year - Month - Date].CurrentMember))
then [LatestMTD-PY] could be like:
Code Snippet
([Measures].[MTD-PY],
Tail(NonEmpty([Time].[Year - Month - Date].[Simple Date],
{[Measures].[Orders]})).Item(0))
Previous Month To Date Calculation
Previous Year To Date:
([Time Calculations].[YTD Pr Yr]=
Aggregate(
Crossjoin({[Calendar Year].[Current Period]},
PeriodsToDate(
[Time].[Calendar Year].[Year],
ParallelPeriod(
[Time].[Calendar Year].[Year],1,
[Time].[Calendar Year].CurrentMember)))
)
);
Adam
Dear Friend,
Check this example:
MTD:
Code Snippet
SUM(PeriodsToDate([DimTime].[Hierarquia].[Month],
[DimTime].[Hierarquia].CurrentMember),
[Measures].[NC_ValorCarteira])
Gets Previous Member
Code Snippet
IIF(IsEMPTY(([Measures].[CM_ResFinAcum],[DimTime].[Hierarquia].PrevMember))
,0,([Measures].[CM_ResFinAcum],[DimTime].[Hierarquia].PrevMember))
Helped?
Regards!
|||That is not quite what I am looking for. I can calculate current month to date just fine, but what I am attempting to calcuate is month to date for the previous year. So from Aug 1, 2006 - Aug 21 2006. Given the nature of our business, know growth from this time last year is essential. This is what I am attempting to use and it returns the entire aggregate of the previous years period. Any help would be greatly appreciated.Aggregate
(
PeriodsToDate(
[Time].[Year - Month - Week - Day of Week].[Month],
ParallelPeriod(
[Time].[Year - Month - Week - Day of Week].[Month],12,
[Time].[Year - Month - Week - Day of Week].CurrentMember)
),
[Measures].[Orders]
)
Adam|||
Hi Adam,
I'm not sure about the structure of your [Time] dimension, so here's a sample Adventure Works query:
Code Snippet
With
Member [Measures].[MTDSales] as
Aggregate(PeriodsToDate([Date].[Calendar].[Month]),
[Measures].[Sales Amount]),
FORMAT_STRING = 'Currency'
Member [Measures].[MTDSales-PY] as
([Measures].[MTDSales],
ParallelPeriod([Date].[Calendar].[Calendar Year])),
FORMAT_STRING = 'Currency'
select
{[Measures].[Sales Amount], [Measures].[MTDSales],
[Measures].[MTDSales-PY]} on 0,
Non Empty
{[Date].[Calendar].[Month].&[2003]&[7].Children,
[Date].[Calendar].[Month].&[2004]&[7].Children} on 1
from [Adventure Works]
|||Using that structure, I am still getting all of last years numbers. The ideal goal is to compare the current months MTD with the corresponding MTD of last year to evaluate growth. It is misleading to compare the current MTD with the entirety of the correspond prior years MTD. What I need is someone to exlude days from the prior year MTD calculation.|||
AdamAtAirNWater wrote:
It is misleading to compare the current MTD with the entirety of the correspond prior years MTD. What I need is someone to exlude days from the prior year MTD calculation.
But that's how I thought the sample Adventure Works query worked. For example, compare these 2 result rows:
...
July 15, 2003 $30,792.07 $3,103,364.27 $2,642,983.51
...
July 15, 2004 $1,379.50 $23,234.19 $3,103,364.27
The [MTDSales-PY] for July 15, 2004 is $3,103,364.27, which is identical to [MTDSales] for July 15, 2003 (the total for all days of July, 2003 is $3,552,319.38). To better understand your issue, could you point out specific examples in the sample query results?
|||Perhaps I made an error in the way I adapted it into my calculation. I am attempting to put this into a calculation in a cube.How would I translate that into an expression for a cube?
Adam
|||This is what I am currently using in my cube to calculate MTD and the Prior MTD. As I said, it is returning the entire value for the previous MTD
Code Snippet
CREATE MEMBER CURRENTCUBE.[MEASURES].[MTD]
AS Aggregate(PeriodsToDate([Time].[Year - Month - Dayof Month].[Month]),
[Measures].[Orders]),
FORMAT_STRING = "Standard",
VISIBLE = 1;
CREATE MEMBER CURRENTCUBE.[MEASURES].[MTD-Prior]
AS ([Measures].[MTD],
ParallelPeriod([Time].[Year - Month - Dayof Month].[Year])),
FORMAT_STRING = "#",
VISIBLE = 1;
|||
Well, the expressions look similar to the Adventure Works sample, so I'm wondering whether there's an issue with the [Time].[Year - Month - Dayof Month] hierarchy not being natural. In any case, it's worth trying the full forms of PeriodsToDate() and ParallelPeriod(), like:
Code Snippet
CREATE MEMBER CURRENTCUBE.[MEASURES].[MTD]
AS Aggregate(PeriodsToDate([Time].[Year - Month - Dayof Month].[Month],
[Time].[Year - Month - Dayof Month].CurrentMember),
[Measures].[Orders]),
FORMAT_STRING = "Standard",
VISIBLE = 1;
CREATE MEMBER CURRENTCUBE.[MEASURES].[MTD-Prior]
AS ([Measures].[MTD],
ParallelPeriod([Time].[Year - Month - Dayof Month].[Year], 1,
[Time].[Year - Month - Dayof Month].CurrentMember)),
FORMAT_STRING = "#",
VISIBLE = 1;
Adam
|||
AdamAtAirNWater wrote:
I do have a yellow triangle on the hierarchy with the message that states "Attribute relationships do not exist between one or more levels in this hierarchy. The following hierarchies do not have a direct or indirect relationship defined to their parent."
This indicates that the [Year - Month - Dayof Month] hierarchy is not natural:
SQL Server 2005 Books Online
Attribute Relationships
...
Natural Hierarchy Relationships
A hierarchy is a natural hierarchy when each attribute included in the user-defined hierarchy has a one to many relationship with the attribute immediately below it.
...Relationships representing natural hierarchies are enforced by creating an attribute relationship between the attribute for a level and the attribute for the level below it.
...
My guess is that the "Month" and/or 'DayOfMonth" attributes in the hierarchy are not unique across higher levels of the hierarchy. For example, if 'DayOfMonth" was like 1, 2, etc, the same member could appear under multiple months. So it should be qualified (could be by month and year) to make it unique. If you study how the [Date] dimension and Fiscal hierarchy in Adventure Works are designed, it will become clearer.
|||I have fixed the hierarchy issues and have even have the calculation almost exactly where I want it. If I use:
Code Snippet
([Measures].[MTD],
ParallelPeriod([Time].[Year - Month - Date].[Simple Date], 365,
[Time].[Year - Month - Date].CurrentMember))
then its gets me the correct information. The only question I now have is there anyway that I can see this number at the month level of the hierarchy. When view my data in the cube, I have to drill down to the day and it give me the correct month to date on that sepecific day, but ideally I don't want to have to go that deep in order to see it. Is that possible?
Thanks again for all the help. It is greatly apprieciated and I have learned a lot.
Adam
|||But which date should be selected for browsing MTD at the month level - is it the last day of the month with data? I'm guessing that most months earlier than the current one have data for all days, so in those cases it will be the total for the month.|||I would want to show the last date with data of the current month for the previous years month.
Adam
|||
One way to do that would be to add a dedicated measure - which would work at any level. So if [MTD-PY] is defined as you indicated above:
Code Snippet
([Measures].[MTD],
ParallelPeriod([Time].[Year - Month - Date].[Simple Date], 365,
[Time].[Year - Month - Date].CurrentMember))
then [LatestMTD-PY] could be like:
Code Snippet
([Measures].[MTD-PY],
Tail(NonEmpty([Time].[Year - Month - Date].[Simple Date],
{[Measures].[Orders]})).Item(0))
Saturday, February 25, 2012
Previous Month To Date Calculation
Previous Year To Date:
([Time Calculations].[YTD Pr Yr]=
Aggregate(
Crossjoin({[Calendar Year].[Current Period]},
PeriodsToDate(
[Time].[Calendar Year].[Year],
ParallelPeriod(
[Time].[Calendar Year].[Year],1,
[Time].[Calendar Year].CurrentMember)))
)
);
Adam
Dear Friend,
Check this example:
MTD:
Code Snippet
SUM(PeriodsToDate([DimTime].[Hierarquia].[Month],
[DimTime].[Hierarquia].CurrentMember),
[Measures].[NC_ValorCarteira])
Gets Previous Member
Code Snippet
IIF(IsEMPTY(([Measures].[CM_ResFinAcum],[DimTime].[Hierarquia].PrevMember))
,0,([Measures].[CM_ResFinAcum],[DimTime].[Hierarquia].PrevMember))
Helped?
Regards!
|||That is not quite what I am looking for. I can calculate current month to date just fine, but what I am attempting to calcuate is month to date for the previous year. So from Aug 1, 2006 - Aug 21 2006. Given the nature of our business, know growth from this time last year is essential. This is what I am attempting to use and it returns the entire aggregate of the previous years period. Any help would be greatly appreciated.Aggregate
(
PeriodsToDate(
[Time].[Year - Month - Week - Day of Week].[Month],
ParallelPeriod(
[Time].[Year - Month - Week - Day of Week].[Month],12,
[Time].[Year - Month - Week - Day of Week].CurrentMember)
),
[Measures].[Orders]
)
Adam|||
Hi Adam,
I'm not sure about the structure of your [Time] dimension, so here's a sample Adventure Works query:
Code Snippet
With
Member [Measures].[MTDSales] as
Aggregate(PeriodsToDate([Date].[Calendar].[Month]),
[Measures].[Sales Amount]),
FORMAT_STRING = 'Currency'
Member [Measures].[MTDSales-PY] as
([Measures].[MTDSales],
ParallelPeriod([Date].[Calendar].[Calendar Year])),
FORMAT_STRING = 'Currency'
select
{[Measures].[Sales Amount], [Measures].[MTDSales],
[Measures].[MTDSales-PY]} on 0,
Non Empty
{[Date].[Calendar].[Month].&[2003]&[7].Children,
[Date].[Calendar].[Month].&[2004]&[7].Children} on 1
from [Adventure Works]
|||Using that structure, I am still getting all of last years numbers. The ideal goal is to compare the current months MTD with the corresponding MTD of last year to evaluate growth. It is misleading to compare the current MTD with the entirety of the correspond prior years MTD. What I need is someone to exlude days from the prior year MTD calculation.|||
AdamAtAirNWater wrote:
It is misleading to compare the current MTD with the entirety of the correspond prior years MTD. What I need is someone to exlude days from the prior year MTD calculation.
But that's how I thought the sample Adventure Works query worked. For example, compare these 2 result rows:
...
July 15, 2003 $30,792.07 $3,103,364.27 $2,642,983.51
...
July 15, 2004 $1,379.50 $23,234.19 $3,103,364.27
The [MTDSales-PY] for July 15, 2004 is $3,103,364.27, which is identical to [MTDSales] for July 15, 2003 (the total for all days of July, 2003 is $3,552,319.38). To better understand your issue, could you point out specific examples in the sample query results?
|||Perhaps I made an error in the way I adapted it into my calculation. I am attempting to put this into a calculation in a cube.How would I translate that into an expression for a cube?
Adam
|||This is what I am currently using in my cube to calculate MTD and the Prior MTD. As I said, it is returning the entire value for the previous MTD
Code Snippet
CREATE MEMBER CURRENTCUBE.[MEASURES].[MTD]
AS Aggregate(PeriodsToDate([Time].[Year - Month - Dayof Month].[Month]),
[Measures].[Orders]),
FORMAT_STRING = "Standard",
VISIBLE = 1;
CREATE MEMBER CURRENTCUBE.[MEASURES].[MTD-Prior]
AS ([Measures].[MTD],
ParallelPeriod([Time].[Year - Month - Dayof Month].[Year])),
FORMAT_STRING = "#",
VISIBLE = 1;
|||
Well, the expressions look similar to the Adventure Works sample, so I'm wondering whether there's an issue with the [Time].[Year - Month - Dayof Month] hierarchy not being natural. In any case, it's worth trying the full forms of PeriodsToDate() and ParallelPeriod(), like:
Code Snippet
CREATE MEMBER CURRENTCUBE.[MEASURES].[MTD]
AS Aggregate(PeriodsToDate([Time].[Year - Month - Dayof Month].[Month],
[Time].[Year - Month - Dayof Month].CurrentMember),
[Measures].[Orders]),
FORMAT_STRING = "Standard",
VISIBLE = 1;
CREATE MEMBER CURRENTCUBE.[MEASURES].[MTD-Prior]
AS ([Measures].[MTD],
ParallelPeriod([Time].[Year - Month - Dayof Month].[Year], 1,
[Time].[Year - Month - Dayof Month].CurrentMember)),
FORMAT_STRING = "#",
VISIBLE = 1;
Adam
|||
AdamAtAirNWater wrote:
I do have a yellow triangle on the hierarchy with the message that states "Attribute relationships do not exist between one or more levels in this hierarchy. The following hierarchies do not have a direct or indirect relationship defined to their parent."
This indicates that the [Year - Month - Dayof Month] hierarchy is not natural:
SQL Server 2005 Books Online
Attribute Relationships
...
Natural Hierarchy Relationships
A hierarchy is a natural hierarchy when each attribute included in the user-defined hierarchy has a one to many relationship with the attribute immediately below it.
...Relationships representing natural hierarchies are enforced by creating an attribute relationship between the attribute for a level and the attribute for the level below it.
...
My guess is that the "Month" and/or 'DayOfMonth" attributes in the hierarchy are not unique across higher levels of the hierarchy. For example, if 'DayOfMonth" was like 1, 2, etc, the same member could appear under multiple months. So it should be qualified (could be by month and year) to make it unique. If you study how the [Date] dimension and Fiscal hierarchy in Adventure Works are designed, it will become clearer.
|||I have fixed the hierarchy issues and have even have the calculation almost exactly where I want it. If I use:
Code Snippet
([Measures].[MTD],
ParallelPeriod([Time].[Year - Month - Date].[Simple Date], 365,
[Time].[Year - Month - Date].CurrentMember))
then its gets me the correct information. The only question I now have is there anyway that I can see this number at the month level of the hierarchy. When view my data in the cube, I have to drill down to the day and it give me the correct month to date on that sepecific day, but ideally I don't want to have to go that deep in order to see it. Is that possible?
Thanks again for all the help. It is greatly apprieciated and I have learned a lot.
Adam
|||But which date should be selected for browsing MTD at the month level - is it the last day of the month with data? I'm guessing that most months earlier than the current one have data for all days, so in those cases it will be the total for the month.|||I would want to show the last date with data of the current month for the previous years month.
Adam
|||
One way to do that would be to add a dedicated measure - which would work at any level. So if [MTD-PY] is defined as you indicated above:
Code Snippet
([Measures].[MTD],
ParallelPeriod([Time].[Year - Month - Date].[Simple Date], 365,
[Time].[Year - Month - Date].CurrentMember))
then [LatestMTD-PY] could be like:
Code Snippet
([Measures].[MTD-PY],
Tail(NonEmpty([Time].[Year - Month - Date].[Simple Date],
{[Measures].[Orders]})).Item(0))