<?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 Controlling filter context in percentile calculation in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Controlling-filter-context-in-percentile-calculation/m-p/3822449#M149492</link>
    <description>&lt;P&gt;I am struggling to control different filter context transitions within a complex dax measure. I am trying to compute the median duration for a certain percentile of events, where that percentile is partitioned within a table. My table is structured like this:&lt;BR /&gt;&lt;BR /&gt;index&amp;nbsp; &amp;nbsp; &amp;nbsp;opportunity_id&amp;nbsp; event_order&amp;nbsp; &amp;nbsp;duration&amp;nbsp; status&amp;nbsp; &amp;nbsp;date&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;109750&lt;/TD&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;120&lt;/TD&gt;&lt;TD&gt;Active (Not Recruiting)&lt;/TD&gt;&lt;TD&gt;24/01/2023 00:00&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;86263&lt;/TD&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;361&lt;/TD&gt;&lt;TD&gt;Active (Not Recruiting)&lt;/TD&gt;&lt;TD&gt;12/07/2022 00:00&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;109752&lt;/TD&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;134&lt;/TD&gt;&lt;TD&gt;Active (Not Recruiting)&lt;/TD&gt;&lt;TD&gt;07/02/2023 00:00&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;86264&lt;/TD&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;361&lt;/TD&gt;&lt;TD&gt;Active (Not Recruiting)&lt;/TD&gt;&lt;TD&gt;12/07/2022 00:00&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;My strategy has been to calculate the &lt;STRONG&gt;target&lt;/STRONG&gt; event order (e.g. the one which is the 25th percentile) partitioned by the opportunity_id, and then calculate the median duration for a table filtered to only include records where the event_order = &lt;STRONG&gt;target&lt;/STRONG&gt;:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;25% Duration (Static) = 

VAR _percentile = 0.25

// compute the 25% percentile event order partitioned by opportunity id
VAR _tbl =

        ADDCOLUMNS (
            'Table',
                "@target",
                    CEILING(
                        CALCULATE (
                            MAX ( 'Table'[event_order]),
                            FILTER (
                                ALL('Table'),
                                'Table'[opportunity_id] = EARLIER ( 'Table'[opportunity_id] )
                            ))
                    * _percentile, 
                1)
            )


VAR  _result =

        MEDIANX(
            FILTER (
            _tbl,
            [event_order] = [@target]
        ), 
        [duration])

RETURN _result&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;SPAN class=""&gt;This returns the correct results for all visual types. However, the use of ALL() makes it insensitive to external filtering &lt;/SPAN&gt;&lt;I&gt;&lt;EM&gt;in the right order&lt;/EM&gt;&lt;/I&gt;&lt;SPAN class=""&gt;, which is desired. For example, if applying a certain filter will remove those records from the _tbl result, when what I need is for the new event order and target to be recalculated dynamically. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;To enable dynamic filtering I have tried to re-calculate the event_order dynamically within the measure like so&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;25% Duration (dynamic) = 

VAR _percentile = 0.25

// compute the 25% percentile event order partitioned by opportunity id
VAR _tbl =

        ADDCOLUMNS (
            'Table',
                "@target",
                    CEILING(
                        CALCULATE (
                            MAX ( 'Table'[event_order]),
                            FILTER (
                                ALL('Table'),
                                'Table'[opportunity_id] = EARLIER ( 'Table'[opportunity_id] )
                            ))
                    * _percentile, 
                1),
                "@event_order",
                ROWNUMBER(
                    ALLSELECTED('Table'), 
                    ORDERBY([event_order], ASC, [index], ASC), 
                    LAST, 
                    PARTITIONBY([opportunity_id])
)
            )


VAR  _result =

        MEDIANX(
            FILTER (
            _tbl,
            [@event_order] = [@target]
        ), 
        [duration])

RETURN _result&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;This returns the correct results in visuals partitioned by opportunty_id (and allows for external filtering) but as soon as I add a date range it becomes inaccurate because it partitions for each date range in the visual. I.e. it returns the 25th percentile for each date range in which it occurs.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there any way to control the filter context in such a way that it computes accurate results against timelines (i.e. just one 25th percentile event per opportunity) while still respecting filtering that happens at the reporting layer?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 09 Apr 2024 10:02:02 GMT</pubDate>
    <dc:creator>cashewNut</dc:creator>
    <dc:date>2024-04-09T10:02:02Z</dc:date>
    <item>
      <title>Controlling filter context in percentile calculation</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Controlling-filter-context-in-percentile-calculation/m-p/3822449#M149492</link>
      <description>&lt;P&gt;I am struggling to control different filter context transitions within a complex dax measure. I am trying to compute the median duration for a certain percentile of events, where that percentile is partitioned within a table. My table is structured like this:&lt;BR /&gt;&lt;BR /&gt;index&amp;nbsp; &amp;nbsp; &amp;nbsp;opportunity_id&amp;nbsp; event_order&amp;nbsp; &amp;nbsp;duration&amp;nbsp; status&amp;nbsp; &amp;nbsp;date&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;109750&lt;/TD&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;120&lt;/TD&gt;&lt;TD&gt;Active (Not Recruiting)&lt;/TD&gt;&lt;TD&gt;24/01/2023 00:00&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;86263&lt;/TD&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;361&lt;/TD&gt;&lt;TD&gt;Active (Not Recruiting)&lt;/TD&gt;&lt;TD&gt;12/07/2022 00:00&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;109752&lt;/TD&gt;&lt;TD&gt;a&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;134&lt;/TD&gt;&lt;TD&gt;Active (Not Recruiting)&lt;/TD&gt;&lt;TD&gt;07/02/2023 00:00&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;86264&lt;/TD&gt;&lt;TD&gt;b&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;361&lt;/TD&gt;&lt;TD&gt;Active (Not Recruiting)&lt;/TD&gt;&lt;TD&gt;12/07/2022 00:00&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;My strategy has been to calculate the &lt;STRONG&gt;target&lt;/STRONG&gt; event order (e.g. the one which is the 25th percentile) partitioned by the opportunity_id, and then calculate the median duration for a table filtered to only include records where the event_order = &lt;STRONG&gt;target&lt;/STRONG&gt;:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;25% Duration (Static) = 

VAR _percentile = 0.25

// compute the 25% percentile event order partitioned by opportunity id
VAR _tbl =

        ADDCOLUMNS (
            'Table',
                "@target",
                    CEILING(
                        CALCULATE (
                            MAX ( 'Table'[event_order]),
                            FILTER (
                                ALL('Table'),
                                'Table'[opportunity_id] = EARLIER ( 'Table'[opportunity_id] )
                            ))
                    * _percentile, 
                1)
            )


VAR  _result =

        MEDIANX(
            FILTER (
            _tbl,
            [event_order] = [@target]
        ), 
        [duration])

RETURN _result&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;SPAN class=""&gt;This returns the correct results for all visual types. However, the use of ALL() makes it insensitive to external filtering &lt;/SPAN&gt;&lt;I&gt;&lt;EM&gt;in the right order&lt;/EM&gt;&lt;/I&gt;&lt;SPAN class=""&gt;, which is desired. For example, if applying a certain filter will remove those records from the _tbl result, when what I need is for the new event order and target to be recalculated dynamically. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;To enable dynamic filtering I have tried to re-calculate the event_order dynamically within the measure like so&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;25% Duration (dynamic) = 

VAR _percentile = 0.25

// compute the 25% percentile event order partitioned by opportunity id
VAR _tbl =

        ADDCOLUMNS (
            'Table',
                "@target",
                    CEILING(
                        CALCULATE (
                            MAX ( 'Table'[event_order]),
                            FILTER (
                                ALL('Table'),
                                'Table'[opportunity_id] = EARLIER ( 'Table'[opportunity_id] )
                            ))
                    * _percentile, 
                1),
                "@event_order",
                ROWNUMBER(
                    ALLSELECTED('Table'), 
                    ORDERBY([event_order], ASC, [index], ASC), 
                    LAST, 
                    PARTITIONBY([opportunity_id])
)
            )


VAR  _result =

        MEDIANX(
            FILTER (
            _tbl,
            [@event_order] = [@target]
        ), 
        [duration])

RETURN _result&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;This returns the correct results in visuals partitioned by opportunty_id (and allows for external filtering) but as soon as I add a date range it becomes inaccurate because it partitions for each date range in the visual. I.e. it returns the 25th percentile for each date range in which it occurs.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there any way to control the filter context in such a way that it computes accurate results against timelines (i.e. just one 25th percentile event per opportunity) while still respecting filtering that happens at the reporting layer?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Apr 2024 10:02:02 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Controlling-filter-context-in-percentile-calculation/m-p/3822449#M149492</guid>
      <dc:creator>cashewNut</dc:creator>
      <dc:date>2024-04-09T10:02:02Z</dc:date>
    </item>
    <item>
      <title>Re: Controlling filter context in percentile calculation</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Controlling-filter-context-in-percentile-calculation/m-p/3826149#M149604</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="470668" data-lia-user-login="cashewNut" class="lia-mention lia-mention-user"&gt;cashewNut&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try to modify your formula like below:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;25% Duration (Adjusted) = 
VAR _percentile = 0.25
VAR _filteredTable = FILTER(ALLSELECTED('Table'), [status] = "Active (Not Recruiting)")
VAR _partitionedTable = 
    ADDCOLUMNS (
        _filteredTable,
        "@target",
            CEILING(
                CALCULATE (
                    MAX ( 'Table'[event_order]),
                    ALLEXCEPT('Table', 'Table'[opportunity_id])
                ) * _percentile, 
            1)
    )
VAR _result = 
    MEDIANX(
        FILTER (
            _partitionedTable,
            [event_order] = [@target]
        ), 
        [duration]
    )
RETURN _result&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best Regards,&lt;BR /&gt;Adamk Kong&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this post&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;&lt;STRONG&gt;helps&lt;/STRONG&gt;&lt;/EM&gt;, then please consider&lt;EM&gt;&lt;STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;Accept it as the solution&lt;/STRONG&gt;&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;to help the other members find it more quickly.&lt;/P&gt;</description>
      <pubDate>Wed, 10 Apr 2024 08:22:21 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Controlling-filter-context-in-percentile-calculation/m-p/3826149#M149604</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2024-04-10T08:22:21Z</dc:date>
    </item>
    <item>
      <title>Re: Controlling filter context in percentile calculation</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Controlling-filter-context-in-percentile-calculation/m-p/3833170#M149891</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;Anonymous&lt;/a&gt;&amp;nbsp;! I am trying to avoid specifying filters directly in the query. I've ended up getting the query to work in production where I have a relationship to the filtering tables ('Table1') in this example, using a combination of PERCENTILE.INC(), KEEPFILTERS(), and ALLACCEPT()&lt;/P&gt;&lt;DIV class=""&gt;&lt;SPAN class=""&gt;VAR _percentile =&lt;BR /&gt;ADDCOLUMNS (&lt;BR /&gt;FILTER (&lt;BR /&gt;'Table',&lt;BR /&gt;"@target",&lt;BR /&gt;CALCULATE (&lt;BR /&gt;MINX (&lt;BR /&gt;FILTER (&lt;BR /&gt;'Table',&lt;BR /&gt;[event_order]&lt;BR /&gt;&amp;gt;= PERCENTILEX.INC (&lt;BR /&gt;VALUES ( 'Table'[event_order] ),&lt;BR /&gt;'Table'[event_order],&lt;BR /&gt;_percentile&lt;BR /&gt;)&lt;BR /&gt;),&lt;BR /&gt;[event_order]&lt;BR /&gt;),&lt;BR /&gt;ALLEXCEPT ( 'Table', 'Table[opportunity_id] ),&lt;BR /&gt;KEEPFILTERS ( 'TABLE1' ),&lt;BR /&gt;)&lt;BR /&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;SPAN class=""&gt;//calculate the median duration for records in a result set limited to the ordering targets&lt;BR /&gt;VAR _result =&lt;BR /&gt;MEDIANX (&lt;BR /&gt;FILTER (&lt;BR /&gt;_percentile ,&lt;BR /&gt;[_event_order] = [@target]&lt;BR /&gt;),&lt;BR /&gt;[Duration]&lt;BR /&gt;)&lt;BR /&gt;RETURN&lt;BR /&gt;_result&lt;/SPAN&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 12 Apr 2024 08:41:54 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Controlling-filter-context-in-percentile-calculation/m-p/3833170#M149891</guid>
      <dc:creator>cashewNut</dc:creator>
      <dc:date>2024-04-12T08:41:54Z</dc:date>
    </item>
  </channel>
</rss>

