<?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: Sum of last N data - grouped by category in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Sum-of-last-N-data-grouped-by-category/m-p/3048444#M104908</link>
    <description>&lt;P&gt;I revise the expected result..&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 28 Jan 2023 16:54:29 GMT</pubDate>
    <dc:creator>amuljono</dc:creator>
    <dc:date>2023-01-28T16:54:29Z</dc:date>
    <item>
      <title>Sum of last N data - grouped by category</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Sum-of-last-N-data-grouped-by-category/m-p/3048254#M104887</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I need to sum the last 5 years data, grouped by several columns. Data retrieved from 2 tables (forecast and historical) which has same data structures but different date range.&amp;nbsp;&lt;SPAN&gt;Forecast table need to get historical data from Historical table.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Below are the data,&amp;nbsp;&lt;/P&gt;&lt;P&gt;Forecast:&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Historical:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;The expected result (updated):&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;i.e. in 2022 for Asset-1, Category 1Y and Type X1, the calculation will be:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;2022 (type X1) sales:&amp;nbsp;45 + 2021 sales (type H):&amp;nbsp;20&amp;nbsp;+ 2020 sales (type H): 15 + 2019 sales (type H): 10&amp;nbsp;+ 2018 sales (type H): 5. Total summation is 95&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;in 2023 for Asset-1, Category 1Y and Type X1, the calculation will be:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;2023 (type X1) sales: 50 + 2022 sales (type X1): 45 + 2021 sales (type H): 20 + 2020 sales (type H): 15 + 2019 sales (type H): 10. Total summation is 140&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;in 2022 for Asset-1, Category 1Y and Type X2, the calculation will be:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;2022 (type X2) sales: 65 +&amp;nbsp;2021 sales (type H):&amp;nbsp;20&amp;nbsp;+ 2020 sales (type H): 15 + 2019 sales (type H): 10&amp;nbsp;+ 2018 sales (type H): 5. Total summation is 115&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;in 2023 for Asset-1, Category 1Y and Type X2, the calculation will be:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;2023 (type X2) sales: 70+ 2022 sales (type X1): 65&amp;nbsp;+ 2021 sales (type H): 20 + 2020 sales (type H): 15 + 2019 sales (type H): 10. Total summation is 180&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;So basically only Asset, Category and Year will be considered in the Historical table. For Forecast table, Asset, Category, Year and Type should be considered.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I append the 2 tables then create new colum:&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Last5Y = SUMMARIZE(&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;TOPN(5, GROUPBY(FILTER('Sales profile', 'Sales profile'[Year]-4 &amp;amp;&amp;amp; 'Sales profile'[Year]&amp;lt;='Sales profile'[Year]),'Sales profile'[Asset], 'Sales profile'[Category]),'Sales profile'[Year],ASC), "sum", SUM('Sales profile'[Sales])&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;However, the result is not as per expected.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;img /&gt;&lt;P&gt;&amp;nbsp;it seems the calculation added all Sales data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Very appreicate any guidance around this.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;thanks&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Sun, 29 Jan 2023 01:24:21 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Sum-of-last-N-data-grouped-by-category/m-p/3048254#M104887</guid>
      <dc:creator>amuljono</dc:creator>
      <dc:date>2023-01-29T01:24:21Z</dc:date>
    </item>
    <item>
      <title>Re: Sum of last N data - grouped by category</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Sum-of-last-N-data-grouped-by-category/m-p/3048391#M104901</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="505956" data-lia-user-login="amuljono" class="lia-mention lia-mention-user"&gt;amuljono&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This calculated column is created in which table? Any relationships between the two tables?&lt;/P&gt;</description>
      <pubDate>Sat, 28 Jan 2023 15:38:07 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Sum-of-last-N-data-grouped-by-category/m-p/3048391#M104901</guid>
      <dc:creator>tamerj1</dc:creator>
      <dc:date>2023-01-28T15:38:07Z</dc:date>
    </item>
    <item>
      <title>Re: Sum of last N data - grouped by category</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Sum-of-last-N-data-grouped-by-category/m-p/3048439#M104906</link>
      <description>&lt;P&gt;&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;&lt;/P&gt;&lt;P&gt;the calculated column is created in the new table created from append queries.&lt;/P&gt;&lt;P&gt;thanks&lt;/P&gt;</description>
      <pubDate>Sat, 28 Jan 2023 16:47:50 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Sum-of-last-N-data-grouped-by-category/m-p/3048439#M104906</guid>
      <dc:creator>amuljono</dc:creator>
      <dc:date>2023-01-28T16:47:50Z</dc:date>
    </item>
    <item>
      <title>Re: Sum of last N data - grouped by category</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Sum-of-last-N-data-grouped-by-category/m-p/3048444#M104908</link>
      <description>&lt;P&gt;I revise the expected result..&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 28 Jan 2023 16:54:29 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Sum-of-last-N-data-grouped-by-category/m-p/3048444#M104908</guid>
      <dc:creator>amuljono</dc:creator>
      <dc:date>2023-01-28T16:54:29Z</dc:date>
    </item>
    <item>
      <title>Re: Sum of last N data - grouped by category</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Sum-of-last-N-data-grouped-by-category/m-p/3048896#M104926</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="505956" data-lia-user-login="amuljono" class="lia-mention lia-mention-user"&gt;amuljono&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;Please refer to attached sample file with the solution&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;Updated = 
ADDCOLUMNS ( 
    Forecast,
    "Last 5 Years Sales",
    VAR T1 = 
        SELECTCOLUMNS ( 
            CALCULATETABLE ( 
                Forecast, 
                ALLEXCEPT ( Forecast, Forecast[Asset], Forecast[Category], Forecast[Type] )
            ),
            "Year", Forecast[Year],
            "Sales", Forecast[Sales]
        )
    VAR T2 = 
        SELECTCOLUMNS ( 
            FILTER ( 
                Historical,
                Historical[Asset] = Forecast[Asset]
                    &amp;amp;&amp;amp; Historical[Category] = Forecast[Category]
            ),
            "Year", Historical[Year],
            "Sales", Historical[Sales]
        )
    VAR T3 = FILTER ( UNION ( T1, T2 ), [Year] &amp;lt;= EARLIER ( [Year] ) )
    VAR T4 = TOPN ( 5, T3, [Year] )
    RETURN
        SUMX ( T4, [Sales] )
)&lt;/LI-CODE&gt;
&lt;P&gt;2nd Option&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;Updated 2 = 
ADDCOLUMNS ( 
    Forecast,
    "Last 5 Years Sales",
    VAR T1 = 
        CALCULATETABLE ( 
            Forecast, 
            ALLEXCEPT ( Forecast, Forecast[Asset], Forecast[Category], Forecast[Type] )
        )
    VAR T2 = 
        FILTER ( 
            Historical,
            Historical[Asset] = Forecast[Asset]
                &amp;amp;&amp;amp; Historical[Category] = Forecast[Category]
        )
    VAR T3 = FILTER ( UNION ( T1, T2 ), [Year] &amp;lt;= EARLIER ( [Year] ) )
    VAR T4 = TOPN ( 5, T3, [Year] )
    RETURN
        SUMX ( T4, [Sales] )
)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 29 Jan 2023 12:08:28 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Sum-of-last-N-data-grouped-by-category/m-p/3048896#M104926</guid>
      <dc:creator>tamerj1</dc:creator>
      <dc:date>2023-01-29T12:08:28Z</dc:date>
    </item>
    <item>
      <title>Re: Sum of last N data - grouped by category</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Sum-of-last-N-data-grouped-by-category/m-p/3054904#M105343</link>
      <description>&lt;P&gt;&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;- really appreciate your help. kudos&lt;/P&gt;</description>
      <pubDate>Wed, 01 Feb 2023 10:39:27 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Sum-of-last-N-data-grouped-by-category/m-p/3054904#M105343</guid>
      <dc:creator>amuljono</dc:creator>
      <dc:date>2023-02-01T10:39:27Z</dc:date>
    </item>
  </channel>
</rss>

