Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Anonymous
Not applicable

how to get Median expression in paginated report(Calculate a Median in SSRS)

Hi Team,

 

iam trying to get Median of TAT_hours in paginated expession 

usually iam trying =Median(Fields!TAT_HOURS.Value) but getting an

 

error

(The 'Value' expression for the textrun 'TAT_HOURS.Paragraphs[0].TextRuns[0]' contains an error: [BC30451] 'Median' is not declared. It may be inaccessible due to its protection level.)

 

kindly check and provide the right one

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Thanks for the reply from Sahir_Maharaj , please allow me to provide another insight:

Hi, @Anonymous 

Based on my testing results, lists are not supported within custom functions in report builds.

vlinyulumsft_0-1731309261285.png

Currently, a more effective solution I’ve found is to use SQL statements directly. For example:

 

The original data is as follows:

vlinyulumsft_1-1731309261287.png

You can modify the SQL statement in the following location:

 

vlinyulumsft_2-1731309296446.png

SELECT 
    *,
    PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY Table1.hour) OVER () AS Median_TAT_HOURS
FROM 
    Table1

 

vlinyulumsft_3-1731309323972.png

Here are the final results:

vlinyulumsft_4-1731309323973.png

Of course, if you have any new discoveries or questions, please feel free to get in touch with us.
 

Best Regards,

Leroy Lu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
 

 

View solution in original post

4 REPLIES 4
JohnDonnelly12
New Member

Try this (it worked for me)

 

This custom code can be added to the report:

Public Shared Function Median(ByVal items As Object()) As Decimal
    If items Is Nothing Then
        Return Nothing
    End If

    Dim counter As Integer = items.Length
    If counter = 0 Then
        Return 0
    End If

    System.Array.Sort(items)

    If counter Mod 2 = 1 Then
        Return items(CInt((counter / 2) - 0.5))
    Else
        Dim FirstIndex As Integer = counter \ 2
        Dim SecondIndex As Integer = FirstIndex - 1

        Dim FirstValue As Integer = items(FirstIndex)
        Dim SecondValue As Integer = items(SecondIndex)

        Return (FirstValue + SecondValue) / 2
    End If
End Function

Which can then be called by using the following =Code.Median(Lookupset(Fields!Contract.Value, Fields!Contract.Value, Fields!Contract.Value, "DS_CallData_LKP"))

 

In this example the dataset "DS_CallData_LKP" is powering the entire report, but is being referenced back again to get list of values to be sorted for the median. Using a lookupset() instead of the hidden rows/columns method that is seen a lot helps keep the report simple for editing later down the line.

Sahir_Maharaj
Super User
Super User

Hello @Anonymous,

 

Can you please try this approach to use custom VB.NET code in SSRS to calculate the median:

Public Function GetMedian(ByVal items As Object()) As Decimal
    If items.Length = 0 Then
        Return Nothing
    End If

    Dim sortedList As List(Of Decimal) = New List(Of Decimal)
    For Each item As Object In items
        If IsNumeric(item) Then
            sortedList.Add(Convert.ToDecimal(item))
        End If
    Next

    sortedList.Sort()

    Dim count As Integer = sortedList.Count
    If count Mod 2 = 0 Then
        ' Even number of items - return average of middle two
        Return (sortedList(count \ 2 - 1) + sortedList(count \ 2)) / 2
    Else
        ' Odd number of items - return middle item
        Return sortedList(count \ 2)
    End If
End Function

In the expression where you want to display the median:

=Code.GetMedian(Fields!TAT_HOURS.Value)

Hope this helps.


Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution? (Yes, its FREE!)
➤ Lets connect on LinkedIn: Join my network of 15K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning
Anonymous
Not applicable

Hi Sahir,

 

After trying above code getting an error like below 

 

There is an error on line 5 of custom code: [BC30002] Type 'List' is not defined.
----------------------------
The definition of the report '' is invalid.
----------------------------
An error occurred during local report processing.

 

 

Anonymous
Not applicable

Thanks for the reply from Sahir_Maharaj , please allow me to provide another insight:

Hi, @Anonymous 

Based on my testing results, lists are not supported within custom functions in report builds.

vlinyulumsft_0-1731309261285.png

Currently, a more effective solution I’ve found is to use SQL statements directly. For example:

 

The original data is as follows:

vlinyulumsft_1-1731309261287.png

You can modify the SQL statement in the following location:

 

vlinyulumsft_2-1731309296446.png

SELECT 
    *,
    PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY Table1.hour) OVER () AS Median_TAT_HOURS
FROM 
    Table1

 

vlinyulumsft_3-1731309323972.png

Here are the final results:

vlinyulumsft_4-1731309323973.png

Of course, if you have any new discoveries or questions, please feel free to get in touch with us.
 

Best Regards,

Leroy Lu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
 

 

Helpful resources

Announcements
July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.