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

Friday, February 24, 2012

Calculations Question

Hello,

Is there a way to make a Calculated Member aggregatable? I want to create a Calculated Member that performs a certain formula at the leaf level only. On the upper levels of the hierarchy I want the value aggregated, not the formula re-applied. Is there a way to achieve this?

Another question, is there a way to apply a special formula for a measure? and what is the "MeasureExpression" Property of a measure to be used for?

Thanks

For the first part:

You could create real measure that has all values as NULL. This will not take any space in database. Then you assign calculations in your calculated area and this measure will behave like real measure. This is based on Mosha Pasumansky suggestion here:

http://sqljunkies.com/WebLog/mosha/archive/2006/11/16/sqlpass_udm_presentation.aspx

Quote:

6. It isn't a problem that calculated measures don't aggregate up. If the designer need to add calculated measure which has aggregation semantics of a real measure, than there is very simple way to do it. Add a real measure to the measure group, and specify NULL as its column binding. It will cost nothing during processing, the partition size is going to be exactly the same, because AS will easily detect that measure value is constant and will compress it to 0 bits. But then in MDX Script, when expressions are assigned to that measure - they will aggregate up using measure's aggregation function - and it should work very efficiently too.

Some info about "MeasureExpression" property can be found in this thread: http://forums.microsoft.com/TechNet/ShowPost.aspx?PostID=768757&SiteID=17

Vidas Matelis

Calculations among Subreports

I have three subreports on my main reports.what I need is that on third subreport i have a formula thats subtracting two values from two subreports beside it.Values are defined in shared variable on subreports.Formula on third subreport is accessing those shared
variables and doing calculation.Problem is:

Formula is displaying result only for the first record on subreports ,whereas i want the result of calculation for all the corresponding records.

(First 2 subreports are linked with main report)check in formula functions have you added
WhilePrintingRecords to the start of your formulas.|||I have whileprintingrecods statment in mu formula.formula is :

whileprintingrecords;

shared numbervar firstnumber;
shared numbervar secondnumber;

firstnumber - secondnumber|||Are the variables also defined in the main report?
Do you have WhilePrintingRecords in all formulas using these variables?
And what do you mean by 'from two subreports beside it'. Where are the subreports in the design?|||Here's what i am doing:

1) three subreports on main report.

2) third subreport is using shared variables from two subreport

3) before describing the shared variables in 1st two subreports i am using

whileprintingrecords;
shared numbervar firstnumber := command.hours
(same in 2nd subreport)

4) Formula in 3rd subreport:

whileprintingrecords;
shared numbervar firstnumber;
shared numbervar secondnumber;

secondnumber - firstnumber

5) When report is printing ,its displaying result only for first record.|||Where are the subreports in the main report's design?
What do you mean by "its displaying result only for first record"? The first record in the main report? The first record per subreport? How many records can the subreports have? Can the first two subreports have no records, meaning the variables won't be (re)set.
So many questions...|||Formula is displaying result only for the first record on subreports ,whereas i want the result of calculation for all the corresponding records.

firstly create a formula on main report
called for sample :

init_var

shared numbervar firstnumber:=0;
shared numbervar secondnumber:=0;


it formula put in header of main report.

now on sub report, create formulla
called for sample :

/************************************************/
calculate_values_SR1

shared numbervar firstnumber;
firstnumber:=myfield_value + firstnumber;

it formula put in detail of first sub report.(I assume your data are there.)
in each detail the formula does work again

/************************************************/
calculate_values_SR2

shared numbervar secondnumber;
secondnumber:=myfield_value + secondnumber;

it formula put in detail of second sub report.
/************************************************/
now on yur third sub report

shared numbervar firstnumber;
shared numbervar secondnumber;
secondnumber - firstnumber


I hope it gives a idea.|||Reply to JaganEllis questions:

1) The first record per subreport?

Yes, i need to print resluts of all records on 1st 2 subreport on 3rd subreport correspondingly.
5 10 15
9 11 20

2) How many records can the subreports have?
depends on records in table

3) Can the first two subreports have no records, meaning the variables won't be (re)set.

it is possible.
Subreports are in detail section of Main report.

Reply to Hensa22
--------

Your solution is not working on dummy data (my own test report) but seemed to work on actual report but as table has not enough values so not able to tell how reliable it is.

Sunday, February 19, 2012

Calculation gives different result on dev. machine and client machine

I have a report that takes in seconds and needs to calculate Hours:Minutes:Seconds. So I created a formula (called @.Duration, as follows). It works fine on my development machine, but when I deploy it to the Client machine, the Duration always comes up as 00:00:00 no matter how many seconds were passed to it. At first, I was thinking it could be due to an old dll on the Client machine, but I don't use anything aside from your basic mathematical Functions (+ - * / \) and basic String Functions (Right, CStr).

Does anyone have any idea what I can do to fix it?

//Code for Formula @.Duration

NumberVar nTimeLeft := {CallDuration};

NumberVar nHours := nTimeLeft \ 3600;
nTimeLeft := nTimeLeft - (nHours * 3600);

NumberVar nMinutes:= nTimeLeft \ 60;
nTimeLeft := nTimeLeft - (nMinutes * 60);

NumberVar nSeconds:= nTimeLeft;

//Sanity Check
IF (nHours * 3600) + (nMinutes * 60) + (nSeconds) = {CallDuration} THEN
Right ('0' + CStr(nHours), 2) + ':' + Right ('0' + CStr(nMinutes), 2) + ':' + Right ('0' + CStr(nSeconds), 2)
ELSE
CStr(nHours) + ':' + CStr(nMinutes) + ':' + CStr(nSeconds) + ' InvalidTime!';Found out why it's doing this, but need help fixing it...

On my machine, I have the default Number Format set up as ####. It seems that on the Client's machine (all of them), the default is #,###.00. As a result, my numbers would be displayed as follows:

1.00:2.00:3.00 for 1 hour, 2 minutes, and 3 seconds

when it should be displayed like this:

01:02:03

I have the code adding a leading zero, then taking the Right 2 characters to get it to display as 01 instead of 1. Since Crystal Formats the Numbers as 1.00, the Right function is capturing the 00 at the end and displaying like this:

00:00:00

Since the output is a string and not a number, there is no option (that I can see) for formatting the number before it's displayed.

Does anyone have any ideas on how I can get it to display properly?|||Problem solved...

I changed CStr(nHours) to ToText(nHours,0) and it worked. (The ',0' is the number of decimal places.)

Tuesday, February 14, 2012

calculating percents in a group header

I am attempting to calculate a percent value in a group header of a report.
My formula looks somethig like:
Iif(sum(Fields.empgolamt.Value,â'table1_Group2â') <> 0,
sum(Fields!empamt.Value,â'table1_Group2â') /
sum(Fields!empgolamt,â'table_Group2â'), 0)
but this results in a divide by zero error. Is there some way to perform
this calculation in a group header or footer (it gives the same error in the
footer).I noticed in your code you are using different datasets to check for
zero and for doing the actual computation. You check the zero condition
in table1_Group2 and then you the denominator comes from table_Group
(without the "1" suffixing "table"). This might be your problem.|||This is just a typo (can't copy and paste from the the expressions window).
The data set referenced in the expression in the report is consistent - there
is only one table in the report.
"Bob" wrote:
> I noticed in your code you are using different datasets to check for
> zero and for doing the actual computation. You check the zero condition
> in table1_Group2 and then you the denominator comes from table_Group
> (without the "1" suffixing "table"). This might be your problem.
>|||There have been many related threads about "division by zero" in this
newsgroup. Please check e.g.:
*
http://msdn.microsoft.com/newsgroups/default.aspx?dg=microsoft.public.sqlserver.reportingsvcs&mid=3dc7d0e0-d962-47ab-a832-6127ffa45a37&sloc=en-us
*
http://msdn.microsoft.com/newsgroups/default.aspx?dg=microsoft.public.sqlserver.reportingsvcs&mid=c688a5d9-684a-4d54-970a-56a0cb5e7a8d&sloc=en-us
-- Robert
This posting is provided "AS IS" with no warranties, and confers no rights.
"reportwriter" <reportwriter@.discussions.microsoft.com> wrote in message
news:C7D4367C-F704-4ACC-95D5-5922DCDEFD1E@.microsoft.com...
>I am attempting to calculate a percent value in a group header of a report.
> My formula looks somethig like:
> Iif(sum(Fields.empgolamt.Value,"table1_Group2") <> 0,
> sum(Fields!empamt.Value,"table1_Group2") /
> sum(Fields!empgolamt,"table_Group2"), 0)
> but this results in a divide by zero error. Is there some way to perform
> this calculation in a group header or footer (it gives the same error in
> the
> footer).
>

Calculating Luhn Digit in Crystal Reports

I have an 18 digit number and i need a SQL formula in Crystal Reports to calculate the Luhn digit.

Below is how Luhn is calculated.

Step 1: Double the value of alternate digits beginning with the first right hand digit.

e.g 8 6 3 7 5 = 8 12 3 14 5

Step 2: Add the individual digits compromising the products obtained in Step 1 to each of the unaffected digits in the original number.

e.g 8 12 3 14 5 = 8+1+2+3+1+4+5 (total=24)

Step 3: Subtract the total obtained in Step 2 from the next higher number ending in 0.

e.g 24 = 30-24 = 6 (Luhn = 6!)

Any help here would be fantastic!!This should do it...

Local StringVar sValue := {test.NUMBER};

Local NumberVar nSubtotal;
Local NumberVar i;

For i := (Length(sValue) - 1) To 1 Step -2 Do
(nSubtotal := nSubtotal + CDbl(sValue[i]));

For i := Length(sValue) To 1 Step -2 Do
nSubtotal := nSubTotal + (If CDbl(sValue[i]) < 5
Then CDbl(sValue[i]) * 2
Else
CDbl(sValue[i]) - (9 - CDbl(sValue[i])));

nSubtotal Mod 10;

Sunday, February 12, 2012

Calculating Age based on Document Date in months

Hi, I'm trying to write a formula that will calculate a patient's age based on the visit date and then converting it into months. Anyone know how to do that?
The variables would be:
{DOCUMENT.CLINICALDATE}
{PERSON.DATEOFBIRTH}The next will calculate age in years
({DOCUMENT.CLINICALDATE} - {PERSON.DATEOFBIRTH}) / 365

The next will calculate age in months
({DOCUMENT.CLINICALDATE} - {PERSON.DATEOFBIRTH}) / 365 * 12