<?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 Need to return the attribute for the min date of current filter context in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Need-to-return-the-attribute-for-the-min-date-of-current-filter/m-p/681965#M436</link>
    <description>&lt;P&gt;I have a &lt;STRONG&gt;Date&lt;/STRONG&gt; table, a &lt;STRONG&gt;Customer&lt;/STRONG&gt; table and a &lt;STRONG&gt;Sales&lt;/STRONG&gt; table. Date and Customer are one-to-many to Sales based on Calendar Date and Customer ID. I need to build a measure that counts distinct customer IDs based on a Sales attribute (segment). However, each Customer ID must only be counted in one segment - the segment that goes with their first/earliest Sales Date based on the current date filter context. I have tried and tried to no avail. I thought the code below might work, but my SSAS Tabular model doesn't support the TREATAS function and I wasn't sure how to adapt. And I may be completely off track. Any help is much appreciated!!!&lt;/P&gt;&lt;PRE&gt;MarcoTreatAsAlgorithm = 
&lt;FONT color="#0000FF"&gt;CALCULATE&lt;/FONT&gt; (&lt;BR /&gt;    &lt;FONT color="#339966"&gt;// Computes table containing last transaction for each&amp;nbsp;Customer_id (or whatever ID of interest)&lt;BR /&gt;    // in the current filter context:&lt;/FONT&gt;
   &lt;FONT color="#33CCCC"&gt; VAR&lt;/FONT&gt; &lt;FONT color="#33CCCC"&gt;LastCustomerTransactionDate&lt;/FONT&gt; =
        &lt;FONT color="#0000FF"&gt;ADDCOLUMNS&lt;/FONT&gt; (
            &lt;FONT color="#0000FF"&gt;VALUES&lt;/FONT&gt; ( CustomerTransactions[Customer_id] ),
            &lt;FONT color="#FF0000"&gt;"MaxFeatureTransNum"&lt;/FONT&gt;, &lt;FONT color="#0000FF"&gt;CALCULATE&lt;/FONT&gt; ( &lt;FONT color="#0000FF"&gt;MAX&lt;/FONT&gt; ( CustomerTransactions[Transaction_num] ) )
        )&lt;FONT color="#339966"&gt;&lt;BR /&gt;    // Use the above VAR to establish filter context using TREATAS and computes&lt;BR /&gt;    // the last activity status for the MAX(Transaction_num]) in the same date context:&lt;/FONT&gt;
    &lt;FONT color="#33CCCC"&gt;VAR&lt;/FONT&gt; &lt;FONT color="#33CCCC"&gt;LastCustomerTransactionState&lt;/FONT&gt; =
       &lt;FONT color="#0000FF"&gt; CALCULATETABLE&lt;/FONT&gt; (
            &lt;FONT color="#0000FF"&gt;ADDCOLUMNS&lt;/FONT&gt; (
                &lt;FONT color="#0000FF"&gt;VALUES&lt;/FONT&gt; ( CustomerTransactions[Customer_id] ),
               &lt;FONT color="#FF0000"&gt; "MaxCustomerActivityCode_id"&lt;/FONT&gt;, &lt;FONT color="#0000FF"&gt;CALCULATE&lt;/FONT&gt; ( &lt;FONT color="#0000FF"&gt;MAX&lt;/FONT&gt; ( CustomerTransactions[CustomerActivityCode_id] ) )
            ),
            &lt;FONT color="#0000FF"&gt;TREATAS&lt;/FONT&gt; (
                &lt;FONT color="#33CCCC"&gt;LastCustomerTransactionDate&lt;/FONT&gt;,
                CustomerTransactions[Customer_id],
                CustomerTransactions[Transaction_num]
            )
        )&lt;BR /&gt;    &lt;FONT color="#339966"&gt;// Count records from the above table where the last status &lt;BR /&gt;    // within the current date context is 2 (change "2" to whatever filter value you need):&lt;/FONT&gt;
    &lt;FONT color="#33CCCC"&gt;VAR Result&lt;/FONT&gt; =
        &lt;FONT color="#0000FF"&gt;COUNTROWS&lt;/FONT&gt; (
           &lt;FONT color="#0000FF"&gt; FILTER&lt;/FONT&gt; ( LastCustomerTransactionState, [MaxCustomerActivityCode_id] = 2 )
        )
    &lt;FONT color="#33CCCC"&gt;RETURN&lt;/FONT&gt;
       &lt;FONT color="#33CCCC"&gt; Result&lt;/FONT&gt;,
   &lt;FONT color="#0000FF"&gt; USERELATIONSHIP&lt;/FONT&gt; ( PBIDate[Date], CustomerTransactions[TransactionAddedDate] )
)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 02 May 2019 00:15:09 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2019-05-02T00:15:09Z</dc:date>
    <item>
      <title>Need to return the attribute for the min date of current filter context</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Need-to-return-the-attribute-for-the-min-date-of-current-filter/m-p/681965#M436</link>
      <description>&lt;P&gt;I have a &lt;STRONG&gt;Date&lt;/STRONG&gt; table, a &lt;STRONG&gt;Customer&lt;/STRONG&gt; table and a &lt;STRONG&gt;Sales&lt;/STRONG&gt; table. Date and Customer are one-to-many to Sales based on Calendar Date and Customer ID. I need to build a measure that counts distinct customer IDs based on a Sales attribute (segment). However, each Customer ID must only be counted in one segment - the segment that goes with their first/earliest Sales Date based on the current date filter context. I have tried and tried to no avail. I thought the code below might work, but my SSAS Tabular model doesn't support the TREATAS function and I wasn't sure how to adapt. And I may be completely off track. Any help is much appreciated!!!&lt;/P&gt;&lt;PRE&gt;MarcoTreatAsAlgorithm = 
&lt;FONT color="#0000FF"&gt;CALCULATE&lt;/FONT&gt; (&lt;BR /&gt;    &lt;FONT color="#339966"&gt;// Computes table containing last transaction for each&amp;nbsp;Customer_id (or whatever ID of interest)&lt;BR /&gt;    // in the current filter context:&lt;/FONT&gt;
   &lt;FONT color="#33CCCC"&gt; VAR&lt;/FONT&gt; &lt;FONT color="#33CCCC"&gt;LastCustomerTransactionDate&lt;/FONT&gt; =
        &lt;FONT color="#0000FF"&gt;ADDCOLUMNS&lt;/FONT&gt; (
            &lt;FONT color="#0000FF"&gt;VALUES&lt;/FONT&gt; ( CustomerTransactions[Customer_id] ),
            &lt;FONT color="#FF0000"&gt;"MaxFeatureTransNum"&lt;/FONT&gt;, &lt;FONT color="#0000FF"&gt;CALCULATE&lt;/FONT&gt; ( &lt;FONT color="#0000FF"&gt;MAX&lt;/FONT&gt; ( CustomerTransactions[Transaction_num] ) )
        )&lt;FONT color="#339966"&gt;&lt;BR /&gt;    // Use the above VAR to establish filter context using TREATAS and computes&lt;BR /&gt;    // the last activity status for the MAX(Transaction_num]) in the same date context:&lt;/FONT&gt;
    &lt;FONT color="#33CCCC"&gt;VAR&lt;/FONT&gt; &lt;FONT color="#33CCCC"&gt;LastCustomerTransactionState&lt;/FONT&gt; =
       &lt;FONT color="#0000FF"&gt; CALCULATETABLE&lt;/FONT&gt; (
            &lt;FONT color="#0000FF"&gt;ADDCOLUMNS&lt;/FONT&gt; (
                &lt;FONT color="#0000FF"&gt;VALUES&lt;/FONT&gt; ( CustomerTransactions[Customer_id] ),
               &lt;FONT color="#FF0000"&gt; "MaxCustomerActivityCode_id"&lt;/FONT&gt;, &lt;FONT color="#0000FF"&gt;CALCULATE&lt;/FONT&gt; ( &lt;FONT color="#0000FF"&gt;MAX&lt;/FONT&gt; ( CustomerTransactions[CustomerActivityCode_id] ) )
            ),
            &lt;FONT color="#0000FF"&gt;TREATAS&lt;/FONT&gt; (
                &lt;FONT color="#33CCCC"&gt;LastCustomerTransactionDate&lt;/FONT&gt;,
                CustomerTransactions[Customer_id],
                CustomerTransactions[Transaction_num]
            )
        )&lt;BR /&gt;    &lt;FONT color="#339966"&gt;// Count records from the above table where the last status &lt;BR /&gt;    // within the current date context is 2 (change "2" to whatever filter value you need):&lt;/FONT&gt;
    &lt;FONT color="#33CCCC"&gt;VAR Result&lt;/FONT&gt; =
        &lt;FONT color="#0000FF"&gt;COUNTROWS&lt;/FONT&gt; (
           &lt;FONT color="#0000FF"&gt; FILTER&lt;/FONT&gt; ( LastCustomerTransactionState, [MaxCustomerActivityCode_id] = 2 )
        )
    &lt;FONT color="#33CCCC"&gt;RETURN&lt;/FONT&gt;
       &lt;FONT color="#33CCCC"&gt; Result&lt;/FONT&gt;,
   &lt;FONT color="#0000FF"&gt; USERELATIONSHIP&lt;/FONT&gt; ( PBIDate[Date], CustomerTransactions[TransactionAddedDate] )
)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 02 May 2019 00:15:09 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Need-to-return-the-attribute-for-the-min-date-of-current-filter/m-p/681965#M436</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2019-05-02T00:15:09Z</dc:date>
    </item>
    <item>
      <title>Re: Need to return the attribute for the min date of current filter context</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Need-to-return-the-attribute-for-the-min-date-of-current-filter/m-p/3677957#M142799</link>
      <description>&lt;P&gt;To address your requirement of counting distinct customer IDs in a specific segment based on their first/earliest Sales Date within the current date filter context, you need to create a measure in Power BI that accomplishes a few key tasks: identifying the earliest sales date for each customer, determining the segment for that earliest date, and then counting distinct customers per segment. The solution involves a combination of DAX functions and careful consideration of the relationships and filter contexts in your data model.&lt;/P&gt;
&lt;P&gt;Since your SSAS Tabular model does not support the TREATAS function, you'll need to use an alternative approach. Here's a revised DAX measure that should help you achieve your objective:&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;CustomerSegmentCount = &lt;BR /&gt;VAR EarliestSalesPerCustomer = &lt;BR /&gt;CALCULATETABLE(&lt;BR /&gt;SUMMARIZE(&lt;BR /&gt;Sales,&lt;BR /&gt;Sales[Customer ID],&lt;BR /&gt;"EarliestSaleDate", MIN(Sales[Sales Date])&lt;BR /&gt;),&lt;BR /&gt;REMOVEFILTERS(Date)&lt;BR /&gt;)&lt;BR /&gt;VAR CustomerSegmentAtEarliestSale = &lt;BR /&gt;SUMMARIZE(&lt;BR /&gt;EarliestSalesPerCustomer,&lt;BR /&gt;Sales[Customer ID],&lt;BR /&gt;"Segment", CALCULATE(&lt;BR /&gt;MAX(Sales[Segment]),&lt;BR /&gt;USERELATIONSHIP(Date[Calendar Date], Sales[Sales Date]),&lt;BR /&gt;Sales[Sales Date] = EARLIER(EarliestSalesPerCustomer[EarliestSaleDate])&lt;BR /&gt;)&lt;BR /&gt;)&lt;BR /&gt;RETURN&lt;BR /&gt;COUNTROWS(CustomerSegmentAtEarliestSale)&lt;/P&gt;</description>
      <pubDate>Sat, 03 Feb 2024 17:33:32 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Need-to-return-the-attribute-for-the-min-date-of-current-filter/m-p/3677957#M142799</guid>
      <dc:creator>technolog</dc:creator>
      <dc:date>2024-02-03T17:33:32Z</dc:date>
    </item>
  </channel>
</rss>

