<?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: Average Purchase Price in every instant in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Average-Purchase-Price-in-every-instant/m-p/2873382#M92811</link>
    <description>&lt;P&gt;This worked&amp;nbsp;&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 for your help.&lt;/P&gt;&lt;P&gt;Do you know how can I complete this measure so in the case of a Sell Operation rest the selling price to the calculated average?&lt;/P&gt;</description>
    <pubDate>Sun, 30 Oct 2022 22:53:13 GMT</pubDate>
    <dc:creator>AndresOHV</dc:creator>
    <dc:date>2022-10-30T22:53:13Z</dc:date>
    <item>
      <title>Average Purchase Price in every instant</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Average-Purchase-Price-in-every-instant/m-p/2865700#M92344</link>
      <description>&lt;P&gt;Hello All!&lt;/P&gt;&lt;P&gt;I'm working with a fact table and need to get the average purchase price for every instant.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I need to do is multiply the price of every purchase for the quantity and divide that result in the accumulated quantity considering the same for the previous purchases, as the excel example below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Date&lt;/TD&gt;&lt;TD&gt;ProductID&lt;/TD&gt;&lt;TD&gt;Operation&lt;/TD&gt;&lt;TD&gt;Qty&lt;/TD&gt;&lt;TD&gt;Accumulated Qty&lt;/TD&gt;&lt;TD&gt;Price&l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amp;nbsp;$ 3.146,60&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;7*3307/25 + 7*3251/25 + 3*3334/25 + 7*3251/25&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;19/1/2022 00:00&lt;/TD&gt;&lt;TD&gt;1255&lt;/TD&gt;&lt;TD&gt;Sell&lt;/TD&gt;&lt;TD&gt;-25&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;$ 3.125,00&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I wasn't able to replicate this in PBI, can anyone help me with this?&lt;/P&gt;</description>
      <pubDate>Wed, 26 Oct 2022 18:08:17 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Average-Purchase-Price-in-every-instant/m-p/2865700#M92344</guid>
      <dc:creator>AndresOHV</dc:creator>
      <dc:date>2022-10-26T18:08:17Z</dc:date>
    </item>
    <item>
      <title>Re: Average Purchase Price in every instant</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Average-Purchase-Price-in-every-instant/m-p/2865771#M92350</link>
      <description>&lt;P&gt;OK. This is rather simple and can be easily done in Power Query. But showing only one "Sell" entry and with 0 at that... does not help. You have to provide an example that's REPRESENTATIVE.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Oct 2022 18:57:07 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Average-Purchase-Price-in-every-instant/m-p/2865771#M92350</guid>
      <dc:creator>daXtreme</dc:creator>
      <dc:date>2022-10-26T18:57:07Z</dc:date>
    </item>
    <item>
      <title>Re: Average Purchase Price in every instant</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Average-Purchase-Price-in-every-instant/m-p/2865779#M92352</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="455001" data-lia-user-login="AndresOHV" class="lia-mention lia-mention-user"&gt;AndresOHV&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;LI-CODE lang="javascript"&gt;AVG Price = 
VAR CurrentOperation = 'Table'[Operation]
VAR CurrentDate = 'Table'[Date]
VAR CurrentAccQty = 'Table'[Accumulated Qty]
VAR CurrentIDTable = CALCULATETABLE ( 'Table', ALLEXCEPT ( 'Table', 'Table'[ProductID] ) )
VAR BuyOnAndBefore = FILTER ( CurrentIDTable, 'Table'[Date] &amp;lt;= CurrentDate &amp;amp;&amp;amp; 'Table'[Operation] = "Buy" )
RETURN
    IF (
        CurrentOperation = "Buy",
        DIVIDE ( SUMX ( BuyOnAndBefore, [Qty] * 'Table'[Price] ), CurrentAccQty )
    )&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 26 Oct 2022 19:06:02 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Average-Purchase-Price-in-every-instant/m-p/2865779#M92352</guid>
      <dc:creator>tamerj1</dc:creator>
      <dc:date>2022-10-26T19:06:02Z</dc:date>
    </item>
    <item>
      <title>Re: Average Purchase Price in every instant</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Average-Purchase-Price-in-every-instant/m-p/2866063#M92369</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&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 for your reply.&lt;/P&gt;&lt;P&gt;I'm trying to replicate this buy I'm having a "circular dependency" when creating the column.&lt;/P&gt;&lt;P&gt;The productId and the date come from 2 different dimension tables. Wouldn't be better to create a measure instead of a column?&lt;/P&gt;</description>
      <pubDate>Wed, 26 Oct 2022 22:07:59 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Average-Purchase-Price-in-every-instant/m-p/2866063#M92369</guid>
      <dc:creator>AndresOHV</dc:creator>
      <dc:date>2022-10-26T22:07:59Z</dc:date>
    </item>
    <item>
      <title>Re: Average Purchase Price in every instant</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Average-Purchase-Price-in-every-instant/m-p/2866516#M92384</link>
      <description>&lt;P&gt;Hi ,&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="455001" data-lia-user-login="AndresOHV" class="lia-mention lia-mention-user"&gt;AndresOHV&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For measure , you can use this dax :&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Measure = var _date =SELECTEDVALUE('Sheet1'[Date]) 
var _product_id = SELECTEDVALUE('Sheet1'[ProductID])
var _operation =MAX('Sheet1'[Operation])
var _acc_qty = MAX('Sheet1'[Accumulated Qty])
var _t = FILTER( ALLSELECTED( 'Sheet1') , 'Sheet1'[ProductID] =_product_id &amp;amp;&amp;amp; 'Sheet1'[Date] &amp;lt;= _date &amp;amp;&amp;amp; 'Sheet1'[Operation] = "Buy" )
return
DIVIDE(SUMX(_t , [Price] * [Qty]),_acc_qty)&lt;/LI-CODE&gt;
&lt;P&gt;The result is as follows:&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best Regards,&lt;/P&gt;
&lt;P&gt;Aniya Zhang&lt;/P&gt;
&lt;P&gt;If this post&amp;nbsp;&lt;STRONG&gt;helps&lt;/STRONG&gt;, then please consider&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;Accept&lt;/EM&gt;&lt;/STRONG&gt;&lt;EM&gt; it as the solution&lt;/EM&gt;&amp;nbsp;to help the other members find it more quickly&lt;/P&gt;</description>
      <pubDate>Thu, 27 Oct 2022 02:59:32 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Average-Purchase-Price-in-every-instant/m-p/2866516#M92384</guid>
      <dc:creator>v-yueyunzh-msft</dc:creator>
      <dc:date>2022-10-27T02:59:32Z</dc:date>
    </item>
    <item>
      <title>Re: Average Purchase Price in every instant</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Average-Purchase-Price-in-every-instant/m-p/2867565#M92428</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="455001" data-lia-user-login="AndresOHV" class="lia-mention lia-mention-user"&gt;AndresOHV&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;Still not very clear, therefore, I made some assumptions again.&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;AVG Price = 
VAR CurrentDate = MAX ( 'Date'[Date] )
VAR CurrentOperation = SELECTEDVALUE ( Sales[Operation] )
VAR AccumulatedQty = [Accumulated Qty]
VAR T1 = 
    CALCULATETABLE ( 
        SUMMARIZE ( 
            Sales, 
            Sales[ProductID], 
            Sales[Price], 
            Sales[Operation], 
            'Date'[Date] 
        ),
        REMOVEFILTERS ( ),
        VALUES ( 'Sales'[ProductID] ),
        'Sales'[Operation] = "Buy" ,
        'Date'[Date] &amp;lt;= CurrentDate
    )
VAR T2 =
    ADDCOLUMNS ( 
        T1,
        "@Qty", [Qty]
    )
RETURN
    IF (
        CurrentOperation = "Buy",
        DIVIDE ( 
            SUMX ( T2, [@Qty] * [Price] ), 
            AccumulatedQty 
        )
    )&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 27 Oct 2022 09:52:31 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Average-Purchase-Price-in-every-instant/m-p/2867565#M92428</guid>
      <dc:creator>tamerj1</dc:creator>
      <dc:date>2022-10-27T09:52:31Z</dc:date>
    </item>
    <item>
      <title>Re: Average Purchase Price in every instant</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Average-Purchase-Price-in-every-instant/m-p/2868549#M92492</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="433015" data-lia-user-login="v-yueyunzh-msft" class="lia-mention lia-mention-user"&gt;v-yueyunzh-msft&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to use this but I get a different result. I think it's because you're using just one table and in the real model I have the date and product id in different tabels so I'm not sure how to make it work.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Oct 2022 15:29:13 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Average-Purchase-Price-in-every-instant/m-p/2868549#M92492</guid>
      <dc:creator>AndresOHV</dc:creator>
      <dc:date>2022-10-27T15:29:13Z</dc:date>
    </item>
    <item>
      <title>Re: Average Purchase Price in every instant</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Average-Purchase-Price-in-every-instant/m-p/2869349#M92546</link>
      <description>&lt;P&gt;Hi ,&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="455001" data-lia-user-login="AndresOHV" class="lia-mention lia-mention-user"&gt;AndresOHV&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You are right , the measure need to write by the table relationship. So do you want to share the sample .pbix file to me so that we can help you better.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best Regards,&lt;/P&gt;
&lt;P&gt;Aniya Zhang&lt;/P&gt;
&lt;P&gt;If this post&amp;nbsp;&lt;STRONG&gt;helps&lt;/STRONG&gt;, then please consider&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;Accept&lt;/EM&gt;&lt;/STRONG&gt;&lt;EM&gt; it as the solution&lt;/EM&gt;&amp;nbsp;to help the other members find it more quickly&lt;/P&gt;</description>
      <pubDate>Fri, 28 Oct 2022 02:10:13 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Average-Purchase-Price-in-every-instant/m-p/2869349#M92546</guid>
      <dc:creator>v-yueyunzh-msft</dc:creator>
      <dc:date>2022-10-28T02:10:13Z</dc:date>
    </item>
    <item>
      <title>Re: Average Purchase Price in every instant</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Average-Purchase-Price-in-every-instant/m-p/2871413#M92663</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="433015" data-lia-user-login="v-yueyunzh-msft" class="lia-mention lia-mention-user"&gt;v-yueyunzh-msft&lt;/a&gt;&amp;nbsp;! Thank you for your help.&lt;/P&gt;&lt;P&gt;I'm copying a link to the pbi file:&lt;/P&gt;&lt;P&gt;&lt;A href="https://1drv.ms/u/s!AvSFZ2hooRIDgdJPNOk7duNVPTrnbQ?e=STE3Xr" target="_blank"&gt;https://1drv.ms/u/s!AvSFZ2hooRIDgdJPNOk7duNVPTrnbQ?e=STE3Xr&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Oct 2022 16:15:31 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Average-Purchase-Price-in-every-instant/m-p/2871413#M92663</guid>
      <dc:creator>AndresOHV</dc:creator>
      <dc:date>2022-10-28T16:15:31Z</dc:date>
    </item>
    <item>
      <title>Re: Average Purchase Price in every instant</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Average-Purchase-Price-in-every-instant/m-p/2871624#M92681</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="455001" data-lia-user-login="AndresOHV" class="lia-mention lia-mention-user"&gt;AndresOHV&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;Please refer to attched sample file with the proposed solution. Please let me know if this is as per your requirement or something else is required.&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;Acc QTY = 
CALCULATE (
    [Qty], 
    FILTER (
        ALL ( 'Calendar' ), 
        'Calendar'[Fecha] &amp;lt;= MAX ( 'Calendar'[Fecha] )
    ), 
    ALL ( FactExample[Operation] ),
    ALL ( FactExample[Price] )
)&lt;/LI-CODE&gt;&lt;LI-CODE lang="javascript"&gt;AVG Price = 
VAR CurrentDate = MAX ( 'Calendar'[Fecha] )
VAR CurrentOperation = SELECTEDVALUE ( FactExample[Operation] )
VAR AccumulatedQty = [Acc QTY]
VAR T1 = 
    CALCULATETABLE ( 
        SUMMARIZE ( 
            FactExample, 
            Ticker[Ticker],
            'Calendar'[Fecha],
            FactExample[Price], 
            FactExample[Operation]
        ),
        REMOVEFILTERS ( ),
        VALUES ( FactExample[Ticker] ),
        FactExample[Operation] &amp;lt;&amp;gt; "Sell" ,
        'Calendar'[Fecha] &amp;lt;= CurrentDate
    )
VAR T2 =
    ADDCOLUMNS ( 
        T1,
        "@Qty", [Qty]
    )
RETURN
    IF (
        CurrentOperation &amp;lt;&amp;gt; "Sell",
        DIVIDE ( 
            SUMX ( T2, [@Qty] * [Price] ), 
            AccumulatedQty 
        )
    )&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 28 Oct 2022 19:32:08 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Average-Purchase-Price-in-every-instant/m-p/2871624#M92681</guid>
      <dc:creator>tamerj1</dc:creator>
      <dc:date>2022-10-28T19:32:08Z</dc:date>
    </item>
    <item>
      <title>Re: Average Purchase Price in every instant</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Average-Purchase-Price-in-every-instant/m-p/2873382#M92811</link>
      <description>&lt;P&gt;This worked&amp;nbsp;&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 for your help.&lt;/P&gt;&lt;P&gt;Do you know how can I complete this measure so in the case of a Sell Operation rest the selling price to the calculated average?&lt;/P&gt;</description>
      <pubDate>Sun, 30 Oct 2022 22:53:13 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Average-Purchase-Price-in-every-instant/m-p/2873382#M92811</guid>
      <dc:creator>AndresOHV</dc:creator>
      <dc:date>2022-10-30T22:53:13Z</dc:date>
    </item>
  </channel>
</rss>

