<?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: Group and aggregate by calculated column in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Group-and-aggregate-by-calculated-column/m-p/2772575#M86366</link>
    <description>&lt;P&gt;Now this feels a lot cleaner&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="317289" data-lia-user-login="tamerj1" class="lia-mention lia-mention-user"&gt;tamerj1&lt;/a&gt;&amp;nbsp;code-wise and also because we only summarize/group once instead of twice. Many thanks, this is super!&lt;/P&gt;</description>
    <pubDate>Fri, 16 Sep 2022 07:13:35 GMT</pubDate>
    <dc:creator>csaba09</dc:creator>
    <dc:date>2022-09-16T07:13:35Z</dc:date>
    <item>
      <title>Group and aggregate by calculated column</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Group-and-aggregate-by-calculated-column/m-p/2771226#M86291</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd like to write a DAX query that returns a table containing an aggregation grouped by a physical column AND a calculated column.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the below tables in the model:&lt;/P&gt;&lt;P&gt;fact_sales:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;product_id&lt;/TD&gt;&lt;TD&gt;country_id&lt;/TD&gt;&lt;TD&gt;sales&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;11&lt;/TD&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;22&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;11&lt;/TD&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;11&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;22&lt;/TD&gt;&lt;TD&gt;9&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;dim_product:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;product_id&lt;/TD&gt;&lt;TD&gt;product_type&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;fruit&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;fruit&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;legume&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;vegetable&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;legume&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;dim_country:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;country_id&lt;/TD&gt;&lt;TD&gt;country_name&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;11&lt;/TD&gt;&lt;TD&gt;Spain&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;22&lt;/TD&gt;&lt;TD&gt;Portugal&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'd like to sum sales and group by country name and whether the product is 'fruit' or 'not fruit', returning the below:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;country_name&lt;/TD&gt;&lt;TD&gt;fruit_or_not&lt;/TD&gt;&lt;TD&gt;sales&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Spain&lt;/TD&gt;&lt;TD&gt;fruit&lt;/TD&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Spain&lt;/TD&gt;&lt;TD&gt;not_fruit&lt;/TD&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Portugal&lt;/TD&gt;&lt;TD&gt;fruit&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Portugal&lt;/TD&gt;&lt;TD&gt;not_fruit&lt;/TD&gt;&lt;TD&gt;9&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So the key here is I want to create a calculated 'fruit_or_not' column in the query and use that for grouping later in the same query.&lt;/P&gt;&lt;P&gt;How would I go about this?&lt;/P&gt;&lt;P&gt;I tried creating a table variable and then using that in a SUMMARIZECOLUMN:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;EVALUATE

VAR enriched_dim_product = 
	ADDCOLUMNS(
		dim_product,
		"fruit_or_not", IF(dim_product[product_type] = "fruit", "fruit", "non_fruit")
	)
		
RETURN

SUMMARIZECOLUMNS(
	dim_country[country_name],
	enriched_dim_product[fruit_or_not],	
	"sales", SUM(fact_sales[sales])
)&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;But this throws an error saying it cannot find the table called 'enriched_product_table'. I know I could access 'enriched_product_table' and the 'fruit_or_not' column using SUMMARIZE, but then I can't figure out how to also bake in the country name and the sum of sales.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any tips?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 15 Sep 2022 17:50:18 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Group-and-aggregate-by-calculated-column/m-p/2771226#M86291</guid>
      <dc:creator>csaba09</dc:creator>
      <dc:date>2022-09-15T17:50:18Z</dc:date>
    </item>
    <item>
      <title>Re: Group and aggregate by calculated column</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Group-and-aggregate-by-calculated-column/m-p/2771263#M86295</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="442565" data-lia-user-login="csaba09" class="lia-mention lia-mention-user"&gt;csaba09&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your case (which I pressume is a simplification of the real problem), I would have just added the "fruit_or_not" attribute on to the product dimension (calculated column or in Power Query). Then Power BI woild do the grouping for you due to filter transition:&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;Would that one also work in your case? I mean you would need the fruit_or_not attribute anyway if you'd like to show it in a graph?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let me know! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/Tom&lt;BR /&gt;&lt;A href="https://www.tackytech.blog/" target="_blank" rel="noopener"&gt;https://www.tackytech.blog/&lt;/A&gt;&lt;BR /&gt;&lt;A href="https://www.instagram.com/tackytechtom/" target="_blank" rel="noopener"&gt;https://www.instagram.com/tackytechtom/&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Sep 2022 18:16:29 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Group-and-aggregate-by-calculated-column/m-p/2771263#M86295</guid>
      <dc:creator>tackytechtom</dc:creator>
      <dc:date>2022-09-15T18:16:29Z</dc:date>
    </item>
    <item>
      <title>Re: Group and aggregate by calculated column</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Group-and-aggregate-by-calculated-column/m-p/2771281#M86298</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="442565" data-lia-user-login="csaba09" class="lia-mention lia-mention-user"&gt;csaba09&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;please try&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;EVALUATE
VAR T1 =
    SUMMARIZE (
        fact_sales,
        dim_country[country_name],
        dim_product[product_type],
        "@sales", SUM ( fact_sales[sales] )
    )
VAR T2 =
    ADDCOLUMNS (
        T1,
        "fruit_or_not", IF ( [product_type] = "fruit", "fruit", "non_fruit" )
    )
RETURN
    SUMMARIZE (
        T2,
        [country_name],
        [fruit_or_not],
        "sales",
            VAR Country = [country_name]
            VAR Fruit = [fruit_or_not]
            RETURN
                SUMX (
                    FILTER ( T2, [country_name] = Country &amp;amp;&amp;amp; [fruit_or_not] = Fruit ),
                    [@sales]
                )
    )&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 16 Sep 2022 03:39:59 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Group-and-aggregate-by-calculated-column/m-p/2771281#M86298</guid>
      <dc:creator>tamerj1</dc:creator>
      <dc:date>2022-09-16T03:39:59Z</dc:date>
    </item>
    <item>
      <title>Re: Group and aggregate by calculated column</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Group-and-aggregate-by-calculated-column/m-p/2771320#M86300</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="442565" data-lia-user-login="csaba09" class="lia-mention lia-mention-user"&gt;csaba09&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I only now noticed that you were explicitly asking for a table variable. Here my shot:&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;tableNew = 
VAR _table1 =
SUMMARIZE (
    fact_sales,
    dim_country[country_name],
    dim_product[product_type],
    fact_sales[sales]
)
VAR _table2 =
ADDCOLUMNS (
    _table1,
    "fruit_or_not", IF ( [product_type] = "fruit", "fruit", "non_fruit" )
)
RETURN
GROUPBY ( 
    _table2, 
    [country_name], 
    [fruit_or_not], 
    "sales", SUMX(CURRENTGROUP(), [sales]) 
)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/Tom&lt;BR /&gt;&lt;A href="https://www.tackytech.blog/" target="_blank"&gt;https://www.tackytech.blog/&lt;/A&gt;&lt;BR /&gt;&lt;A href="https://www.instagram.com/tackytechtom/" target="_blank"&gt;https://www.instagram.com/tackytechtom/&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Sep 2022 18:46:32 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Group-and-aggregate-by-calculated-column/m-p/2771320#M86300</guid>
      <dc:creator>tackytechtom</dc:creator>
      <dc:date>2022-09-15T18:46:32Z</dc:date>
    </item>
    <item>
      <title>Re: Group and aggregate by calculated column</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Group-and-aggregate-by-calculated-column/m-p/2771678#M86309</link>
      <description>&lt;P&gt;Thank you &lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="317289" data-lia-user-login="tamerj1" class="lia-mention lia-mention-user"&gt;tamerj1&lt;/a&gt;&amp;nbsp;, works like a charm!&lt;/P&gt;</description>
      <pubDate>Thu, 15 Sep 2022 21:30:46 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Group-and-aggregate-by-calculated-column/m-p/2771678#M86309</guid>
      <dc:creator>csaba09</dc:creator>
      <dc:date>2022-09-15T21:30:46Z</dc:date>
    </item>
    <item>
      <title>Re: Group and aggregate by calculated column</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Group-and-aggregate-by-calculated-column/m-p/2771694#M86311</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="353745" data-lia-user-login="tackytechtom" class="lia-mention lia-mention-user"&gt;tackytechtom&lt;/a&gt;&amp;nbsp;, the query works well! And as you mentioned in your other reply of course it's probably best to include the extra column in the model, spot on! Only this time I wanted to see if we can do the magic already when DAX querying the SSAS cube because with the real data I'm working with this will make a BIG difference in the row number that arrives to the Query Editor from the cube &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Sep 2022 21:38:57 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Group-and-aggregate-by-calculated-column/m-p/2771694#M86311</guid>
      <dc:creator>csaba09</dc:creator>
      <dc:date>2022-09-15T21:38:57Z</dc:date>
    </item>
    <item>
      <title>Re: Group and aggregate by calculated column</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Group-and-aggregate-by-calculated-column/m-p/2772140#M86332</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="442565" data-lia-user-login="csaba09" class="lia-mention lia-mention-user"&gt;csaba09&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;Actually there is a much simpler solution by defining a new column&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;DEFINE
    COLUMN dim_product[fruit_or_not] =
        IF ( dim_product[product_type] = "fruit", "fruit", "non_fruit" )

EVALUATE
SUMMARIZECOLUMNS (
    dim_country[country_name],
    dim_productt[fruit_or_not],
    "sales", SUM ( fact_sales[sales] )
)&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 16 Sep 2022 03:38:33 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Group-and-aggregate-by-calculated-column/m-p/2772140#M86332</guid>
      <dc:creator>tamerj1</dc:creator>
      <dc:date>2022-09-16T03:38:33Z</dc:date>
    </item>
    <item>
      <title>Re: Group and aggregate by calculated column</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Group-and-aggregate-by-calculated-column/m-p/2772575#M86366</link>
      <description>&lt;P&gt;Now this feels a lot cleaner&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="317289" data-lia-user-login="tamerj1" class="lia-mention lia-mention-user"&gt;tamerj1&lt;/a&gt;&amp;nbsp;code-wise and also because we only summarize/group once instead of twice. Many thanks, this is super!&lt;/P&gt;</description>
      <pubDate>Fri, 16 Sep 2022 07:13:35 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Group-and-aggregate-by-calculated-column/m-p/2772575#M86366</guid>
      <dc:creator>csaba09</dc:creator>
      <dc:date>2022-09-16T07:13:35Z</dc:date>
    </item>
  </channel>
</rss>

