I want to find the second minimum number in my row.
I can complete Min and Max by the inbuilt expression.
More information about the SSRS, it is loaded from SSAS datasource.
In the SSRS design, I have followed Chris steps
1.) Put the custom code in to report properties
2.) Input the expression into two separate columns (setMinMaxReset and setMinMax):
=Code.setMinMaxReset(Fields!ID_AverageChangeRevenue_Value.Value)
=Code.setMinMax(Fields!ID_AverageChangeRevenue_Value.Value)
SSRS design
3.) The =code.min2 was also input into the column MIN2
same for MAX2
but it turn out the result is incorrect. Thank you for your help
SSRS Review
You can use custom code in your report to store min2 and max2 in variables (to avoid confusion with multiple get/set functions I declared variables as public)
Public Dim max1 As Integer
Public Dim max2 As Integer
Public Dim min1 As Integer
Public Dim min2 As Integer
Public Function setMinMax(ByVal v As Integer) As Integer
If max1=0 Then
max1 = v
ElseIf v>max1 Then
max2 = max1
max1 = v
ElseIf v<max1 And v>max2 Then
max2 = v
End If
If min1 = 0 Then
min1 = v
ElseIf v < min1 Then
min2 = min1
min1 = v
Elseif min2=0
min2 = v
ElseIf v<min2
min2 = v
End If
Return v
End Function
Public Function resetMinMax(ByVal s As String) As String
max1 = 0
max2 = 0
min1 = 0
min2 = 0
Return s
End Function
For each row group(green color) you will use resetMinMax passing the group string as a parameter. The function will initialize the values for each group row and display the group name
=Code.resetMinMax(Fields!r.Value)
For each value cell (blue color) you will use the setMinMax passing the value as parameter. The function will do the min/max calculations and display the parameter value
=Code.setMinMax(Sum(Fields!v.Value))
For min & max values just call each variable
= Code.max1
= Code.max2
= Code.min1
= Code.min2
Related
I have a Database with many columns, one of them containing Names.
My vb.net software acts as telegram server and waits for the user to send its full name.
The database could have its name spelled differently, for example "Marco Dell'Orso" could be spelled "Marco Dellorso" or "Marco Dell Orso" od "Dell Orso Marco" or whatever. The user could also misspell his name and invert two letters.. for esample "MaCRo Dell'Orso"
I would need a way to return the 5 rows that are the most similar to the words used in the query. What would be the best way? I was thinking of splitting the name on whitechars and then use LIKE in the query with the single words, but that does not work with mistyped words.
EDIT:
My current plan is to that if the database contains more than one or less then one rows with the exact name, then split the input into the single words and return all strings that contain ANY of the input words. this should reduce the rows to analyze from 42000 to a few hundred. Once I have these few hundred lines, i could run a Levenshtein function on the rows and return the 5 most matching..
Is this a good idea?
Solved it this way by combining my custom function with a premade Levenshtein function from this link: How to calculate distance similarity measure of given 2 strings? . I assign a score for each single word that appears in the other wordcomplex. then I add a score based on the Levenshtein comparison of each word to another. works great:
Public Class Form1
Private Sub TextBox1_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyUp
calc()
End Sub
Private Sub TextBox2_KeyUp(sender As Object, e As KeyEventArgs) Handles TextBox2.KeyUp
calc()
End Sub
Sub calc()
Label1.Text = compare(TextBox1.Text, TextBox2.Text)
End Sub
Public Function compare(source As String, target As String) As Integer
Dim score As Double
Dim sourcewords As String() = source.Split(New Char() {" "c, "'"c, "`"c, "´"c})
Dim targetwords As String() = target.Split(New Char() {" "c, "'"c, "`"c, "´"c})
For Each s In sourcewords
If target.Contains(s) Then score = score + 1
For Each t In targetwords
score = score + 1 / (DamerauLevenshteinDistance(s, t, 100) + 1)
Next
Next
For Each s In targetwords
If source.Contains(s) Then score = score + 1
For Each t In sourcewords
score = score + 1 / (DamerauLevenshteinDistance(s, t, 100) + 1)
Next
Next
Return score
End Function
''' <summary>
''' Computes the Damerau-Levenshtein Distance between two strings, represented as arrays of
''' integers, where each integer represents the code point of a character in the source string.
''' Includes an optional threshhold which can be used to indicate the maximum allowable distance.
''' </summary>
''' <param name="source">An array of the code points of the first string</param>
''' <param name="target">An array of the code points of the second string</param>
''' <param name="threshold">Maximum allowable distance</param>
''' <returns>Int.MaxValue if threshhold exceeded; otherwise the Damerau-Leveshteim distance between the strings</returns>
Public Shared Function DamerauLevenshteinDistance(source As String, target As String, threshold As Integer) As Integer
Dim length1 As Integer = source.Length
Dim length2 As Integer = target.Length
' Return trivial case - difference in string lengths exceeds threshhold
If Math.Abs(length1 - length2) > threshold Then
Return Integer.MaxValue
End If
' Ensure arrays [i] / length1 use shorter length
If length1 > length2 Then
Swap(target, source)
Swap(length1, length2)
End If
Dim maxi As Integer = length1
Dim maxj As Integer = length2
Dim dCurrent As Integer() = New Integer(maxi) {}
Dim dMinus1 As Integer() = New Integer(maxi) {}
Dim dMinus2 As Integer() = New Integer(maxi) {}
Dim dSwap As Integer()
For i As Integer = 0 To maxi
dCurrent(i) = i
Next
Dim jm1 As Integer = 0, im1 As Integer = 0, im2 As Integer = -1
For j As Integer = 1 To maxj
' Rotate
dSwap = dMinus2
dMinus2 = dMinus1
dMinus1 = dCurrent
dCurrent = dSwap
' Initialize
Dim minDistance As Integer = Integer.MaxValue
dCurrent(0) = j
im1 = 0
im2 = -1
For i As Integer = 1 To maxi
Dim cost As Integer = If(source(im1) = target(jm1), 0, 1)
Dim del As Integer = dCurrent(im1) + 1
Dim ins As Integer = dMinus1(i) + 1
Dim [sub] As Integer = dMinus1(im1) + cost
'Fastest execution for min value of 3 integers
Dim min As Integer = If((del > ins), (If(ins > [sub], [sub], ins)), (If(del > [sub], [sub], del)))
If i > 1 AndAlso j > 1 AndAlso source(im2) = target(jm1) AndAlso source(im1) = target(j - 2) Then
min = Math.Min(min, dMinus2(im2) + cost)
End If
dCurrent(i) = min
If min < minDistance Then
minDistance = min
End If
im1 += 1
im2 += 1
Next
jm1 += 1
If minDistance > threshold Then
Return Integer.MaxValue - 1
End If
Next
Dim result As Integer = dCurrent(maxi)
Return If((result > threshold), Integer.MaxValue, result)
End Function
Private Shared Sub Swap(Of T)(ByRef arg1 As T, ByRef arg2 As T)
Dim temp As T = arg1
arg1 = arg2
arg2 = temp
End Sub
End Class
One way is to use the build-in soundex function of MySQL.
SELECT SOUNDEX(name) FROM table;
Or, the better way, there are a few MySQL-functions on the web implementing DoubleMetaphone. I think this is what you are searching:
GitHub
I'm using Join(Lookupset) to find unique group values which returns a sequence number. This is my function:
Join(LookupSet(Fields!itemId.Value & Fields!UseByDate.Value & Fields!rackId.Value
, Fields!itemId.Value & Fields!UseByDate.Value & Fields!rackId.Value
, Fields!CustomerSeqNo.Value
, "PickingList"), ",")
The problem is on some items there are multiple transactions. I want to remove the duplicates.
I found a blog http://blogs.msdn.com/b/bobmeyers/archive/2012/06/18/creating-short-lists-using-the-lookupset-function.aspx but could not get SSRS Report Builder to reference Linq assembly. My issue is
How can I just show the unique values?
You don't need Linq, but you do still need custom code (in BIDS go to Report -> Report Properties -> Code)
You can put a RemoveDuplicates function in here, something like this:
Public Shared Function RemoveDuplicates(m_Array As Object()) As String()
System.Array.Sort(m_Array)
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 unique As [String]() = New [String](k - 1) {}
System.Array.Copy(m_Array, 0, unique, 0, k)
Return unique
End Function
To use it in your Join:
Join(Code.RemoveDuplicates(LookupSet(...)),",")
I agree with #user3697615 that Report Code is best. However, I prefer to build it straight into a string:
public shared function JoinDistinct(
dups as object(),
delimiter as string
) as string
dim result as string = ""
system.array.sort(dups)
for i as integer = 0 to dups.length - 1
if i <> 0 then result += delimiter
if i = 0 orElse dups(i) <> dups(i-1) then result += dups(i)
next i
return result
end function
This way, we eliminate one nested function on the call:
=Code.JoinDistinct(LookupSet(...), ",")
If you're like me, you also want the elements in order based on frequency (descending order).
I created the following VisualBasic code to do so
Public Shared Function RemoveDuplicates(dataset As Object()) As String()
Dim unique As New System.Collections.Generic.List(Of String)
Dim frequency As New System.Collections.Generic.List(Of Integer)
For i As Integer = 0 To dataset.Length - 1
Dim index As Integer = -1
For j As Integer = 0 To unique.Count - 1
If dataset(i).Equals(unique(j)) Then
index = j
Exit For
End If
Next
If index < 0 Then
unique.Add(dataset(i))
frequency.Add(1)
Else
frequency(index) += 1
End If
Next
Dim uniqueArray As [String]() = unique.ToArray()
Array.Sort(frequency.ToArray(), uniqueArray)
Array.Reverse(uniqueArray)
return uniqueArray
End Function
This is based off others' answers where the SSRS expression is the following
Join(Code.RemoveDuplicates(LookupSet(...)),",")
Note: I learned VisualBasic in about an hour to solve this problem, so my algorithm probably isn't the most efficient.
I liked pwilcox's idea, so I wrote this one which filters out null and blank values.
Public Function JoinDistinct(arr As Object(), delimiter As String) As String
System.Array.Sort(arr)
Dim result As String = String.Empty
Dim lastvalue As String = String.Empty
For i As Integer = 0 To arr.Length - 1
If Not arr(i) Is Nothing And arr(i) <> lastvalue And arr(i) <> String.Empty Then
If result = String.Empty Then
result = arr(i)
Else
result = result + delimiter + arr(i)
End If
End If
lastvalue = arr(i)
Next
Return result
End Function
Usage:
=Code.JoinDistinct(LookupSet(...), ",")
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))
i have report that generates number of policies from 2 datasets.
the table is linked to dataset1.
i used the formula
CountDistinct(Fields!Policy_ID.Value)
my problem is how to get distinct count for the field Policy_ID from the second dataset "AccountingV10Dataset", specially that i have grouping Contract_Start_Month, Contract_Cover_Type, and Primary_LOB.
below a screen shot to the report design to help you to understand my request
the report should be generated as below:
I would derive the Distinct Count back in the Dataset layer e.g. in SQL. The SSRS Aggregate Functions only go so far.
I managed to solved this way:
i created the below code to calculate disting count from another dataset:
Dim suma As Decimal = New Decimal()
Public Function SumLookup(ByVal items As Object(),ByVal CountSum As String) As Decimal
If items Is Nothing Then
Return Nothing
End If
Dim ct as Integer = New Integer()
Dim PolId as Integer = New Integer()
suma = 0
ct = 0
PolId = 0
For Each item As Object In items
suma += Convert.ToDecimal(item)
If (CountSum = "SUM") then
ct += 1
Else If (PolID <> Convert.ToInt32(item)) then
ct += 1
END If
PolId = Convert.ToInt32(item)
Next
If (ct = 0) Then return 0 else If(CountSum = "SUM") then return suma else return ct
End Function
Public Function GetMyVal() as Decimal
GetMyVal = suma
End Function
and i call this function by:
=code.SumLookup(Lookupset(Fields!Contract_Start_Month.Value & "-" & Fields!Contract_Cover_Type.Value & "-" & Fields!Primary_LOB.Value,Fields!Contract_Year_Start_Month.Value & "-" & Fields!Cover_Type.Value & "-" & Fields!LOB.Value,Fields!Layer_ID.Value, "AccountingV10Dataset"),"COUNT")
Hope my solution will helpfull for other users as it did for me.
Regards,
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