<?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: Calculating time difference before and after a specific time in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculating-time-difference-before-and-after-a-specific-time/m-p/1496363#M28848</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;// Assuming you have a table
// with columns Datetime and Machine.
// When you select a machine,
// you want to know the diff
// between the two closest times
// to 6am, one before and one after it.
// If more than 1 machine is selected,
// BLANK should be returned. Bear in
// mind that the measure honours all
// the currently set filters.
// Let the table be T.

[Diff (min)] =
var __onlyOneMachineVisible = HASONEVALUE( T[Machine] )
var __minDate = INT( MIN( T[Datetime] ) )
var __maxDate = INT( MAX( T[Datetime] ) )
var __onlyOneDayVisible = ( __minDate = __maxDate )
var __canCalculate = true()
    &amp;amp;&amp;amp; __onlyOneMachineVisible
    &amp;amp;&amp;amp; __onlyOneDayVisible
var __output =
    if( __canCalculate,
        
        var __closestBefore6am =
            CALCULATE(
                MAX( T[Datetime] ),
                KEEPFILTERS(
                    // We make use of the fact
                    // that one can compare datetimes
                    // with real numbers since a date
                    // under the hood is a real number,
                    // where the integer part stands for
                    // the day and the fractional part
                    // stands for the hour.
                    T[Datetime] &amp;lt;= __minDate + .25
                )
            )
        var __closestAfter6am =
            CALCULATE(
                MIN( T[Datetime] ),
                KEEPFILTERS(
                    T[Datetime] &amp;gt; __minDate + .25
                )
            )
        var __shouldCalculate =
            and(
                not ISBLANK( __closestBefore6am ),
                not ISBLANK( __closestAfter6am )
            )
        var __diff =
            if( __shouldCalculate,
                DATEDIFF( 
                    __closestBefore6am,
                    __closestAfter6am,
                    MINUTE
                )
            )
        return
            __diff
    )
return
    __output&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 16 Nov 2020 12:30:05 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2020-11-16T12:30:05Z</dc:date>
    <item>
      <title>Calculating time difference before and after a specific time</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculating-time-difference-before-and-after-a-specific-time/m-p/1495882#M28833</link>
      <description>&lt;P&gt;Hi, I'm a bit stumped on this problem and was hoping for some help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need to find the &lt;STRONG&gt;difference between&lt;/STRONG&gt; the last date/time &lt;STRONG&gt;BEFORE 6am&lt;/STRONG&gt;, and the first date/time &lt;STRONG&gt;AFTER 6am&lt;/STRONG&gt;. I'm trying to find out the time it takes for 2 shifts to handover in the morning. In this case it would be 16 mins. Any help would be much appreciated&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Date / time&lt;/TD&gt;&lt;TD&gt;Machine&lt;/TD&gt;&lt;TD&gt;Widgets&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01/01/20 05:54&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;40&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;01/01/20 05:57&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;A&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;37&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;01/01/20 06:13&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;A&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;42&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;01/01/20 06:22&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;41&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Mon, 16 Nov 2020 09:18:56 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculating-time-difference-before-and-after-a-specific-time/m-p/1495882#M28833</guid>
      <dc:creator>shane7mcdonald</dc:creator>
      <dc:date>2020-11-16T09:18:56Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating time difference before and after a specific time</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculating-time-difference-before-and-after-a-specific-time/m-p/1495918#M28834</link>
      <description>&lt;P&gt;Hi &lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="126932" data-lia-user-login="shane7mcdonald" class="lia-mention lia-mention-user"&gt;shane7mcdonald&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Where do you want this, in a measure? If so:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Measure =
VAR date_ =
    INT ( MAX ( Table1[DateTime] ) )
VAR lastBefore6_ =
    CALCULATE (
        MAX ( Table1[DateTime] ),
        Table1[DateTime] &amp;lt; ( date_ + ( 6 / 24 ) )
    )
VAR firstAfter6_ =
    CALCULATE (
        MIN ( Table1[DateTime] ),
        Table1[DateTime] &amp;gt;= ( date_ + ( 6 / 24 ) )
    )
VAR diffInMinutes_ = ( firstAfter6_ - lastBefore6_ ) * 24 * 60
RETURN
    diffInMinutes_&lt;/LI-CODE&gt;
&lt;P&gt;Take into account though that the code above is tailored to the table you show. I guess&amp;nbsp;you are looking for a more general result, like the average of that time difference across days, etc. You'll have to tweak it a bit for that, but the main logic will be the same.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Please mark the question solved when done and consider &lt;FONT color="#FF9900"&gt;giving a thumbs up if posts are helpful.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;Contact me privately for support with any larger-scale BI needs, tutoring, etc.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;Cheers&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Nov 2020 09:25:35 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculating-time-difference-before-and-after-a-specific-time/m-p/1495918#M28834</guid>
      <dc:creator>AlB</dc:creator>
      <dc:date>2020-11-16T09:25:35Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating time difference before and after a specific time</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculating-time-difference-before-and-after-a-specific-time/m-p/1496363#M28848</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="csharp"&gt;// Assuming you have a table
// with columns Datetime and Machine.
// When you select a machine,
// you want to know the diff
// between the two closest times
// to 6am, one before and one after it.
// If more than 1 machine is selected,
// BLANK should be returned. Bear in
// mind that the measure honours all
// the currently set filters.
// Let the table be T.

[Diff (min)] =
var __onlyOneMachineVisible = HASONEVALUE( T[Machine] )
var __minDate = INT( MIN( T[Datetime] ) )
var __maxDate = INT( MAX( T[Datetime] ) )
var __onlyOneDayVisible = ( __minDate = __maxDate )
var __canCalculate = true()
    &amp;amp;&amp;amp; __onlyOneMachineVisible
    &amp;amp;&amp;amp; __onlyOneDayVisible
var __output =
    if( __canCalculate,
        
        var __closestBefore6am =
            CALCULATE(
                MAX( T[Datetime] ),
                KEEPFILTERS(
                    // We make use of the fact
                    // that one can compare datetimes
                    // with real numbers since a date
                    // under the hood is a real number,
                    // where the integer part stands for
                    // the day and the fractional part
                    // stands for the hour.
                    T[Datetime] &amp;lt;= __minDate + .25
                )
            )
        var __closestAfter6am =
            CALCULATE(
                MIN( T[Datetime] ),
                KEEPFILTERS(
                    T[Datetime] &amp;gt; __minDate + .25
                )
            )
        var __shouldCalculate =
            and(
                not ISBLANK( __closestBefore6am ),
                not ISBLANK( __closestAfter6am )
            )
        var __diff =
            if( __shouldCalculate,
                DATEDIFF( 
                    __closestBefore6am,
                    __closestAfter6am,
                    MINUTE
                )
            )
        return
            __diff
    )
return
    __output&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Nov 2020 12:30:05 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculating-time-difference-before-and-after-a-specific-time/m-p/1496363#M28848</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2020-11-16T12:30:05Z</dc:date>
    </item>
  </channel>
</rss>

