<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: how to get Median expression in paginated report(Calculate a Median in SSRS) in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/how-to-get-Median-expression-in-paginated-report-Calculate-a/m-p/4277740#M169733</link>
    <description>&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;"&gt;Thanks for the reply from Sahir_Maharaj&amp;nbsp;, please allow me to provide another insight:&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;"&gt;Hi,&amp;nbsp;Anonymous&lt;/LI-USER&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;" lang="zh-CN"&gt;Based on my testing results, lists are not supported within custom functions in report builds.&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;" lang="zh-CN"&gt;Currently, a more effective solution I’ve found is to use SQL statements directly. For example:&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Calibri; font-size: 11.0pt;" lang="zh-CN"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;" lang="zh-CN"&gt;The original data is as follows:&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;" lang="zh-CN"&gt;You can modify the SQL statement in the following location:&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;" lang="zh-CN"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;SELECT 
    *,
    PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY Table1.hour) OVER () AS Median_TAT_HOURS
FROM 
    Table1
&lt;/LI-CODE&gt;
&lt;P style="margin: 0in; font-family: Calibri; font-size: 11.0pt;" lang="zh-CN"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;Here are the final results:&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Of course, if you have any new discoveries or questions, please feel free to get in touch with us.&lt;BR /&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;"&gt;Best Regards,&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;"&gt;Leroy Lu&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;"&gt;If this post &lt;EM&gt;&lt;STRONG&gt;helps&lt;/STRONG&gt;&lt;/EM&gt;, then please consider Accept it &lt;EM&gt;&lt;STRONG&gt;as the solution &lt;/STRONG&gt;&lt;/EM&gt;to help the other members find it more quickly.&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 11 Nov 2024 07:16:54 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2024-11-11T07:16:54Z</dc:date>
    <item>
      <title>how to get Median expression in paginated report(Calculate a Median in SSRS)</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/how-to-get-Median-expression-in-paginated-report-Calculate-a/m-p/4272374#M169470</link>
      <description>&lt;P&gt;Hi Team,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;iam trying to get Median of TAT_hours in paginated expession&amp;nbsp;&lt;/P&gt;&lt;P&gt;usually iam trying&amp;nbsp;&lt;STRONG&gt;=Median(Fields!TAT_HOURS.Value) &lt;/STRONG&gt;but getting an&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;error&lt;/P&gt;&lt;P&gt;(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.)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;kindly check and provide the right one&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2024 10:32:34 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/how-to-get-Median-expression-in-paginated-report-Calculate-a/m-p/4272374#M169470</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2024-11-06T10:32:34Z</dc:date>
    </item>
    <item>
      <title>Re: how to get Median expression in paginated report(Calculate a Median in SSRS)</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/how-to-get-Median-expression-in-paginated-report-Calculate-a/m-p/4273089#M169508</link>
      <description>&lt;P&gt;Hello&amp;nbsp;Anonymous&lt;/LI-USER&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you please try this approach to use&amp;nbsp;custom VB.NET code in SSRS to calculate the median:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;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
&lt;/LI-CODE&gt;
&lt;P&gt;In the expression where you want to display the median:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;=Code.GetMedian(Fields!TAT_HOURS.Value)
&lt;/LI-CODE&gt;
&lt;P&gt;Hope this helps.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2024 22:23:14 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/how-to-get-Median-expression-in-paginated-report-Calculate-a/m-p/4273089#M169508</guid>
      <dc:creator>Sahir_Maharaj</dc:creator>
      <dc:date>2024-11-06T22:23:14Z</dc:date>
    </item>
    <item>
      <title>Re: how to get Median expression in paginated report(Calculate a Median in SSRS)</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/how-to-get-Median-expression-in-paginated-report-Calculate-a/m-p/4274122#M169560</link>
      <description>&lt;P&gt;Hi Sahir,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;After trying above code getting an error like below&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is an error on line 5 of custom code: [BC30002] Type 'List' is not defined.&lt;BR /&gt;----------------------------&lt;BR /&gt;The definition of the report '' is invalid.&lt;BR /&gt;----------------------------&lt;BR /&gt;An error occurred during local report processing.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Nov 2024 13:33:21 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/how-to-get-Median-expression-in-paginated-report-Calculate-a/m-p/4274122#M169560</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2024-11-07T13:33:21Z</dc:date>
    </item>
    <item>
      <title>Re: how to get Median expression in paginated report(Calculate a Median in SSRS)</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/how-to-get-Median-expression-in-paginated-report-Calculate-a/m-p/4277740#M169733</link>
      <description>&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;"&gt;Thanks for the reply from Sahir_Maharaj&amp;nbsp;, please allow me to provide another insight:&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;"&gt;Hi,&amp;nbsp;Anonymous&lt;/LI-USER&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;" lang="zh-CN"&gt;Based on my testing results, lists are not supported within custom functions in report builds.&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;" lang="zh-CN"&gt;Currently, a more effective solution I’ve found is to use SQL statements directly. For example:&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Calibri; font-size: 11.0pt;" lang="zh-CN"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;" lang="zh-CN"&gt;The original data is as follows:&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;" lang="zh-CN"&gt;You can modify the SQL statement in the following location:&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;" lang="zh-CN"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;SELECT 
    *,
    PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY Table1.hour) OVER () AS Median_TAT_HOURS
FROM 
    Table1
&lt;/LI-CODE&gt;
&lt;P style="margin: 0in; font-family: Calibri; font-size: 11.0pt;" lang="zh-CN"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;Here are the final results:&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Of course, if you have any new discoveries or questions, please feel free to get in touch with us.&lt;BR /&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;"&gt;Best Regards,&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;"&gt;Leroy Lu&lt;/P&gt;
&lt;P style="margin: 0in; font-family: Arial; font-size: 12.0pt; color: black;"&gt;If this post &lt;EM&gt;&lt;STRONG&gt;helps&lt;/STRONG&gt;&lt;/EM&gt;, then please consider Accept it &lt;EM&gt;&lt;STRONG&gt;as the solution &lt;/STRONG&gt;&lt;/EM&gt;to help the other members find it more quickly.&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Nov 2024 07:16:54 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/how-to-get-Median-expression-in-paginated-report-Calculate-a/m-p/4277740#M169733</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2024-11-11T07:16:54Z</dc:date>
    </item>
    <item>
      <title>Re: how to get Median expression in paginated report(Calculate a Median in SSRS)</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/how-to-get-Median-expression-in-paginated-report-Calculate-a/m-p/4608854#M176425</link>
      <description>&lt;P&gt;Try this (it worked for me)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This custom code can be added to the report:&lt;/P&gt;&lt;PRE&gt;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&lt;/PRE&gt;&lt;P&gt;Which can then be called by using the following&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;=Code.Median(Lookupset(Fields!Contract.Value, Fields!Contract.Value, Fields!Contract.Value, "DS_CallData_LKP"))&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;lookupset()&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;instead of the hidden rows/columns method that is seen a lot helps keep the report simple for editing later down the line.&lt;/P&gt;</description>
      <pubDate>Thu, 13 Mar 2025 14:43:12 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/how-to-get-Median-expression-in-paginated-report-Calculate-a/m-p/4608854#M176425</guid>
      <dc:creator>JohnDonnelly12</dc:creator>
      <dc:date>2025-03-13T14:43:12Z</dc:date>
    </item>
  </channel>
</rss>

