<?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 return a median from a calculated table using ADDMISSINGITEMS. in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-return-a-median-from-a-calculated-table-using/m-p/3424730#M129710</link>
    <description>&lt;P&gt;Hi Owen.&amp;nbsp; Thank you for the email address.&amp;nbsp; I will email you the above files.&lt;/P&gt;</description>
    <pubDate>Mon, 11 Sep 2023 16:29:31 GMT</pubDate>
    <dc:creator>BradBuske</dc:creator>
    <dc:date>2023-09-11T16:29:31Z</dc:date>
    <item>
      <title>How to return a median from a calculated table using ADDMISSINGITEMS.</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-return-a-median-from-a-calculated-table-using/m-p/3399787#M128333</link>
      <description>&lt;P&gt;I'm trying to create a new measure which will return the daily median of a calculated table.&amp;nbsp; I continue to get the error "The expression specified in the query is not a valid table expression."&amp;nbsp; I'm using the ADDMISSINGITEMS function to return zeroes for days where no patients were cared for by a "care team".&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is the DAX that is receiving this error:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;EVALUATE&lt;BR /&gt;MEDIANX(&lt;BR /&gt;FILTER(&lt;BR /&gt;FILTER(&lt;BR /&gt;ADDCOLUMNS(&lt;BR /&gt;ADDMISSINGITEMS('DATE - CENSUS'[FULL DATE],&lt;BR /&gt;'CARE TEAM - CENSUS'[CARE TEAM NAME],&lt;BR /&gt;SUMMARIZECOLUMNS('DATE - CENSUS'[FULL DATE],&lt;BR /&gt;'CARE TEAM - CENSUS'[CARE TEAM NAME],&lt;BR /&gt;"Daily Census Count",[Hospital Encounter Count]),&lt;BR /&gt;'DATE - CENSUS'[FULL DATE],&lt;BR /&gt;'CARE TEAM - CENSUS'[CARE TEAM NAME]),&lt;BR /&gt;"Daily Census Count (With Zeroes)",&lt;BR /&gt;IF(ISBLANK([Daily Census Count]),0,[Daily Census Count])),&lt;BR /&gt;'CARE TEAM - CENSUS'[CARE TEAM NAME] = "Adolescent Medicine"),&lt;BR /&gt;'DATE - CENSUS'[FULL DATE] &amp;lt;&amp;gt; DATE(1900,1,1) &amp;amp;&amp;amp;&lt;BR /&gt;'DATE - CENSUS'[FULL DATE] &amp;lt;&amp;gt; DATE(2099,12,31)),&lt;BR /&gt;[Daily Census Count (With Zeroes)])&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I remove the MEDIANX function from the above DAX, I get exactly the calculated table I need to calculate the daily median.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Fri, 25 Aug 2023 19:07:24 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-return-a-median-from-a-calculated-table-using/m-p/3399787#M128333</guid>
      <dc:creator>BradBuske</dc:creator>
      <dc:date>2023-08-25T19:07:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to return a median from a calculated table using ADDMISSINGITEMS.</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-return-a-median-from-a-calculated-table-using/m-p/3400120#M128343</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="608560" data-lia-user-login="BradBuske" class="lia-mention lia-mention-user"&gt;BradBuske&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;On the immediate issue:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;A DAX query must return a table, not a scalar value, which is the reason for the error message.&lt;/P&gt;
&lt;P&gt;If you want to see the value of a scalar expression using a DAX query, you must include it in a table of some sort.&lt;/P&gt;
&lt;P&gt;For example, you could convert your current expression into a table with this query:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;EVALUATE
VAR MedianValue =
    MEDIANX (
       // ...
    )
RETURN
    { MedianValue } -- use simple table constructor

    -- Alternatively use ROW function
    -- ROW ( "Median", MedianValue ) &lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, a measure to be used in Power BI visuals must return a scalar value, so your final measure could be:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Median Measure =
MEDIANX (
    // ...
)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Use of SUMMARIZECOLUMNS and ADDMISSINGITEMS&lt;/STRONG&gt;:&lt;/P&gt;
&lt;P&gt;I would generally be cautious of using SUMMARIZECOLUMNS in measures (see &lt;A href="https://www.sqlbi.com/articles/introducing-summarizecolumns/" target="_self"&gt;here&lt;/A&gt;). It may work in some scenarios but can throw errors if referenced within other measures e.g. within iterators with context transition.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A possible re-writing of your measure with a few other tweaks to give the same result.&lt;/P&gt;
&lt;P&gt;(I have not been able to test so may require some tweaking):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;MedianMeasure =
CALCULATE (
    MEDIANX (
        -- CROSSJOIN will return all combinations regardless of
        -- existence in fact table
        CROSSJOIN (
            VALUES ( 'DATE - CENSUS'[FULL DATE] ),
            VALUES ( 'CARE TEAM - CENSUS'[CARE TEAM NAME] )
        ),
        COALESCE ( [Hospital Encounter Count], 0 )
    ),
    -- Wrap filters in KEEPFILTERS in order to intersect with existing filters
    KEEPFILTERS ( 'CARE TEAM - CENSUS'[CARE TEAM NAME] = "Adolescent Medicine" ),
    KEEPFILTERS (
        NOT 'DATE - CENSUS'[FULL DATE] IN { DATE ( 1900, 1, 1 ), DATE ( 2099, 12, 31 ) }
    )
)
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards&lt;/P&gt;</description>
      <pubDate>Sat, 26 Aug 2023 02:33:39 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-return-a-median-from-a-calculated-table-using/m-p/3400120#M128343</guid>
      <dc:creator>OwenAuger</dc:creator>
      <dc:date>2023-08-26T02:33:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to return a median from a calculated table using ADDMISSINGITEMS.</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-return-a-median-from-a-calculated-table-using/m-p/3409409#M128848</link>
      <description>&lt;P&gt;Thank you, Owen.&amp;nbsp; This helped.&amp;nbsp; I'm getting results without errors in either Excel or Power BI now.&amp;nbsp; However, I'm getting strange results when slicing on this measure.&amp;nbsp; Below is my improved DAX:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ROW("Daily Census Median",&lt;BR /&gt;MEDIANX(&lt;BR /&gt;CROSSJOIN(&lt;BR /&gt;DISTINCT(FILTER(SELECTCOLUMNS('DATE - CENSUS',"Full Date",[FULL DATE]),NOT [Full Date] IN {DATE(1900,1,1),DATE(2099,12,31)})),&lt;BR /&gt;--DISTINCT(FILTER(SELECTCOLUMNS('DATE - CENSUS',"Full Date",[FULL DATE]),STARTOFMONTH('DATE - CENSUS'[Full Date]) = DATE(2023,1,1))),&lt;BR /&gt;DISTINCT(FILTER(SELECTCOLUMNS('CARE TEAM - CENSUS',"Care Team Name",[CARE TEAM NAME]),[Care Team Name] = "Adolescent Medicine")),&lt;BR /&gt;DISTINCT(FILTER(SELECTCOLUMNS('UNIT TRANSFER STATUS - CENSUS',"Unit Presence Group",[UNIT PRESENCE GROUP]),[Unit Presence Group] = "In unit"))),&lt;BR /&gt;COALESCE([Hospital Encounter Count],0)))&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I slice this measure by "CARE TEAM NAME" using either Excel or Power BI, I get the median without the zeroes (incorrect).&amp;nbsp; If I simply Place the "Daily Census Median" in a report, I get the median with the zeroes (correct).&amp;nbsp; Why does this happen?&amp;nbsp; Is there some additional DAX I need to add to correct this behavior?&amp;nbsp; Note that I'm filtering on "CARE TEAM NAME" for now to simplify testing.&amp;nbsp; I need this to work for all care teams.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Aug 2023 16:29:31 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-return-a-median-from-a-calculated-table-using/m-p/3409409#M128848</guid>
      <dc:creator>BradBuske</dc:creator>
      <dc:date>2023-08-31T16:29:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to return a median from a calculated table using ADDMISSINGITEMS.</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-return-a-median-from-a-calculated-table-using/m-p/3412395#M129011</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="608560" data-lia-user-login="BradBuske" class="lia-mention lia-mention-user"&gt;BradBuske&lt;/a&gt;&amp;nbsp;Thanks for the update &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Hmm, I can't see anything in your DAX that would result in the median without zeroes.&lt;/P&gt;
&lt;P&gt;To help diagnose, could you share a sanitised PBIX file?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Feel free to replace the data with dummy values, as long as we can still see the incorrect results. And add a comment indicating what the correct result should be.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks a lot!&lt;/P&gt;</description>
      <pubDate>Sun, 03 Sep 2023 01:51:26 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-return-a-median-from-a-calculated-table-using/m-p/3412395#M129011</guid>
      <dc:creator>OwenAuger</dc:creator>
      <dc:date>2023-09-03T01:51:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to return a median from a calculated table using ADDMISSINGITEMS.</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-return-a-median-from-a-calculated-table-using/m-p/3416665#M129297</link>
      <description>&lt;P&gt;Hi Owen.&amp;nbsp; I'll attach the PBIX file via email.&amp;nbsp; I was not able to provide "dummy" data ... too time consuming to do that right now.&amp;nbsp; But, you can see a small version of my model along with the median measure.&amp;nbsp; It is definitely removing the zeroes when slicing on the "CARE TEAM NAME".&amp;nbsp; I'll also attach a couple of pivot table reports via email showing the results for the "XZRWROLOTY NP SOSPRTZLRST W" care team (sanitized).&amp;nbsp; For the month of January 2022, this care team has 22 days where zero patients were treated.&amp;nbsp; The median should be zero, but instead 6 is the result.&amp;nbsp; I also tried the PERCENTILEX.INC function ... same result ... 6.&amp;nbsp; This does work perfect when a care team has no days in a month where 0 patients were treated.&amp;nbsp; However, my original median measure works too.&amp;nbsp; I'm trying to improve my original measure to handle zeroes.&amp;nbsp; Thanks and please let me know if you need add'l info.&lt;/P&gt;</description>
      <pubDate>Wed, 06 Sep 2023 00:59:02 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-return-a-median-from-a-calculated-table-using/m-p/3416665#M129297</guid>
      <dc:creator>BradBuske</dc:creator>
      <dc:date>2023-09-06T00:59:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to return a median from a calculated table using ADDMISSINGITEMS.</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-return-a-median-from-a-calculated-table-using/m-p/3424730#M129710</link>
      <description>&lt;P&gt;Hi Owen.&amp;nbsp; Thank you for the email address.&amp;nbsp; I will email you the above files.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Sep 2023 16:29:31 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-return-a-median-from-a-calculated-table-using/m-p/3424730#M129710</guid>
      <dc:creator>BradBuske</dc:creator>
      <dc:date>2023-09-11T16:29:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to return a median from a calculated table using ADDMISSINGITEMS.</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-return-a-median-from-a-calculated-table-using/m-p/3428430#M129961</link>
      <description>&lt;P&gt;Thanks Brad, files received &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I had a quick look and the issue looks to be the bidirectional relationships between the dimension tables and 'HOSPITAL ENCOUNTER DAY HOUR BRIDGE'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Ordinarily, these relationships should be single-directional with 1-side filtering many-side. The problem with these bidirectional relationships is that the dimensions effectively cross-filter each other.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For example, applying a filter CARE TEAM = "XZRWROLOTY NP SOSPRTZLRST W" limits the visible dates to 21-28 &amp;amp; 31 Jan. This means that dates outside this set are not included in the Median calculation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you try changing those three relationships to single-directional (as shown below)? Leave the relationship between 'HOSPITAL ENCOUNTER DAY HOUR BRIDGE' and 'HOSPITAL ENCOUNTER' as bidirectional as I can see that is required.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I believe that should fix the behaviour of the Median measure. Does it work at your end?&lt;/P&gt;
&lt;P&gt;&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;Regards&lt;/P&gt;</description>
      <pubDate>Wed, 13 Sep 2023 11:43:27 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-return-a-median-from-a-calculated-table-using/m-p/3428430#M129961</guid>
      <dc:creator>OwenAuger</dc:creator>
      <dc:date>2023-09-13T11:43:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to return a median from a calculated table using ADDMISSINGITEMS.</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-return-a-median-from-a-calculated-table-using/m-p/3431172#M130094</link>
      <description>&lt;P&gt;Hi Owen,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You are "spot on" with your fix.&amp;nbsp; This works perfectly now!&amp;nbsp; Thank you so much for your assistance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Brad&lt;/P&gt;</description>
      <pubDate>Thu, 14 Sep 2023 14:09:17 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/How-to-return-a-median-from-a-calculated-table-using/m-p/3431172#M130094</guid>
      <dc:creator>BradBuske</dc:creator>
      <dc:date>2023-09-14T14:09:17Z</dc:date>
    </item>
  </channel>
</rss>

