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))
Related
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 ( " )
How can I use MS Access Expression to count unique values in multiple columns as shown below?
I used Countif in Excel to get the "Yes" counts in the status column and now I want to use MS Access expression to get the same results.
use the function to make a row aggregate.
chek this out
Public Function count_sum(col1 As String, col2 As String, col3 As String) As Integer
Dim count_yes As Integer
count_yes = 0
If (col1 = "YES") Then
count_yes = count_yes + 1
End If
If (col2 = "YES") Then
count_yes = count_yes + 1
End If
If (col3 = "YES") Then
count_yes = count_yes + 1
End If
count_sum = count_yes
End Function
call this function using the following query
SELECT col1,col2,col3, count_sum([col1],[col2],[col3]) as Status
FROM Table1;
you can also use this fuction in contionous form.
In status textbox add control source like this OR directly use the above query and select the control source as status.
=Nz(count_sum([col1];[col2];[col3]);0)
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
I have a field called 'Specimen' that has entries formatted like 'CM-Z-01', 'TOR-XY-03', etc. I want to populate 2 additional fields in the same table, called 'TestType' and 'Axis', with the first two sections of the 'Specimen' entry, respectively. So 'TestType' would have 'CM' and 'TOR'; Axis would have 'Z' and 'XY'
I can do the first one just fine, with
UPDATE MechanicalData
SET MechanicalData.TestType = Left(Specimen,InStr(Specimen,"-")-1);
Is there an easy way to grab the middle portion?
You can use the Split function which returns an array with the string parts:
firstPart = Split("TOR-XY-03", "-")(0)
secondPart = Split("TOR-XY-03", "-")(1)
Make a function that you can call in your query based on this function. You cannot use Split directly as it is not supported in SQL.
Public Function SpecimenPart(ByVal s As Variant, ByVal partNo As Long) As Variant
If Nz(s) <> "" Then
Dim parts As Variant
parts = Split(s, "-")
If partNo - 1 <= UBound(parts) Then
SpecimenPart = parts(partNo - 1)
End If
End If
End Function
The parameter partNo is one-based (1 for first part).
UPDATE MechanicalData SET
TestType = SpecimenPart(Specimen, 1),
Axis = SpecimenPart(Specimen, 2);
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