SSRS 2012. Replicate grouping results in report in Query Designer - reporting-services
New to SSRS 2012 & struggling.
I have a report - containing a subreport - on client data. When testing with one client (just to make sure everything was OK) it works perfectly.
When the filter's removed, it's timing out (it's actually saying it's been cancelled by the user, which it hasn't).
Obviously the inclusion of the subreport is the problem (as the main report runs fine without it).
My subreport can validly return multiple records per client. The subreport is grouped on ClientId. I then have a calculated field - say HasIP - that concatenates two fields (IC and PC ) to produce either YesYes or YesNo. The tablix is sorted by HasIP by Z to A.
The info in the ClientID footer are what I'm displaying.
ClientID HasIP
22 YesNo - has two records, but since it has a YesYes, I don't want this one
22 YesYes - has two records, but I want the this one
52 YesYes - may only have 1 record, I want this record
76 YesNo - may only have 1 record, I want this record
Theoretically a client can't have more than one instance of HasIP=YesYes & shouldn't have more than 2 records. How can I recreate this effect in Query Designer? Essentially if HasIP=yesyes, use that record, otherwise use the other record
If I can do this, I can then drop the subreport completely, add this dataset to my main report & use Lookup to get the data.
However, the way our report scenario is set up, I'm not permitted to change the query [I have to click Query Designer button in Dataset Properties] & I have no experience in MDX.
SOLVED!!!:
OK - here's what I've come up with.
Assign numeric order to IC and PC values
I concatenated the IC & PC field values together and assigned values and added the ID of the records in the original subreport. I called this field HasIP
=Switch(Fields!IC.Value+Fields!PC.Value="YesYes", 1,
Fields!IC.Value+Fields!PC.Value="YesNo", 2,
Fields!IC.Value+Fields!PC.Value="NoNo", 3) & "-" & Fields!R_Name.Value
This left me with 2-xxxx, 1-yyyy etc.
I then added a filter to my second dataset:
=Left(Fields!HasIP.Value, 1)
to exclude values of "NoNo"
Concatenate HasIP and other fields in Dataset2 (to be used by LookupSet)
In addition also had to convert the text values into code for output (eg Yes = 1, No = 2, Unknown = 3 etc). I did this by creating a calculated field in Dataset2 (in the report, not the query) which included the HasIP. I delimited HasIP from the value by using a dollar sign.
=Fields!HasIP.Value & "$" &
SWITCH(
left(Fields!CoR.Value, 1)="C", 1,
Left(Fields!CoR.Value, 1)="N", 2,
True, ""
)
This left me with 2-xxxx$2, 1-yyyy$1 etc.
Sort the LookupSet results
LookupSet creates an array (and yes, now you know too). I needed to sort this so that the one I'm after is the first value returned. This is achieved by copying and pasting the following into the custom code section of your report. (I've added the code to allow you to sort in reverse if you need it).
Function JoinSorted(m_Array As Object()) As String()
System.Array.Sort(m_Array)
' Array.Reverse(m_Array) 'uncomment this line if you want to sort in reverse
Dim k As Integer = 0
For i As Integer = 0 To m_Array.Length - 1
If i > 0 AndAlso m_Array(i).Equals(m_Array(i - 1)) Then
Continue For
End If
m_Array(k) = m_Array(i)
k += 1
Next
Dim fred As [String]() = New [String](k - 1) {}
System.Array.Copy(m_Array, 0, fred, 0, k)
Return fred
End Function
You call this by using the following in your report expression:
Join(
Code.JoinSorted(
LookupSet(Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!YourField.Value,"Dataset2")
),""
)
So, regardless of how LookupSet wanted to return the array, I always got 1-yyyy$1 first.
Handling LookupSet output on the report - include default for no records
For some LookupSet results in the report, I needed return a default value if there were no records returned. Otherwise I needed to return the assigned value (1, 2, 3 etc). If found two ways to do this.
This checks to see if the array count [yes, by using length] is greater than zero. If so, then using a mid function on the array result to return the 14th character:
=SWITCH(
LookupSet(
Fields!Main_ID.Value, Fields!Main_ID.Value, Fields!Record_Name.Value, "Dataset2"
)
.Length=0,
2,
LookupSet(
Fields!Main_ID.Value, Fields!Main_ID.Value, Fields!Record_Name.Value, "Dataset2")
.Length>0,
1)
OR
This is the same:
=iif(
LookupSet(
Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!Main_ID.Value,"Dataset2"
).Length>0,
Mid(
Split(
Join(
Code.JoinSorted(
LookupSet(
Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!Main_ID.Value,"Dataset2")
)
,";")
,";").GetValue(0)
, 'start point of mid
InStr(
Join(
Code.JoinSorted(
LookupSet(
Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!Main_ID.Value,"Dataset2"
)
)
,";")
,"$")
+1,
1),
"2"
)
Handling LookupSet output on the report - using of MID when length of value varies
In some cases the value I needed to return varied in length, eg 2 or 10. I also needed to return nothing if the LookupSet didn't return any results. I couldn't use "normal" versions of MID like this:
Mid(
Join(Code.JoinSorted(
LookupSet(Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!Q12d.Value,"Dataset2")),
";"),
InStr(
Join(
Code.JoinSorted(
LookupSet(Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!Q12d.Value,"Dataset2")
),
";"),
"$")+1,
(
InStr(
Join(
Code.JoinSorted(
LookupSet(Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!Q12d.Value,"Dataset2")
),
";"),
";")
-
InStr(
Join(
Code.JoinSorted(
LookupSet(Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!Q12d.Value,"Dataset2")
),
";"),
"$")
- 1)
)
as it threw #ERROR due to the use of -1 (at the end) making the string a negative length for some records.
So, here is what I could use:
=
Mid(
Split(
Join(
Code.JoinSorted(
LookupSet(Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!Q12b.Value,"Dataset2")
),
";"),
";").GetValue(0)
,
InStr(
Join(
Code.JoinSorted(
LookupSet(
Fields!Main_ID.Value,Fields!Main_ID.Value,Fields!Q12b.Value,"Dataset2")
),
";"),
"$")
+1,
1)
By doing this, I could get the results I was after in the details & the footer.
I hope this can help someone else.
Related
Is there any way to get alphabetical numbering of rows in ssrs instead of numerical numbering?
What I need (alphabetical numbering of rows-highlighted in bold(serial column)): I have tried converting the output of rownumber function into string, But nothing seems to work as I don't have any idea. Please help!
You can do this with a bit of custom code. Go to the Report Properties, click the "Code" tab and paste the following code into the custom code window. Public Function GetRowLetter(RowNum As Integer) As String ' stick the RowNum in a variable that we can reduce until it's zero Dim r As Integer Dim i As Integer Dim s As String ' holds result s = "" r = RowNum ' we start at the right side so if the rownum is 28 we want to be back AB ' need to get 'B' first Do While RowNum > 0 r = Int((RowNum - 1) / 26) i = (RowNum - 1) Mod 26 s = Chr(i + 65) & s RowNum = r Loop GetRowLetter = s End Function This will give "A" for 1, "B" for 2 etc, then it will give "AA" for 27, "AB" or 28 etc... If you want to return lower case letters instead, swap the 65 for 98 In your report set the textbox value expression to =Code.GetRowLetter(RowNumber("myDataSetName")) swap out myDataSetName with the name of your dataset or scope you want to apply it to. Remember the dataset and scope names are case sensitive and must be surrounded by quotes ( " )
create a set expression for a cell in a table report
I have a report which creates a spreadsheet. one cell could have data from one of three fields depending on a fourth field status or field two being blank.. I was thinking of writing as CASE statement but that does not work in ssrs. The IIF statement works great for 2 values but how do you write for three values? In common language it would be "If secondary value ="Yes", use secondary name, If account field value is Null, use Contact field value, otherwise use account field value" Thanks
You can use nested IIF statements but they quickly get messy. You should use SWITCH which acts a bit like a case statement. A simple example would be = SWITCH( Fields!myFirstField.Value = <10 , "Small", Fields!myFirstField.Value = <30 , "Medium", Fields!myFirstField.Value = <80 , "Large", True , "HUGE", ) As switch stops at the first true expression there is not need to check ranges of values if you get them in the correct order. The final "True" expression just acts like an ELSE If you need to check multiple criteria per condition then you can do that too like this.. = SWITCH( Fields!myFirstField.Value = <10 AND Fields!mySecondField.Value = 1 , "Small Cat", Fields!myFirstField.Value = <10 AND Fields!mySecondField.Value = 2 , "Small Dog", Fields!myFirstField.Value = <30 , "Medium Animal", Fields!myFirstField.Value = <80 , "Large Animal", True , "HUGE Animal", )
compound data filters in SSRS
There must be a way in SSRS to have multiple conditionals for separate data filters? I have an input report level filter #reportparameter, and data item "Checknum" I need to do something resembling the following: if #reportparameter = "C" and Left(Fields!Checknum,2) = "NC", filter otherwise if #reportparameter = "E" and Left(Fields!Checknum,2) = "VR", then filter two separate conditionals, both compound statements. What does the SSRS Dataset look like, as far as syntax?
If you want this as part of the query, you could add it to the WHERE clause: WHERE (#reportparameter = 'C' and Left(Fields!Checknum,2) = 'NC') OR (#reportparameter = 'E' and Left(Fields!Checknum,2) = 'VR') But if you want to do it in the dataset filter, your filter Expression would be like =IIF((Parameters!reportparameter.Value= "C" AND LEFT(Fields!Checknum.Value, 2) = "NC") OR (Parameters!reportparameter.Value= "E" AND LEFT(Fields!Checknum.Value, 2) = "VR") , 1, 0) The type would be Integer and the Value would be 1.
SSRS - Expression returning error if null
I'm expecting only the names to appear on the calendar if they exist and nothing if they don't. I created an expression where it returns only the last name of a person by using the comma as a delimiter. My current expression: =iif(IsNothing(Fields!EmployeeName.Value), nothing, Left(Fields!EmployeeName.Value,-1 + InStr(Fields!EmployeeName.Value, ","))) Current results where #Error appears if a name doesn't exist:
The error is occuring because you are passing in a number less than 0 to the left function. When your string does not have a comma in it you are passing in -1. To handle this I added two if statements to the expression. The first will return the whole string if the index of the first comma is 0. The second checks for the -1 condition and passes a 0 to the left function when that occurs. = iif( IsNothing(Fields!EmployeeName.Value), nothing, iif( InStr(Fields!EmployeeName.Value, ",") = 0, Fields!EmployeeName.Value, Left(Fields!EmployeeName.Value,iif(-1 + InStr(Fields!EmployeeName.Value, ",") < 0, 0, -1 + InStr(Fields!EmployeeName.Value, ","))) ) )
Linq to Sql - Use sum in the where clause
I'm trying to select orders that have either over or under 2000 products ordered in them, depending on other values. I need to select the information from the Orders table, but check this value in the OrdersProducts table, specifically the sum of OrdersProducts.ProductQty. I also need to do this using predicate builder, because of other requirements. So far, I have this, but it isn't returning the results correctly. Its using nested Lambda expressions, which I didn't know I could do but I tried it and it works, but its not returning correct results. Dim getOrders = From d In db.Orders _ Where d.Status = OrderStatus.Approved _ Select d ' Then a for loop adding parameters via Predicatebuilder... If over2000 = True Then ' over 2000 predicate1 = predicate1.And(Function(d) (d.OrderProducts.Sum(Function(c) c.ProductQty > 2000))) Else ' under 2000 predicate1 = predicate1.And(Function(d) (d.OrderProducts.Sum(Function(c) c.ProductQty < 2000))) End If basePredicate = basePredicate.Or(predicate1) ' End For loop getOrders = getOrders.Where(basePredicate) I removed some code for brevity but I think that gets the point across. How can I do this?? Thanks!
Try changing this: (d.OrderProducts.Sum(Function(c) c.ProductQty > 2000)) to this: (d.OrderProducts.Sum(Function(c) c.ProductQty) > 2000) I haven't built this to test it, but it appears that it was currently trying to sum the results of a boolean comparison instead of summing the quantities and then comparing.