<?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: More elegant DAX for grouped counts - understanding the Contoso Data Set (link to executable code) in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/More-elegant-DAX-for-grouped-counts-understanding-the-Contoso/m-p/4240119#M167844</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="478736" data-lia-user-login="AhsenMajid" class="lia-mention lia-mention-user"&gt;AhsenMajid&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;COUNTROWS ( pk_count_measures_equal )&lt;/LI-CODE&gt;
&lt;P&gt;This part of the code in ADDCOLUMNS isn't going to work as you expect it to, it will return the total rows of that table variable in each row of ADDCOLUMNS, ADDCOLUMNS only provides a row context but not a filter context so your COUNTROWS can't be filtered here, another issue here is thinking that Variables can be filtered in a new filter context, once defined the value of a variable can't be changed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 13 Oct 2024 15:07:10 GMT</pubDate>
    <dc:creator>AntrikshSharma</dc:creator>
    <dc:date>2024-10-13T15:07:10Z</dc:date>
    <item>
      <title>More elegant DAX for grouped counts - understanding the Contoso Data Set (link to executable code)</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/More-elegant-DAX-for-grouped-counts-understanding-the-Contoso/m-p/4239772#M167782</link>
      <description>&lt;P&gt;Hi all!&lt;BR /&gt;&lt;BR /&gt;I wrote the following query to understand the the granularity of the sales table in the contoso db - you can access it at dax.do, or you can access my code at&amp;nbsp;&lt;A href="https://dax.do/nCPqFjYyEYDGbE/" target="_blank" rel="noopener"&gt;https://dax.do/nCPqFjYyEYDGbE/&lt;/A&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Each row in 'Sales' was obviously not an order, so what was it? My thinking is that each row is a different product in a an order, making the granularity of the 'Sales' table Product-Order.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;It turns out this isn't true:&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;Two questions I'd really like help with:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Is there a more elegant way to write the following code? (is it even correct?)&lt;/LI&gt;&lt;LI&gt;What is the granularity of the Sales table?&lt;/LI&gt;&lt;LI&gt;Bonus question after the code&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="71" data-lia-user-login="AlbertoFerrari" class="lia-mention lia-mention-user"&gt;AlbertoFerrari&lt;/a&gt;,&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="41" data-lia-user-login="marcorusso" class="lia-mention lia-mention-user"&gt;marcorusso&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://dax.do/nCPqFjYyEYDGbE/" target="_blank" rel="noopener"&gt;https://dax.do/nCPqFjYyEYDGbE/&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Code below (data, model, and environment at&amp;nbsp;&lt;A href="https://dax.do/nCPqFjYyEYDGbE/" target="_blank" rel="noopener"&gt;t&lt;/A&gt;he link above)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="css"&gt;-- trying to understand the granularity of the 'Sales' 
-- table. Specifically, is each row in the sales table a 
-- different product in a unique order? And if not, 
-- what is it?

-- I Hypothesize that each row in the sales table represents 
-- one product in an order, i.e. the granularity of the sales 
-- table is sales-order x product-key. The number of unique 
-- product keys per order should equal the number of rows for the 
-- order in the sales table. This code shows they do not. 

DEFINE
MEASURE Sales[Rows In Sales Table] = COUNTROWS( 'Sales' )
MEASURE Sales[Distinct Values of Product Key] = COUNTROWS( DISTINCT ( 'Sales'[ProductKey] ) )

EVALUATE

-- create a table that lists all order numbers, 
-- and counts the number of rows in the sales table
-- for that order number, as well the number of
-- distinct product keys for that order number 
VAR product_keys_distinct = 
		ADDCOLUMNS (
			DISTINCT( Sales[Order Number] ),
			"Rows in sales table", [Rows In Sales Table],
			"PKs in Sales table", [Distinct Values of Product Key]
		)

-- filter the product_keys distinct table to include
-- only those product keys for which the hypothesis is true
VAR pk_count_measures_equal = 
	ADDCOLUMNS(
		FILTER (
			product_keys_distinct,
			[Rows in sales table] == [PKs in Sales table]
		),
		"are PKs equal to sales rows", "yes"
	)
    
-- Summarize to return in a final table
VAR equal_summary_table = 
        ADDCOLUMNS(
			DISTINCT ( 
				SELECTCOLUMNS ( 
					pk_count_measures_equal , 
					"are PKs equal to sales rows2", [are PKs equal to sales rows] 
				) 
			), 
			"number of product keys", COUNTROWS ( pk_count_measures_equal ) 
		)

-- filter the product_keys distinct table to include
-- only those product keys for which the hypothesis is false
VAR pk_count_measures_not_equal = 
	ADDCOLUMNS(
		FILTER (
			product_keys_distinct,
			[Rows in sales table] &amp;lt;&amp;gt; [PKs in Sales table]
		),
		"are PKs equal to sales rows", "no"
	)
     
-- Summarize to return in a final table
VAR not_equal_summary_table =        
        ADDCOLUMNS(
			DISTINCT ( 
				SELECTCOLUMNS ( 
					pk_count_measures_not_equal , 
					"are PKs equal to sales rows2", [are PKs equal to sales rows] 
				) 
			), 
			"number of product keys", COUNTROWS ( pk_count_measures_not_equal ) 
		)

-- Union summary tables
RETURN 
	UNION ( not_equal_summary_table ,
            equal_summary_table
    )&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;Bonus question:&lt;BR /&gt;&lt;SPAN&gt;Unioning the tables&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;pk_count_measures_not_equal and&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;pk_count_measures_equal before summarizing them meant that subsequent attempts to use SUMMARIZECOLUMNS failed abysmally - I could group on yes/no, but the sum was the total number of all product keys, no filter.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 13 Oct 2024 05:39:30 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/More-elegant-DAX-for-grouped-counts-understanding-the-Contoso/m-p/4239772#M167782</guid>
      <dc:creator>AhsenMajid</dc:creator>
      <dc:date>2024-10-13T05:39:30Z</dc:date>
    </item>
    <item>
      <title>Re: More elegant DAX for grouped counts - understanding the Contoso Data Set (link to executable code)</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/More-elegant-DAX-for-grouped-counts-understanding-the-Contoso/m-p/4239995#M167816</link>
      <description>&lt;P&gt;In the real world you have orders and order line items. It is not uncommon for products in an order being listed multiple times, under different order line item numbers, or inside of configurations (CTO items) or bundles (BTO items).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Oh, and these "guaranteed to be unique" order numbers can sometimes be reused, for different customers or countries.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Contoso is just a toy.&lt;/P&gt;</description>
      <pubDate>Sun, 13 Oct 2024 11:29:21 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/More-elegant-DAX-for-grouped-counts-understanding-the-Contoso/m-p/4239995#M167816</guid>
      <dc:creator>lbendlin</dc:creator>
      <dc:date>2024-10-13T11:29:21Z</dc:date>
    </item>
    <item>
      <title>Re: More elegant DAX for grouped counts - understanding the Contoso Data Set (link to executable code)</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/More-elegant-DAX-for-grouped-counts-understanding-the-Contoso/m-p/4240119#M167844</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="478736" data-lia-user-login="AhsenMajid" class="lia-mention lia-mention-user"&gt;AhsenMajid&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;COUNTROWS ( pk_count_measures_equal )&lt;/LI-CODE&gt;
&lt;P&gt;This part of the code in ADDCOLUMNS isn't going to work as you expect it to, it will return the total rows of that table variable in each row of ADDCOLUMNS, ADDCOLUMNS only provides a row context but not a filter context so your COUNTROWS can't be filtered here, another issue here is thinking that Variables can be filtered in a new filter context, once defined the value of a variable can't be changed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 13 Oct 2024 15:07:10 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/More-elegant-DAX-for-grouped-counts-understanding-the-Contoso/m-p/4240119#M167844</guid>
      <dc:creator>AntrikshSharma</dc:creator>
      <dc:date>2024-10-13T15:07:10Z</dc:date>
    </item>
    <item>
      <title>Re: More elegant DAX for grouped counts - understanding the Contoso Data Set (link to executable code)</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/More-elegant-DAX-for-grouped-counts-understanding-the-Contoso/m-p/4255644#M168581</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="478736" data-lia-user-login="AhsenMajid" class="lia-mention lia-mention-user"&gt;AhsenMajid&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Have you already solved the problem? If so, can you share your solution here and mark the correct answer as standard to help other members find it faster? Thank you very much for your co-operation!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best Regards,&lt;/P&gt;
&lt;P&gt;Clara Gong&lt;/P&gt;
&lt;P&gt;If there is any post&amp;nbsp;&lt;STRONG&gt;&lt;I&gt;helps&lt;/I&gt;&lt;/STRONG&gt;, then please consider&amp;nbsp;&lt;STRONG&gt;&lt;I&gt;Accept it as the solution&lt;/I&gt;&lt;/STRONG&gt;&amp;nbsp;&amp;nbsp;to help the other members find it more quickly.&lt;/P&gt;</description>
      <pubDate>Thu, 24 Oct 2024 06:37:24 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/More-elegant-DAX-for-grouped-counts-understanding-the-Contoso/m-p/4255644#M168581</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2024-10-24T06:37:24Z</dc:date>
    </item>
  </channel>
</rss>

