<?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: DAX measure to calculate a rolling average of the lead times of the last 3 purchases in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-to-calculate-a-rolling-average-of-the-lead-times-of/m-p/2573197#M73589</link>
    <description>&lt;P&gt;Hi Owen,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks so much for your detailed response, your answer is perfect and super clear.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In our production model we have a date table, I'll make sure I refer to that in the finalised query as per your excellent examples.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One Query, I had an issue initally when updating my code, i was still getting incorrect responses.&lt;BR /&gt;Take a look at the third column (Lead time/average lead time) of the table, when the third column is set to 'dont summarise' I get the wrong value for the rolling average measure in the 4th column.&lt;BR /&gt;&lt;BR /&gt;I came across this by comparing the queries that Power BI is generating in the back end (performance analyser) and noticed mine was different and had more columns in the queries.&amp;nbsp;&amp;nbsp;&lt;BR /&gt;Any insight into why this is the case?&lt;BR /&gt;It's also the reson I didn't figure this out myself, I played with the values fuction but was still getting incorrect answers due to the don't summarise on the 'lead time' column.&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Cheers,&lt;/P&gt;</description>
    <pubDate>Sun, 12 Jun 2022 12:02:15 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2022-06-12T12:02:15Z</dc:date>
    <item>
      <title>DAX measure to calculate a rolling average of the lead times of the last 3 purchases</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-to-calculate-a-rolling-average-of-the-lead-times-of/m-p/2572855#M73567</link>
      <description>&lt;P&gt;Hi Folks,&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Long time lerker, first time poster &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Really hoping someone can assist/guide me a little.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;I’m trying to draw a chart that demonstrates a trend line of a rolling averages of lead times for items that are sporadically purchased.&lt;/P&gt;&lt;P&gt;The chart will be shown in a drill through report for specific items thus will only be displayed for single items at a time. The main report already shows how many items have increased/decreased lead times when selecting two time periods, the user then selects and item to drill into and find out further granular details.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;I need to implement the formula as a measure as the end user will be able to filter by dates, supplier, purchase location and more.&lt;/P&gt;&lt;P&gt;I'd also like to have the ability to look at a supplier's lead time (Regardless of item) in another report and would expect this formula could potentially assist here.&lt;BR /&gt;I’ve utilised the following formula at this stage (found &lt;A href="https://community.powerbi.com/t5/DAX-Commands-and-Tips/Calculate-average-on-the-last-10-dates-records-in-table/m-p/1290619" target="_blank"&gt;here&lt;/A&gt;) but it doesn’t correctly ignore the row context to draw a trend chart.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Lead time past 3 orders =&lt;/P&gt;&lt;P&gt;VAR thisdate =&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; MAX ( 'Supply Lead Time'[Order Date] )&lt;/P&gt;&lt;P&gt;VAR last3leadtimes =&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TOPN (3, FILTER ( ALL ( 'Supply Lead Time'[Order Date] ), 'Supply Lead Time'[Order Date] &amp;lt;= thisdate ), 'Supply Lead Time'[Order Date], DESC )&lt;/P&gt;&lt;P&gt;RETURN&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; CALCULATE ( AVERAGE( 'Supply Lead Time'[Lead time] ), last3leadtimes )&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I spent a couple of hours testing and trying to make it work the way I described through adding in additional filter expressions to no avail.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The Model we have is a star schema with a fact table of lead times (Millions of Purchase orders) and a number of dimension tables including:&lt;BR /&gt;- Supplier (Thousands)&lt;BR /&gt;- Item details (~ A hundred thousand of unique items)&lt;BR /&gt;- Location (Hundreds),&lt;BR /&gt;- Date (5 years)&lt;BR /&gt;&lt;BR /&gt;I’ve attached a &lt;A href="https://1drv.ms/u/s!AlaX8S_BJcZ05xje2tBfCifrfHCI?e=qW4hki" target="_self"&gt;PBIX&lt;/A&gt; as a basic example of data and an &lt;A href="https://1drv.ms/x/s!AlaX8S_BJcZ05xnhXRmGcgXau7Oo?e=fhOSwm" target="_self"&gt;excel&lt;/A&gt; file outlining how I’d like it to calculate and chart. &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 11 Jun 2022 12:49:40 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-to-calculate-a-rolling-average-of-the-lead-times-of/m-p/2572855#M73567</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2022-06-11T12:49:40Z</dc:date>
    </item>
    <item>
      <title>Re: DAX measure to calculate a rolling average of the lead times of the last 3 purchases</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-to-calculate-a-rolling-average-of-the-lead-times-of/m-p/2572899#M73575</link>
      <description>&lt;P&gt;Hi&amp;nbsp;Anonymous&lt;/LI-USER&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The immediate issue is due to this expression:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;FILTER (
    ALL ( 'Supply Lead Time'[Order Date] ),
    'Supply Lead Time'[Order Date] &amp;lt;= thisdate
)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This will return all Order Dates on or before &lt;STRONG&gt;thisdate&lt;/STRONG&gt;, ignoring any other filters such as Item Code. This is because when ALL ( ... ) is used to return a table, it all values in the specified column(s) regardless of filters.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Without making any other changes, a measure like this should fix this problem:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Lead time past 3 orders (without date table) = 
VAR thisdate =
    MAX ( 'Supply Lead Time'[Order Date] )
VAR PreviousDates =
    CALCULATETABLE (
        VALUES ( 'Supply Lead Time'[Order Date] ),
        'Supply Lead Time'[Order Date] &amp;lt;= thisdate
    )
VAR last3leadtimes =
    TOPN ( 3, PreviousDates, 'Supply Lead Time'[Order Date], DESC )
RETURN
    CALCULATE ( AVERAGE( 'Supply Lead Time'[Lead time] ), last3leadtimes )&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The expression for &lt;STRONG&gt;PreviousDates&lt;/STRONG&gt; ensures that the Order Date filter is modified but other filters are retained.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A separate suggestion is to consider adding a Date table to your model, with a relationship to Order Date, and disabling Auto Date/Time under &lt;STRONG&gt;File &amp;gt; Options &amp;amp; Settings &amp;gt; Options &amp;gt; Current File &amp;gt; Data Load &amp;gt; Time intelligence &amp;gt; Auto date/time.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With a Date table, you would need to ensure that you only apply filters on columns of 'Date', not 'Supply Lead Time'[Date].&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is generally preferable to modify Date filters in a Date dimension rather than fact table. I've attached the PBIX containing a measure modified to use a 'Date' table.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;Owen&lt;/P&gt;</description>
      <pubDate>Sat, 11 Jun 2022 14:48:11 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-to-calculate-a-rolling-average-of-the-lead-times-of/m-p/2572899#M73575</guid>
      <dc:creator>OwenAuger</dc:creator>
      <dc:date>2022-06-11T14:48:11Z</dc:date>
    </item>
    <item>
      <title>Re: DAX measure to calculate a rolling average of the lead times of the last 3 purchases</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-to-calculate-a-rolling-average-of-the-lead-times-of/m-p/2573197#M73589</link>
      <description>&lt;P&gt;Hi Owen,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks so much for your detailed response, your answer is perfect and super clear.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In our production model we have a date table, I'll make sure I refer to that in the finalised query as per your excellent examples.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;One Query, I had an issue initally when updating my code, i was still getting incorrect responses.&lt;BR /&gt;Take a look at the third column (Lead time/average lead time) of the table, when the third column is set to 'dont summarise' I get the wrong value for the rolling average measure in the 4th column.&lt;BR /&gt;&lt;BR /&gt;I came across this by comparing the queries that Power BI is generating in the back end (performance analyser) and noticed mine was different and had more columns in the queries.&amp;nbsp;&amp;nbsp;&lt;BR /&gt;Any insight into why this is the case?&lt;BR /&gt;It's also the reson I didn't figure this out myself, I played with the values fuction but was still getting incorrect answers due to the don't summarise on the 'lead time' column.&amp;nbsp; &amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Cheers,&lt;/P&gt;</description>
      <pubDate>Sun, 12 Jun 2022 12:02:15 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-to-calculate-a-rolling-average-of-the-lead-times-of/m-p/2573197#M73589</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2022-06-12T12:02:15Z</dc:date>
    </item>
    <item>
      <title>Re: DAX measure to calculate a rolling average of the lead times of the last 3 purchases</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-to-calculate-a-rolling-average-of-the-lead-times-of/m-p/2573458#M73614</link>
      <description>&lt;P&gt;You're welcome, and glad it's working &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Ah right - good point. I would generally suggest not including unaggregated numerical columns from your fact table in a visual.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The strict rule some follow to avoid this sort of issue is:&lt;/P&gt;
&lt;P&gt;Hide all fact table columns and never include them in visuals. Explicit measures (i.e. written with DAX) must be used to display any value based on columns of a fact table. All columns used for grouping or filtering must be in separate dimension tables.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To explain further:&lt;/P&gt;
&lt;P&gt;When you include a column in the visual set to "do not summarize" (as opposed to an implicit/explicit measure with SUM/AVERAGE etc), this becomes an additional filter. By including "Lead time" this way, each row of the table includes that specific Lead time value as a filter, and this will interfere with the calculation.&lt;/P&gt;
&lt;P&gt;For example, on the last row of the screenshot where Item Code = A, the filters are:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Item Code = A&lt;/LI&gt;
&lt;LI&gt;Order Date = 2021-04-01&lt;/LI&gt;
&lt;LI&gt;Lead time = 4&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;In calculating "Lead time past 3 orders", we want to keep Item Code, we override Order Date, but if Lead time = 4 remains as a filter, we would only include the past 3 orders where Lead time = 4.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The solution is to use a measure if we want to display Lead time, such as Average Lead time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;Owen&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jun 2022 00:35:24 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/DAX-measure-to-calculate-a-rolling-average-of-the-lead-times-of/m-p/2573458#M73614</guid>
      <dc:creator>OwenAuger</dc:creator>
      <dc:date>2022-06-13T00:35:24Z</dc:date>
    </item>
  </channel>
</rss>

