<?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 by with sum and with average in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Group-by-with-sum-and-with-average/m-p/3474290#M132706</link>
    <description>&lt;P&gt;What you're trying to achieve in Power BI involves grouping rows in your transactional tax dataset based on the "transaction_id," and for certain columns, you want to calculate the average for repeating values, sum non-repeating values, and merge text values.&lt;/P&gt;&lt;P&gt;You can accomplish this using DAX measures and Power Query. Here's a step-by-step approach:&lt;/P&gt;&lt;P&gt;**Step 1: Power Query Transformation**&lt;/P&gt;&lt;P&gt;1. Create a unique identifier for each unique combination of "transaction_id" and "Tax_name" because you want to aggregate rows based on "Tax_name" but retain "transaction_id."&lt;BR /&gt;2. In Power Query, add a custom column that concatenates the "Tax_name" values using a delimiter (e.g., a semicolon) for each unique "transaction_id."&lt;/P&gt;&lt;P&gt;Your Power Query code might look something like this (assuming your dataset is named "TaxData"):&lt;/P&gt;&lt;P&gt;```M&lt;BR /&gt;let&lt;BR /&gt;Source = TaxData,&lt;BR /&gt;// Create a unique identifier for each combination of "transaction_id" and "Tax_name"&lt;BR /&gt;AddCustom = Table.AddColumn(Source, "TransactionTaxKey", each [transaction_id] &amp;amp; " - " &amp;amp; [Tax_name]),&lt;BR /&gt;// Group by "TransactionTaxKey" and aggregate other columns&lt;BR /&gt;Grouped = Table.Group(AddCustom, {"TransactionTaxKey"}, {&lt;BR /&gt;{"Total", each List.Average([Total]), type number},&lt;BR /&gt;{"Subtotal", each List.Average([Subtotal]), type number},&lt;BR /&gt;{"Tax_amount", each List.Sum([Tax_amount]), type number},&lt;BR /&gt;{"jurisdiction_level", each [jurisdiction_level]{0}},&lt;BR /&gt;{"jurisdiction_name", each [jurisdiction_name]{0}},&lt;BR /&gt;{"Country_code", each [Country_code]{0}},&lt;BR /&gt;{"state_code", each [state_code]{0}},&lt;BR /&gt;{"Tax_rate", each [Tax_rate]{0}},&lt;BR /&gt;{"Tax_name", each Text.Combine([Tax_name], ";")}&lt;BR /&gt;}),&lt;BR /&gt;// Remove temporary column and keep the original columns&lt;BR /&gt;Expanded = Table.ExpandTableColumn(Grouped, "TransactionTaxKey", {"Total", "Subtotal", "Tax_amount", "jurisdiction_level", "jurisdiction_name", "Country_code", "state_code", "Tax_rate", "Tax_name"})&lt;BR /&gt;in&lt;BR /&gt;Expanded&lt;BR /&gt;```&lt;/P&gt;&lt;P&gt;**Step 2: Create Measures for Averages**&lt;/P&gt;&lt;P&gt;1. Create measures that calculate the average of "Total" and "Subtotal" based on the new grouping.&lt;/P&gt;&lt;P&gt;For "Average Total":&lt;BR /&gt;```DAX&lt;BR /&gt;Average Total = AVERAGE(TaxData[Total])&lt;BR /&gt;```&lt;/P&gt;&lt;P&gt;For "Average Subtotal":&lt;BR /&gt;```DAX&lt;BR /&gt;Average Subtotal = AVERAGE(TaxData[Subtotal])&lt;BR /&gt;```&lt;/P&gt;&lt;P&gt;**Step 3: Create Visualizations**&lt;/P&gt;&lt;P&gt;Now, you can create visuals in Power BI using the measures "Average Total" and "Average Subtotal," as well as the columns you transformed in Power Query.&lt;/P&gt;&lt;P&gt;This approach should give you the desired result where you have grouped rows based on "transaction_id" and performed the necessary calculations for the "Total" and "Subtotal" columns. The text values in the other columns are concatenated based on the "Tax_name" for each unique "transaction_id."&lt;/P&gt;</description>
    <pubDate>Thu, 12 Oct 2023 20:33:49 GMT</pubDate>
    <dc:creator>Alef_Ricardo_</dc:creator>
    <dc:date>2023-10-12T20:33:49Z</dc:date>
    <item>
      <title>Group by with sum and with average</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Group-by-with-sum-and-with-average/m-p/3474166#M132697</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm working on a transactional tax dataset that has the following situations all together:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;- when only one tax applies - there is only one row for that transaction&lt;/P&gt;&lt;P&gt;- when multiple taxes apply - there are many transactions (with the same transaction_id), one for each tax&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As to the columns, when multiple taxes apply, some columns will repeat the values (e.g. total) and some columns will contain unique values (e.g. tax charged).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What i'm trying to do is to group the repeated transaction_id with an average of the columns in which the amounts repeat and with a sum of the amounts that do not repeat&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;transaction_id&lt;/TD&gt;&lt;TD&gt;Total&lt;/TD&gt;&lt;TD&gt;Subtotal&lt;/TD&gt;&lt;TD&gt;Tax_amount&lt;/TD&gt;&lt;TD&gt;jurisdiction_level&lt;/TD&gt;&lt;TD&gt;jurisdiction_name&lt;/TD&gt;&lt;TD&gt;Country_code&lt;/TD&gt;&lt;TD&gt;state_code&lt;/TD&gt;&lt;TD&gt;Tax_rate&lt;/TD&gt;&lt;TD&gt;Tax_name&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;111&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;85&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;country&lt;/TD&gt;&lt;TD&gt;Canada&lt;/TD&gt;&lt;TD&gt;CA&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;10%&lt;/TD&gt;&lt;TD&gt;GST/HST&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;111&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;85&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;state&lt;/TD&gt;&lt;TD&gt;British Columbia&lt;/TD&gt;&lt;TD&gt;CA&lt;/TD&gt;&lt;TD&gt;BC&lt;/TD&gt;&lt;TD&gt;5%&lt;/TD&gt;&lt;TD&gt;PST&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;222&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;90&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;country&lt;/TD&gt;&lt;TD&gt;Canada&lt;/TD&gt;&lt;TD&gt;CA&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;10%&lt;/TD&gt;&lt;TD&gt;GST/HST&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;333&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;TD&gt;180&lt;/TD&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;TD&gt;country&lt;/TD&gt;&lt;TD&gt;Canada&lt;/TD&gt;&lt;TD&gt;CA&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;10%&lt;/TD&gt;&lt;TD&gt;GST/HST&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;The result I was expecting would be something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;transaction_id&lt;/TD&gt;&lt;TD&gt;Total&lt;/TD&gt;&lt;TD&gt;Subtotal&lt;/TD&gt;&lt;TD&gt;Tax_amount&lt;/TD&gt;&lt;TD&gt;jurisdiction_level&lt;/TD&gt;&lt;TD&gt;jurisdiction_name&lt;/TD&gt;&lt;TD&gt;Country_code&lt;/TD&gt;&lt;TD&gt;state_code&lt;/TD&gt;&lt;TD&gt;Tax_rate&lt;/TD&gt;&lt;TD&gt;Tax_name&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;111&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;85&lt;/TD&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;TD&gt;country;state&lt;/TD&gt;&lt;TD&gt;Canada;British Columbia&lt;/TD&gt;&lt;TD&gt;CA&lt;/TD&gt;&lt;TD&gt;BC&lt;/TD&gt;&lt;TD&gt;10%;5%&lt;/TD&gt;&lt;TD&gt;GST/HST;PST&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;222&lt;/TD&gt;&lt;TD&gt;100&lt;/TD&gt;&lt;TD&gt;90&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;country&lt;/TD&gt;&lt;TD&gt;Canada&lt;/TD&gt;&lt;TD&gt;CA&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;10%&lt;/TD&gt;&lt;TD&gt;GST/HST&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;333&lt;/TD&gt;&lt;TD&gt;200&lt;/TD&gt;&lt;TD&gt;180&lt;/TD&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;TD&gt;country&lt;/TD&gt;&lt;TD&gt;Canada&lt;/TD&gt;&lt;TD&gt;CA&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;10%&lt;/TD&gt;&lt;TD&gt;GST/HST&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Note that for the column "total" the group by "transaction_id"&amp;nbsp; only calculated an average, while for the colum "tax_amount" it was calculated a sum. For the other fields there was a merge of the text.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is this something doable in pbi? I have tried many different things and still haven't figured out a way to do this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Oct 2023 19:24:07 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Group-by-with-sum-and-with-average/m-p/3474166#M132697</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2023-10-12T19:24:07Z</dc:date>
    </item>
    <item>
      <title>Re: Group by with sum and with average</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Group-by-with-sum-and-with-average/m-p/3474290#M132706</link>
      <description>&lt;P&gt;What you're trying to achieve in Power BI involves grouping rows in your transactional tax dataset based on the "transaction_id," and for certain columns, you want to calculate the average for repeating values, sum non-repeating values, and merge text values.&lt;/P&gt;&lt;P&gt;You can accomplish this using DAX measures and Power Query. Here's a step-by-step approach:&lt;/P&gt;&lt;P&gt;**Step 1: Power Query Transformation**&lt;/P&gt;&lt;P&gt;1. Create a unique identifier for each unique combination of "transaction_id" and "Tax_name" because you want to aggregate rows based on "Tax_name" but retain "transaction_id."&lt;BR /&gt;2. In Power Query, add a custom column that concatenates the "Tax_name" values using a delimiter (e.g., a semicolon) for each unique "transaction_id."&lt;/P&gt;&lt;P&gt;Your Power Query code might look something like this (assuming your dataset is named "TaxData"):&lt;/P&gt;&lt;P&gt;```M&lt;BR /&gt;let&lt;BR /&gt;Source = TaxData,&lt;BR /&gt;// Create a unique identifier for each combination of "transaction_id" and "Tax_name"&lt;BR /&gt;AddCustom = Table.AddColumn(Source, "TransactionTaxKey", each [transaction_id] &amp;amp; " - " &amp;amp; [Tax_name]),&lt;BR /&gt;// Group by "TransactionTaxKey" and aggregate other columns&lt;BR /&gt;Grouped = Table.Group(AddCustom, {"TransactionTaxKey"}, {&lt;BR /&gt;{"Total", each List.Average([Total]), type number},&lt;BR /&gt;{"Subtotal", each List.Average([Subtotal]), type number},&lt;BR /&gt;{"Tax_amount", each List.Sum([Tax_amount]), type number},&lt;BR /&gt;{"jurisdiction_level", each [jurisdiction_level]{0}},&lt;BR /&gt;{"jurisdiction_name", each [jurisdiction_name]{0}},&lt;BR /&gt;{"Country_code", each [Country_code]{0}},&lt;BR /&gt;{"state_code", each [state_code]{0}},&lt;BR /&gt;{"Tax_rate", each [Tax_rate]{0}},&lt;BR /&gt;{"Tax_name", each Text.Combine([Tax_name], ";")}&lt;BR /&gt;}),&lt;BR /&gt;// Remove temporary column and keep the original columns&lt;BR /&gt;Expanded = Table.ExpandTableColumn(Grouped, "TransactionTaxKey", {"Total", "Subtotal", "Tax_amount", "jurisdiction_level", "jurisdiction_name", "Country_code", "state_code", "Tax_rate", "Tax_name"})&lt;BR /&gt;in&lt;BR /&gt;Expanded&lt;BR /&gt;```&lt;/P&gt;&lt;P&gt;**Step 2: Create Measures for Averages**&lt;/P&gt;&lt;P&gt;1. Create measures that calculate the average of "Total" and "Subtotal" based on the new grouping.&lt;/P&gt;&lt;P&gt;For "Average Total":&lt;BR /&gt;```DAX&lt;BR /&gt;Average Total = AVERAGE(TaxData[Total])&lt;BR /&gt;```&lt;/P&gt;&lt;P&gt;For "Average Subtotal":&lt;BR /&gt;```DAX&lt;BR /&gt;Average Subtotal = AVERAGE(TaxData[Subtotal])&lt;BR /&gt;```&lt;/P&gt;&lt;P&gt;**Step 3: Create Visualizations**&lt;/P&gt;&lt;P&gt;Now, you can create visuals in Power BI using the measures "Average Total" and "Average Subtotal," as well as the columns you transformed in Power Query.&lt;/P&gt;&lt;P&gt;This approach should give you the desired result where you have grouped rows based on "transaction_id" and performed the necessary calculations for the "Total" and "Subtotal" columns. The text values in the other columns are concatenated based on the "Tax_name" for each unique "transaction_id."&lt;/P&gt;</description>
      <pubDate>Thu, 12 Oct 2023 20:33:49 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Group-by-with-sum-and-with-average/m-p/3474290#M132706</guid>
      <dc:creator>Alef_Ricardo_</dc:creator>
      <dc:date>2023-10-12T20:33:49Z</dc:date>
    </item>
    <item>
      <title>Re: Group by with sum and with average</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Group-by-with-sum-and-with-average/m-p/3476149#M132789</link>
      <description>&lt;P&gt;Thank you Alef, where do I add this query? Sorry for the newbie question - I have tried to add this to advanced editor but it didn't work &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Oct 2023 17:57:24 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Group-by-with-sum-and-with-average/m-p/3476149#M132789</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2023-10-13T17:57:24Z</dc:date>
    </item>
    <item>
      <title>Re: Group by with sum and with average</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Group-by-with-sum-and-with-average/m-p/3478239#M132927</link>
      <description>&lt;P&gt;Hi&amp;nbsp;Anonymous&lt;/LI-USER&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;According to your description, here are my steps you can follow as a solution.&lt;/P&gt;
&lt;P&gt;(1) My test data is the same as yours.&lt;/P&gt;
&lt;P&gt;(2) We can create a table.&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Table 2 = SUMMARIZE('Table',
'Table'[transaction_id],
"Total", AVERAGEX(FILTER(ALL('Table'),'Table'[transaction_id]=MAX('Table'[transaction_id])),'Table'[Total]),
"Subtotal",AVERAGEX(FILTER(ALL('Table'),'Table'[transaction_id]=MAX('Table'[transaction_id])),'Table'[Subtotal]),
"Tax_amount",CALCULATE(SUM('Table'[Tax_amount]),FILTER(ALL('Table'),'Table'[transaction_id]=MAX('Table'[transaction_id]))),
"jurisdiction_level", CONCATENATEX(SUMMARIZE('Table',[transaction_id],'Table'[jurisdiction_level]),[jurisdiction_level],";"),
"jurisdiction_name" ,CONCATENATEX(SUMMARIZE('Table',[transaction_id],'Table'[jurisdiction_name]),[jurisdiction_name],";"),
"Country_code" , CONCATENATEX(SUMMARIZE('Table',[transaction_id],[Country_code]),[Country_code],";"),
"state_code" , CONCATENATEX(SUMMARIZE('Table',[transaction_id],[state_code]),[state_code],";"),
"Tax_rate",CONCATENATEX(SUMMARIZE('Table',[transaction_id],[Tax_rate]),FORMAT([Tax_rate],"Percent"),";"),
"Tax_name",CONCATENATEX(SUMMARIZE('Table',[transaction_id],[Tax_name]),[Tax_name],";"))&lt;/LI-CODE&gt;
&lt;P&gt;(3) Then the result is as follows.&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;Best Regards,&lt;/P&gt;
&lt;P&gt;Neeko Tang&lt;/P&gt;
&lt;P&gt;If this post  &lt;STRONG&gt;helps&lt;/STRONG&gt;, then please consider &lt;STRONG&gt;&lt;EM&gt;Accept it as the solution &lt;/EM&gt;&lt;/STRONG&gt; to help the other members find it more quickly.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Oct 2023 08:11:10 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Group-by-with-sum-and-with-average/m-p/3478239#M132927</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2023-10-16T08:11:10Z</dc:date>
    </item>
  </channel>
</rss>

