Grouping Data in Report Builder and adding a calculated field - reporting-services

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.

Related

Comparing multiple fields in two datasets to return a 3rd value

I am in report builder and I have my primary dataset that is from a SQL database, I also then created a second dataset (enter data). I need to compare 2 fields from each dataset to retrieve the correct value from the 2nd dataset and populate a column on my report. I have tried the IIF statements and Lookup statements but I keep getting the error "report item expressions can only refer to fields within the current dataset".
I have a attached a screenshot of what I am trying to do....
The IIF statement I tried to use.. If Acctnum and prodid = each other return IncodeNumber
=IIF((Fields!AcctNum.Value=Fields!AcctNum.Value, "IncodeAccount") AND
(Fields!ProdId.Value =Fields!ProdId.Value, "IncodeAccount")),(Fields!IncodeNumber.Value, "IncodeAccount"),"True")
See code in my problem.
You need to use LOOKUP(). The problem with LOOKUP() is that is can only compare a single value from each dataset. However, we can easily get around this issue by concatenating the two values you need to compare.
Note: This assumes the expression will be in a tablix that is bound to your first dataset and that IncodeAccount is your second dataset - the values you want to lookup. If this is not the case just adjust the expression accordingly
So for you, you probably need to do something like this..
=LOOKUP(
Fields!AcctNum.Value & "||" & Fields!ProdId.Value,
Fields!AcctNum.Value & "||" & Fields!ProdId.Value,
Fields!IncodeNumber.Value,
"IncodeAccount"
)
I've used two pipe symbols to join the values to avoid incorrect matches being found. e.g. Account 123 and product ID 4567 would incorrectly match to Account 1234 and product ID 567 as they would both be 1234567 when joined. By using the || the match would be 123||4567 and 1234||567 respectively.
You may need to convert the values to string using CStr()
Alternative approach
If you are going to do this 'join' multiple times in the same dataset then you could add a calculated column to the dataset that concatenates the two columns. Then you can use this single field in the lookup which will make things a little simpler.
Or, you could do this concatenation in a database view which would make things even easier.

IIF query expression using Like "*" for the True condition

I have a database which tracks employee QA. I'd like to be able to search by a single Staff Member, a whole team, or a Unit. I have three controls that correspond to those fields and only one can ever have a value at once. In my quesry I'd like to have threee expressions that will limit my results by one of those three fields. I'm adding just one to start and I've hit a problem.
I found this https://www.acuitytraining.co.uk/microsoft-training-courses/access/if-statements/ which seems to do what I want. Here is the code I'm trying.
IIf(IsNull([Forms]![MainMenu]![btnManagersMenu].[Form]![cmbStaffSelect]),
[UserLogin] Like "*",[UserLogin]=[Forms]![MainMenu]![btnManagersMenu].
[Form]![cmbStaffSelect])
Which works fine if the control has a value. (condition is false) If the dropdown has no value (condition is true) I get zero results. I suspect the problem lies with the Like "*" on my UserLogin field. Here is my query wizard and the buildler wizard for the IIF expression
Can anyone see why I'm not getting any results for the dropdown control being empty. To my thinking this should give me an unfiltered list of results. I have double checked my data and there are 137 records that should appear if I'm not limited by the staff selection.
The short version of this is if cmbStaffSelect has a value I want my records limited by that value. If cmbStaffSelect is blank I want to get all records.
Keep in mind that the iif function will always evaluate both the then and else arguments, before returning the appropriate value depending on the value returned when evaluating the supplied test expression.
As such, if either the then or else arguments have the potential to error when evaluated (regardless of the result of the evaluation of the test expression), then the iif expression has the potential to error.
As an alternative, you could use the Nz function to achieve the same result:
[UserLogin] LIKE Nz([Forms]![MainMenu]![btnManagersMenu].[Form]![cmbStaffSelect],"*")
Perhaps your IsNull([Forms]![MainMenu]![btnManagersMenu].[Form]![cmbStaffSelect]) is always returning false because cmbStaffSelect might be equal to empty string?
Try something like this:
IIf(Trim([Forms]![MainMenu]![btnManagersMenu].[Form]![cmbStaffSelect] & "") = "",
[UserLogin] Like "*",[UserLogin]=[Forms]![MainMenu]![btnManagersMenu].
[Form]![cmbStaffSelect])
This checks to see if the cmbStaffSelect is "" ... if cmbStaffSelect is null - it converts it to "" by appending an "" to the null value.
I believe your hunch is exactly correct. If you want your query result to return the * symbol for the UserLogin field; then alter your IIF statement to be: [UserLogin] = "*"

lookup in ssrs and return a value

I have a table with three columns, in column 1 I have a name and column 2 I have a quantity.
I want to look up column 1 and return the value in column 2.
I used to the expression below and it wouldn't return what I wanted.
=Lookup(fields!NAME.Value, "Paul" ,1 , 0)
Could anybody tell me what expression I need to use?
You are close, sort of. To use the Lookup function properly, you need to change some things. Here is an example using the Lookup function.
=Lookup(Fields!Field1.Value, Fields!Field1.Value, Fields!Field2.Value, "DatasetB")
It takes 4 parameters. The first is the field/value from the current (in scope) data set that you want to be the value to match in the lookup data set. The second is the field in the lookup data set to match on. The third in the field to return when a match is found, and the last parameter is the name of the lookup data set.
Based on the expression in your question, it may actually work like this:
=Lookup("Paul" , Fields!NAME.Value, Fields!QUANTITY.Value , "DataSet2")
Of course, hard coding the name in the first parameter is probably not what you want to do.

How do I pass a value returned from one dataset as parameters to a second dataset to return a value from the second dataset in a cell expression?

How do I pass two dynamic values returned from one dataset as parameters to another dataset to return one row of a second dataset in a cell?
For each cell in a header row I need to determine A) Which column to get based off the meal period, and then B) take that value and pass it and another parameter to a query to get one of the columns (I'll take first) from the query.
I am already using cascading parameters, but I'm not sure if or how to use them in this scenario.
Example:
#SalesReceipt
#MealPeriod (A cascaded parameter. There should be one meal period per receipt)
Expression in an example cell:
=IIF(Parameters!#MealPeriod.value = "Lunch", Fields!Burger_Lunch_Type.value, Fields!Burger_Dinner_Type.value)
That returns either the lunch or dinner Type from a fact table, depending on meal period.
Given the Type and Item ID I need to get a value from another query or dataset and add that text to the cell expression. So the expression would look something like:
=IIF(Parameters!#MealPeriod.value = "Lunch", Fields!Burger_Lunch_Type.value, Fields!Burger_Dinner_Type.value) & " and " & (select top row from dataset given the correct type field and Item ID)
The cell should look like: Table1.Type and Table2.Breadtype AKA Bacon Burger and Whole Wheat.
Does that make sense?
I would use the dreaded Lookup function for this:
http://technet.microsoft.com/en-us/library/ee210531.aspx
I say dreaded because it is notoriously tricky to code and debug. I've even seen a case where a failed Lookup caused the entire row to disappear!
The Lookup target dataset itself should not use the parameters required for the Lookup - it's purpose is to return all the possible rows needed by the Lookup function.
If you need to use two columns as the key to your lookup, you can concatenate them e.g.
Fields!Burger_Lunch_Type.value & "|" & Fields!ItemID.value
Good luck!

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.