<?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 Moving Average based on Date and Time Dimension in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-Moving-Average-based-on-Date-and-Time-Dimension/m-p/1719607#M35554</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I would describe myself as a beginner with PowerBI. I have a question about the handling of time dimensions.&lt;BR /&gt;&lt;BR /&gt;My objective is to see which post leads to an increase regarding the order amount wihtin the next 48 hours. Therefore I would calculate&amp;nbsp;a moving average based on the date and time.&lt;BR /&gt;For each day and each hour of the day there should be the average number of orders calculated for the next 48 hours. For example: If there are 300 Orders between 1st January 2021 8 am and&amp;nbsp;3rd January 2021 8 am the calculated value for&amp;nbsp;1st January 2021 8 am should be number_of_orders_in_intervall/number of days=300/2=150. For 9 am the same calculation will be made with the upper border of&amp;nbsp;3rd January 2021 9 am ...&lt;BR /&gt;&lt;BR /&gt;In my data model I distinguish between a time and a date dimension. Both are linked to both of my fact table via a foreign key.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Unfortunately I could not make it until yet. This is my DAX-Measure for the moving Average for Days only which seems to work well.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Moving Average Order (N Days Interval) = 

VAR C = CALCULATE(AVERAGEX(DIM_DATE,[Total Orders]+0),ALL(Dim_Date),FILTER(ALL(Dim_Date[Date]),Dim_Date[Date]&amp;gt;=MAX(Dim_Date[Date])
    &amp;amp;&amp;amp;Dim_Date[Date]&amp;lt;MAX(Dim_Date[Date])+'Days for Moving Average'[Days for Moving Average-Wert]))
VAR B = ADDCOLUMNS(Dim_Date,"Moving Average",C)  
RETURN CALCULATE(AVERAGEX(B,[Moving Average]),ALL(Dim_Date[Date]))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Now I want the calculate the moving average based on dates and hours.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;// Since there is no datetime Table in my Model I try to use crossjoin to get a table 
// with one row for each hour of each day (For two days there should be 48 rows)
var _table = CROSSJOIN(FILTER(Dim_Date,Dim_Date[Date]&amp;gt;=DATE(2021,01,01)),Dim_Time)


var _start = MAX(Dim_Date[Date])+TIME(MAX(Dim_Time[Hour of Day]),0,0))
// 'Days for Moving Average'[Days for Moving Average-Wert] is a 
// parameter to change the intervall for the average calculation e.g. from 01-01-2021 3 days in the future instead of 2 days
var _end = _start + 'Days for Moving Average'[Days for Moving Average-Wert]

var _average = CALCULATE(
    AVERAGEX(_table,[Total Orders]), 
    FILTER(
      _table,
      (Dim_Date[Date] + TIME(Dim_Time[Hour of Day],0,0) &amp;gt;= _start &amp;amp;&amp;amp; Dim_Date[Date] + TIME(Dim_Time[Hour of Day],0,0) &amp;lt;= _end 
  )
))

RETURN _average&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;BR /&gt;Unfortunately the code above does not work. If I use Date and TIME(Hour of Day,0,0) for the x-axis and the calculated moving average is zero.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;I would greatly appreciate any advice. Many thanks in advance.&amp;nbsp;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;</description>
    <pubDate>Tue, 16 Mar 2021 10:14:15 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2021-03-16T10:14:15Z</dc:date>
    <item>
      <title>Calculate Moving Average based on Date and Time Dimension</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-Moving-Average-based-on-Date-and-Time-Dimension/m-p/1719607#M35554</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I would describe myself as a beginner with PowerBI. I have a question about the handling of time dimensions.&lt;BR /&gt;&lt;BR /&gt;My objective is to see which post leads to an increase regarding the order amount wihtin the next 48 hours. Therefore I would calculate&amp;nbsp;a moving average based on the date and time.&lt;BR /&gt;For each day and each hour of the day there should be the average number of orders calculated for the next 48 hours. For example: If there are 300 Orders between 1st January 2021 8 am and&amp;nbsp;3rd January 2021 8 am the calculated value for&amp;nbsp;1st January 2021 8 am should be number_of_orders_in_intervall/number of days=300/2=150. For 9 am the same calculation will be made with the upper border of&amp;nbsp;3rd January 2021 9 am ...&lt;BR /&gt;&lt;BR /&gt;In my data model I distinguish between a time and a date dimension. Both are linked to both of my fact table via a foreign key.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Unfortunately I could not make it until yet. This is my DAX-Measure for the moving Average for Days only which seems to work well.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Moving Average Order (N Days Interval) = 

VAR C = CALCULATE(AVERAGEX(DIM_DATE,[Total Orders]+0),ALL(Dim_Date),FILTER(ALL(Dim_Date[Date]),Dim_Date[Date]&amp;gt;=MAX(Dim_Date[Date])
    &amp;amp;&amp;amp;Dim_Date[Date]&amp;lt;MAX(Dim_Date[Date])+'Days for Moving Average'[Days for Moving Average-Wert]))
VAR B = ADDCOLUMNS(Dim_Date,"Moving Average",C)  
RETURN CALCULATE(AVERAGEX(B,[Moving Average]),ALL(Dim_Date[Date]))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Now I want the calculate the moving average based on dates and hours.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;// Since there is no datetime Table in my Model I try to use crossjoin to get a table 
// with one row for each hour of each day (For two days there should be 48 rows)
var _table = CROSSJOIN(FILTER(Dim_Date,Dim_Date[Date]&amp;gt;=DATE(2021,01,01)),Dim_Time)


var _start = MAX(Dim_Date[Date])+TIME(MAX(Dim_Time[Hour of Day]),0,0))
// 'Days for Moving Average'[Days for Moving Average-Wert] is a 
// parameter to change the intervall for the average calculation e.g. from 01-01-2021 3 days in the future instead of 2 days
var _end = _start + 'Days for Moving Average'[Days for Moving Average-Wert]

var _average = CALCULATE(
    AVERAGEX(_table,[Total Orders]), 
    FILTER(
      _table,
      (Dim_Date[Date] + TIME(Dim_Time[Hour of Day],0,0) &amp;gt;= _start &amp;amp;&amp;amp; Dim_Date[Date] + TIME(Dim_Time[Hour of Day],0,0) &amp;lt;= _end 
  )
))

RETURN _average&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;BR /&gt;Unfortunately the code above does not work. If I use Date and TIME(Hour of Day,0,0) for the x-axis and the calculated moving average is zero.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;I would greatly appreciate any advice. Many thanks in advance.&amp;nbsp;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Tue, 16 Mar 2021 10:14:15 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-Moving-Average-based-on-Date-and-Time-Dimension/m-p/1719607#M35554</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2021-03-16T10:14:15Z</dc:date>
    </item>
  </channel>
</rss>

