Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSRS: How to count true rows

I have a report with a dataset that has a column with booleans. In the table footer I want to display x / y where x is how many rows that were true and y is how many rows there was total.

Currently I have this:

=Count(Fields!Transfered.Value).ToString() + " / " + CountRows().ToString()

But the first becomes same as the last part. I then tried this:

=Sum(Fields!Transfered.Value).ToString() + " / " + CountRows().ToString()

But that generates an error, which I guess I can understand. I thought that if I converted the booleans into numbers where true is 1 and false is 0, then it could work out nicely. But how can I do that? Or is it a smarter way to do this all together?

Update: Have now also tried

=Sum(CInt(Fields!Transfered.Value)).ToString() + " / " + CountRows().ToString()

And got a negative result... O.o

Also found a way that worked, which I posted as an answer. But I won't accept it as an answer yet incase someone has a better way to do this =)

like image 767
Svish Avatar asked May 18 '09 14:05

Svish


People also ask

How do I display the number of records in SSRS report?

Let me show you how I add the total number of rows in a SSRS table. I start by going to the Design layout tab. Next, select the table as shown in the above image. As I mentioned earlier, I want the text box to appear above the table in the left-hand corner.

Is there a COUNTIF Function in SQL?

The function Countifs can often be implemented with an and condition in the case expression. The function counta can be implemented with a case expression as well. For that, SQL makes a distinction between empty strings and the null value.

How do I sum a column in SSRS report?

To do so first, goto the Yearly Income Total Column (Education Level Total Row -> Yearly Income Column), and right-click on it will open the context menu. From the context, Please select the Add Total option as shown in the below screenshot. Let me open the Preview tab to check the Grand Totals at the details level.

What is running total in SSRS?

Running Total is the running aggregate of all non null numeric values. RunningValue function can be used in SSRS To create Running Total Column.


3 Answers

I can tell you why things went wrong...

  • Count(Fields!Transfered.Value) is simply the number of rows. aka CountRows()
  • Sum(Fields!Transfered.Value) is trying to aggregate "true" or "false" = error
  • Sum(CInt(Fields!Transfered.Value)) will sum -1 and 0 because VB.NET true = -1
  • Sum(IIF(Fields!Transfered.Value, 1, 0)) fixes the sign issue = your solution

To avoid the extra IIF, you could use negate the sum of all the -1s

= -Sum(CInt(Fields!Transfered.Value)).ToString() + " / " + CountRows().ToString()

In the end, either solution would be OK and both are equally kludgy

like image 91
gbn Avatar answered Nov 02 '22 23:11

gbn


Figured out a way to do it, although there is probably a better more clear and logical way...

=Sum(IIF(Fields!Transfered.Value, 1, 0)).ToString() + " / " + CountRows().ToString()
like image 36
Svish Avatar answered Nov 02 '22 21:11

Svish


I came across this today and at least verified that I wasn't the only one that got a negative sum value for bools out of SSRS. Thanks.

My solution was to use ABS for absolute value. I like that better than just negating the expression solely because it's easier to see visually where the '-' might be missed if you're not careful in reading the expression.

but it's essentially the exact same thing

like image 26
Vonvee Avatar answered Nov 02 '22 22:11

Vonvee