<?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 Automated Unit Testing in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Automated-Unit-Testing/m-p/3529251#M135607</link>
    <description>&lt;P&gt;Hi Forum,&lt;/P&gt;&lt;P&gt;Inspired by this &lt;A href="https://p3adaptive.com/2018/09/automated-testing-using-dax-for-power-bi/" target="_self"&gt;article&lt;/A&gt;, I'd like to create automated unit tests to test measures within my Power BI Semantic Model (Dataset).&lt;/P&gt;&lt;P&gt;I'm planning to:&lt;/P&gt;&lt;P&gt;- Use Powershell to connect to and run through the tests&lt;/P&gt;&lt;P&gt;-&amp;nbsp;DAX scripts to query the Semantic Model itself&lt;/P&gt;&lt;P&gt;Since these are unit tests, I want to contain "fake" data within the test itself (not have to worry about the underlying database changing).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A few approaches I've attempted.&lt;/P&gt;&lt;P&gt;1. Overwrite Existing Table for Test (Doesn't work; PBI won't let me overwrite the actual_table in the semantic model)&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;DEFINE

TABLE actual_table = UNION(ROW("order","A1"),ROW("order","B1")) -- attempt to update as a "local variable"

MEASURE 'Measures Table'[COUNT_ORDERS] = DISTINCTCOUNT('actual_table'[order])) 

EVALUATE
ROW("COUNT_ORDERS",'Measures Table'[COUNT_ORDERS])&lt;/LI-CODE&gt;&lt;P&gt;Error: Table '&amp;lt;ccon&amp;gt;actual_table&amp;lt;/ccon&amp;gt;' cannot be created because a table or variable with the same name already exists.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. Update Table Reference (Pretty sure PBI was designed purposely not to do this)&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;DEFINE

TABLE test_table = UNION(ROW("order","A1"),ROW("order","B1"))

MEASURE 'Measures Table'[COUNT_ORDERS] = DISTINCTCOUNT('actual_table'[order])) -- somehow update table reference 'actual_table' to 'test_table'

EVALUATE
ROW("COUNT_ORDERS",'Measures Table'[COUNT_ORDERS])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3. SWITCH between actual table and test table (I don't like this approach because I'd have to duplicate the SWITCH across all measures)&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;DEFINE

TABLE test_table = UNION(ROW("order","A1"),ROW("order","B1"))

MEASURE 'Measures Table'[COUNT_ORDERS] = 

SWITCH(2,
	   1,DISTINCTCOUNT('actual_table'[order]),
	   2,DISTINCTCOUNT('test_table'[order])
)

EVALUATE
ROW("COUNT_ORDERS",'Measures Table'[COUNT_ORDERS])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you have any alternative recommendations? Your help is greatly appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 12 Nov 2023 00:15:14 GMT</pubDate>
    <dc:creator>dritsche</dc:creator>
    <dc:date>2023-11-12T00:15:14Z</dc:date>
    <item>
      <title>Automated Unit Testing</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Automated-Unit-Testing/m-p/3529251#M135607</link>
      <description>&lt;P&gt;Hi Forum,&lt;/P&gt;&lt;P&gt;Inspired by this &lt;A href="https://p3adaptive.com/2018/09/automated-testing-using-dax-for-power-bi/" target="_self"&gt;article&lt;/A&gt;, I'd like to create automated unit tests to test measures within my Power BI Semantic Model (Dataset).&lt;/P&gt;&lt;P&gt;I'm planning to:&lt;/P&gt;&lt;P&gt;- Use Powershell to connect to and run through the tests&lt;/P&gt;&lt;P&gt;-&amp;nbsp;DAX scripts to query the Semantic Model itself&lt;/P&gt;&lt;P&gt;Since these are unit tests, I want to contain "fake" data within the test itself (not have to worry about the underlying database changing).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;A few approaches I've attempted.&lt;/P&gt;&lt;P&gt;1. Overwrite Existing Table for Test (Doesn't work; PBI won't let me overwrite the actual_table in the semantic model)&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;DEFINE

TABLE actual_table = UNION(ROW("order","A1"),ROW("order","B1")) -- attempt to update as a "local variable"

MEASURE 'Measures Table'[COUNT_ORDERS] = DISTINCTCOUNT('actual_table'[order])) 

EVALUATE
ROW("COUNT_ORDERS",'Measures Table'[COUNT_ORDERS])&lt;/LI-CODE&gt;&lt;P&gt;Error: Table '&amp;lt;ccon&amp;gt;actual_table&amp;lt;/ccon&amp;gt;' cannot be created because a table or variable with the same name already exists.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. Update Table Reference (Pretty sure PBI was designed purposely not to do this)&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;DEFINE

TABLE test_table = UNION(ROW("order","A1"),ROW("order","B1"))

MEASURE 'Measures Table'[COUNT_ORDERS] = DISTINCTCOUNT('actual_table'[order])) -- somehow update table reference 'actual_table' to 'test_table'

EVALUATE
ROW("COUNT_ORDERS",'Measures Table'[COUNT_ORDERS])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;3. SWITCH between actual table and test table (I don't like this approach because I'd have to duplicate the SWITCH across all measures)&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;DEFINE

TABLE test_table = UNION(ROW("order","A1"),ROW("order","B1"))

MEASURE 'Measures Table'[COUNT_ORDERS] = 

SWITCH(2,
	   1,DISTINCTCOUNT('actual_table'[order]),
	   2,DISTINCTCOUNT('test_table'[order])
)

EVALUATE
ROW("COUNT_ORDERS",'Measures Table'[COUNT_ORDERS])&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you have any alternative recommendations? Your help is greatly appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 12 Nov 2023 00:15:14 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Automated-Unit-Testing/m-p/3529251#M135607</guid>
      <dc:creator>dritsche</dc:creator>
      <dc:date>2023-11-12T00:15:14Z</dc:date>
    </item>
    <item>
      <title>Re: Automated Unit Testing</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Automated-Unit-Testing/m-p/3540378#M136053</link>
      <description>&lt;P&gt;Have your test data be part of the regular data source refreshes.&amp;nbsp; For production use a filter that excludes the test data. For unit testing flip the filter to exclude the production data.&lt;/P&gt;</description>
      <pubDate>Sat, 18 Nov 2023 18:25:47 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Automated-Unit-Testing/m-p/3540378#M136053</guid>
      <dc:creator>lbendlin</dc:creator>
      <dc:date>2023-11-18T18:25:47Z</dc:date>
    </item>
  </channel>
</rss>

