<?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 Variable as a single measure in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Variable-as-a-single-measure/m-p/1755569#M36648</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a complex measure with some variables:&lt;/P&gt;&lt;DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Weighted Average = 
VAR OneLessMonthDailySales=DIVIDE(
    CALCULATE([Sold Prod Qnty],DATEADD(DateKey[Date],-1,MONTH)),
    CALCULATE([Unique Sales Day Count],DATEADD(DateKey[Date],-1,MONTH)),0)
VAR TwoLessMonthDailySales=DIVIDE(
    CALCULATE([Sold Prod Qnty],DATEADD(DateKey[Date],-2,MONTH)),
    CALCULATE([Unique Sales Day Count],DATEADD(DateKey[Date],-2,MONTH)),0)
VAR ThreeLessMonthDailySales=DIVIDE(
    CALCULATE([Sold Prod Qnty],DATEADD(DateKey[Date],-3,MONTH)),
    CALCULATE([Unique Sales Day Count],DATEADD(DateKey[Date],-3,MONTH)),0)
VAR FourLessMonthDailySales=DIVIDE(
    CALCULATE([Sold Prod Qnty],DATEADD(DateKey[Date],-4,MONTH)),
    CALCULATE([Unique Sales Day Count],DATEADD(DateKey[Date],-4,MONTH)),0)
VAR FiveLessMonthDailySales=DIVIDE(
    CALCULATE([Sold Prod Qnty],DATEADD(DateKey[Date],-5,MONTH)),
    CALCULATE([Unique Sales Day Count],DATEADD(DateKey[Date],-5,MONTH)),0)
VAR SixLessMonthDailySales=DIVIDE(
    CALCULATE([Sold Prod Qnty],DATEADD(DateKey[Date],-6,MONTH)),
    CALCULATE([Unique Sales Day Count],DATEADD(DateKey[Date],-6,MONTH)),0)
VAR WeightedAverage=
(3*OneLessMonthDailySales+2.5*TwoLessMonthDailySales+2*ThreeLessMonthDailySales+1.5*FourLessMonthDailySales+1*FiveLessMonthDailySales)/10
Return
WeightedAverage*[Unique Sales Day Count]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As you can see some outer measures have been repetedly presented in that expession such as [Sold Prod Qnty] and [Inique Sales Day Count], I would like to replace those repeted measure by single variable difining them from the very begining, like in the following examle:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Weighted Average = 
VAR SQ=[Sold Prod Qnty]
VAR SD=[Unique Sales Day Count]
VAR OneLessMonthDailySales=DIVIDE(
    CALCULATE(SQ,DATEADD(DateKey[Date],-1,MONTH)),
    CALCULATE(SD,DATEADD(DateKey[Date],-1,MONTH)),0)
VAR TwoLessMonthDailySales=DIVIDE(
    CALCULATE(SQ,DATEADD(DateKey[Date],-2,MONTH)),
    CALCULATE(SD,DATEADD(DateKey[Date],-2,MONTH)),0)
VAR ThreeLessMonthDailySales=DIVIDE(
    CALCULATE(SQ,DATEADD(DateKey[Date],-3,MONTH)),
    CALCULATE(SD,DATEADD(DateKey[Date],-3,MONTH)),0)
VAR FourLessMonthDailySales=DIVIDE(
    CALCULATE(SQ,DATEADD(DateKey[Date],-4,MONTH)),
    CALCULATE(SD,DATEADD(DateKey[Date],-4,MONTH)),0)
VAR FiveLessMonthDailySales=DIVIDE(
    CALCULATE(SQ,DATEADD(DateKey[Date],-5,MONTH)),
    CALCULATE(SD,DATEADD(DateKey[Date],-5,MONTH)),0)
VAR SixLessMonthDailySales=DIVIDE(
    CALCULATE(SQ,DATEADD(DateKey[Date],-6,MONTH)),
    CALCULATE(SD,DATEADD(DateKey[Date],-6,MONTH)),0)
VAR WeightedAverage=
(3*OneLessMonthDailySales+2.5*TwoLessMonthDailySales+2*ThreeLessMonthDailySales+1.5*FourLessMonthDailySales+1*FiveLessMonthDailySales)/10
Return
WeightedAverage*SD&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;As you can see the measures [Sold Prod Qnty] and [Inique Sales Day Count] are defined as starting variables "SQ" and "SD" andare used as the replacements of themseves in below given expressions.&amp;nbsp;&lt;/P&gt;&lt;P&gt;..But for some reasons the approach does not work! I do not undestant why.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please help in clearing the issue.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
    <pubDate>Tue, 30 Mar 2021 18:52:38 GMT</pubDate>
    <dc:creator>George1973</dc:creator>
    <dc:date>2021-03-30T18:52:38Z</dc:date>
    <item>
      <title>Variable as a single measure</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Variable-as-a-single-measure/m-p/1755569#M36648</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a complex measure with some variables:&lt;/P&gt;&lt;DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Weighted Average = 
VAR OneLessMonthDailySales=DIVIDE(
    CALCULATE([Sold Prod Qnty],DATEADD(DateKey[Date],-1,MONTH)),
    CALCULATE([Unique Sales Day Count],DATEADD(DateKey[Date],-1,MONTH)),0)
VAR TwoLessMonthDailySales=DIVIDE(
    CALCULATE([Sold Prod Qnty],DATEADD(DateKey[Date],-2,MONTH)),
    CALCULATE([Unique Sales Day Count],DATEADD(DateKey[Date],-2,MONTH)),0)
VAR ThreeLessMonthDailySales=DIVIDE(
    CALCULATE([Sold Prod Qnty],DATEADD(DateKey[Date],-3,MONTH)),
    CALCULATE([Unique Sales Day Count],DATEADD(DateKey[Date],-3,MONTH)),0)
VAR FourLessMonthDailySales=DIVIDE(
    CALCULATE([Sold Prod Qnty],DATEADD(DateKey[Date],-4,MONTH)),
    CALCULATE([Unique Sales Day Count],DATEADD(DateKey[Date],-4,MONTH)),0)
VAR FiveLessMonthDailySales=DIVIDE(
    CALCULATE([Sold Prod Qnty],DATEADD(DateKey[Date],-5,MONTH)),
    CALCULATE([Unique Sales Day Count],DATEADD(DateKey[Date],-5,MONTH)),0)
VAR SixLessMonthDailySales=DIVIDE(
    CALCULATE([Sold Prod Qnty],DATEADD(DateKey[Date],-6,MONTH)),
    CALCULATE([Unique Sales Day Count],DATEADD(DateKey[Date],-6,MONTH)),0)
VAR WeightedAverage=
(3*OneLessMonthDailySales+2.5*TwoLessMonthDailySales+2*ThreeLessMonthDailySales+1.5*FourLessMonthDailySales+1*FiveLessMonthDailySales)/10
Return
WeightedAverage*[Unique Sales Day Count]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As you can see some outer measures have been repetedly presented in that expession such as [Sold Prod Qnty] and [Inique Sales Day Count], I would like to replace those repeted measure by single variable difining them from the very begining, like in the following examle:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Weighted Average = 
VAR SQ=[Sold Prod Qnty]
VAR SD=[Unique Sales Day Count]
VAR OneLessMonthDailySales=DIVIDE(
    CALCULATE(SQ,DATEADD(DateKey[Date],-1,MONTH)),
    CALCULATE(SD,DATEADD(DateKey[Date],-1,MONTH)),0)
VAR TwoLessMonthDailySales=DIVIDE(
    CALCULATE(SQ,DATEADD(DateKey[Date],-2,MONTH)),
    CALCULATE(SD,DATEADD(DateKey[Date],-2,MONTH)),0)
VAR ThreeLessMonthDailySales=DIVIDE(
    CALCULATE(SQ,DATEADD(DateKey[Date],-3,MONTH)),
    CALCULATE(SD,DATEADD(DateKey[Date],-3,MONTH)),0)
VAR FourLessMonthDailySales=DIVIDE(
    CALCULATE(SQ,DATEADD(DateKey[Date],-4,MONTH)),
    CALCULATE(SD,DATEADD(DateKey[Date],-4,MONTH)),0)
VAR FiveLessMonthDailySales=DIVIDE(
    CALCULATE(SQ,DATEADD(DateKey[Date],-5,MONTH)),
    CALCULATE(SD,DATEADD(DateKey[Date],-5,MONTH)),0)
VAR SixLessMonthDailySales=DIVIDE(
    CALCULATE(SQ,DATEADD(DateKey[Date],-6,MONTH)),
    CALCULATE(SD,DATEADD(DateKey[Date],-6,MONTH)),0)
VAR WeightedAverage=
(3*OneLessMonthDailySales+2.5*TwoLessMonthDailySales+2*ThreeLessMonthDailySales+1.5*FourLessMonthDailySales+1*FiveLessMonthDailySales)/10
Return
WeightedAverage*SD&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;As you can see the measures [Sold Prod Qnty] and [Inique Sales Day Count] are defined as starting variables "SQ" and "SD" andare used as the replacements of themseves in below given expressions.&amp;nbsp;&lt;/P&gt;&lt;P&gt;..But for some reasons the approach does not work! I do not undestant why.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please help in clearing the issue.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 30 Mar 2021 18:52:38 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Variable-as-a-single-measure/m-p/1755569#M36648</guid>
      <dc:creator>George1973</dc:creator>
      <dc:date>2021-03-30T18:52:38Z</dc:date>
    </item>
    <item>
      <title>Re: Variable as a single measure</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Variable-as-a-single-measure/m-p/1755637#M36658</link>
      <description>&lt;P&gt;Once you define a variable, it's treated as a constant in the rest of the measure. The CALCULATE function changes the evaluation context but a constant is fixed regardless of evaluation context so CALCULATE doesn't do anything to a variable you've already defined. So, for example, OneLessMonthDailySales will be the same as FiveLessMonthDailySales, since shifting the dates doesn't do anything to a value that's already been determined.&lt;/P&gt;</description>
      <pubDate>Tue, 30 Mar 2021 20:09:52 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Variable-as-a-single-measure/m-p/1755637#M36658</guid>
      <dc:creator>AlexisOlson</dc:creator>
      <dc:date>2021-03-30T20:09:52Z</dc:date>
    </item>
    <item>
      <title>Re: Variable as a single measure</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Variable-as-a-single-measure/m-p/1761915#M36873</link>
      <description>&lt;P&gt;Thanks for the respond. Now it's crlear and I will be more carefoul with that.&amp;nbsp;&lt;BR /&gt;..but it does not solve my problem to optimize the measure perfomance. What could be done instead?&lt;/P&gt;</description>
      <pubDate>Fri, 02 Apr 2021 16:58:43 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Variable-as-a-single-measure/m-p/1761915#M36873</guid>
      <dc:creator>George1973</dc:creator>
      <dc:date>2021-04-02T16:58:43Z</dc:date>
    </item>
    <item>
      <title>Re: Variable as a single measure</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Variable-as-a-single-measure/m-p/1761954#M36874</link>
      <description>&lt;P&gt;Hi George,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can you try using&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;IF.EAGER as in IF.EAGER your value will be computed for only one case when the tested condition is positive. In this way you can set a variable with measure value. This will improve the performace and will solve the issue as well.&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 02 Apr 2021 18:03:25 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Variable-as-a-single-measure/m-p/1761954#M36874</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2021-04-02T18:03:25Z</dc:date>
    </item>
    <item>
      <title>Re: Variable as a single measure</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Variable-as-a-single-measure/m-p/1761960#M36875</link>
      <description>&lt;P&gt;H,&lt;BR /&gt;Can you provide a simple example of the "If.eager", because I have not heard about at so far &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Apr 2021 18:13:54 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Variable-as-a-single-measure/m-p/1761960#M36875</guid>
      <dc:creator>George1973</dc:creator>
      <dc:date>2021-04-02T18:13:54Z</dc:date>
    </item>
    <item>
      <title>Re: Variable as a single measure</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Variable-as-a-single-measure/m-p/1761974#M36878</link>
      <description>&lt;P&gt;if.eager({condition that you can identify as a flag}, {Write your divide measure here}).&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Apr 2021 18:34:02 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Variable-as-a-single-measure/m-p/1761974#M36878</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2021-04-02T18:34:02Z</dc:date>
    </item>
    <item>
      <title>Re: Variable as a single measure</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Variable-as-a-single-measure/m-p/1761999#M36883</link>
      <description>&lt;P&gt;I'm not sure if it's necessarily possible to optimize significantly. Because the context is different each time, you need to evaluate each of the prior months separately. I don't see a way to get around that since you can't actually reuse a computation.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That said, you might be able to get some degree of parallelization if you write it differently. See if this works any better:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Weighted Average =
VAR PriorMonths =
    ADDCOLUMNS (
        GENERATESERIES ( 1, 5 ),
        "MonthDailySales",
            DIVIDE (
                CALCULATE ( [Sold Prod Qnty], DATEADD ( 'Date'[Date], -[Value], MONTH ) ),
                CALCULATE ( [Unique Sales Day Count], DATEADD ( 'Date'[Date], -[Value], MONTH )
                )
            ),
        "Weight",
            ( 7 - [Value] ) / 2
    )
VAR WtdAvg =
    DIVIDE (
        SUMX ( PriorMonths, [Weight] * [MonthDailySales] ),
        SUMX ( PriorMonths, [Weight] )
    )
RETURN
    DIVIDE ( WtdAvg, [Unique Sales Day Count] )&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 02 Apr 2021 19:42:34 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Variable-as-a-single-measure/m-p/1761999#M36883</guid>
      <dc:creator>AlexisOlson</dc:creator>
      <dc:date>2021-04-02T19:42:34Z</dc:date>
    </item>
    <item>
      <title>Re: Variable as a single measure</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Variable-as-a-single-measure/m-p/1762372#M36899</link>
      <description>&lt;P&gt;WOW, very interesting. I will try it and let you know. Thanks a lot&lt;/P&gt;</description>
      <pubDate>Sat, 03 Apr 2021 14:23:18 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Variable-as-a-single-measure/m-p/1762372#M36899</guid>
      <dc:creator>George1973</dc:creator>
      <dc:date>2021-04-03T14:23:18Z</dc:date>
    </item>
  </channel>
</rss>

