Showing posts with label formula. Show all posts
Showing posts with label formula. Show all posts

Tuesday, March 27, 2012

Calculated measure's MDX formula in (regular) Non-calculated measure

Hi,

This is sort of an extension to my previous question: converting seconds to HH:MM:SS format. I got the MDX formula for that from deepak. but for that i have to create a Calculated measure!!!!

I already have the measures which display seconds, now i have to create a calculated measure which will display the seconds in HH:MM:SS. but I dont want the extra seconds measure to be displayed. I know i can make it visible=false. but this is cumbersome for me because all this is done programmatically using AMO in a program.

so is there a way to provide the MDX formula in the regaular measure itself without having to create a calculated measure.

Thus display/convert the seconds to the HH:MM:SS format in the original measure?

Regards

I forgot one more thing, can we use the "MeasureExpression" and "FormatString" properties of the measure itself? I tried puttin the MDX formula here but it gives a error.|||

Using the MeasureExpression property is the preferred technique to accomplish what you want. It works for the cases I have used it in. What error do you receive, and what is the MDX you're providing as an expression?

PGoldy

|||Only multiplication and division are allowed for measure expression.|||

Hi,

Thanks for the reply.

Here is the scenario with the MDX formula:

Consider that I have a regular measure M1. The measure M1 contains seconds as data. (eg: 400 seconds, 1000 seconds..etc.) currently what I do is whenever I have a measure which contains seconds and has to be displayed in the HH:MM:SS format:

1. I rename the M1 measure as "_M1" and make it visible=false,
2. Then I create a calulated measure with the name M1 and with the formula TimeSerial(0,0,Measures.[_M1]) and FormatString as 'hh:mm:ss'
3. Now the calculated measure M1 contains the the values of seconds formatted to the HH:MM:SS format. example: (TimeSerial(0,0, 37895) = 10:37:35 ; TimeSerial(0,0,1023) = 00:17:03)

What I require is not to create the extra calculated measure M1, but use the TimeSerial(0,0,MeasureName) formula in the regular measure M1. When I put the TimeSerial(0,0,MeasureName) formula in the MDXExpression property and the FormatString as 'hh:mm:ss', I get the following error : "The Cube xxxx cannot be saved because of the following errors: Errors in the metadata manager. The measure expression of the M1 measure is not of the form [measure1]*[measure2] or [measure1]/[measure2]"

Is there a way out for this? what other alternative is possible?

Regards

Calculated Measure: Formula limitations

Hi,

I am using AMO to programmaticaly create the SSAS objects like Cubes, MeasureGroups, Measures, Calculated Measure, partitions .etc.

I pick the necessary information from an existing Oracle OLAP/datawarehouse. In the Oracle OLAP the calculated measure can contain any formulae which the oracle db can support. This means its a huge list....! for example, Log, Decode functions.

Though SQL Db provides these functions, they are not avaliable to use directly in the MDXScript of the Calculated Measure.

so now when I am synchronizing the Oracle OLAP to SSAS programmatically at runtime, how do I resolve these inconsistencies in the formulae?

Regards

Vijay R

I think you mixing several concepts.

You can still referece Oracle functions while in DSV. You can create Named query or Named caclulation referencing any feature avaliable in your relational database.

Edward.
--
This posting is provided "AS IS" with no warranties, and confers no rights.

|||

Hi Edward,

I guess I did not explain it correctly, I will try and explain it again.

I have a Oracle OLAP with an J2EE web app. I can create measures, calculated measures and the likes. when i create calculated measures in the Oracle OLAP using the J2EE UI, I can provide any formulae which the Oracle core can understand.

so now for eg: I have defined 2 measures M1 and M2 in Oracle OLAP. Next I define a calculated measure in Oracle OLAP, Here I define a formula like (M1 + M2)/100. Now when i view the data I get three columns M1, M2 and Calculated Values (M1+M2)/100.

Now I have a AMO program in .NET that automatically picks up the new measures and their related table info from the Oracle Relational tables and creates the same ones in SSAS. This means it creates a cube, then creates a measure group, sets the source of the measure group to the Oracle relational table (fact table), then creates the measures and partitions. The source property for all these is still the oracle relational database. but the information as to which tables, columns to use is picked from the existing oracle OLAP.

finally I process the cube in SSAS, and it is browseable.

The problem is when i try to create the calculated measure that is there in Oracle OLAP. I retrive the formula of the measure as (M1 + M2)/100. Now what I do in AMO is add a MDX script that creates a Member and adds this formula to it. Now this becomes a calculated measure in SSAS. whose value is got on the leaf cells using the "CALCULATE" command in the MDX script.

This works fine for all operators like + , - , * , / and also for Avg, Sum. but when it gets a formula like decode .... or abs, the formula in the MDX script is not recognised by SSAS. This is because MSXScript does not have those functions. I cannot map them to any Oracle functions because they are all built in Oracle funtions. like you know 'decode' is a common Database function avaliable in SQL also.

I am still stuck with this. currently we are not syncronizing the measures which have such functions. I have written a filter for them :-). so how do we do this. I was thinking that we write a SQL CLR function and pass the formula to it, which converts the formula to a MDX normalized form or something. still not sure.

Okay, I forgot. I have not used names queries or named calcualtions. I thought that names queries are for a partition. I have a fixed SSAS datasource which is mapped to the oracle db. when i use AMO to create the partition i just specify the table name in the SSAS datasource. About named calculations, I have to frankly accept I have no idea about it. ( I will look at this)

today there was another problem while using aggregation functions like avg and sum in the calculated measures (see my new post ). Not sure what is happening.

Thanks for the reply.

Regards

|||

Several things.

One, please take a look at the named calculations. This allows you to create a new column in your DSV "pusihng" calculations to relational database.

Second, take a look at the Analysis Services stored procedures. See if you can write a stored procedure implementing the functionality you need. Later you will use stored procedure in your MDX query.

Hope that helps.

Edward.
--
This posting is provided "AS IS" with no warranties, and confers no rights.

Sunday, March 25, 2012

Calculated Measure Error

In the calculated measure formula when something is being divided by 0.XX (like 0.35..fractions) its producing those wrong figures where decimal is at a wrong place. But if we divide the numerator and denominator by 100, we get the correct figures.

[something].[Something] *100 / 0.XX *100 = good

[something].[Something] / 0.XX = wrong

Why is that? and what can be done to do it right. Thanks.

Are you using AS 2000 or AS 2005, and can you reproduce this problem with one of the standard cubes (like Foodmart or Adventure Works)?

Calculated Fields

When you use the FORMULA attribute for a column in EM does SQL Server
actually store the "calculated" data or just materialize it when it's
fetched ?
TIA
Liz
Liz wrote:
> When you use the FORMULA attribute for a column in EM does SQL Server
> actually store the "calculated" data or just materialize it when it's
> fetched ?
> TIA
> Liz
The value is not permanently stored unlesss you create an index on the
computed column. For that reason computed columns in tables are of
little value unless you intend to index them.
If you try to update a computed column directly you will get an error.
That's why for most purposes I think it is safer and makes the meaning
clearer if you put computations into views rather than tables. Views
can be indexed too, so I would only consider a computed column where an
indexed view or some other method couldn't achieve the same purpose.
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
|||"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:1166266554.326117.237240@.16g2000cwy.googlegro ups.com...
> Liz wrote:
> The value is not permanently stored unlesss you create an index on the
> computed column. For that reason computed columns in tables are of
> little value unless you intend to index them.
I tried it out because I'm working with a table that has incredibly bizarre
column names so it just makes it easier to reference a+b+c as MYFIELD ... I
did index the calculated column and then had a brief glitch; a row which
should have matched did not ... then the problem "went away" ... which makes
me uneasy. If the "underlying" columns are indexed does the "calculated
column" effectively "use" the index ? I guess I can look at an execution
plan and find out ...
Thanks,
Liz

> If you try to update a computed column directly you will get an error.
> That's why for most purposes I think it is safer and makes the meaning
> clearer if you put computations into views rather than tables. Views
> can be indexed too, so I would only consider a computed column where an
> indexed view or some other method couldn't achieve the same purpose.
> --
> David Portas, SQL Server MVP
> Whenever possible please post enough code to reproduce your problem.
> Including CREATE TABLE and INSERT statements usually helps.
> State what version of SQL Server you are using and specify the content
> of any error messages.
> SQL Server Books Online:
> http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
> --
>
|||Calculated fields are 'calculated' upon request. The data is not
materialized. (There is a way, however, involving indexing... But that is
not 'normal'.)
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
You can't help someone get up a hill without getting a little closer to the
top yourself.
- H. Norman Schwarzkopf
"Liz" <liz@.tiredofspam.com> wrote in message
news:OeszSGNIHHA.2632@.TK2MSFTNGP06.phx.gbl...
> When you use the FORMULA attribute for a column in EM does SQL Server
> actually store the "calculated" data or just materialize it when it's
> fetched ?
> TIA
> Liz
>

Calculated Fields

When you use the FORMULA attribute for a column in EM does SQL Server
actually store the "calculated" data or just materialize it when it's
fetched ?
TIA
LizLiz wrote:
> When you use the FORMULA attribute for a column in EM does SQL Server
> actually store the "calculated" data or just materialize it when it's
> fetched ?
> TIA
> Liz
The value is not permanently stored unlesss you create an index on the
computed column. For that reason computed columns in tables are of
little value unless you intend to index them.
If you try to update a computed column directly you will get an error.
That's why for most purposes I think it is safer and makes the meaning
clearer if you put computations into views rather than tables. Views
can be indexed too, so I would only consider a computed column where an
indexed view or some other method couldn't achieve the same purpose.
--
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--|||"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:1166266554.326117.237240@.16g2000cwy.googlegroups.com...
> Liz wrote:
>> When you use the FORMULA attribute for a column in EM does SQL Server
>> actually store the "calculated" data or just materialize it when it's
>> fetched ?
>> TIA
>> Liz
> The value is not permanently stored unlesss you create an index on the
> computed column. For that reason computed columns in tables are of
> little value unless you intend to index them.
I tried it out because I'm working with a table that has incredibly bizarre
column names so it just makes it easier to reference a+b+c as MYFIELD ... I
did index the calculated column and then had a brief glitch; a row which
should have matched did not ... then the problem "went away" ... which makes
me uneasy. If the "underlying" columns are indexed does the "calculated
column" effectively "use" the index ? I guess I can look at an execution
plan and find out ...
Thanks,
Liz
> If you try to update a computed column directly you will get an error.
> That's why for most purposes I think it is safer and makes the meaning
> clearer if you put computations into views rather than tables. Views
> can be indexed too, so I would only consider a computed column where an
> indexed view or some other method couldn't achieve the same purpose.
> --
> David Portas, SQL Server MVP
> Whenever possible please post enough code to reproduce your problem.
> Including CREATE TABLE and INSERT statements usually helps.
> State what version of SQL Server you are using and specify the content
> of any error messages.
> SQL Server Books Online:
> http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
> --
>|||Calculated fields are 'calculated' upon request. The data is not
materialized. (There is a way, however, involving indexing... But that is
not 'normal'.)
--
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
You can't help someone get up a hill without getting a little closer to the
top yourself.
- H. Norman Schwarzkopf
"Liz" <liz@.tiredofspam.com> wrote in message
news:OeszSGNIHHA.2632@.TK2MSFTNGP06.phx.gbl...
> When you use the FORMULA attribute for a column in EM does SQL Server
> actually store the "calculated" data or just materialize it when it's
> fetched ?
> TIA
> Liz
>

Calculated Fields

When you use the FORMULA attribute for a column in EM does SQL Server
actually store the "calculated" data or just materialize it when it's
fetched ?
TIA
LizLiz wrote:
> When you use the FORMULA attribute for a column in EM does SQL Server
> actually store the "calculated" data or just materialize it when it's
> fetched ?
> TIA
> Liz
The value is not permanently stored unlesss you create an index on the
computed column. For that reason computed columns in tables are of
little value unless you intend to index them.
If you try to update a computed column directly you will get an error.
That's why for most purposes I think it is safer and makes the meaning
clearer if you put computations into views rather than tables. Views
can be indexed too, so I would only consider a computed column where an
indexed view or some other method couldn't achieve the same purpose.
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
--|||"David Portas" <REMOVE_BEFORE_REPLYING_dportas@.acm.org> wrote in message
news:1166266554.326117.237240@.16g2000cwy.googlegroups.com...
> Liz wrote:
> The value is not permanently stored unlesss you create an index on the
> computed column. For that reason computed columns in tables are of
> little value unless you intend to index them.
I tried it out because I'm working with a table that has incredibly bizarre
column names so it just makes it easier to reference a+b+c as MYFIELD ... I
did index the calculated column and then had a brief glitch; a row which
should have matched did not ... then the problem "went away" ... which makes
me uneasy. If the "underlying" columns are indexed does the "calculated
column" effectively "use" the index ? I guess I can look at an execution
plan and find out ...
Thanks,
Liz

> If you try to update a computed column directly you will get an error.
> That's why for most purposes I think it is safer and makes the meaning
> clearer if you put computations into views rather than tables. Views
> can be indexed too, so I would only consider a computed column where an
> indexed view or some other method couldn't achieve the same purpose.
> --
> David Portas, SQL Server MVP
> Whenever possible please post enough code to reproduce your problem.
> Including CREATE TABLE and INSERT statements usually helps.
> State what version of SQL Server you are using and specify the content
> of any error messages.
> SQL Server Books Online:
> http://msdn2.microsoft.com/library/ms130214(en-US,SQL.90).aspx
> --
>|||Calculated fields are 'calculated' upon request. The data is not
materialized. (There is a way, however, involving indexing... But that is
not 'normal'.)
Arnie Rowland, Ph.D.
Westwood Consulting, Inc
Most good judgment comes from experience.
Most experience comes from bad judgment.
- Anonymous
You can't help someone get up a hill without getting a little closer to the
top yourself.
- H. Norman Schwarzkopf
"Liz" <liz@.tiredofspam.com> wrote in message
news:OeszSGNIHHA.2632@.TK2MSFTNGP06.phx.gbl...
> When you use the FORMULA attribute for a column in EM does SQL Server
> actually store the "calculated" data or just materialize it when it's
> fetched ?
> TIA
> Liz
>

Tuesday, March 20, 2012

Calculate elapsed time between dates and exclude a time span

I need a formula to calculate the time (let's say in minutes) between two dates/times.
The problem is that I have to exclude the time between 06 PM and 06 AM and also exclude the time in the weekend (Saturday and Sunday).
I will use this in a couple of reports made in Reporting Services. The main purpouse is to keep track of the time an incident is open in a help desk program and non working hours should not be counted.
If anyone have an algoritm that could be modified for this and is willing to share this I would be very grateful.
Many thanks!
/Per Lissel
From http://www.developmentnow.com/g/115_0_0_0_0_0/sql-server-reporting-services.ht
Posted via DevelopmentNow.com Group
http://www.developmentnow.comThis made me curious. I didn't have an answer but I knew a group that would.
Being the Reporting Services MVP I have access to the private SQL Server MVP
newsgroup. These guys are truly amazing what they can do with SQL
statements. MVP Steve Kass provided me with the following. Essentially you
create a table that has on and off times. You insert rows for any week in
the past (his example did this for the year 2000). Then the rest of the
script fills in the table with values for 2100 weeks of values.
Next the fuction creation uses this helper table to come back with the
values. It makes my brain hurt to follow what the function is doing. Anyway,
give it a try and test it out from outside of Reporting Services.
This will also allow you to fill in Holidays as well.
>>>>>>>>>>>>>>
Bruce,
See if this approach with a helper table fits the bill:
create table Minute_Count(
d datetime primary key,
elapsed_minutes int not null,
switch varchar(10) not null check (switch in ('On','Off'))
);
insert into Minute_Count values ('2000-01-03T06:00:00',0,'On');
insert into Minute_Count values ('2000-01-03T18:00:00',12*60,'Off');
insert into Minute_Count values ('2000-01-04T06:00:00',12*60,'On');
insert into Minute_Count values ('2000-01-04T18:00:00',24*60,'Off');
insert into Minute_Count values ('2000-01-05T06:00:00',24*60,'On');
insert into Minute_Count values ('2000-01-05T18:00:00',36*60,'Off');
insert into Minute_Count values ('2000-01-06T06:00:00',36*60,'On');
insert into Minute_Count values ('2000-01-06T18:00:00',48*60,'Off');
insert into Minute_Count values ('2000-01-07T06:00:00',48*60,'On');
insert into Minute_Count values ('2000-01-07T18:00:00',60*60,'Off');
-- any Monday-Friday week
declare @.week int;
set @.week = 1;
while @.week < 2100 begin
insert into Minute_Count
select
dateadd(week,@.week,d),
elapsed_minutes + 60*@.week*60,
switch
from Minute_Count
set @.week = @.week * 2
end;
-- select * from Minute_Count;
go
create function DeskTimeDiff_minutes(
@.from datetime,
@.to datetime
) returns int as begin
declare @.fromSerial int;
declare @.toSerial int;
with S(d,elapsed_minutes,switch) as (
select top 1 d,elapsed_minutes, switch
from Minute_Count
where d <= @.from
order by d desc
)
select @.fromSerial = elapsed_minutes +
case when switch = 'On'
then datediff(minute,d,@.from)
else 0 end
from S;
with S(d,elapsed_minutes,switch) as (
select top 1 d,elapsed_minutes, switch
from Minute_Count
where d <= @.to
order by d desc
)
select @.toSerial = elapsed_minutes +
case when switch = 'On'
then datediff(minute,d,@.to)
else 0 end
from S;
return @.toSerial - @.fromSerial;
end;
go
>>>>>>>>>>>>
--
Bruce Loehle-Conger
MVP SQL Server Reporting Services
"Per Lissel" <per.lissel[at]outokumpu.com> wrote in message
news:02479620-d21d-463d-8c50-07e27ca783c8@.developmentnow.com...
>I need a formula to calculate the time (let's say in minutes) between two
>dates/times.
> The problem is that I have to exclude the time between 06 PM and 06 AM and
> also exclude the time in the weekend (Saturday and Sunday).
> I will use this in a couple of reports made in Reporting Services. The
> main purpouse is to keep track of the time an incident is open in a help
> desk program and non working hours should not be counted.
> If anyone have an algoritm that could be modified for this and is willing
> to share this I would be very grateful.
> Many thanks!
> /Per Lissel
>
>
> From
> http://www.developmentnow.com/g/115_0_0_0_0_0/sql-server-reporting-services.htm
> Posted via DevelopmentNow.com Groups
> http://www.developmentnow.com|||OK, here is another solution from SQL Server MVP Allen White. Note that
you'll have to tweak it to get rig of calls to employee and location tables
(used to factor in time zone). Also the below code is going 8 to 5 so that
needs to be tweaked as well. Note that no additional table is needed.
--
Bruce Loehle-Conger
MVP SQL Server Reporting Services
CREATE FUNCTION [dbo].fnGetWorkMinutes
(@.start_dt datetime=null, @.end_dt datetime=null, @.co_user_id int=null)
RETURNS int
AS
-- Returns the number of weekday hours between the supplied
dates
BEGIN
declare @.d1 datetime, @.d2 datetime, @.d1a datetime, @.d2a datetime,
@.hours float, @.minutes int, @.offset int
set @.offset = 0
if @.co_user_id is not null
begin
select @.offset = l.timezone_offset
from [HelpDesk].[dbo].[Company_User] c INNER JOIN
[HelpDesk].[dbo].[Location_Def] l
on c.[loc_id] = l.[loc_id]
where c.co_user_id = @.co_user_id
end
set @.d1 = dateadd(hour, @.offset, @.start_dt)
set @.d2 = dateadd(hour, @.offset, @.end_dt)
if convert(char(10),@.d1,101) = convert(char(10),@.d2,101)
BEGIN
SELECT @.minutes = DATEDIFF(minute, @.d1, @.d2)
END
ELSE
BEGIN
SET @.d1 = CASE WHEN @.d1 > convert(char(10),@.d1,101) + ' 17:00'
THEN convert(char(10),dateadd(day,1,@.d1),101) + ' 08:00'
ELSE @.d1 END
SET @.d2 = CASE WHEN @.d2 < convert(char(10),@.d2,101) + ' 08:00'
THEN convert(char(10),@.d2,101) + ' 08:00'
ELSE @.d2 END
SET @.d1 = CASE WHEN DATEPART(weekday,@.d1+@.@.DATEFIRST)=1 THEN
convert(char(10),dateadd(day,1,@.d1),101) + ' 08:00'
WHEN DATEPART(weekday,@.d1+@.@.DATEFIRST)=7 THEN
convert(char(10),dateadd(day,2,@.d1),101) + ' 08:00'
ELSE @.d1 END
SET @.d2 = CASE WHEN DATEPART(weekday,@.d2+@.@.DATEFIRST)=1 THEN
convert(char(10),dateadd(day,-2,@.d2),101) + ' 17:00'
WHEN DATEPART(weekday,@.d2+@.@.DATEFIRST)=7 THEN
convert(char(10),dateadd(day,-1,@.d2),101) + ' 17:00'
ELSE @.d2 END
set @.d1a = dateadd(day,1,@.d1)
set @.d2a = dateadd(day,-1,@.d2)
SELECT @.minutes = CASE WHEN DATEDIFF(day, @.d1, @.d2) > 1 THEN
(DATEDIFF(day, @.d1a, @.d2a) + 1 - (2 * DATEDIFF(week,
@.d1, @.d2))) * 480
ELSE 0 END +
CASE WHEN convert(char(10),@.d1,101) =convert(char(10),@.d2,101)
THEN DATEDIFF(minute, @.d1, @.d2)
WHEN convert(char(10),@.d1,101) <
convert(char(10),@.d2,101)
THEN DATEDIFF(minute, @.d1, convert(char(10),@.d1,101) +
' 17:00')
ELSE 0 END +
CASE WHEN DATEDIFF(day, @.d1, @.d2) > 0 THEN
DATEDIFF(minute, convert(char(10),@.d2,101) + ' 08:00',
@.d2)
ELSE 0 END
END
RETURN(@.minutes)
END
go|||On Dec 19, 4:16 am, Per Lissel<per.lissel[at]outokumpu.com> wrote:
> I need a formula to calculate the time (let's say in minutes) between two dates/times.
> The problem is that I have to exclude the time between 06 PM and 06 AM and also exclude the time in the weekend (Saturday and Sunday).
> I will use this in a couple of reports made in Reporting Services. The main purpouse is to keep track of the time an incident is open in a help desk program and non working hours should not be counted.
> If anyone have an algoritm that could be modified for this and is willing to share this I would be very grateful.
> Many thanks!
> /Per Lissel
> Fromhttp://www.developmentnow.com/g/115_0_0_0_0_0/sql-server-reporting-se...
> Posted via DevelopmentNow.com Groupshttp://www.developmentnow.com
Just write a .NET Function and put it in the Code window. I did this
in a WindowsApplication Project then copied it over...
In a TextBox, I used:
=Code.ElapsedBetweenBusinessDates( #12/12/2007 7:00#, #12/17/2007
15:00# )
and it returned at Render time:
1.20:00:00
which is a TimeSpan object of 1 day, 20 hours; or 44 hours. According
to your rules, that would be 11 hours on the 12th, 12 hours on the
13th, 12 on the 14th, none on the 15th (weekend), none on the 16th,
and 9 hours on the 17th, for a total of 44 hours.
Function ElapsedBetweenBusinessDates(ByVal d1 As Date, ByVal d2 As
Date) As System.TimeSpan
Dim stDT As Date, enDT As Date
Dim curDT As Date, curDD As Date, elapsedDT As New
System.TimeSpan
Dim cutoffDT As Date
If d1 < d2 Then ' make sure the dates are ordered
stDT = d1
enDT = d2
Else
stDT = d2
enDT = d1
End If
curDT = stDT
While curDT < enDT
curDD = curDT.Date
If Weekday(curDD) = 7 Then ' Saturday
curDT = DateAdd("d", 1, curDD)
ElseIf Weekday(curDD) = 1 Then ' Sunday
curDT = DateAdd("d", 1, curDD)
ElseIf Hour(curDT) < 6 Then ' if before 6 AM, round up
curDT = curDD.AddHours(6)
ElseIf curDT >= curDD.AddHours(18) Then ' if after 6 PM,
jump to next day
curDT = curDD.AddDays(1)
Else ' curDT is a businessday between 6 AM and 6 PM
cutoffDT = curDD.AddHours(18)
If cutoffDT > enDT Then cutoffDT = enDT
elapsedDT = elapsedDT.Add(cutoffDT.Subtract(curDT))
curDT = cutoffDT
End If
End While
ElapsedBetweenBusinessDates = elapsedDT
End Function
Now, with all that being said, if you have to be pedantic about it,
you should have a database that tracks what days are weekends and
company holidays, and use a SQL approach to calculating the span. We
have to use that approach here for billing purposes.
-- Scott|||Here is another answer from a Hugo Kornelis:
>>>>>>>
Hi Bruce,
I've seen similar requests in the groups. My usual approach is to:
1) Calculate real number of minutes (or hours, seconds, or whatever time
span is requested) using DATEDIFF.
2) Calculate the number of days between the dates, multiply by the
amount of non-business time between close and open time, subtract.
3) Calculate the number of weeks between the dates, multiply by the
amount of EXTRA non-business time in weekends (remember that the nights
have already been catered for!), subtract.
In a single formula:
DATEDIFF(minute, StartDate, EndDate)
- DATEDIFF(day, StartDate, EndDate) * 720
- DATEDIFF(week, StartDate, EndDate) * 1440
(untested)
--
Hugo Kornelis, SQL Server MVP
>>>>>>>>>>
Bruce Loehle-Conger
MVP SQL Server Reporting Services
"Orne" <polysillycon@.yahoo.com> wrote in message
news:3583aa33-e51a-4c97-8526-fad60056aa61@.w56g2000hsf.googlegroups.com...
> On Dec 19, 4:16 am, Per Lissel<per.lissel[at]outokumpu.com> wrote:
>> I need a formula to calculate the time (let's say in minutes) between two
>> dates/times.
>> The problem is that I have to exclude the time between 06 PM and 06 AM
>> and also exclude the time in the weekend (Saturday and Sunday).
>> I will use this in a couple of reports made in Reporting Services. The
>> main purpouse is to keep track of the time an incident is open in a help
>> desk program and non working hours should not be counted.
>> If anyone have an algoritm that could be modified for this and is willing
>> to share this I would be very grateful.
>> Many thanks!
>> /Per Lissel
>> Fromhttp://www.developmentnow.com/g/115_0_0_0_0_0/sql-server-reporting-se...
>> Posted via DevelopmentNow.com Groupshttp://www.developmentnow.com
> Just write a .NET Function and put it in the Code window. I did this
> in a WindowsApplication Project then copied it over...
> In a TextBox, I used:
> =Code.ElapsedBetweenBusinessDates( #12/12/2007 7:00#, #12/17/2007
> 15:00# )
> and it returned at Render time:
> 1.20:00:00
> which is a TimeSpan object of 1 day, 20 hours; or 44 hours. According
> to your rules, that would be 11 hours on the 12th, 12 hours on the
> 13th, 12 on the 14th, none on the 15th (weekend), none on the 16th,
> and 9 hours on the 17th, for a total of 44 hours.
> Function ElapsedBetweenBusinessDates(ByVal d1 As Date, ByVal d2 As
> Date) As System.TimeSpan
> Dim stDT As Date, enDT As Date
> Dim curDT As Date, curDD As Date, elapsedDT As New
> System.TimeSpan
> Dim cutoffDT As Date
> If d1 < d2 Then ' make sure the dates are ordered
> stDT = d1
> enDT = d2
> Else
> stDT = d2
> enDT = d1
> End If
> curDT = stDT
> While curDT < enDT
> curDD = curDT.Date
> If Weekday(curDD) = 7 Then ' Saturday
> curDT = DateAdd("d", 1, curDD)
> ElseIf Weekday(curDD) = 1 Then ' Sunday
> curDT = DateAdd("d", 1, curDD)
> ElseIf Hour(curDT) < 6 Then ' if before 6 AM, round up
> curDT = curDD.AddHours(6)
> ElseIf curDT >= curDD.AddHours(18) Then ' if after 6 PM,
> jump to next day
> curDT = curDD.AddDays(1)
> Else ' curDT is a businessday between 6 AM and 6 PM
> cutoffDT = curDD.AddHours(18)
> If cutoffDT > enDT Then cutoffDT = enDT
> elapsedDT = elapsedDT.Add(cutoffDT.Subtract(curDT))
> curDT = cutoffDT
> End If
> End While
> ElapsedBetweenBusinessDates = elapsedDT
> End Function
>
> Now, with all that being said, if you have to be pedantic about it,
> you should have a database that tracks what days are weekends and
> company holidays, and use a SQL approach to calculating the span. We
> have to use that approach here for billing purposes.
> -- Scott
>

Monday, March 19, 2012

Calcualted formula

Please someone helpe me out writing a caculated memeber which calcautle, "sale shipe date" minus 14.

If you already know the "sale ship date", you can use .Lag( 14 ) on that member.

e.g.:

Code Snippet

[Sales ship date].CurrentMember.Lag( 14 )

Best regards

- Jens

Calcualted formula

Please someone helpe me out writing a caculated memeber which calcautle, "sale shipe date" minus 14.

If you already know the "sale ship date", you can use .Lag( 14 ) on that member.

e.g.:

Code Snippet

[Sales ship date].CurrentMember.Lag( 14 )

Best regards

- Jens

Calc % in crystal detail againt running total

Quick question.
I have a grand total being calculated in a formula, for each detail line I am trying to get a percentage of that number against a value in the detail. The problem is that the grand total is running and only calculates against the current running total not against the grand total when the report finishes. Any way around this?Hi!

1) Insert the Formula Field from Report Design Page

2) Named as Percentage and write the code to the formula field coding area

{table.numberfiled}/100

3) And Drag and Drop the Formula Filed on Details Section Where do u want place.