<?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: Optimize SUMX for Last Date per Customer in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Optimize-SUMX-for-Last-Date-per-Customer/m-p/3244530#M119464</link>
    <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="337990" data-lia-user-login="Menar" class="lia-mention lia-mention-user"&gt;Menar&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Yes, I noticed that but I was in hurry just leaving the office. Sorry, I don't have access to my laptop now to test but I expect this could work&lt;/P&gt;
&lt;P&gt;LastSalesByCustomer TJ =&lt;BR /&gt;VAR CurrentDate =&lt;BR /&gt;MAX ( 'Date Table'[Date] )&lt;BR /&gt;VAR SelectedProducts =&lt;BR /&gt;ALLSELECTED ( 'Product' )&lt;BR /&gt;RETURN&lt;BR /&gt;SUMX (&lt;BR /&gt;SUMMARIZE ( 'Fact Table', Customer[Customer ID], 'Product'[Product ID] ),&lt;BR /&gt;VAR T1 =&lt;BR /&gt;CALCULATETABLE ( 'Fact Table', ALL ( 'Date Table' ) )&lt;BR /&gt;VAR T2 =&lt;BR /&gt;CALCULATETABLE ( 'Fact Table', ALL ( 'Date Table' ), SelectedProducts )&lt;BR /&gt;VAR T3 =&lt;BR /&gt;FILTER ( T2, 'Fact Table'[Transaction Date] &amp;lt; CurrentDate )&lt;BR /&gt;VAR MaxDate =&lt;BR /&gt;MAXX ( T3, 'Fact Table'[Transaction Date] )&lt;BR /&gt;VAR T4 =&lt;BR /&gt;FILTER ( T1, 'Fact Table'[Transaction Date] = MaxDate )&lt;BR /&gt;VAR Result =&lt;BR /&gt;SUMX ( T4, 'Fact Table'[Sales] )&lt;BR /&gt;RETURN&lt;BR /&gt;Result&lt;BR /&gt;)&lt;/P&gt;</description>
    <pubDate>Fri, 19 May 2023 15:12:09 GMT</pubDate>
    <dc:creator>tamerj1</dc:creator>
    <dc:date>2023-05-19T15:12:09Z</dc:date>
    <item>
      <title>Optimize SUMX for Last Date per Customer</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Optimize-SUMX-for-Last-Date-per-Customer/m-p/3244255#M119441</link>
      <description>&lt;P&gt;Hello Everyone,&lt;BR /&gt;&lt;BR /&gt;I have been working on a measure which only shows the sum of 'Sales' per customer but only for their last transaction date in the last 3 months.&lt;BR /&gt;The last 3 months are computed from a user selection in a date slicer.&lt;BR /&gt;&lt;BR /&gt;I have the following code which does work, however i am a bit concerned about performance issues as my fact table will be quit large (about 10 million rows).&lt;BR /&gt;Any tips on how to improve the performance ? I do not know if the nested CALCULATE has a huge impact.&lt;BR /&gt;I have already tried to put variables as much as possible.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;LatestSalesbyCustomer = 
var selecteddate = MAX('Date Table'[Date])

var Last3MonthsDates = DATESINPERIOD(
    'Secondary Date Table'[Date],
    referencedate,
    -3,
    MONTH)

var CustomerInTheLast3Months =
    CALCULATETABLE(
                    VALUES('Fact Table'[Customerid]),
                    REMOVEFILTERS('Date Table'[Date]),
                    Last3MonthsDates,
                    USERELATIONSHIP('Date Table'[Date], 'Secondary Date Table'[Date])
                    )

var result =
SUMX(
CustomerInTheLast3Months,
    CALCULATE(
                CALCULATE(
                            SUM('Fact Table'[Sales]),
                            var latestdate = CALCULATETABLE(
                                                        LASTDATE('Fact Table'[Transaction Date]),
                                                        REMOVEFILTERS('Fact Table'),
                                                        VALUES('Fact Table'[Customerid]),
                                                        Last3MonthsDates)
                            return
                            latestdate)
                        ,
                    REMOVEFILTERS('Date Table'[Date]),
                    Last3MonthsDates,
                    USERELATIONSHIP('Secondary Date Table'[Date],'Date Table'[Date])
            )
)
return result&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;EM&gt;Link to PBI File :&amp;nbsp;&lt;A href="https://www.dropbox.com/scl/fi/sqtgaait9x0psmj7eg72n/Database-sum-sales-by-cust-for-max-date.xlsx?dl=0&amp;amp;rlkey=9vef4v1c89uh6b2elli1978d5" target="_blank"&gt;https://www.dropbox.com/scl/fi/sqtgaait9x0psmj7eg72n/Database-sum-sales-by-cust-for-max-date.xlsx?dl=0&amp;amp;rlkey=9vef4v1c89uh6b2elli1978d5&lt;/A&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;EM&gt;Link to Source Data =&amp;nbsp;&lt;A href="https://www.dropbox.com/s/ugileocfavvvtty/Sales%20for%20last%20date%20per%20customer.pbix?dl=0" target="_blank"&gt;https://www.dropbox.com/s/ugileocfavvvtty/Sales%20for%20last%20date%20per%20customer.pbix?dl=0&lt;/A&gt;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Thanks in advance for any help !&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2023 12:30:48 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Optimize-SUMX-for-Last-Date-per-Customer/m-p/3244255#M119441</guid>
      <dc:creator>Menar</dc:creator>
      <dc:date>2023-05-19T12:30:48Z</dc:date>
    </item>
    <item>
      <title>Re: Optimize SUMX for Last Date per Customer</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Optimize-SUMX-for-Last-Date-per-Customer/m-p/3244321#M119447</link>
      <description>&lt;P&gt;You can try&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Latest Sales = VAR selecteddate = MAX( 'Date Table'[Date] )
VAR Last3MonthsDates =
	DATESINPERIOD(
		'Secondary Date Table'[Date],
		selecteddate,
		-3,
		MONTH
	)
VAR CustomersWithDates =
	CALCULATETABLE(
		ADDCOLUMNS(
			VALUES( 'Fact Table'[Customerid] ),
			"@last sale",
				CALCULATE( MAX( 'Fact Table'[Transaction Date] ) )
		),
		Last3MonthsDates,
		REMOVEFILTERS( 'Date Table' ),
		USERELATIONSHIP( 'Date Table'[Date], 'Secondary Date Table'[Date] )
	)
VAR Result =
	CALCULATE(
		SUM( 'Fact Table'[Sales] ),
		TREATAS(
			CustomersWithDates,
			'Fact Table'[Customerid],
			'Fact Table'[Transaction Date]
		)
	)
RETURN
	Result&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 19 May 2023 13:05:32 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Optimize-SUMX-for-Last-Date-per-Customer/m-p/3244321#M119447</guid>
      <dc:creator>johnt75</dc:creator>
      <dc:date>2023-05-19T13:05:32Z</dc:date>
    </item>
    <item>
      <title>Re: Optimize SUMX for Last Date per Customer</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Optimize-SUMX-for-Last-Date-per-Customer/m-p/3244322#M119448</link>
      <description>&lt;P&gt;Hey man, is it okay to do this in Python via Power Query? This may reduce performance time significant as the calculations are being made before displaying the data in PBI. If so, I can have look this week.&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2023 13:06:00 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Optimize-SUMX-for-Last-Date-per-Customer/m-p/3244322#M119448</guid>
      <dc:creator>milanpasschier3</dc:creator>
      <dc:date>2023-05-19T13:06:00Z</dc:date>
    </item>
    <item>
      <title>Re: Optimize SUMX for Last Date per Customer</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Optimize-SUMX-for-Last-Date-per-Customer/m-p/3244357#M119452</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="337990" data-lia-user-login="Menar" class="lia-mention lia-mention-user"&gt;Menar&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;Please refer to attached sample file amended qith the proposed solution&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;LastSalesByCustomer TJ = 
VAR CurrentDate = 
    MAX ( 'Date Table'[Date] )
VAR T1 = 
    CALCULATETABLE ( 'Fact Table', ALL ( 'Date Table' ) )
VAR T2 = 
    CALCULATETABLE ( 'Fact Table', ALL ( 'Date Table' ), ALLSELECTED ( 'Product' ) )
VAR T3 =
    FILTER ( T2, 'Fact Table'[Transaction Date] &amp;lt; CurrentDate )
VAR MaxDate = 
    MAXX ( T3, 'Fact Table'[Transaction Date] )
VAR T4 =
    FILTER ( T1, 'Fact Table'[Transaction Date] = MaxDate )
VAR Result =
    SUMX ( T4, 'Fact Table'[Sales] )
RETURN 
    Result&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2023 13:23:34 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Optimize-SUMX-for-Last-Date-per-Customer/m-p/3244357#M119452</guid>
      <dc:creator>tamerj1</dc:creator>
      <dc:date>2023-05-19T13:23:34Z</dc:date>
    </item>
    <item>
      <title>Re: Optimize SUMX for Last Date per Customer</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Optimize-SUMX-for-Last-Date-per-Customer/m-p/3244487#M119460</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="240987" data-lia-user-login="johnt75" class="lia-mention lia-mention-user"&gt;johnt75&lt;/a&gt;&amp;nbsp; thanks, I was thinking about addcolumn but your version does not work if I select a date in the date slicer.&lt;BR /&gt;&lt;BR /&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="457057" data-lia-user-login="milanpasschier3" class="lia-mention lia-mention-user"&gt;milanpasschier3&lt;/a&gt;&amp;nbsp;Thanks but I do not want to do it in Python&lt;BR /&gt;&lt;BR /&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;thank you but the total is wrong in your version&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2023 14:30:58 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Optimize-SUMX-for-Last-Date-per-Customer/m-p/3244487#M119460</guid>
      <dc:creator>Menar</dc:creator>
      <dc:date>2023-05-19T14:30:58Z</dc:date>
    </item>
    <item>
      <title>Re: Optimize SUMX for Last Date per Customer</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Optimize-SUMX-for-Last-Date-per-Customer/m-p/3244529#M119463</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="337990" data-lia-user-login="Menar" class="lia-mention lia-mention-user"&gt;Menar&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Yes, I noticed that but I was in hurry just leaving the office. Sorry, I don't have access to my laptop now to test but I expect this could work&lt;/P&gt;
&lt;P&gt;LastSalesByCustomer TJ =&lt;BR /&gt;VAR CurrentDate =&lt;BR /&gt;MAX ( 'Date Table'[Date] )&lt;BR /&gt;VAR SelectedProducts =&lt;BR /&gt;ALLSELECTED ( 'Product' )&lt;BR /&gt;RETURN&lt;BR /&gt;SUMX (&lt;BR /&gt;SUMMARIZE ( 'Fact Table', Customer[Customer ID], 'Product'[Product ID] ),&lt;BR /&gt;VAR T1 =&lt;BR /&gt;CALCULATETABLE ( 'Fact Table', ALL ( 'Date Table' ) )&lt;BR /&gt;VAR T2 =&lt;BR /&gt;CALCULATETABLE ( 'Fact Table', ALL ( 'Date Table' ), SelectedProducts )&lt;BR /&gt;VAR T3 =&lt;BR /&gt;FILTER ( T2, 'Fact Table'[Transaction Date] &amp;lt; CurrentDate )&lt;BR /&gt;VAR MaxDate =&lt;BR /&gt;MAXX ( T3, 'Fact Table'[Transaction Date] )&lt;BR /&gt;VAR T4 =&lt;BR /&gt;FILTER ( T1, 'Fact Table'[Transaction Date] = MaxDate )&lt;BR /&gt;VAR Result =&lt;BR /&gt;SUMX ( T4, 'Fact Table'[Sales] )&lt;BR /&gt;RETURN&lt;BR /&gt;Result&lt;BR /&gt;)&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2023 15:12:08 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Optimize-SUMX-for-Last-Date-per-Customer/m-p/3244529#M119463</guid>
      <dc:creator>tamerj1</dc:creator>
      <dc:date>2023-05-19T15:12:08Z</dc:date>
    </item>
    <item>
      <title>Re: Optimize SUMX for Last Date per Customer</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Optimize-SUMX-for-Last-Date-per-Customer/m-p/3244530#M119464</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="337990" data-lia-user-login="Menar" class="lia-mention lia-mention-user"&gt;Menar&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Yes, I noticed that but I was in hurry just leaving the office. Sorry, I don't have access to my laptop now to test but I expect this could work&lt;/P&gt;
&lt;P&gt;LastSalesByCustomer TJ =&lt;BR /&gt;VAR CurrentDate =&lt;BR /&gt;MAX ( 'Date Table'[Date] )&lt;BR /&gt;VAR SelectedProducts =&lt;BR /&gt;ALLSELECTED ( 'Product' )&lt;BR /&gt;RETURN&lt;BR /&gt;SUMX (&lt;BR /&gt;SUMMARIZE ( 'Fact Table', Customer[Customer ID], 'Product'[Product ID] ),&lt;BR /&gt;VAR T1 =&lt;BR /&gt;CALCULATETABLE ( 'Fact Table', ALL ( 'Date Table' ) )&lt;BR /&gt;VAR T2 =&lt;BR /&gt;CALCULATETABLE ( 'Fact Table', ALL ( 'Date Table' ), SelectedProducts )&lt;BR /&gt;VAR T3 =&lt;BR /&gt;FILTER ( T2, 'Fact Table'[Transaction Date] &amp;lt; CurrentDate )&lt;BR /&gt;VAR MaxDate =&lt;BR /&gt;MAXX ( T3, 'Fact Table'[Transaction Date] )&lt;BR /&gt;VAR T4 =&lt;BR /&gt;FILTER ( T1, 'Fact Table'[Transaction Date] = MaxDate )&lt;BR /&gt;VAR Result =&lt;BR /&gt;SUMX ( T4, 'Fact Table'[Sales] )&lt;BR /&gt;RETURN&lt;BR /&gt;Result&lt;BR /&gt;)&lt;/P&gt;</description>
      <pubDate>Fri, 19 May 2023 15:12:09 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Optimize-SUMX-for-Last-Date-per-Customer/m-p/3244530#M119464</guid>
      <dc:creator>tamerj1</dc:creator>
      <dc:date>2023-05-19T15:12:09Z</dc:date>
    </item>
  </channel>
</rss>

