<?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 Calculate correct value for current month and average of next months for non-current months in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-correct-value-for-current-month-and-average-of-next/m-p/3329386#M124725</link>
    <description>&lt;P&gt;Hi all, I would like to get some help understanding what I''m doing wrong with the formula for a calculated column using DAX:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I should add a calculated column 'cost_real' in which for each row the "real" cost should be calculated as follows:&lt;BR /&gt;&lt;BR /&gt;a. If the date is in the current month, then take cost_prod&amp;nbsp;&lt;BR /&gt;b. If date is not in current month, then take average of next month's cost_prod for that specific product.&lt;BR /&gt;&lt;BR /&gt;The data table has a column named 'dt' for the dates, and the product column is called 'cost_prod'. I need to do this using DAX, and so far I came up with this formula for the first but it doesn't seem to work entirely:&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;cost_real = 

var current_month = MONTH(TODAY())
var current_year = YEAR(TODAY())
var next_month = IF(current_month = 12, 1, current_month + 1)
var next_year = IF(current_month = 12, current_year + 1, current_year)
var cost_prod =
    CALCULATE(
        AVERAGE(rimanenze_pv_day_nonoil[cost_prod]),
        FILTER(
            rimanenze_pv_day_nonoil,
            YEAR(rimanenze_pv_day_nonoil[dt]) = next_year &amp;amp;&amp;amp;
            MONTH(rimanenze_pv_day_nonoil[dt]) = next_month
        )
    )
var result = 
    IF(
        YEAR(rimanenze_pv_day_nonoil[dt]) = current_year &amp;amp;&amp;amp; MONTH(rimanenze_pv_day_nonoil[dt]) = current_month,
        rimanenze_pv_day_nonoil[cost_prod],
        cost_prod
    )

return&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Here is also a snapshot of the table as is. There is no model etc, it's only this table here&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 13 Jul 2023 12:37:57 GMT</pubDate>
    <dc:creator>ale_BI</dc:creator>
    <dc:date>2023-07-13T12:37:57Z</dc:date>
    <item>
      <title>Calculate correct value for current month and average of next months for non-current months</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-correct-value-for-current-month-and-average-of-next/m-p/3329386#M124725</link>
      <description>&lt;P&gt;Hi all, I would like to get some help understanding what I''m doing wrong with the formula for a calculated column using DAX:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I should add a calculated column 'cost_real' in which for each row the "real" cost should be calculated as follows:&lt;BR /&gt;&lt;BR /&gt;a. If the date is in the current month, then take cost_prod&amp;nbsp;&lt;BR /&gt;b. If date is not in current month, then take average of next month's cost_prod for that specific product.&lt;BR /&gt;&lt;BR /&gt;The data table has a column named 'dt' for the dates, and the product column is called 'cost_prod'. I need to do this using DAX, and so far I came up with this formula for the first but it doesn't seem to work entirely:&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;cost_real = 

var current_month = MONTH(TODAY())
var current_year = YEAR(TODAY())
var next_month = IF(current_month = 12, 1, current_month + 1)
var next_year = IF(current_month = 12, current_year + 1, current_year)
var cost_prod =
    CALCULATE(
        AVERAGE(rimanenze_pv_day_nonoil[cost_prod]),
        FILTER(
            rimanenze_pv_day_nonoil,
            YEAR(rimanenze_pv_day_nonoil[dt]) = next_year &amp;amp;&amp;amp;
            MONTH(rimanenze_pv_day_nonoil[dt]) = next_month
        )
    )
var result = 
    IF(
        YEAR(rimanenze_pv_day_nonoil[dt]) = current_year &amp;amp;&amp;amp; MONTH(rimanenze_pv_day_nonoil[dt]) = current_month,
        rimanenze_pv_day_nonoil[cost_prod],
        cost_prod
    )

return&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Here is also a snapshot of the table as is. There is no model etc, it's only this table here&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jul 2023 12:37:57 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-correct-value-for-current-month-and-average-of-next/m-p/3329386#M124725</guid>
      <dc:creator>ale_BI</dc:creator>
      <dc:date>2023-07-13T12:37:57Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate correct value for current month and average of next months for non-current months</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-correct-value-for-current-month-and-average-of-next/m-p/3329612#M124748</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="455265" data-lia-user-login="ale_BI" class="lia-mention lia-mention-user"&gt;ale_BI&lt;/a&gt;&amp;nbsp;I suggest to debug "var cost_prod". Do you get correct / excepect values when you apply this solution as separate measure? If yes, then your issue is part IF (...&lt;/P&gt;&lt;P&gt;Hope this help&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jul 2023 14:14:19 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-correct-value-for-current-month-and-average-of-next/m-p/3329612#M124748</guid>
      <dc:creator>some_bih</dc:creator>
      <dc:date>2023-07-13T14:14:19Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate correct value for current month and average of next months for non-current months</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-correct-value-for-current-month-and-average-of-next/m-p/3329634#M124750</link>
      <description>&lt;P&gt;In facts, the var cost_prod doesn't seem to work because if I only return that I get all blank values, while I would expect to see the averages of the column cost_prod for the next month and year, at each row.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 13 Jul 2023 14:23:40 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-correct-value-for-current-month-and-average-of-next/m-p/3329634#M124750</guid>
      <dc:creator>ale_BI</dc:creator>
      <dc:date>2023-07-13T14:23:40Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate correct value for current month and average of next months for non-current months</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-correct-value-for-current-month-and-average-of-next/m-p/3335509#M125083</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="455265" data-lia-user-login="ale_BI" class="lia-mention lia-mention-user"&gt;ale_BI&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please replacing&lt;STRONG&gt;&amp;nbsp;rimanenze_pv_day_nonoil[cost_prod]&lt;/STRONG&gt; with &lt;STRONG&gt;cost_prod&lt;/STRONG&gt; in the last line of the formula. Here is the modified formula:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;cost_real = 
VAR current_month = MONTH(TODAY())
VAR current_year = YEAR(TODAY())
VAR next_month = IF(current_month = 12, 1, current_month + 1)
VAR next_year = IF(current_month = 12, current_year + 1, current_year)
VAR cost_prod =
    CALCULATE(
        AVERAGE(rimanenze_pv_day_nonoil[cost_prod]),
        FILTER(
            rimanenze_pv_day_nonoil,
            YEAR(rimanenze_pv_day_nonoil[dt]) = next_year &amp;amp;&amp;amp;
            MONTH(rimanenze_pv_day_nonoil[dt]) = next_month
        )
    )
VAR result = 
    IF(
        YEAR(rimanenze_pv_day_nonoil[dt]) = current_year &amp;amp;&amp;amp; MONTH(rimanenze_pv_day_nonoil[dt]) = current_month,
        rimanenze_pv_day_nonoil[cost_prod],
        cost_prod
    )

RETURN
    result&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can use this formula to create a calculated column named &lt;STRONG&gt;cost_real&lt;/STRONG&gt; in your table. This column might calculate the correct value for the current month and the average of next months for non-current months.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&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;&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;&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;&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;&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;&lt;/P&gt;
&lt;P&gt;Best Regards,&lt;/P&gt;
&lt;P&gt;Stephen Tao&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If this post&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;helps&lt;/EM&gt;&lt;/STRONG&gt;, then please consider&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;Accept it as the solution&lt;/EM&gt;&lt;/STRONG&gt;&amp;nbsp;to help the other members find it more quickly. &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jul 2023 02:59:35 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-correct-value-for-current-month-and-average-of-next/m-p/3335509#M125083</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2023-07-18T02:59:35Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate correct value for current month and average of next months for non-current months</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-correct-value-for-current-month-and-average-of-next/m-p/3336879#M125153</link>
      <description>&lt;P&gt;Thank you for the response&amp;nbsp;Anonymous&lt;/a&gt;&amp;nbsp;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Unfortunately, I am still having issues with this as the column shows blank values for the previous months. I think the issue also has to do with how I create the current_month. So I modified it to this:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;cost_real = 
VAR current_month = MONTH(rimanenze_pv_day_nonoil[dt])
VAR current_year = YEAR(rimanenze_pv_day_nonoil[dt])
VAR next_month = IF(current_month = 12, 1, current_month + 1)
VAR next_year = IF(current_month = 12, current_year + 1, current_year)
VAR cost_prod =
    CALCULATE(
        AVERAGE(rimanenze_pv_day_nonoil[cost_prod]),
        FILTER(
            rimanenze_pv_day_nonoil,
            YEAR(rimanenze_pv_day_nonoil[dt]) = next_year &amp;amp;&amp;amp;
            MONTH(rimanenze_pv_day_nonoil[dt]) = next_month
        )
    )
VAR result = 
    IF(
        YEAR(rimanenze_pv_day_nonoil[dt]) = current_year &amp;amp;&amp;amp; MONTH(rimanenze_pv_day_nonoil[dt]) = current_month,
        rimanenze_pv_day_nonoil[cost_prod],
        cost_prod
    )

RETURN
    result&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;While this does show values, it doesn't seem to understand when it should and should not use the ELSE condition of the IF function. Because while before it always took the month from today and added 1, now it takes each month as being the current month by doing row-per-row calculations. For example, if I return just&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;YEAR(rimanenze_pv_day_nonoil[dt]) = current_year &amp;amp;&amp;amp; MONTH(rimanenze_pv_day_nonoil[dt]) = current_month&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;BR /&gt;to show True values, it shows True on any other cell with whatsoever date, because it always reads it as the current date available. Do you have anymore tips on how to proceed, please? This issue is really cursing me. Thanks a lot again.&lt;/P&gt;</description>
      <pubDate>Tue, 18 Jul 2023 15:36:17 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-correct-value-for-current-month-and-average-of-next/m-p/3336879#M125153</guid>
      <dc:creator>ale_BI</dc:creator>
      <dc:date>2023-07-18T15:36:17Z</dc:date>
    </item>
  </channel>
</rss>

