Print Page | Close Window

Formula to iterate through multiple columns

Printed From: Crystal Reports Book
Category: Crystal Reports 9 through 2020
Forum Name: Technical Questions
Forum Discription: Formulas, charting data, Crystal syntax, etc.
URL: http://www.crystalreportsbook.com/forum/forum_posts.asp?TID=10576
Printed Date: 01 May 2024 at 5:57pm


Topic: Formula to iterate through multiple columns
Posted By: Matt_123
Subject: Formula to iterate through multiple columns
Date Posted: 19 Jul 2010 at 12:38pm
Hi,

My issue is a little difficult to explain. I'm working in Crystal Reports XI and I have a situation where I need to basically read through fields across multiple columns for values and count each time the field for the column is null or not.

Here it is another way: A person can have no, one, or more 'options' selected. Each 'option' is its own column that either is null or contains data labeling it true. I need the report to list out each 'option' and count the total times that 'option' is true for a set of people.

In the original code I was given had a formula containing
'if {Table1.a} <> "" then "a"
else if {Table1.b} <> "" then "b"
else if {Table1.c} <> "" then "c"
...'
where the report was grouped on this formula, and distinct counts of unique ids were taken to show the number of times each person had an option selected.

The problem being it only counts the first column it comes across as true, so it misses data. I need it to continue through each possible column. I can't change the database and the most direct way (hard coding each formula) is extreamley time-consuming (100+ formulas). I've attempted creating loops but I'm still too unfamiliar with crystal and have not been successful.

Is there a way for crystal to do this? Any help would be fantastic.
-Matt



Replies:
Posted By: DBlank
Date Posted: 19 Jul 2010 at 2:35pm
can you post morte real samples


Posted By: Matt_123
Date Posted: 20 Jul 2010 at 7:46am
Thanks for ther reply,
Here is perhaps a better description:
 
I have multiple columns (that I simply represented as "{table1.a}, {table1.b}, and {table1.c}" above). I need the formula to iterate through each one and check if it is null or not. Then I need it to return a string that I designate (represented as "a, b, and c" above) which I can group on. Ideally, I need it to return the string even if it is null but crystal seems to have some quirks when it comes to that.

Once I've grouped on the formula I can run distinct counts (and percentages) on two unique IDs (this in particular scenario I have {client_id} and {visit_id} where clients can have multiple visits and I need to count how many clients that outcome is true for and how many visits that outcome is true for).

In short: I need to create a grouped list of multiple {table.columns} and then run distinct counts of ids on them like you would with one {table.column}.

The original code of
'if {table1.a} <> "" then "atext"
else if {table1.b} <> "" then "btext"
else if {table1.c} <> "" then "ctext"
...'
does basically what I need it to except it stops at the first IF statement that is true (and doesn't list the values that are not true). I think what I need is functionally a 'if - then, if - then, if- then..." list or something wacky.

Please let me know if this doesn't make any sense and specify what you would like to see. I might be going at the report the wrong way...
 
Thanks,
-Matt


Posted By: DBlank
Date Posted: 20 Jul 2010 at 8:05am
I think there is an easier way.
You can use Running Totals to conditionally include or exclude rows.
For your patients make a Running Total
Right click on RT and select NEW
name=PatientCount
Field to sumamrize= patientID
Type=DistinctCount
Evaluate=Use a formula
//Need to tell me the logic for include or exclude here//
Reset=Never
Place in report footer
 
Same thing for visits but change it to the visitID


Posted By: Matt_123
Date Posted: 20 Jul 2010 at 10:42am

I see what you mean, Running Totals might work.

Question, what kind of formula are you suggesting for the 'Evaluate' section?
 
I was thinking about doing a Reset on group (since I need to display counts for every outcome) but the issue I'm running into is that I don't have a single column to group by. Each outcome is its own column and I'd need to combine multiple columns into one group (which I think was the idea behind the original formula). And by that method I'd need some way to force crystal to go through each IF statement. However, that is probably the wrong way to approach it.
 
Thanks for the replies,
-Matt


Posted By: DBlank
Date Posted: 20 Jul 2010 at 10:46am
Can a row fall into more than one outcome?
If so don't group on it or it will screw up your numbers.
 
Can you give me a real example of soemthing that needs to be counted and some data rows and I can write a formula for a RT to count them.


Posted By: Matt_123
Date Posted: 20 Jul 2010 at 11:36am

Yes, a client can have more than one outcome.

Would something like this work?:
 
Client_ID, Visit_ID, OC_1, OC_2, OC_3, OC_4
01,              v01,    123,    null,    789,    null
01,              v02,    123,    null,    null,    221
02,              v03,    null,    null,    789,    221
03,              v04,    123,    456,    null,    221
 
*each outcome(OC) has a code associated to it.
 
Results would show as:
Type                  Clients    %   Visits   %
Outcome_1             2      66%     3    75%
Outcome_2             1      33%     1    25%
...
 
This is basically what I'm trying to accomplish, let me know if you need more information.
 
The actual report is even a bit more complex in that I need to set up a heirarchy based on other columns in this table - where say
Outcome_1 ....
         Outcome_2....
                  Outcome_3....
         Outcome_4....
But I'm not touching that yet.
 
Thanks!,
-Matt


Posted By: DBlank
Date Posted: 20 Jul 2010 at 11:42am

so you need to count the number of clinets that have at least one non null value in one of the OC fields.

in the RT use an evaluate formula of:
NOT (isnull(OC_1) and isnull(OC_2) and isnull(OC_3) and isnull(OC_4))
 
Is that the count you are after here?


Posted By: DBlank
Date Posted: 20 Jul 2010 at 11:43am
Note that RTs have to be placed after the last record evaluated to see teh total (don't use them in headers) so in this case it needs to be in the report footer.
You can place it on a details line and see the actual counting process...


Posted By: DBlank
Date Posted: 20 Jul 2010 at 11:46am
as an FYI Crystal is squirrely with NULLs.
It will stop evaluating on Nulls so that was part of the issue you originally had.
If you use Nulls in the formula you have to use them first. You can also flip the formula option (or report option) from 'using Nulls' to 'Use default values for Nulls' to help avoid that problem. That being said sometimes you still need to know the difference between Null, "" and not "" so it is nice to turn the feature on and off.


Posted By: Matt_123
Date Posted: 20 Jul 2010 at 12:16pm

Yeah the original code didn't account for nulls but they did have 'convert nulls to default' on which sometimes works.

In response to your earlier reply: unfortunately no, In your example it counts the total clients if any Outcomes are not null. I need to count the number of clients for each Outcome where that Outcome is not null for a client.
 
For example, if I was to hard code it I would manually write out
"Outcome_1" and place distinctcount of the formula for the client count
 
[Formula code: OC_1_ClientCount]
if not isnull({OC_1})
then {Client_ID}
else mailto:%7b@null - {@null }         *  
 
mailto:@null - '@null' is a blank formula placed to remove the +1 count.
 
And then do the same for OC_2, OC_3, etc. down the list.
 
Does that make sense?
 
Thanks for your help with this wacky issue,
-Matt
 
 


Posted By: DBlank
Date Posted: 20 Jul 2010 at 1:00pm
OK
just change the formula to patientOC1True
and the formula to
NOT isnull(OC_1)
 
then create another for OC_2 etc.



Print Page | Close Window