Access 2016 - Using an IIf statement with the Count function - ms-access

=Count([item]) & IIf([Count]>1," Items","Item)
I am trying to use this in a text box on a form in Access 2016 to create a count of rows with the text "Items" if there are more than 1 and Item if there is only 1. So it would result in either "3 Items" or "1 Item" The count function works, I get the right number of rows, but I always get "Items", even when there is only 1 item. Can anyone tell me what I am doing wrong? Can I use Count() with an IIf expression?
Many thanks, Scott

Count([item]) doesn't magically create a variable [Count] that you can use afterwards. You have to repeat the expression:
=Count([item]) & IIf(Count([item]) > 1, " Items", " Item")

Related

SSRS 2008 - two datasets in one expression

My report has two datasets, main one and a second dataset (which holds only one row). I need to count how many values of a certain column in the main dataset are below a value taken from the second dataset. The expression I use is:
=Count(IIf(Fields!TestValue.Value < First(Fields!NetUnitWeight.Value, "SecondDataSet"), 1, Nothing), "MainDataSet")
& " of " & CountRows("MainDataSet")
But I get the following error:
[rsAggregateofAggregate] The Value expression for the textrun 'Textbox1.Paragraphs[0].TextRuns[0]' contains an aggregate function (or RunningValue or RowNumber functions) in the argument to another aggregate function (or RunningValue). Aggregate functions cannot be nested inside other aggregate functions.
I tried to solve it by putting First(Fields!NetUnitWeight.Value, "SecondDataSet") in a report variable, but this didn't work. How can I solve it?
OK, I found a workaround. I added a new report parameter, right-clicked it to open its properties, then clicked "Default Values". I selected "Get values from a query" and chose the dataset and value field I needed. Now this expression works without errors:
=Count(IIf(Fields!TestValue.Value < Parameters!NetUnitWeight.Value, 1, Nothing), "MainDataSet")
& " of " & CountRows("MainDataSet")

Grouping Data in Report Builder and adding a calculated field

I have the following query output from SQL:
Query and Report Formats
I have managed to restrain my data for the first columns but I don't know how to show the values as in the example in the last column named "Return"
The logic behind the completion of the "Return" column is the following:
After I group the data by the unique combination Section_ID x Route_ID:
If I have a return on the Section_ID the value of the "Return" column should be "Yes", else if there are not return it should be "No".
How can I achieve the report output as in the picture?
Thanks.
You can use a LookupSet() and Join() functions to get all returns by Section and Route combination in a string. Once you have all returns you can use the InStr() function to check if there is at least one return and return Yes in that case, otherwise return No.
So I've used the following expression:
=IIF(
InStr(Join(LookupSet(Fields!SectionID.Value & "-" & Fields!RouteID.Value,
Fields!SectionID.Value & "-" & Fields!RouteID.Value,
Fields!Return.Value,"DataSet25"),","),"Yes")>0,"Yes","No"
)
Based on the data returned by your query I've recreated your example, this is the result.
Note there are two rows for Section 4 and Route 26 combination, the expression returns Yes because one of the rows has a return.
Let me know if this helps.

Conditional concatenation breaks if value is nothing

I'm trying to concatenate a string on the end of a sum, but if the sum is nothing, it breaks. It seems like this is due to SSRS evaluating both conditions of the IIf statement, but I can't figure out how to get around this.
I've got....
=IIf(IsNothing(Sum(Fields!Work.Value)), "", Sum(Fields!Work.Value).ToString + " J")
Which will print out the work summary + " J" if there is one, and #Error if not. What's the SSRS workaround?
Update / Clarification
The report in question is grouping on dates and then summing up Work, so it's not the case that Work is null, per se, but that for this particular date for this particular user, there are no rows in the group.. So, there are no rows to sum up in the error causing instance.
Sample Data Set
Name Date Work
Andy 12/1/15 511.30
Andy 12/1/15 549.70
Drew 12/2/15 484.80
Drew 12/2/15 322.36
Sample Report (current)
Name 12/1/15 12/2/15
Andy 1061 J #Error
Drew #Error 807.16 J
Sample Report (expected)
Name 12/1/15 12/2/15
Andy 1061 J
Drew 807.16 J
Have you considered doing the two parts of your desired output in two different expressions in the same Cell?
I assume for the current structure you have used a matrix, with Rows for the Name, and Columns for the date. You can the set the Data to be Sum of Work, as shown here, and in the red text in the image below.
=Sum(Fields!Work.Value)
You then then right click the cell and select "Create Placeholder" to insert a second expression in the same cell.
Set the value of this expression to be as shown here, and in the blue text below
=iif(Sum(Fields!Work.Value) > 0, " J", "")
Then when the report is run, it will always show the Sum if there is one, and if the value of the Sum is greater than zero, it will also display the J as required.
Hopefully this is the behaviour you require. Please let me know if you require further assistance with this solution
Try this:
=IIF(SUM(IIF(Isnothing(Fields!Total.Value),0,Fields!Total.Value)) = 0,
"",
SUM(IIF(Isnothing(Fields!Total.Value),0,Fields!Total.Value)).ToString + " J"
)
Let me know if this can help you
IIF does not short-circuit so this #Error is from SSRS trying to use the ToString function on NULLs.
The workaround is to add another IsNothing check in the false section before using ToString:
=IIF(IsNothing(Sum(Fields!Work.Value))
, ""
, IIF(IsNothing(Sum(Fields!Work.Value)), "", Sum(Fields!Work.Value)).ToString & " J")
To solve this exact problem, because if there is no Work to display then the report should display nothing, instead of using the IIf statement with concatenation, it is simple and sufficient to set a conditional visibility on the cell.
In the cell, use the expression:
Sum(Fields!Work.Value).ToString + " J"
Then for the same cell, select:
Text Box Properties > Visibility > Show or hide based on an expression
and enter:
=IsNothing(Sum(Fields!Work.Value))
While this solves this specific problem, if the solution requires the empty cell to display anything other than blank, then the original IIf short-circuit issue is still an issue.

SSRS IF Number of selected parameters Expression

I'm trying to display the parameters within the footer for a reference to the reader of the printed report.
=IIF(Count(Parameters!P_Faculty) >3, "%", "Faculty"&join(Parameters!P_Faculty.Label, ", "))
I wanted to do a count on the number of options of a parameter which are selected so I can limit the display to "%" if there are more than 3 Faculty Parameters selected.
I receive no run-time error instead the text box displays "#ERROR". I'm guessing my error is with the Count(Parameters!P_Faculty)>3
How could I simulate this effect?
Sorry, it turns out I was going about this the wrong way.
I did not know that Parameters have their own Count value:
Parameters!P_UIO_ID.Count
Full solution:
=IIF(Parameters!P_UIO_ID.Count > 3, "%", join(Parameters!P_UIO_ID.Label, ", "))

IIF Statement Not Returning a Value in SSRS

I'm very confused by this situation, i've written quite a few IIF statements and always get them to work. I have two columns in my dataset called CATEGORY and PCT, what i'm trying to do is return the PCT value for only one specific value in CATEGORY.
For example, I have the following table of data
Category PCT
A .50
B .75
I have placed a textbox on my report and have written the following expression to return the PCT value if Category = B, like so:
=IIF(Fields!Category.Value = "B", Fields!PCT.Value, " ")
For some reason this expression returns empty every single time. When I just put Fields!Category.Value in the textbox as a test then the value A returns which is as expected.
I'm really thrown-off by this and I know i'm missing something very simple - can someone help?
Its important that we understand the context of the textbox as the expression seems valid.
If you placed a single textbox on your report and used your above expression (with references to the datasets) ONLY the first row of your dataset will be evaluated. In which case the expression will always evaluate to the same result.
Can you confirm that the control you are using is indeed a textbox? If it is then i believe you do need a reference to the dataset and the expression will look more like this:
=iif(First(Fields!Category.Value, "datasetName") = "B", First(Fields!PCT.Value, "datasetName"), " ")
This would only evaluate the first row in your dataset.
If you were to do this in a tablix using your original expression then it should work.