SSRS Display Totals by Group - reporting-services

I am trying to display the total for records that are grouped together.
I am using the code below under Report Properties -> Code
Dim public totalBalance as Integer
Dim public Cnt as Integer
Public Function AddTotal(ByVal balance AS Integer ) AS Integer
totalBalance = totalBalance + balance
Cnt=Cnt+1
return balance
End Function
Public Function GetTotal()
return totalBalance
End Function
[Expression A is =Code.AddTotal(sum(DateDiff(dateinterval.Second, Lookup(Fields!dg_InteractionId.Value, Fields!Id.Value, Fields!StartDate.Value, "DataSet1"), Lookup(Fields!dg_InteractionId.Value, Fields!Id.Value, Fields!EndDate.Value, "DataSet1")))/CountRows())
While Expression B is =Code.GetTotal()
AS you can see the total 1651 for the top group is correct but the total for the second group 2597 is incorrect as it includes the total of the first group together. The correct result desired is 946 for the second group total.
Kindly advise.

You can change your GetTotal() to reset the totalBalance variable when is called to display the group total
Public Function GetTotal()
Dim bal AS integer
bal = totalBalance
totalBalance = 0
return bal
End Function
Because of this change, you should also create a separate variable for the grand total which will be updated in the AddTotal() function, and create a GetGrandTotal() function which will return its value.
Also since you have declared your variables as public you can display them directly without functions like =Code.totalBalance

Related

Adding Multiple Datasets and total

Phased Budget
Pretty new to all this.
I'm building a report in SSRS with multiple datasets.
The report has 12 trading periods and an Annual Total.
I've added a total line which sums the budget for 2 datasets using an expression.
The total works fine for the Annual Total but not for any of the periods. It just shows the annual total in each month.
What am I doing wrong?
You can use custom code to store DataSet1 values to an array and then use the stored values to calculate net margin.
Add the following custom code to your report
Dim i1 As Integer
Dim i2 As Integer
Dim sumarray(12) As Decimal
Public Function setSum(ByVal v As Decimal) As Decimal
sumarray(i1) = v
i1 = i1 + 1
Return v
End Function
Public Function getSum() As Decimal
i2 = i2 + 1
Return sumarray(i2-1)
End Function
For your gross margin monthly(dataset1) total use the expression:
= Code.setSum( SUM(Fields!Bud.Value) )
For your net margin monthly total (dataset2) use the expression:
= SUM(Fields!Bud.Value) + Code.getSum()

How to assign Rank in SSRS report?

I have a column (impact/hr) which gets populated after some calculations. Now I would like to assign each row a rank. For example Maximum dollar amt gets the rank of 1. Any tips to write an expression would be appreciated. Thank you.
Just add some custom code in Report-Report Properties...-Code:
Dim Rank AS Integer
Function GetRank() AS Integer
Rank = Rank + 1
Return Rank
End Function
In your tablix's detail cell where you want the rank, use the following formula:
=Code.GetRank()
Each time it processes that cell, it adds one to the rank and displays the result.
It may be your secondary option !
You can arrange the Impact\Hr in ascending order and the show the Rank by using RowNumber function.
For More info see the RowNumber function.
Use the Rank function.
select [Impact per Hr], [My Rank] = Rank() over (order by [Impact per Hr] desc)
then use asc/desc depending on which value you want to be ranked high or low.
This is independent of any order by statement as well
Rank() is preferable to Row_Number() for applications like this because rows of the same value will have the same Rank, but will have a different Row_Number.
Step 1: Add the following code in code section of reports properties in Reports Builder.
Public dim previousValue as Integer = 0
Public dim i as Integer = 0
Public dim flag as Boolean = False
Public dim lastRank as Integer = 1
Public Function Rank(sourceValue as Integer ) As Integer
if previousValue = sourceValue
i = i + 1
flag = True
end if
if previousValue > sourceValue
lastRank = lastRank + 1
flag = False
end if
previousValue =sourceValue
if flag = True
return lastRank
else
lastRank = lastRank + i
i = 0
return lastRank
end if
End Function
Step 2: Call the above function from the cell where you want to display rank like this:
=code.Rank(SUM(Fields!SumTotal_Ending_Balance.Value))

Write this SQL query in LINQ to Entity format

I have this sql query need to convert to linq to entity
select kv.KeyID, kv.KeyVotID, v.VotID, v.FullName
from E_KeyVot kv
join E_Vot v
on kv.VotID = v.VotID
where kv.KeyID=2
order by v.FullName
This what I have tried but I'm sure it is not correct:
Public Function GetKeyVot() As IQueryable(Of Ems_KeyVot)
Try
Dim _objQuery As IQueryable(Of Ems_KeyVot, Ems_Vot) = _
From a In Context.Ems_KeyVot
Join b In Context.Ems_Vot On a.votid Equals b.votid
Where a.keyid = pub_KeyID
Order By b.FullName
Return _objQuery
Catch ex As Exception
End Try
End Function
Based on your query, the issue you have is that your return types don't agree with the query. The query returns an IQueryable(Of Ems_KeyVot, Ems_Vot), but your function is expecting an IQueryable(Of Ems_KeyVot).
In your query, you are projecting values from both tables. Since you can't return an anonymous type from your function, you need to create another class to return the results:
Public Class Votes
Public Property KeyID As Integer
Public Property KeyVotID As Integer
Public Property VotID As Integer
Public Property FullName As String
End Class
With that, you can modify your query as follows:
Dim _objQuery As IQueryable(Of Ems_KeyVot, Ems_Vot) = _
From a In Context.Ems_KeyVot
Join b In Context.Ems_Vot On a.votid Equals b.votid
Where a.keyid = pub_KeyID
Order By b.FullName
Select New Votes With { .KeyID = a.KeyID, .KeyVotID = a.KeyVotID, .VotID = b.VotID, .FullName = b.FullName }
Also change your function definition to
Public Function GetKeyVot() As IQueryable(Of Votes)
One other note, I see that you have a try catch block, but you aren't doing anything with the exception. If you can't handle an exception, remove the block and let it bubble up to something that can.

function return 0 value in Ms Access

I created a function in Ms Access and called it to the sub procedure in the form, but it returns 0.
This is the code in the function:
Public Function Sum(a, b) As Double
Dim total
total = a + b
End Function
The code in the sub procedure in the form is:
Private Sub cmdDisplay_Click()
Dim a As Double
Dim b As Double
a = Val(Text0)
b = Val(Text2)
MsgBox (Sum(a, b))
End Sub
it displays 0 in every time I tested the button which it should have been added a and b together. Please help
To return a value you must assign to the function name, which behaves just like a local variable typed to the functions return type;
Public Function Sum(a, b) As Double
Dim total
total = a + b
Sum = total '//sum is the function name and a variable of type double
End Function
or better (if you really need a sum function):
Public Function Sum(a as double, b as double) As Double
Sum = a + b
End Function

VBA data type error - bad syntax?

Hopefully someone can help me out with this. I have written a query in Access 2003 that combines a linked table "taxon_group_max_per_site" and a cross tab query "Distinct Species by group_Crosstab".
From the table I have the field "Taxonomic Group" and "Max", and from the cross tab the fields "Total_Of_Species_S".
The table and the cross tab are linked and the query works fine until I add in some VBA to give each Taxonomic group a score based on "Max" and "Total_Of_Species_S".
The code below brings up "Error 13: type mismatch"
Public Function Invert_Diversity_Score (Total_Of_Species_S As Integer) As Integer
If Total_Of_Species_S < Round("[Max]*0.5", 0) Then
Invert_Diversity_Score = 0
Else
If Total_Of_Species_S < Round("[Max] * 0.75", 0) Then
Invert_Diversity_Score = 1
Else
If Total_Of_Species_S < Round("[Max] * 0.875", 0) Then
Invert_Diversity_Score = 2
Else
Invert_Diversity_Score = 3
End If
End If
End If
End Function
The debugger shows that "[Max]*0.5" and the other multiplications do not produce a number it says "[Max] * 0.5"= "[Max] * 0.5", which I think is the source of the type mismatch. How do I get the field to multiple properly? It looks exactly like the format shown in the VBA help.
The round function is expecting a number as parameter, not a string! Assuming (max) is a number, you can then calculate:
Round([Max] * 0.75, 0)
But
Round("[Max] * 0.75", 0)
Will definitely not return anything viable
"[Max] * 0.875" is just a string, how is VBA supposed to know that you are referring to the column [Max] from one of your tables?
Shouldn't [Max] be passed into the function as a second integer parameter? Something like this:
Public Function Invert_Diversity_Score (Total_Of_Species_S As Integer,
MaxVal as Integer) As Integer
We need the code that shows how you are calling the function to really sort this out ...
For one, you should use the ElseIf keyword, there is no need to stack the Ifs here.
Second - what is "[Max]*0.5" supposed to mean? To VB, it is a string, which unsurprisingly you can't multiply with an integer.
Assuming Max is some kind of global constant:
Public Function Invert_Diversity_Score(Total_Of_Species_S As Integer) As Integer
If Total_Of_Species_S < Round(Max * 0.5, 0) Then
Invert_Diversity_Score = 0
ElseIf Total_Of_Species_S < Round(Max * 0.75, 0) Then
Invert_Diversity_Score = 1
ElseIf Total_Of_Species_S < Round(Max * 0.875, 0) Then
Invert_Diversity_Score = 2
Else
Invert_Diversity_Score = 3
End If
End Function
If it is not a constant, then you must pass it into the function as well:
Public Function Invert_Diversity_Score( _
Total_Of_Species_S As Integer, _
Max as Integer _
) As Integer