<?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: Fix running balances for dates when both purchase AND sale transactions occur (multiple criteria) in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Fix-running-balances-for-dates-when-both-purchase-AND-sale/m-p/3385148#M127551</link>
    <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="291628" data-lia-user-login="Jihwan_Kim" class="lia-mention lia-mention-user"&gt;Jihwan_Kim&lt;/a&gt;&amp;nbsp;your solution works perfectly, thanks a lot!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is one particular thing though about a table visual I've got on my Power BI data model.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This particular visual contains 2 date columns: the fact date called "Data" which is the date each transaction took place and a second column that I need for another purpose not worth explaining.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The thing is as you can see on the screenshot below when the visual brings both dates columns the column with the shares balance measure you helped me create (here called "Saldo de cotas") ends up bringing weird results:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As soon as I remove that second date column "Data da venda relacionada à respectiva compra" from the visual the shares balance measure is back to normal, bringing the results that are meant to come.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way to add an element to the DAX code that could shield the solution from that second dates column? Something like ALL or ALLSELECTED somewhere in the formula so once that particular table visual comes with both dates columns the measure column still brings the correct results?&lt;/P&gt;</description>
    <pubDate>Thu, 17 Aug 2023 18:29:28 GMT</pubDate>
    <dc:creator>leolapa_br</dc:creator>
    <dc:date>2023-08-17T18:29:28Z</dc:date>
    <item>
      <title>Fix running balances for dates when both purchase AND sale transactions occur (multiple criteria)</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Fix-running-balances-for-dates-when-both-purchase-AND-sale/m-p/3384032#M127498</link>
      <description>&lt;DIV&gt;I have an imported table from Excel via PQ 'fTrans' that contains the columns 'Ticker', 'Date', 'Shares' and 'Transaction'.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;Then I created the following measure:&lt;/SPAN&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Shares Balance = 
CALCULATE( 
    SUM( fTrans[Shares] ), 
    ALLEXCEPT( fTrans, fTrans[Ticker] ), 
    fTrans[Transaction] = "Purchase", 
    fTrans[Date] &amp;lt;= MAX( fTrans[Date] )
) - 
CALCULATE( 
    SUM( fTrans[Shares] ), 
    ALLEXCEPT( fTrans, fTrans[Ticker] ), 
    fTrans[Transaction] = "Sale", 
    fTrans[Date] &amp;lt;= MAX( fTrans[Date] )
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;Which I pasted as an additional column to the following table visual:&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Shares&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;Ticker&amp;nbsp; &amp;nbsp;Date&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Shares Transaction Balance&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;STRONG&gt;--------------------------------------------------------&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;ASAI3&amp;nbsp; &amp;nbsp;03/Mar/2023&amp;nbsp; &amp;nbsp; &amp;nbsp; 57&amp;nbsp; &amp;nbsp; &amp;nbsp;Purchase&amp;nbsp; &amp;nbsp; &amp;nbsp;57&lt;/DIV&gt;&lt;DIV&gt;ASAI3&amp;nbsp; &amp;nbsp;08/May/2023&amp;nbsp; &amp;nbsp; &amp;nbsp;92&amp;nbsp; &amp;nbsp; &amp;nbsp;Purchase&amp;nbsp; &amp;nbsp; 149&lt;/DIV&gt;&lt;DIV&gt;ASAI3&amp;nbsp; &amp;nbsp;09/Jun/2023&amp;nbsp; &amp;nbsp; &amp;nbsp; 149&amp;nbsp; &amp;nbsp; Sale&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/DIV&gt;&lt;DIV&gt;ASAI3&amp;nbsp; &amp;nbsp;19/Jul/2023&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;81&amp;nbsp; &amp;nbsp; &amp;nbsp; Purchase&amp;nbsp; &amp;nbsp; &amp;nbsp;81&lt;/DIV&gt;&lt;DIV&gt;ASAI3&amp;nbsp; &amp;nbsp;16/Aug/2023&amp;nbsp; &amp;nbsp; &amp;nbsp;81&amp;nbsp; &amp;nbsp; &amp;nbsp; Sale&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/DIV&gt;&lt;DIV&gt;AURE3&amp;nbsp; &amp;nbsp;23/Feb/2021&amp;nbsp; &amp;nbsp; 42&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Purchase&amp;nbsp; &amp;nbsp; &amp;nbsp;42&lt;/DIV&gt;&lt;DIV&gt;AURE3&amp;nbsp; &amp;nbsp;01/Jun/2023&amp;nbsp; &amp;nbsp; 45&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Purchase&amp;nbsp; &amp;nbsp; &amp;nbsp;87&lt;/DIV&gt;&lt;DIV&gt;AURE3&amp;nbsp; &amp;nbsp;03/Aug/2023&amp;nbsp; &amp;nbsp;47&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Purchase&amp;nbsp; &amp;nbsp; &amp;nbsp;92&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;STRONG&gt;(134)&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;AURE3&amp;nbsp; &amp;nbsp;03/Aug/2023&amp;nbsp; &amp;nbsp;42&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Sale&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;92&lt;/DIV&gt;&lt;DIV&gt;AURE3&amp;nbsp; &amp;nbsp;05/Oct/2023&amp;nbsp; &amp;nbsp; 48&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Purchase&amp;nbsp; &amp;nbsp; &amp;nbsp;95&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;STRONG&gt;(140)&lt;/STRONG&gt;&lt;/DIV&gt;&lt;DIV&gt;AURE3&amp;nbsp; &amp;nbsp;05/Oct/2023&amp;nbsp; &amp;nbsp; 45&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Sale&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;95&lt;/DIV&gt;&lt;DIV&gt;AURE3&amp;nbsp; &amp;nbsp;03/Nov/2021&amp;nbsp; &amp;nbsp;95&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Sale&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;0&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Since the measure accounts for all purchases AND sales occurred on the same day, the 'Shares Balance' column shows the same balance for both rows, but what I really wanted for the purchase transaction row was to get the balance prior to the sale transaction taking place &lt;STRONG&gt;(numbers in parenthesis)&lt;/STRONG&gt;.&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;How can I tweak the above DAX code to get that?&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;Thanks in advance!&lt;/DIV&gt;</description>
      <pubDate>Thu, 17 Aug 2023 01:06:18 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Fix-running-balances-for-dates-when-both-purchase-AND-sale/m-p/3384032#M127498</guid>
      <dc:creator>leolapa_br</dc:creator>
      <dc:date>2023-08-17T01:06:18Z</dc:date>
    </item>
    <item>
      <title>Re: Fix running balances for dates when both purchase AND sale transactions occur (multiple criteria)</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Fix-running-balances-for-dates-when-both-purchase-AND-sale/m-p/3384254#M127507</link>
      <description>&lt;P&gt;Hi, I am not sure if I understood your question correctly, but please check the below picture and the attached pbix file.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&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;LI-CODE lang="markup"&gt;expected result measure: =
VAR _t =
    ADDCOLUMNS (
        SUMMARIZE (
            ALL ( fTrans ),
            fTrans[Ticker],
            fTrans[Date],
            fTrans[Transaction],
            fTrans[Shares]
        ),
        "@transindex",
            SWITCH (
                TRUE (),
                fTrans[Transaction] = "Sale", 2,
                fTrans[Transaction] = "Purchase", 1
            ),
        "@transvalue",
            SWITCH (
                TRUE (),
                fTrans[Transaction] = "Sale", CALCULATE ( SUM ( fTrans[Shares] ) ) * -1,
                fTrans[Transaction] = "Purchase", CALCULATE ( SUM ( fTrans[Shares] ) )
            )
    )
RETURN
    IF (
        HASONEVALUE ( fTrans[Ticker] ),
        SUMX (
            WINDOW (
                1,
                ABS,
                0,
                REL,
                _t,
                ORDERBY ( fTrans[Date], ASC, [@transindex], ASC ),
                ,
                PARTITIONBY ( fTrans[Ticker] )
            ),
            [@transvalue]
        )
    )
&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 17 Aug 2023 04:10:50 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Fix-running-balances-for-dates-when-both-purchase-AND-sale/m-p/3384254#M127507</guid>
      <dc:creator>Jihwan_Kim</dc:creator>
      <dc:date>2023-08-17T04:10:50Z</dc:date>
    </item>
    <item>
      <title>Re: Fix running balances for dates when both purchase AND sale transactions occur (multiple criteria)</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Fix-running-balances-for-dates-when-both-purchase-AND-sale/m-p/3385148#M127551</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="291628" data-lia-user-login="Jihwan_Kim" class="lia-mention lia-mention-user"&gt;Jihwan_Kim&lt;/a&gt;&amp;nbsp;your solution works perfectly, thanks a lot!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is one particular thing though about a table visual I've got on my Power BI data model.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This particular visual contains 2 date columns: the fact date called "Data" which is the date each transaction took place and a second column that I need for another purpose not worth explaining.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The thing is as you can see on the screenshot below when the visual brings both dates columns the column with the shares balance measure you helped me create (here called "Saldo de cotas") ends up bringing weird results:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As soon as I remove that second date column "Data da venda relacionada à respectiva compra" from the visual the shares balance measure is back to normal, bringing the results that are meant to come.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way to add an element to the DAX code that could shield the solution from that second dates column? Something like ALL or ALLSELECTED somewhere in the formula so once that particular table visual comes with both dates columns the measure column still brings the correct results?&lt;/P&gt;</description>
      <pubDate>Thu, 17 Aug 2023 18:29:28 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Fix-running-balances-for-dates-when-both-purchase-AND-sale/m-p/3385148#M127551</guid>
      <dc:creator>leolapa_br</dc:creator>
      <dc:date>2023-08-17T18:29:28Z</dc:date>
    </item>
  </channel>
</rss>

