Showing posts with label calculate. Show all posts
Showing posts with label calculate. Show all posts

Wednesday, March 7, 2012

How to Calculate YTD on Calculated Measures

Hi ,

I am having to calculate the YTD and Twelve months to Date for calculated measures. For example, i have a calculated measure: Close Ratio=(X/Y) . So how i write an MDX to get YTD values for this calculated ratio.

Its not as simple as adding this calculated measure in the scope statement like the regular measures. It looks like For calculating close ratio -YTD[QTR4] = (X[qtr1] + X[qtr2]+ X[qtr3]+ X[qtr4] ) / (Y[QTR1] + Y[QTR2] + Y[QTR3] + Y[QTR4]) .

My requirement is to dynamically calculate YTD values for the calculated measures like ratios and % values. Can any one give me an idea how to go about writing an MDX for this?

If you have AS 2005 Enterprise Edition, you can use the Time Intelligence Wizard to generate calculations like YTD; otherwise, this article may help you craft them manually. The application of Aggregate() on a separate calculation hierarchy should enable YTD to work with a ratio calculated measure:

http://www.sqlmag.com/articles/index.cfm?articleid=46157&

>>

  • [June 2005]

  • Analysis Services 2005 Brings You Automated Time Intelligence

  • The Business Intelligence Wizard makes time analysis a snap

  • By: Mosha Pasumansky , Robert Zare |||

    Do you create YTD member as Measure or as a member on Time dimension or on an utility dimension.

    If you create an YTD not as Measure, you shouldn't have any problem.

    For example

    create member [Utility].[ytd] as Aggregate(PeriodsToDate([TimeDim].currentMember, [TimeDim].[YearLevel]), Measures.CurrentMember)

    That is all.

  • How to calculate the total days between open and close date

    Hi All,

    I have a table call case and case_status have two fields, date and status as below:

    date status

    04/01/2006 open

    04/05/2006 closed

    04/10/2006 open

    04/15/2006 closed

    Whenever i open and closed the case, one record is insert into the case_status table.

    Now I would need to calculate the total days of the case in storeprocedure.

    Anyone can help me please.

    Aung

    This articledoes something similar. check if it helps.|||

    One try:

    CREATE

    PROCEDURE [dbo].[caseDays]

    AS

    BEGIN

    -- SET NOCOUNT ON added to prevent extra result sets from-- interfering with SELECT statements.SETNOCOUNTON;RETURN(SELECTSUM(datediff( dd, c.openDate, d.closedDate))as myCaseDateFROM(SELECT a.cDateas openDate, row_NUmber()over(ORDERBY a.cDate)as ROWNUMBERFROM case_statusAS aWHERE(a.status='open'))as c

    inner

    join(SELECT b.cDateas closedDate, row_NUmber()over(ORDERBY b.cDate)as ROWNUMBERFROM case_statusAS bWHERE(b.status='closed'))as dON c.ROWNUMBER=d.ROWNUMBER)

    END

    I hope this one will be close to your solution.

    Limno

    |||

    Thanks for your responsed.

    But my problem is total days in two date between open and closed. I still facing this problem.

    Thanks

    Aung

    |||

    Hello:

    datediff( dd,openDate,closedDate)

    This function will give you how many days between open and closed days.

    If this is not what you want, give a little more details about your problem.

    Limno

    |||Wouldn't the table also need a CaseID field so you know what case was being opened and closed?

    How to calculate the percentage?

    In my cube, I've a date dimension and a time dimension. I would like to know how can I calculate the percentage of order count for a specific time?

    I can get this.

    Hour11/6/0611/7/069am601010am801011am6020

    But how can I get this.

    Hour11/6/0611/7/06

    9am30%25%

    10am40%25%

    11am30%50%

    I would like to use the calculation feature in available in the cube. How can I do this? Thanks!

    Depends on whether the percentage is always based on the time hierarchy, regardless of which query axis it lies on, or is based on whichever hierarchy is on rows - Axis(1). In the former case, something like:

    Member [Measures].[OrderFractionByTime] as

    '[Measures].[Order Count]/

    ([Measures].[Order Count], [TimeOfDay].Parent)',

    FORMAT_STRING = "Percent"

    |||Hi Deepak,

    Thanks for your reply but there are something that I don't understand. In your code, you use [TimeOfDay].Parent in the member calculation. However, in my own cube, the date dimension and time dimension are 2 separate dimensions. So, I don't expect my TimeOfDay.Parent will get the expected set.

    The following is my Date and Time dimensions structure.
    DimDate - The Date dimension is populated by extracting data from my OLTP.
    Year
    Month
    DayOfMonth
    Date
    DayNameOfWeek

    DimTime - The Time dimension is generated by cross join all the available hour, minute and second. (No. of records: 24 x 60 x 60)
    Hour
    Minute
    Second

    P.S. I'm quite new to BI and datawarehouse. If my Time hierarchy is not correct or using best practice, please let me know so that I can improve it.

    Regards,
    Alex|||

    Hi Alex,

    Are you using AS 2000 or AS 2005 - if it's AS 2000, then something like:

    Member [Measures].[OrderFractionByTime] as

    '[Measures].[Order Count]/

    ([Measures].[Order Count], [DimTime].Parent)',

    FORMAT_STRING = "Percent"

    |||Hi Deepak,

    I'm using AS 2005. The problem that I don't understand what [DimTime].Parent is pointing to. The Time dimension and Date dimension doesn't have any direct relationship in my cube. Is there any design fault?

    Regards,
    Alex|||

    Alex,

    With AS 2005, the hierarchy should also be specified with [DimTime], so [DimTime].[TimeHierarchy].Parent points to the parent of the current [DimTime] member. For example, the parent of the hour "01" will be [DimTime].[TimeHierarchy].[All].

    http://msdn2.microsoft.com/en-us/library/ms145513.aspx

    >>

    SQL Server 2005 Books Online

    Parent (MDX)

    Updated: 17 July 2006

    Returns the parent of a member.

    ...

    >>

    How to calculate the percentage?

    In my cube, I've a date dimension and a time dimension. I would like to know how can I calculate the percentage of order count for a specific time?

    I can get this.

    Hour11/6/0611/7/069am601010am801011am6020

    But how can I get this.

    Hour11/6/0611/7/06

    9am30%25%

    10am40%25%

    11am30%50%

    I would like to use the calculation feature in available in the cube. How can I do this? Thanks!

    Depends on whether the percentage is always based on the time hierarchy, regardless of which query axis it lies on, or is based on whichever hierarchy is on rows - Axis(1). In the former case, something like:

    Member [Measures].[OrderFractionByTime] as

    '[Measures].[Order Count]/

    ([Measures].[Order Count], [TimeOfDay].Parent)',

    FORMAT_STRING = "Percent"

    |||Hi Deepak,

    Thanks for your reply but there are something that I don't understand. In your code, you use [TimeOfDay].Parent in the member calculation. However, in my own cube, the date dimension and time dimension are 2 separate dimensions. So, I don't expect my TimeOfDay.Parent will get the expected set.

    The following is my Date and Time dimensions structure.
    DimDate - The Date dimension is populated by extracting data from my OLTP.
    Year
    Month
    DayOfMonth
    Date
    DayNameOfWeek

    DimTime - The Time dimension is generated by cross join all the available hour, minute and second. (No. of records: 24 x 60 x 60)
    Hour
    Minute
    Second

    P.S. I'm quite new to BI and datawarehouse. If my Time hierarchy is not correct or using best practice, please let me know so that I can improve it.

    Regards,
    Alex|||

    Hi Alex,

    Are you using AS 2000 or AS 2005 - if it's AS 2000, then something like:

    Member [Measures].[OrderFractionByTime] as

    '[Measures].[Order Count]/

    ([Measures].[Order Count], [DimTime].Parent)',

    FORMAT_STRING = "Percent"

    |||Hi Deepak,

    I'm using AS 2005. The problem that I don't understand what [DimTime].Parent is pointing to. The Time dimension and Date dimension doesn't have any direct relationship in my cube. Is there any design fault?

    Regards,
    Alex|||

    Alex,

    With AS 2005, the hierarchy should also be specified with [DimTime], so [DimTime].[TimeHierarchy].Parent points to the parent of the current [DimTime] member. For example, the parent of the hour "01" will be [DimTime].[TimeHierarchy].[All].

    http://msdn2.microsoft.com/en-us/library/ms145513.aspx

    >>

    SQL Server 2005 Books Online

    Parent (MDX)

    Updated: 17 July 2006

    Returns the parent of a member.

    ...

    >>

    How to calculate the midpoint

    I have three fields date, low, high. I need to calculate the midpoint
    of low and high and display it.

    Date,Low,High
    20071106,92.03,92.13
    20071106,88.77,88.87
    20071106,90.20,90.30
    20071106,95.21,95.31
    20071106,93.13,93.23
    20071106,91.01,91.11On Wed, 07 Nov 2007 06:48:09 -0800, amj1020 wrote:

    Quote:

    Originally Posted by

    >I have three fields date, low, high. I need to calculate the midpoint
    >of low and high and display it.


    Hi amj1020,

    SELECT "date", (low + high) / 2.0 AS midpoint
    FROM YourTable;

    --
    Hugo Kornelis, SQL Server MVP
    My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis

    how to calculate the least anmongst a list of Quarters

    Hi,

    i get a list of Quarters that look like following as an input to my proc.

    'Q306/Q106/Q406/Q405'

    I need to find out whihc one of the above is the least Quarter value.

    In the above example it is 'Q405'

    The Quarter values that come into the proc keep changing...It could be even 1 or 2 or 3 Quarters etc...

    Is there any datatype called Quarter in SQL Server.It shows Quarter as a key word...

    Can some one please help on this....

    Hello,

    There is no "quarter" datatype in sql server.

    For your example, the parameter string will need to first be parsed (to split the quarters - you can use a recursive cte for this) then ordered. If you are using SQL 2005, you can also roll your own "quarter" datatype that could include ranking functions such as MIN/MAX etc.

    What version of sql are you using?

    Cheers

    Rob

    |||

    Thanks for the reply...

    I am using SQL Server2000. Can you please be more elaborate on this as I am a novice.what exactly is recursive cte...

    Can you please send some sample code if possible...

    Thanks...

    |||

    This function will help split up string with a delimited character. It was written in SQL 2005, but you should be able to change MAX to a length less than 8000 to make it work.

    set ANSI_NULLS ON

    set QUOTED_IDENTIFIER ON

    GO

    CREATE FUNCTION [dbo].[SplitData] (@.Data VARCHAR(MAX), @.delimeter CHAR(1))

    RETURNS @.v_Values TABLE ([Value] VARCHAR(MAX), [Position] BIGINT IDENTITY(1,1))

    AS

    BEGIN

    DECLARE @.v_value VARCHAR(MAX),

    @.v_pos BIGINT

    SET @.v_Pos = 1

    WHILE @.data > '' AND @.v_Pos > 0

    BEGIN

    SELECT @.v_pos = CHARINDEX(@.delimeter, @.data,1),

    @.v_Value = CASE @.v_Pos

    WHEN 0 THEN

    @.data

    ELSE

    LEFT(@.data, @.v_pos-1)

    END,

    @.data = CASE

    WHEN @.v_Value = @.data THEN

    NULL

    ELSE

    SUBSTRING(@.data, @.v_pos + 1, LEN(@.data))

    END

    INSERT INTO @.v_Values ([Value])

    VALUES (@.v_Value)

    END

    return

    END

    |||

    I couldnt really understand how to use the above function...

    Is it possible to get the seperate values by splitting the following data

    'Q106/Q206/Q306 Version1/Q406 Version2/Current'

    I need to be able to spilt the above Data...the delimiter is '/'...

    is there something like a spilt function in Sql Server2000....

    Please get back.

    Thanks

    Swapna

    |||

    The split function is the function I posted. The first parameter is the value and the second parameter is the delimiter.

    SELECT Value

    FROM dbo.SplitData('Q106/Q206/Q306 Version1/Q406 Version2/Current','/')

    Hope this helps...

    |||

    Here is the version for 2000.

    Run it with this:

    SELECT * FROM dbo.SplitData('Q106/Q206/Q306 Version1/Q406 Version2/Current','/')

    /*

    Results:

    Value Position

    Q106 1

    Q206 2

    Q306 Version1 3

    Q406 Version2 4

    Current 5

    */

    set ANSI_NULLS ON

    set QUOTED_IDENTIFIER ON

    GO

    ALTER FUNCTION [dbo].[SplitData] (@.Data VARCHAR(8000), @.delimeter CHAR(1))

    RETURNS @.v_Values TABLE ([Value] VARCHAR(1000), [Position] BIGINT IDENTITY(1,1))

    AS

    BEGIN

    DECLARE @.v_value VARCHAR(1000),

    @.v_pos BIGINT

    SET @.v_Pos = 1

    WHILE @.data > '' AND @.v_Pos > 0

    BEGIN

    SELECT @.v_pos = CHARINDEX(@.delimeter, @.data,1),

    @.v_Value = CASE @.v_Pos

    WHEN 0 THEN

    @.data

    ELSE

    LEFT(@.data, @.v_pos-1)

    END,

    @.data = CASE

    WHEN @.v_Value = @.data THEN

    NULL

    ELSE

    SUBSTRING(@.data, @.v_pos + 1, LEN(@.data))

    END

    INSERT INTO @.v_Values ([Value])

    VALUES (@.v_Value)

    END

    return

    END

    GO

    |||

    Thanks a lot....this almost saved my project...

    Thanks

    How to calculate the index size ?

    Hi:
    I need to calculate the total size of all indexes, is the index_size
    reported by sp_spaceused 'tablename' is what i need ?
    Please help
    Thanks
    JCVoonThe following is a link to useful sps in what you are looking to do.
    Specifically look at:
    sp_columns_rowset
    sp_MShelpindex
    sp_MStablespace
    sp_MSindexspace
    In SQL Books there is an article called: Estimating the Size of a Table ,
    which will give you some ideas
    --
    --
    Jack Vamvas
    ___________________________________
    Receive free SQL tips - www.ciquery.com/sqlserver.htm
    ___________________________________
    "jcvoon" <jcvoon@.maximas.com.my> wrote in message
    news:1147078009.708121.91560@.i40g2000cwc.googlegroups.com...
    > Hi:
    > I need to calculate the total size of all indexes, is the index_size
    > reported by sp_spaceused 'tablename' is what i need ?
    > Please help
    > Thanks
    > JCVoon
    >|||Jack Vamvas :
    Thanks.
    sp_MSindexspace is what i need, in fact the index_size reported by
    sp_spaceused 'tablename' is sum of the Size reported by
    sp_MSindexspace 'tablename' .
    Regards
    JCVoon

    How to calculate the index size ?

    Hi:
    I need to calculate the total size of all indexes, is the index_size
    reported by sp_spaceused 'tablename' is what i need ?
    Please help
    Thanks
    JCVoonThe following is a link to useful sps in what you are looking to do.
    Specifically look at:
    sp_columns_rowset
    sp_MShelpindex
    sp_MStablespace
    sp_MSindexspace
    In SQL Books there is an article called: Estimating the Size of a Table ,
    which will give you some ideas
    --
    --
    Jack Vamvas
    ___________________________________
    Receive free SQL tips - www.ciquery.com/sqlserver.htm
    ___________________________________
    "jcvoon" <jcvoon@.maximas.com.my> wrote in message
    news:1147078009.708121.91560@.i40g2000cwc.googlegroups.com...
    > Hi:
    > I need to calculate the total size of all indexes, is the index_size
    > reported by sp_spaceused 'tablename' is what i need ?
    > Please help
    > Thanks
    > JCVoon
    >|||Jack Vamvas :
    Thanks.
    sp_MSindexspace is what i need, in fact the index_size reported by
    sp_spaceused 'tablename' is sum of the Size reported by
    sp_MSindexspace 'tablename' .
    Regards
    JCVoon

    How to calculate the difference between two dates - aging

    I have created a Model and using Report Builder to create a report. The
    only thing I cannot get is the formula for the date difference. I need to
    take an [Opened Date and Time] and the [Closed Date and Time] and get the
    difference in DD:HH:MM.
    Thanks in advance!Check this:
    http://msdn2.microsoft.com/en-us/library/aa258269(SQL.80).aspx
    Cheers,
    MB
    "Cliff Parker" <cliff.parker@.stewart.com> wrote in message
    news:614DB115-6BA5-4824-B4EF-5D1455F1FEC6@.microsoft.com...
    >I have created a Model and using Report Builder to create a report. The
    >only thing I cannot get is the formula for the date difference. I need to
    >take an [Opened Date and Time] and the [Closed Date and Time] and get the
    >difference in DD:HH:MM.
    > Thanks in advance!|||Hi
    Take a look at DATEDIFF system function in the BOL.
    "Cliff Parker" <cliff.parker@.stewart.com> wrote in message
    news:614DB115-6BA5-4824-B4EF-5D1455F1FEC6@.microsoft.com...
    >I have created a Model and using Report Builder to create a report. The
    >only thing I cannot get is the formula for the date difference. I need to
    >take an [Opened Date and Time] and the [Closed Date and Time] and get the
    >difference in DD:HH:MM.
    > Thanks in advance!|||The DATEDIFF function can give you the difference in minutes.
    Expressing that in the form DD:HH:MM is not so simple, as there is
    nothing in SQL Server in that format. If all you need to do is
    display it you could turn it into a string.
    Calculating the three parts is not as simple as using datepart three
    times. It requires a bit of arithmetic.
    DECLARE @.from datetime
    DECLARE @.to datetime
    SET @.from = '20060704 8:00'
    SET @.to = '20061031 10:30'
    SELECT DATEDIFF(minute,@.from, @.to) % 60 as Minutes
    SELECT (DATEDIFF(minute,@.from, @.to) / 60) % 24 as Hours
    SELECT DATEDIFF(minute,@.from, @.to) / (60 * 24) as Days
    To put this into the DD:HH:MM format we could use something like:
    DECLARE @.from datetime
    DECLARE @.to datetime
    SET @.from = '20060704 8:00'
    SET @.to = '20061031 10:30'
    SELECT DATEDIFF(minute,@.from, @.to) % 60 as Minutes
    SELECT (DATEDIFF(minute,@.from, @.to) / 60) % 24 as Hours
    SELECT DATEDIFF(minute,@.from, @.to) / (60 * 24) as Days
    SELECT DATEDIFF(minute,@.from, @.to) % 60 as Minutes
    SELECT (DATEDIFF(minute,@.from, @.to) / 60) % 24 as Hours
    SELECT CONVERT(varchar(6),DATEDIFF(minute,@.from, @.to) / (60 * 24))
    + ':' +
    RIGHT(CONVERT(varchar(6),(DATEDIFF(minute,@.from, @.to) / 60) %
    24)+100,2)
    + ':' +
    RIGHT(CONVERT(varchar(6),(DATEDIFF(minute,@.from, @.to) %
    60))+100,2)
    --
    119:02:30
    The trick used to add the leading zeroes was to add 100 before
    converting to a string and taking the two rightmost characters.
    Hopefully that gives you something to start with.
    Roy Harvey
    Beacon Falls, CT
    On Wed, 21 Mar 2007 00:12:50 -0500, "Cliff Parker"
    <cliff.parker@.stewart.com> wrote:
    >I have created a Model and using Report Builder to create a report. The
    >only thing I cannot get is the formula for the date difference. I need to
    >take an [Opened Date and Time] and the [Closed Date and Time] and get the
    >difference in DD:HH:MM.
    >Thanks in advance!|||On Wed, 21 Mar 2007 08:02:47 -0400, Roy Harvey wrote:
    >The DATEDIFF function can give you the difference in minutes.
    >Expressing that in the form DD:HH:MM is not so simple, as there is
    >nothing in SQL Server in that format. If all you need to do is
    >display it you could turn it into a string.
    >Calculating the three parts is not as simple as using datepart three
    >times. It requires a bit of arithmetic.
    (snip)
    Hi Roy (and Cliff),
    Well, for the HH:MM part, there is an alternative: compute the
    difference in minutes, add that to a starting date (any date will do) at
    midnight, and convert that to string using a "time only" format:
    DECLARE @.from datetime;
    DECLARE @.to datetime;
    SET @.from = '20060704 8:00';
    SET @.to = '20061031 10:30';
    SELECT CAST(DATEDIFF(day, @.from, @.to) AS varchar(10)) + ':'
    + CONVERT(char(5),
    DATEADD(minute,
    DATEDIFF(minute, @.from, @.to),
    '19000101'), -- Any date will do
    108);
    Results in
    119:02:30
    Hugo Kornelis, SQL Server MVP
    My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis|||On Wed, 21 Mar 2007 21:27:33 +0100, Hugo Kornelis
    <hugo@.perFact.REMOVETHIS.info.INVALID> wrote:
    >SELECT CAST(DATEDIFF(day, @.from, @.to) AS varchar(10)) + ':'
    > + CONVERT(char(5),
    > DATEADD(minute,
    > DATEDIFF(minute, @.from, @.to),
    > '19000101'), -- Any date will do
    > 108);
    Using DATEDIFF for calculating days is not always correct depending on
    the times of day of the two datetimes. Try it for times on either
    side of midnight, such as
    SET @.from = '20060704 21:00';
    SET @.to = '20060705 01:30';
    and it returns 1:04:30 rather than 1:04:30. Which is why I coded the
    day calculation as:
    DATEDIFF(minute,@.from, @.to) / (60 * 24)
    Roy Harvey
    Beacon Falls, CT|||On Wed, 21 Mar 2007 17:54:53 -0400, Roy Harvey wrote:
    >On Wed, 21 Mar 2007 21:27:33 +0100, Hugo Kornelis
    ><hugo@.perFact.REMOVETHIS.info.INVALID> wrote:
    >>SELECT CAST(DATEDIFF(day, @.from, @.to) AS varchar(10)) + ':'
    >> + CONVERT(char(5),
    >> DATEADD(minute,
    >> DATEDIFF(minute, @.from, @.to),
    >> '19000101'), -- Any date will do
    >> 108);
    >Using DATEDIFF for calculating days is not always correct depending on
    >the times of day of the two datetimes. Try it for times on either
    >side of midnight, such as
    >SET @.from = '20060704 21:00';
    >SET @.to = '20060705 01:30';
    >and it returns 1:04:30 rather than 1:04:30. Which is why I coded the
    >day calculation as:
    > DATEDIFF(minute,@.from, @.to) / (60 * 24)
    Hi Roy,
    That was a stupid error - thanks for catching it!
    --
    Hugo Kornelis, SQL Server MVP
    My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis|||Roy,
    Thanks for the information. It was right on the money. You have helped me
    out big time!
    Many cudos!
    Cliff Parker

    How to calculate the difference between two dates - aging

    I have created a Model and using Report Builder to create a report. The
    only thing I cannot get is the formula for the date difference. I need to
    take an [Opened Date and Time] and the [Closed Date and Time] and ge
    t the
    difference in DD:HH:MM.
    Thanks in advance!Check this:
    http://msdn2.microsoft.com/en-us/library/aa258269(SQL.80).aspx
    Cheers,
    MB
    "Cliff Parker" <cliff.parker@.stewart.com> wrote in message
    news:614DB115-6BA5-4824-B4EF-5D1455F1FEC6@.microsoft.com...
    >I have created a Model and using Report Builder to create a report. The
    >only thing I cannot get is the formula for the date difference. I need to
    >take an [Opened Date and Time] and the [Closed Date and Time] and g
    et the
    >difference in DD:HH:MM.
    > Thanks in advance!|||Hi
    Take a look at DATEDIFF system function in the BOL.
    "Cliff Parker" <cliff.parker@.stewart.com> wrote in message
    news:614DB115-6BA5-4824-B4EF-5D1455F1FEC6@.microsoft.com...
    >I have created a Model and using Report Builder to create a report. The
    >only thing I cannot get is the formula for the date difference. I need to
    >take an [Opened Date and Time] and the [Closed Date and Time] and g
    et the
    >difference in DD:HH:MM.
    > Thanks in advance!|||The DATEDIFF function can give you the difference in minutes.
    Expressing that in the form DD:HH:MM is not so simple, as there is
    nothing in SQL Server in that format. If all you need to do is
    display it you could turn it into a string.
    Calculating the three parts is not as simple as using datepart three
    times. It requires a bit of arithmetic.
    DECLARE @.from datetime
    DECLARE @.to datetime
    SET @.from = '20060704 8:00'
    SET @.to = '20061031 10:30'
    SELECT DATEDIFF(minute,@.from, @.to) % 60 as Minutes
    SELECT (DATEDIFF(minute,@.from, @.to) / 60) % 24 as Hours
    SELECT DATEDIFF(minute,@.from, @.to) / (60 * 24) as Days
    To put this into the DD:HH:MM format we could use something like:
    DECLARE @.from datetime
    DECLARE @.to datetime
    SET @.from = '20060704 8:00'
    SET @.to = '20061031 10:30'
    SELECT DATEDIFF(minute,@.from, @.to) % 60 as Minutes
    SELECT (DATEDIFF(minute,@.from, @.to) / 60) % 24 as Hours
    SELECT DATEDIFF(minute,@.from, @.to) / (60 * 24) as Days
    SELECT DATEDIFF(minute,@.from, @.to) % 60 as Minutes
    SELECT (DATEDIFF(minute,@.from, @.to) / 60) % 24 as Hours
    SELECT CONVERT(varchar(6),DATEDIFF(minute,@.from
    , @.to) / (60 * 24))
    + ':' +
    RIGHT(CONVERT(varchar(6),(DATEDIFF(minut
    e,@.from, @.to) / 60) %
    24)+100,2)
    + ':' +
    RIGHT(CONVERT(varchar(6),(DATEDIFF(minut
    e,@.from, @.to) %
    60))+100,2)
    119:02:30
    The trick used to add the leading zeroes was to add 100 before
    converting to a string and taking the two rightmost characters.
    Hopefully that gives you something to start with.
    Roy Harvey
    Beacon Falls, CT
    On Wed, 21 Mar 2007 00:12:50 -0500, "Cliff Parker"
    <cliff.parker@.stewart.com> wrote:

    >I have created a Model and using Report Builder to create a report. The
    >only thing I cannot get is the formula for the date difference. I need to
    >take an [Opened Date and Time] and the [Closed Date and Time] and g
    et the
    >difference in DD:HH:MM.
    >Thanks in advance!|||On Wed, 21 Mar 2007 08:02:47 -0400, Roy Harvey wrote:

    >The DATEDIFF function can give you the difference in minutes.
    >Expressing that in the form DD:HH:MM is not so simple, as there is
    >nothing in SQL Server in that format. If all you need to do is
    >display it you could turn it into a string.
    >Calculating the three parts is not as simple as using datepart three
    >times. It requires a bit of arithmetic.
    (snip)
    Hi Roy (and Cliff),
    Well, for the HH:MM part, there is an alternative: compute the
    difference in minutes, add that to a starting date (any date will do) at
    midnight, and convert that to string using a "time only" format:
    DECLARE @.from datetime;
    DECLARE @.to datetime;
    SET @.from = '20060704 8:00';
    SET @.to = '20061031 10:30';
    SELECT CAST(DATEDIFF(day, @.from, @.to) AS varchar(10)) + ':'
    + CONVERT(char(5),
    DATEADD(minute,
    DATEDIFF(minute, @.from, @.to),
    '19000101'), -- Any date will do
    108);
    Results in
    119:02:30
    Hugo Kornelis, SQL Server MVP
    My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis|||On Wed, 21 Mar 2007 21:27:33 +0100, Hugo Kornelis
    <hugo@.perFact.REMOVETHIS.info.INVALID> wrote:

    >SELECT CAST(DATEDIFF(day, @.from, @.to) AS varchar(10)) + ':'
    > + CONVERT(char(5),
    > DATEADD(minute,
    > DATEDIFF(minute, @.from, @.to),
    > '19000101'), -- Any date will do
    > 108);
    Using DATEDIFF for calculating days is not always correct depending on
    the times of day of the two datetimes. Try it for times on either
    side of midnight, such as
    SET @.from = '20060704 21:00';
    SET @.to = '20060705 01:30';
    and it returns 1:04:30 rather than 1:04:30. Which is why I coded the
    day calculation as:
    DATEDIFF(minute,@.from, @.to) / (60 * 24)
    Roy Harvey
    Beacon Falls, CT|||On Wed, 21 Mar 2007 17:54:53 -0400, Roy Harvey wrote:

    >On Wed, 21 Mar 2007 21:27:33 +0100, Hugo Kornelis
    ><hugo@.perFact.REMOVETHIS.info.INVALID> wrote:
    >
    >Using DATEDIFF for calculating days is not always correct depending on
    >the times of day of the two datetimes. Try it for times on either
    >side of midnight, such as
    >SET @.from = '20060704 21:00';
    >SET @.to = '20060705 01:30';
    >and it returns 1:04:30 rather than 1:04:30. Which is why I coded the
    >day calculation as:
    > DATEDIFF(minute,@.from, @.to) / (60 * 24)
    Hi Roy,
    That was a stupid error - thanks for catching it!
    Hugo Kornelis, SQL Server MVP
    My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis|||Roy,
    Thanks for the information. It was right on the money. You have helped me
    out big time!
    Many cudos!
    Cliff Parker

    How to calculate the difference between two dates - aging

    I have created a Model and using Report Builder to create a report. The
    only thing I cannot get is the formula for the date difference. I need to
    take an [Opened Date and Time] and the [Closed Date and Time] and get the
    difference in DD:HH:MM.
    Thanks in advance!
    Check this:
    http://msdn2.microsoft.com/en-us/library/aa258269(SQL.80).aspx
    Cheers,
    MB
    "Cliff Parker" <cliff.parker@.stewart.com> wrote in message
    news:614DB115-6BA5-4824-B4EF-5D1455F1FEC6@.microsoft.com...
    >I have created a Model and using Report Builder to create a report. The
    >only thing I cannot get is the formula for the date difference. I need to
    >take an [Opened Date and Time] and the [Closed Date and Time] and get the
    >difference in DD:HH:MM.
    > Thanks in advance!
    |||Hi
    Take a look at DATEDIFF system function in the BOL.
    "Cliff Parker" <cliff.parker@.stewart.com> wrote in message
    news:614DB115-6BA5-4824-B4EF-5D1455F1FEC6@.microsoft.com...
    >I have created a Model and using Report Builder to create a report. The
    >only thing I cannot get is the formula for the date difference. I need to
    >take an [Opened Date and Time] and the [Closed Date and Time] and get the
    >difference in DD:HH:MM.
    > Thanks in advance!
    |||The DATEDIFF function can give you the difference in minutes.
    Expressing that in the form DD:HH:MM is not so simple, as there is
    nothing in SQL Server in that format. If all you need to do is
    display it you could turn it into a string.
    Calculating the three parts is not as simple as using datepart three
    times. It requires a bit of arithmetic.
    DECLARE @.from datetime
    DECLARE @.to datetime
    SET @.from = '20060704 8:00'
    SET @.to = '20061031 10:30'
    SELECT DATEDIFF(minute,@.from, @.to) % 60 as Minutes
    SELECT (DATEDIFF(minute,@.from, @.to) / 60) % 24 as Hours
    SELECT DATEDIFF(minute,@.from, @.to) / (60 * 24) as Days
    To put this into the DD:HH:MM format we could use something like:
    DECLARE @.from datetime
    DECLARE @.to datetime
    SET @.from = '20060704 8:00'
    SET @.to = '20061031 10:30'
    SELECT DATEDIFF(minute,@.from, @.to) % 60 as Minutes
    SELECT (DATEDIFF(minute,@.from, @.to) / 60) % 24 as Hours
    SELECT DATEDIFF(minute,@.from, @.to) / (60 * 24) as Days
    SELECT DATEDIFF(minute,@.from, @.to) % 60 as Minutes
    SELECT (DATEDIFF(minute,@.from, @.to) / 60) % 24 as Hours
    SELECT CONVERT(varchar(6),DATEDIFF(minute,@.from, @.to) / (60 * 24))
    + ':' +
    RIGHT(CONVERT(varchar(6),(DATEDIFF(minute,@.from, @.to) / 60) %
    24)+100,2)
    + ':' +
    RIGHT(CONVERT(varchar(6),(DATEDIFF(minute,@.from, @.to) %
    60))+100,2)
    119:02:30
    The trick used to add the leading zeroes was to add 100 before
    converting to a string and taking the two rightmost characters.
    Hopefully that gives you something to start with.
    Roy Harvey
    Beacon Falls, CT
    On Wed, 21 Mar 2007 00:12:50 -0500, "Cliff Parker"
    <cliff.parker@.stewart.com> wrote:

    >I have created a Model and using Report Builder to create a report. The
    >only thing I cannot get is the formula for the date difference. I need to
    >take an [Opened Date and Time] and the [Closed Date and Time] and get the
    >difference in DD:HH:MM.
    >Thanks in advance!
    |||On Wed, 21 Mar 2007 21:27:33 +0100, Hugo Kornelis
    <hugo@.perFact.REMOVETHIS.info.INVALID> wrote:

    >SELECT CAST(DATEDIFF(day, @.from, @.to) AS varchar(10)) + ':'
    > + CONVERT(char(5),
    > DATEADD(minute,
    > DATEDIFF(minute, @.from, @.to),
    > '19000101'), -- Any date will do
    > 108);
    Using DATEDIFF for calculating days is not always correct depending on
    the times of day of the two datetimes. Try it for times on either
    side of midnight, such as
    SET @.from = '20060704 21:00';
    SET @.to = '20060705 01:30';
    and it returns 1:04:30 rather than 1:04:30. Which is why I coded the
    day calculation as:
    DATEDIFF(minute,@.from, @.to) / (60 * 24)
    Roy Harvey
    Beacon Falls, CT
    |||Roy,
    Thanks for the information. It was right on the money. You have helped me
    out big time!
    Many cudos!
    Cliff Parker

    How to calculate table and index size

    Can anyone point me in the right direction on how to calculate table and
    index sizes
    TIA
    RobEXEC sp_spaceused 'tablename'
    http://www.aspfaq.com/
    (Reverse address to reply.)
    "Rob Diamant" <rob@.usi.com> wrote in message
    news:#4qxCfGsEHA.808@.TK2MSFTNGP12.phx.gbl...
    > Can anyone point me in the right direction on how to calculate table and
    > index sizes
    > TIA
    > Rob
    >

    How to calculate table and index size

    Can anyone point me in the right direction on how to calculate table and
    index sizes
    TIA
    RobEXEC sp_spaceused 'tablename'
    --
    http://www.aspfaq.com/
    (Reverse address to reply.)
    "Rob Diamant" <rob@.usi.com> wrote in message
    news:#4qxCfGsEHA.808@.TK2MSFTNGP12.phx.gbl...
    > Can anyone point me in the right direction on how to calculate table and
    > index sizes
    > TIA
    > Rob
    >

    How to calculate table and index size

    Can anyone point me in the right direction on how to calculate table and
    index sizes
    TIA
    Rob
    EXEC sp_spaceused 'tablename'
    http://www.aspfaq.com/
    (Reverse address to reply.)
    "Rob Diamant" <rob@.usi.com> wrote in message
    news:#4qxCfGsEHA.808@.TK2MSFTNGP12.phx.gbl...
    > Can anyone point me in the right direction on how to calculate table and
    > index sizes
    > TIA
    > Rob
    >

    How to Calculate Sum for distinct values in MDX

    Hi ALL,
    I need help in calculating sum of market value based on property_id. The sum should be calculated by finding the average market value for a given property and then sum the individual average of the property to get the Distinct sum.

    I have a very little knowlegde in Cubes and analysis services. I need to perform this distinct sum by using calculated members using MDX on SQL server 2000.

    Data example

    P_Code Cus Prpty_Id Mrkt Val

    3000 1234 1111 $10,000 $10,000
    3000 1234 2222 $20,000
    3000 1234 3333 $30,000 $20,000
    3000 5678 1111 $10,000
    3000 5678 2222 $20,000 $30,000
    3000 5678 3333 $30,000
    3000 1020 1111 $10,000
    3000 1020 3333 $30,000

    Distinct Sum $60,000

    Thanks in Advance

    BrijeshWhat about SELECT SUM(DISTINCT Mrkt)
    FROM tbl
    GROUP BY P_Code|||Nope: http://weblogs.sqlteam.com/jeffs/archive/2007/07/31/60274.aspx

    How to calculate response time.

    Hello.
    I have a task to do which is as follows:
    I have to do an "id query" on a primary key of my db and then calculate the response time of the query.
    Task says that i'm able to take screenshots of a graphical view of response time.And as a hint says that there are sql commands that can do such thing.Allthough i've searched the books i have access to and the internet i can't find such thing.I dunno maybe someone more familiar with this stuff can understand what i'm supposed to do.I don't want to give me the whole solution but just if u can tell me where to look.
    I don't understand what means "do an id query on primary key 'SMTH' " and i can't find anythin which is related on calculating system response time to execute a query.
    Any help is much appreciated.
    Thx a lot :)See set statistics statement, set statistics time in particular

    How to calculate record size

    Hi,
    Can anyone help me to calculate record size for a table.
    1. Lets say I have a table with 10 fields, each of them is type int. Now,
    will record size be diferent if I save in each field value 0 versus if I
    save value NULL?
    2. What if table has all column as NOT NULL? Will then SQL Server still
    create NULL bitmap for record:
    Null Bitmap (Null_Bitmap) = 2 + (( Num_Cols + 7) / 8 )
    3. BOL has different size calculations for fixed-length columns and variable
    length column. Is NUMERIC(19,2) NOT NULL field considered to be fixed length
    or variable length? Which data types are variable length? I do know that
    varchar, varbinary, nvarchar is variable. But are there any other?
    Thank you for helpI should add that I am using SQL SErver 2000.
    Thanks
    "NoSpam" <NoSpam@.NoSpam.com> wrote in message
    news:uvsHEJMzEHA.2040@.tk2msftngp13.phx.gbl...
    > Hi,
    > Can anyone help me to calculate record size for a table.
    > 1. Lets say I have a table with 10 fields, each of them is type int. Now,
    > will record size be diferent if I save in each field value 0 versus if I
    > save value NULL?
    > 2. What if table has all column as NOT NULL? Will then SQL Server still
    > create NULL bitmap for record:
    > Null Bitmap (Null_Bitmap) = 2 + (( Num_Cols + 7) / 8 )
    > 3. BOL has different size calculations for fixed-length columns and
    > variable length column. Is NUMERIC(19,2) NOT NULL field considered to be
    > fixed length or variable length? Which data types are variable length? I
    > do know that varchar, varbinary, nvarchar is variable. But are there any
    > other?
    >
    > Thank you for help
    >
    >|||Kalen Delaney's "Inside SQL server 2000" will give you the full story on
    this but in summary:
    1. NO, Size will always be the same.
    2. If I remember right if there are NO nullable columns then the null bitmap
    is not stored in the row.
    3. Add text (and ntext) as effectively variable length. How much is stored
    in the row is dependant on whether the text is to be stored within the row,
    or a pointer to seperate text pages, and hence could vary from 16 bytes
    upwards. Your specific example of numeric (19,2) is fixed length. BOL has
    defined physical sizes for all the different data types.
    Mike John
    "NoSpam" <NoSpam@.NoSpam.com> wrote in message
    news:e5fliKMzEHA.3184@.TK2MSFTNGP10.phx.gbl...
    >I should add that I am using SQL SErver 2000.
    > Thanks
    > "NoSpam" <NoSpam@.NoSpam.com> wrote in message
    > news:uvsHEJMzEHA.2040@.tk2msftngp13.phx.gbl...
    >> Hi,
    >> Can anyone help me to calculate record size for a table.
    >> 1. Lets say I have a table with 10 fields, each of them is type int. Now,
    >> will record size be diferent if I save in each field value 0 versus if I
    >> save value NULL?
    >> 2. What if table has all column as NOT NULL? Will then SQL Server still
    >> create NULL bitmap for record:
    >> Null Bitmap (Null_Bitmap) = 2 + (( Num_Cols + 7) / 8 )
    >> 3. BOL has different size calculations for fixed-length columns and
    >> variable length column. Is NUMERIC(19,2) NOT NULL field considered to be
    >> fixed length or variable length? Which data types are variable length? I
    >> do know that varchar, varbinary, nvarchar is variable. But are there any
    >> other?
    >>
    >> Thank you for help
    >>
    >|||Mike,
    Thanks for the information.
    "Mike John" <Mike.John@.knowledgepool.com> wrote in message
    news:e2BQoSPzEHA.1564@.TK2MSFTNGP09.phx.gbl...
    > Kalen Delaney's "Inside SQL server 2000" will give you the full story on
    > this but in summary:
    > 1. NO, Size will always be the same.
    > 2. If I remember right if there are NO nullable columns then the null
    > bitmap is not stored in the row.
    > 3. Add text (and ntext) as effectively variable length. How much is stored
    > in the row is dependant on whether the text is to be stored within the
    > row, or a pointer to seperate text pages, and hence could vary from 16
    > bytes upwards. Your specific example of numeric (19,2) is fixed length.
    > BOL has defined physical sizes for all the different data types.
    > Mike John
    > "NoSpam" <NoSpam@.NoSpam.com> wrote in message
    > news:e5fliKMzEHA.3184@.TK2MSFTNGP10.phx.gbl...
    >>I should add that I am using SQL SErver 2000.
    >> Thanks
    >> "NoSpam" <NoSpam@.NoSpam.com> wrote in message
    >> news:uvsHEJMzEHA.2040@.tk2msftngp13.phx.gbl...
    >> Hi,
    >> Can anyone help me to calculate record size for a table.
    >> 1. Lets say I have a table with 10 fields, each of them is type int.
    >> Now, will record size be diferent if I save in each field value 0 versus
    >> if I save value NULL?
    >> 2. What if table has all column as NOT NULL? Will then SQL Server still
    >> create NULL bitmap for record:
    >> Null Bitmap (Null_Bitmap) = 2 + (( Num_Cols + 7) / 8 )
    >> 3. BOL has different size calculations for fixed-length columns and
    >> variable length column. Is NUMERIC(19,2) NOT NULL field considered to be
    >> fixed length or variable length? Which data types are variable length? I
    >> do know that varchar, varbinary, nvarchar is variable. But are there any
    >> other?
    >>
    >> Thank you for help
    >>
    >>
    >

    How to calculate record size

    Hi,
    Can anyone help me to calculate record size for a table.
    1. Lets say I have a table with 10 fields, each of them is type int. Now,
    will record size be diferent if I save in each field value 0 versus if I
    save value NULL?
    2. What if table has all column as NOT NULL? Will then SQL Server still
    create NULL bitmap for record:
    Null Bitmap (Null_Bitmap) = 2 + (( Num_Cols + 7) / 8 )
    3. BOL has different size calculations for fixed-length columns and variable
    length column. Is NUMERIC(19,2) NOT NULL field considered to be fixed length
    or variable length? Which data types are variable length? I do know that
    varchar, varbinary, nvarchar is variable. But are there any other?
    Thank you for helpI should add that I am using SQL SErver 2000.
    Thanks
    "NoSpam" <NoSpam@.NoSpam.com> wrote in message
    news:uvsHEJMzEHA.2040@.tk2msftngp13.phx.gbl...
    > Hi,
    > Can anyone help me to calculate record size for a table.
    > 1. Lets say I have a table with 10 fields, each of them is type int. Now,
    > will record size be diferent if I save in each field value 0 versus if I
    > save value NULL?
    > 2. What if table has all column as NOT NULL? Will then SQL Server still
    > create NULL bitmap for record:
    > Null Bitmap (Null_Bitmap) = 2 + (( Num_Cols + 7) / 8 )
    > 3. BOL has different size calculations for fixed-length columns and
    > variable length column. Is NUMERIC(19,2) NOT NULL field considered to be
    > fixed length or variable length? Which data types are variable length? I
    > do know that varchar, varbinary, nvarchar is variable. But are there any
    > other?
    >
    > Thank you for help
    >
    >|||Kalen Delaney's "Inside SQL server 2000" will give you the full story on
    this but in summary:
    1. NO, Size will always be the same.
    2. If I remember right if there are NO nullable columns then the null bitmap
    is not stored in the row.
    3. Add text (and ntext) as effectively variable length. How much is stored
    in the row is dependant on whether the text is to be stored within the row,
    or a pointer to seperate text pages, and hence could vary from 16 bytes
    upwards. Your specific example of numeric (19,2) is fixed length. BOL has
    defined physical sizes for all the different data types.
    Mike John
    "NoSpam" <NoSpam@.NoSpam.com> wrote in message
    news:e5fliKMzEHA.3184@.TK2MSFTNGP10.phx.gbl...
    >I should add that I am using SQL SErver 2000.
    > Thanks
    > "NoSpam" <NoSpam@.NoSpam.com> wrote in message
    > news:uvsHEJMzEHA.2040@.tk2msftngp13.phx.gbl...
    >|||Mike,
    Thanks for the information.
    "Mike John" <Mike.John@.knowledgepool.com> wrote in message
    news:e2BQoSPzEHA.1564@.TK2MSFTNGP09.phx.gbl...
    > Kalen Delaney's "Inside SQL server 2000" will give you the full story on
    > this but in summary:
    > 1. NO, Size will always be the same.
    > 2. If I remember right if there are NO nullable columns then the null
    > bitmap is not stored in the row.
    > 3. Add text (and ntext) as effectively variable length. How much is stored
    > in the row is dependant on whether the text is to be stored within the
    > row, or a pointer to seperate text pages, and hence could vary from 16
    > bytes upwards. Your specific example of numeric (19,2) is fixed length.
    > BOL has defined physical sizes for all the different data types.
    > Mike John
    > "NoSpam" <NoSpam@.NoSpam.com> wrote in message
    > news:e5fliKMzEHA.3184@.TK2MSFTNGP10.phx.gbl...
    >

    How to calculate record size

    Hi,
    Can anyone help me to calculate record size for a table.
    1. Lets say I have a table with 10 fields, each of them is type int. Now,
    will record size be diferent if I save in each field value 0 versus if I
    save value NULL?
    2. What if table has all column as NOT NULL? Will then SQL Server still
    create NULL bitmap for record:
    Null Bitmap (Null_Bitmap) = 2 + (( Num_Cols + 7) / 8 )
    3. BOL has different size calculations for fixed-length columns and variable
    length column. Is NUMERIC(19,2) NOT NULL field considered to be fixed length
    or variable length? Which data types are variable length? I do know that
    varchar, varbinary, nvarchar is variable. But are there any other?
    Thank you for help
    I should add that I am using SQL SErver 2000.
    Thanks
    "NoSpam" <NoSpam@.NoSpam.com> wrote in message
    news:uvsHEJMzEHA.2040@.tk2msftngp13.phx.gbl...
    > Hi,
    > Can anyone help me to calculate record size for a table.
    > 1. Lets say I have a table with 10 fields, each of them is type int. Now,
    > will record size be diferent if I save in each field value 0 versus if I
    > save value NULL?
    > 2. What if table has all column as NOT NULL? Will then SQL Server still
    > create NULL bitmap for record:
    > Null Bitmap (Null_Bitmap) = 2 + (( Num_Cols + 7) / 8 )
    > 3. BOL has different size calculations for fixed-length columns and
    > variable length column. Is NUMERIC(19,2) NOT NULL field considered to be
    > fixed length or variable length? Which data types are variable length? I
    > do know that varchar, varbinary, nvarchar is variable. But are there any
    > other?
    >
    > Thank you for help
    >
    >
    |||Kalen Delaney's "Inside SQL server 2000" will give you the full story on
    this but in summary:
    1. NO, Size will always be the same.
    2. If I remember right if there are NO nullable columns then the null bitmap
    is not stored in the row.
    3. Add text (and ntext) as effectively variable length. How much is stored
    in the row is dependant on whether the text is to be stored within the row,
    or a pointer to seperate text pages, and hence could vary from 16 bytes
    upwards. Your specific example of numeric (19,2) is fixed length. BOL has
    defined physical sizes for all the different data types.
    Mike John
    "NoSpam" <NoSpam@.NoSpam.com> wrote in message
    news:e5fliKMzEHA.3184@.TK2MSFTNGP10.phx.gbl...
    >I should add that I am using SQL SErver 2000.
    > Thanks
    > "NoSpam" <NoSpam@.NoSpam.com> wrote in message
    > news:uvsHEJMzEHA.2040@.tk2msftngp13.phx.gbl...
    >
    |||Mike,
    Thanks for the information.
    "Mike John" <Mike.John@.knowledgepool.com> wrote in message
    news:e2BQoSPzEHA.1564@.TK2MSFTNGP09.phx.gbl...
    > Kalen Delaney's "Inside SQL server 2000" will give you the full story on
    > this but in summary:
    > 1. NO, Size will always be the same.
    > 2. If I remember right if there are NO nullable columns then the null
    > bitmap is not stored in the row.
    > 3. Add text (and ntext) as effectively variable length. How much is stored
    > in the row is dependant on whether the text is to be stored within the
    > row, or a pointer to seperate text pages, and hence could vary from 16
    > bytes upwards. Your specific example of numeric (19,2) is fixed length.
    > BOL has defined physical sizes for all the different data types.
    > Mike John
    > "NoSpam" <NoSpam@.NoSpam.com> wrote in message
    > news:e5fliKMzEHA.3184@.TK2MSFTNGP10.phx.gbl...
    >

    how to calculate prior period with 52 weeks ?

    I defined the calc for prior period for sales amount. But is it possible to get last 52 weeks from the current period?

    how to define in this calc?

    -- PRIOR PERIOD CALCULATIONS

    ([Time Calculations].[Prior Period]=

    IIF(([Date].[Fiscal Hierarchy].currentmember.lag(1),

    [Time Calculations].&[ Current Period])=0,null,

    ([Date].[Fiscal Hierarchy].currentmember.lag(1),

    [Time Calculations].&[ Current Period]))

    );

    If your hierarchy has a week-level, then I believe what you are looking for is this:

    Code Snippet

    SET [Time Calculations].[Prior Period] AS 'LastPeriods( 52, [Date].[Fiscal Hierarchy].currentmember )

    This gives you a set of the current week and the previous 52 weeks. Assuming [Date].[Fiscal Hierarchy].currentmember is at week-level.

    Best regards

    - Jens