<?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: Reference same table based on another field in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Reference-same-table-based-on-another-field/m-p/4271755#M169439</link>
    <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="803815" data-lia-user-login="Steve_M" class="lia-mention lia-mention-user"&gt;Steve_M&lt;/a&gt;&amp;nbsp; No Problem.&lt;BR /&gt;Change MAX to SUM in DAX Query and check that again please&lt;/P&gt;</description>
    <pubDate>Wed, 06 Nov 2024 04:11:41 GMT</pubDate>
    <dc:creator>VahidDM</dc:creator>
    <dc:date>2024-11-06T04:11:41Z</dc:date>
    <item>
      <title>Reference same table based on another field</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Reference-same-table-based-on-another-field/m-p/4271517#M169426</link>
      <description>&lt;P&gt;Hi, I am new to DAX, and this is a little beyond me for now.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to write a job costing report in Power BI, but I don't have any access to the data model. I can create measures, but the report is using a Live connection, and I can't make any changes to it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a transaction table (12 million rows) that I need to reference back based on a custom field in the transaction to get the job cost.&amp;nbsp; The transaction has a job number that I group the revenue by an Item ID.&amp;nbsp; The transaction includes a custom field that contains a PO Number that I then need to go back into the table to retrieve the costs.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;sample data&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I used ChatGPT to come up with this which gives a result but is very slow (18 secs) to update the visual.&amp;nbsp; Any idea on how to do this efficiently?&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;Job Cost = 
SUMX(
    SUMMARIZE(
        'Transaction Lines',
        'Transaction lines'[Document Number],
        'Transaction lines'[Item Internal ID],
        "AmountSum",
        CALCULATE(
            ('Transaction Lines'[Amount]),
            FILTER(
                ALL('Transaction Lines'),
                'Transaction Lines'[Document Number] = 
                MAXX(
                    VALUES('Transaction Lines'[Logistics Kitting Job Costing PO#]),
                    RIGHT('Transaction Lines'[Logistics Kitting Job Costing PO#], 18)
                )
                &amp;amp;&amp;amp; 'Transaction Lines'[Item Internal ID] = MAX('Transaction Lines'[Item Internal ID])      
            )
        )
    ),
    [AmountSum]
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;&lt;P&gt;Sample visual&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Steve&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2024 00:44:49 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Reference-same-table-based-on-another-field/m-p/4271517#M169426</guid>
      <dc:creator>Steve_M</dc:creator>
      <dc:date>2024-11-06T00:44:49Z</dc:date>
    </item>
    <item>
      <title>Re: Reference same table based on another field</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Reference-same-table-based-on-another-field/m-p/4271612#M169427</link>
      <description>&lt;P&gt;Hello Steve,&lt;/P&gt;
&lt;P&gt;I understand that you're trying to optimize your DAX measure for calculating the job cost in Power BI, especially since you're working with a large dataset (12 million rows) and have limitations due to the live connection. Let's work through a more efficient way to achieve your goal.&lt;/P&gt;
&lt;H3&gt;&lt;STRONG&gt;Understanding the Requirement&lt;/STRONG&gt;&lt;/H3&gt;
&lt;P&gt;From your description:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;Objective&lt;/STRONG&gt;: Calculate the job cost by referencing the same transaction table based on a custom field (PO Number).&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Data Structure&lt;/STRONG&gt;:
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;Transaction Lines Table&lt;/STRONG&gt;:
&lt;UL&gt;
&lt;LI&gt;&lt;CODE&gt;Document Number&lt;/CODE&gt;: Identifies each transaction.&lt;/LI&gt;
&lt;LI&gt;&lt;CODE&gt;Item Internal ID&lt;/CODE&gt;: The item identifier.&lt;/LI&gt;
&lt;LI&gt;&lt;CODE&gt;Amount&lt;/CODE&gt;: The transaction amount.&lt;/LI&gt;
&lt;LI&gt;&lt;CODE&gt;Logistics Kitting Job Costing PO#&lt;/CODE&gt;: Contains the PO Number, from which you extract the rightmost 18 characters.&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;You need to sum the &lt;CODE&gt;Amount&lt;/CODE&gt; for transactions where:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The &lt;CODE&gt;Document Number&lt;/CODE&gt; matches the PO Number extracted from &lt;CODE&gt;Logistics Kitting Job Costing PO#&lt;/CODE&gt;.&lt;/LI&gt;
&lt;LI&gt;The &lt;CODE&gt;Item Internal ID&lt;/CODE&gt; matches.&lt;/LI&gt;
&lt;/OL&gt;
&lt;H3&gt;&lt;STRONG&gt;Issues with the Current Measure&lt;/STRONG&gt;&lt;/H3&gt;
&lt;P&gt;Your current measure is slow because:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;Use of &lt;CODE&gt;FILTER(ALL('Transaction Lines'), ...)&lt;/CODE&gt;&lt;/STRONG&gt;: This scans the entire table (12 million rows) for each evaluation, which is computationally intensive.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Complex Calculations inside Iterators&lt;/STRONG&gt;: Using &lt;CODE&gt;SUMX&lt;/CODE&gt; over &lt;CODE&gt;SUMMARIZE&lt;/CODE&gt; with nested calculations adds to the processing time.&lt;/LI&gt;
&lt;/UL&gt;
&lt;H3&gt;&lt;STRONG&gt;Optimized Measure&lt;/STRONG&gt;&lt;/H3&gt;
&lt;P&gt;We can optimize the measure by:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;Avoiding Full Table Scans&lt;/STRONG&gt;: Use direct column filters in &lt;CODE&gt;CALCULATE&lt;/CODE&gt; instead of &lt;CODE&gt;FILTER(ALL(...))&lt;/CODE&gt;.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Leveraging Variables&lt;/STRONG&gt;: Store intermediate values to prevent redundant calculations.&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Minimizing the Use of Iterators&lt;/STRONG&gt;: Use measures that can take advantage of query optimizations.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Here's the optimized measure:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="php"&gt;Job Cost =
VAR PoNumber =
RIGHT( MAX( 'Transaction Lines'[Logistics Kitting Job Costing PO#] ), 18 )
VAR ItemID =
MAX( 'Transaction Lines'[Item Internal ID] )
RETURN
IF(
NOT ISBLANK( PoNumber ),
CALCULATE(
SUM( 'Transaction Lines'[Amount] ),
'Transaction Lines'[Document Number] = PoNumber,
'Transaction Lines'[Item Internal ID] = ItemID
),
BLANK()
)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;H3&gt;&lt;STRONG&gt;Tips&lt;/STRONG&gt;&lt;/H3&gt;
&lt;OL&gt;
&lt;LI&gt;
&lt;P&gt;&lt;STRONG&gt;Data Types Consistency&lt;/STRONG&gt;:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Ensure that the data types for &lt;CODE&gt;Document Number&lt;/CODE&gt;, &lt;CODE&gt;Logistics Kitting Job Costing PO#&lt;/CODE&gt;, and &lt;CODE&gt;Item Internal ID&lt;/CODE&gt; are consistent (e.g., all text or all numbers). Mismatched data types can cause performance issues.&lt;/LI&gt;
&lt;LI&gt;If necessary, use data type conversion functions like &lt;CODE&gt;VALUE&lt;/CODE&gt; or &lt;CODE&gt;FORMAT&lt;/CODE&gt;.&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
&lt;LI&gt;
&lt;P&gt;&lt;STRONG&gt;Avoid Using &lt;CODE&gt;RIGHT&lt;/CODE&gt; in Filters if Possible&lt;/STRONG&gt;:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;If the PO Number always has a fixed length, consider pre-processing this in the data source or see if there's a way to access it directly without using &lt;CODE&gt;RIGHT&lt;/CODE&gt;.&lt;/LI&gt;
&lt;LI&gt;Alternatively, you can create a calculated column (if possible) that extracts the PO Number, which would improve performance. However, since you can't modify the data model, this may not be an option.&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
&lt;LI&gt;
&lt;P&gt;&lt;STRONG&gt;Test Performance&lt;/STRONG&gt;:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;After implementing the optimized measure, test the performance to see if it meets your requirements.&lt;/LI&gt;
&lt;LI&gt;Monitor the query performance using tools like DAX Studio or the Performance Analyzer in Power BI to identify any bottlenecks.&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
&lt;LI&gt;
&lt;P&gt;&lt;STRONG&gt;Consider Using &lt;CODE&gt;KEEPFILTERS&lt;/CODE&gt;&lt;/STRONG&gt;:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;If you encounter issues with filter context, you might consider wrapping your filters with &lt;CODE&gt;KEEPFILTERS&lt;/CODE&gt; to ensure they behave as intended.&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
&lt;/OL&gt;
&lt;LI-CODE lang="php"&gt;Job Cost =
VAR PoNumber =
    RIGHT( MAX( 'Transaction Lines'[Logistics Kitting Job Costing PO#] ), 18 )
VAR ItemID =
    MAX( 'Transaction Lines'[Item Internal ID] )
RETURN
IF(
    NOT ISBLANK( PoNumber ),
    CALCULATE(
        SUM( 'Transaction Lines'[Amount] ),
        KEEPFILTERS( 'Transaction Lines'[Document Number] = PoNumber ),
        KEEPFILTERS( 'Transaction Lines'[Item Internal ID] = ItemID )
    ),
    BLANK()
)
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;Live Connection Constraints&lt;/STRONG&gt;: Since you can't modify the data model, options like creating calculated columns are not available.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this post&lt;STRONG&gt; helps&lt;/STRONG&gt;, please consider &lt;STRONG&gt;accepting&lt;/STRONG&gt;&lt;EM&gt;&lt;STRONG&gt;&amp;nbsp;it as the solution&amp;nbsp;&lt;/STRONG&gt;&lt;/EM&gt;to help the other members find it more quickly.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Appreciate your Kudos!!&lt;/STRONG&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://www.linkedin.com/in/vahid-dm/" target="_blank" rel="noopener noreferrer"&gt;LinkedIn&lt;/A&gt;|&lt;A href="https://twitter.com/VahidDMcom" target="_blank" rel="noopener noreferrer"&gt;Twitter&lt;/A&gt;|&lt;A href="https://www.vahiddm.com/" target="_blank" rel="noopener noreferrer"&gt;Blog&amp;nbsp;&lt;/A&gt;|&lt;A href="https://www.youtube.com/@databis" target="_blank" rel="noopener noreferrer"&gt;YouTube&lt;/A&gt;&lt;A href="https://www.youtube.com/@databis" target="_blank" rel="noopener noreferrer"&gt;&amp;nbsp;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2024 02:07:47 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Reference-same-table-based-on-another-field/m-p/4271612#M169427</guid>
      <dc:creator>VahidDM</dc:creator>
      <dc:date>2024-11-06T02:07:47Z</dc:date>
    </item>
    <item>
      <title>Re: Reference same table based on another field</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Reference-same-table-based-on-another-field/m-p/4271660#M169435</link>
      <description>&lt;P&gt;Hi Vahid, thanks for your quick and detailed response. This has saved the time, which is now 12 seconds down from 24. The only issue is that the totals aren't correct.&amp;nbsp; JobCost 2 is a new formula, but the total isn't right, showing 1,700 instead of 7,300.&amp;nbsp; Any ideas?&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2024 02:29:29 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Reference-same-table-based-on-another-field/m-p/4271660#M169435</guid>
      <dc:creator>Steve_M</dc:creator>
      <dc:date>2024-11-06T02:29:29Z</dc:date>
    </item>
    <item>
      <title>Re: Reference same table based on another field</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Reference-same-table-based-on-another-field/m-p/4271755#M169439</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="803815" data-lia-user-login="Steve_M" class="lia-mention lia-mention-user"&gt;Steve_M&lt;/a&gt;&amp;nbsp; No Problem.&lt;BR /&gt;Change MAX to SUM in DAX Query and check that again please&lt;/P&gt;</description>
      <pubDate>Wed, 06 Nov 2024 04:11:41 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Reference-same-table-based-on-another-field/m-p/4271755#M169439</guid>
      <dc:creator>VahidDM</dc:creator>
      <dc:date>2024-11-06T04:11:41Z</dc:date>
    </item>
    <item>
      <title>Re: Reference same table based on another field</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Reference-same-table-based-on-another-field/m-p/4283179#M169998</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="803815" data-lia-user-login="Steve_M" class="lia-mention lia-mention-user"&gt;Steve_M&lt;/a&gt;&amp;nbsp;,&lt;BR /&gt;It's been a while since I heard back from you and I wanted to follow up. Have you had a chance to try the solutions that have been offered? If the issue has been resolved, can you mark the post as resolved? If you're still experiencing challenges, please feel free to let us know and we'll be happy to continue to help!&lt;BR /&gt;Looking forward to your reply!&lt;/P&gt;</description>
      <pubDate>Thu, 14 Nov 2024 06:27:26 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Reference-same-table-based-on-another-field/m-p/4283179#M169998</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2024-11-14T06:27:26Z</dc:date>
    </item>
    <item>
      <title>Re: Reference same table based on another field</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Reference-same-table-based-on-another-field/m-p/4284545#M170049</link>
      <description>&lt;P&gt;I still haven't solved this, but I needed to park it for now and move onto another project.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Nov 2024 18:52:04 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Reference-same-table-based-on-another-field/m-p/4284545#M170049</guid>
      <dc:creator>Steve_M</dc:creator>
      <dc:date>2024-11-14T18:52:04Z</dc:date>
    </item>
  </channel>
</rss>

