<?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 dynamic average of subset of years in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-dynamic-average-of-subset-of-years/m-p/2291636#M56345</link>
    <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have data that only begins halfway through my total period as can be seen in this graph:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;With "Sales by SFC" only beginning in 2018, the value for "SFC Sales on Total Sales" also only starts in that very same year. Also, there is a year slicer on my page in which I can restrict the current view to only a subset of years such as 2020-2022.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now, I would like to create an average of "SFC Sales on Total Sales" that only encompasses the years 2018-2022 (creating a general average would skew the result downwards due to the 0s in the previous years). My attempt in doing so looks as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;SFC Sales on Total Sales (2018 onwards) = 
var skewedaverage =
DIVIDE(
    sum(SalesOnSFC[Sales by SFC]),
    sum(TotalSales[Sales]),
    blank())

var correctaverage =
calculate(
    skewedaverage,
    year('Date'[Date]) &amp;gt;= 2018
)

return correctaverage  &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem with my DAX code is that it disregards my year slicer settings in its calculations. In other words, my calculated average is static. This is a problem when the slicer is set to a time frame like 2019-2021. Can therefore someone please help me modifying my code so that it calculates the average dynamically in accordance to the settings of the year slicer?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance!&lt;/P&gt;</description>
    <pubDate>Wed, 19 Jan 2022 17:19:05 GMT</pubDate>
    <dc:creator>ThomasSan</dc:creator>
    <dc:date>2022-01-19T17:19:05Z</dc:date>
    <item>
      <title>Calculate dynamic average of subset of years</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-dynamic-average-of-subset-of-years/m-p/2291636#M56345</link>
      <description>&lt;P&gt;Hi everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have data that only begins halfway through my total period as can be seen in this graph:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;With "Sales by SFC" only beginning in 2018, the value for "SFC Sales on Total Sales" also only starts in that very same year. Also, there is a year slicer on my page in which I can restrict the current view to only a subset of years such as 2020-2022.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now, I would like to create an average of "SFC Sales on Total Sales" that only encompasses the years 2018-2022 (creating a general average would skew the result downwards due to the 0s in the previous years). My attempt in doing so looks as follows:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;SFC Sales on Total Sales (2018 onwards) = 
var skewedaverage =
DIVIDE(
    sum(SalesOnSFC[Sales by SFC]),
    sum(TotalSales[Sales]),
    blank())

var correctaverage =
calculate(
    skewedaverage,
    year('Date'[Date]) &amp;gt;= 2018
)

return correctaverage  &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The problem with my DAX code is that it disregards my year slicer settings in its calculations. In other words, my calculated average is static. This is a problem when the slicer is set to a time frame like 2019-2021. Can therefore someone please help me modifying my code so that it calculates the average dynamically in accordance to the settings of the year slicer?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you in advance!&lt;/P&gt;</description>
      <pubDate>Wed, 19 Jan 2022 17:19:05 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-dynamic-average-of-subset-of-years/m-p/2291636#M56345</guid>
      <dc:creator>ThomasSan</dc:creator>
      <dc:date>2022-01-19T17:19:05Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate dynamic average of subset of years</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-dynamic-average-of-subset-of-years/m-p/2291732#M56349</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;Here is one way to do this:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;After 2019 = &lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;var&lt;/SPAN&gt; &lt;SPAN&gt;_year&lt;/SPAN&gt;&lt;SPAN&gt; = &lt;/SPAN&gt;&lt;SPAN&gt;MAX&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;'Calendar'[Year]&lt;/SPAN&gt;&lt;SPAN&gt;) &lt;/SPAN&gt;&lt;SPAN&gt;return&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;CALCULATE&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;SUM&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;SlicerYears[Value]&lt;/SPAN&gt;&lt;SPAN&gt;),&lt;/SPAN&gt;&lt;SPAN&gt;all&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;SlicerYears&lt;/SPAN&gt;&lt;SPAN&gt;),&lt;/SPAN&gt;&lt;SPAN&gt;SlicerYears[Year]&lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt;_year&lt;/SPAN&gt;&lt;SPAN&gt;,&lt;/SPAN&gt;&lt;SPAN&gt;SlicerYears[Year]&lt;/SPAN&gt;&lt;SPAN&gt;&amp;gt;=&lt;/SPAN&gt;&lt;SPAN&gt;2019&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;BR /&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&lt;SPAN&gt;So include var _year and ALL to your CALCULATE.&lt;BR /&gt;&lt;BR /&gt;&lt;STRONG&gt;End result:&lt;BR /&gt;&lt;BR /&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;img /&gt;&lt;img /&gt;&lt;P&gt;I hope this post helps to solve your issue and if it does consider accepting it as a solution and giving the post a thumbs up!&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 19 Jan 2022 18:05:01 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-dynamic-average-of-subset-of-years/m-p/2291732#M56349</guid>
      <dc:creator>ValtteriN</dc:creator>
      <dc:date>2022-01-19T18:05:01Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate dynamic average of subset of years</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-dynamic-average-of-subset-of-years/m-p/2292768#M56418</link>
      <description>&lt;P&gt;Thank you for your reply, &lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="343431" data-lia-user-login="ValtteriN" class="lia-mention lia-mention-user"&gt;ValtteriN&lt;/a&gt;&amp;nbsp;!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you mean modifying my formula so that it reads as follows&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;SFC Sales on Total Sales (2018 onwards) = 
var maxyear =
MAX('Date'[Year])

var skewedaverage =
DIVIDE(
    sum(SalesOnSFC[Sales by SFC]),
    sum(TotalSales[Sales]),
    blank())

var correctaverage =
calculate(
    skewedaverage,
    ALL('Date'[Year]),
    year('Date'[Date])=maxyear,
    year('Date'[Date]) &amp;gt;= 2018
)

return correctaverage  &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;?&lt;/P&gt;&lt;P&gt;However, in doing so, it appears that the lower bound is disregarded now (i.e. values from earlier than 2018 are included so the average is skewed down again as the 0s are now also factored in).&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jan 2022 07:24:28 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-dynamic-average-of-subset-of-years/m-p/2292768#M56418</guid>
      <dc:creator>ThomasSan</dc:creator>
      <dc:date>2022-01-20T07:24:28Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate dynamic average of subset of years</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-dynamic-average-of-subset-of-years/m-p/2292813#M56422</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;&lt;BR /&gt;The correctaverage should reference facttable except for the maxyear variable. As you can see with my example I am referring to 'SlicerYears' which is my fact table.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jan 2022 07:44:07 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-dynamic-average-of-subset-of-years/m-p/2292813#M56422</guid>
      <dc:creator>ValtteriN</dc:creator>
      <dc:date>2022-01-20T07:44:07Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate dynamic average of subset of years</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-dynamic-average-of-subset-of-years/m-p/2292953#M56431</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="343431" data-lia-user-login="ValtteriN" class="lia-mention lia-mention-user"&gt;ValtteriN&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Oh, I got what you meant. This is my code at the moment:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;SFC Sales on Total Sales (2018 onwards) = 
var maxyear =
MAX('Date'[Year])

var sfcsales=
calculate(
    sum(SalesOnSFC[Sales by SFC]),
    ALL(SalesOnSFC),
    year(SalesOnSFC[date])=maxyear,
    year(SalesOnSFC[date]) &amp;gt;= 2018
)

var allsales=
calculate(
    sum(TotalSales[Sales]),
    ALL(TotalSales),
    year(TotalSales[date])=maxyear,
    year(TotalSales[date]) &amp;gt;= 2018
)

var ratio =
DIVIDE(
    sfcsales,
    allsales,
    blank()
)

return ratio   &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is also a screenshot of my data modell regarding all three tables involved&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;However, it seems that now I only get the latest value returned&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;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jan 2022 08:46:39 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-dynamic-average-of-subset-of-years/m-p/2292953#M56431</guid>
      <dc:creator>ThomasSan</dc:creator>
      <dc:date>2022-01-20T08:46:39Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate dynamic average of subset of years</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-dynamic-average-of-subset-of-years/m-p/2292972#M56432</link>
      <description>&lt;P&gt;Hi &lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="329838" data-lia-user-login="ThomasSan" class="lia-mention lia-mention-user"&gt;ThomasSan&lt;/a&gt;&amp;nbsp;,&lt;BR /&gt;&lt;BR /&gt;Let's step back a bit, now that I see your visual I have another idea. You could just include a visual level filter where year&amp;gt;2018 and then calculate the ratio normally.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jan 2022 08:52:32 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-dynamic-average-of-subset-of-years/m-p/2292972#M56432</guid>
      <dc:creator>ValtteriN</dc:creator>
      <dc:date>2022-01-20T08:52:32Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate dynamic average of subset of years</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-dynamic-average-of-subset-of-years/m-p/2293325#M56445</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="343431" data-lia-user-login="ValtteriN" class="lia-mention lia-mention-user"&gt;ValtteriN&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That was the final key. So the following code is the one that works:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;SFC Sales on Total Sales (2018 onwards) = 
var sfcsales=
calculate(
    sum(SalesOnSFC[Sales by SFC]),
    year(SalesOnSFC[date]) &amp;gt;= 2018
)

var allsales=
calculate(
    sum(TotalSales[Sales]),
    year(TotalSales[date]) &amp;gt;= 2018
)

var ratio =
DIVIDE(
    sfcsales,
    allsales,
    blank()
)

return ratio  &lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 20 Jan 2022 11:51:17 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-dynamic-average-of-subset-of-years/m-p/2293325#M56445</guid>
      <dc:creator>ThomasSan</dc:creator>
      <dc:date>2022-01-20T11:51:17Z</dc:date>
    </item>
  </channel>
</rss>

