<?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: Calculate stoptime with shift in DAX in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-stoptime-with-shift-in-DAX/m-p/3086469#M107924</link>
    <description>&lt;P&gt;TSQL Solution:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Drop Table IF Exists #ResultTable&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Create table #ResultTable ([Date] date)&lt;BR /&gt;Declare @StartResult datetime ,@EndResult datetime&lt;/P&gt;&lt;P&gt;select @StartResult = MIN(StartDate), @EndResult =Max(FinishDate)&lt;BR /&gt;from [dbo].[InputTable]&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;-- Create CALENDAR ( MIN ( InputTable[StartDate] ), MAX ( InputTable[FinishDate] )&lt;BR /&gt;While (@StartResult &amp;lt;&amp;gt; @EndResult)&lt;BR /&gt;Begin&lt;BR /&gt;insert into #ResultTable&lt;BR /&gt;select @StartResult&lt;BR /&gt;Set @StartResult = @StartResult+1&lt;BR /&gt;End&lt;BR /&gt;insert into #ResultTable&lt;BR /&gt;select @EndResult&lt;/P&gt;&lt;P&gt;--"Date", [Date],&lt;BR /&gt;-- "Shift", [Shift],&lt;BR /&gt;-- "From", [From] + [Date],&lt;BR /&gt;-- "To", [To] + [Date]&lt;BR /&gt;GO&lt;/P&gt;&lt;P&gt;With cte as (&lt;/P&gt;&lt;P&gt;select *,case when Shift=2 then DATEADD(day,1,[TOs]) else [Tos] End as [TO]&lt;BR /&gt;&lt;BR /&gt;from (&lt;BR /&gt;select Date,Shift, CONVERT(DATETIME, CONVERT(CHAR(8), Date, 112)&lt;BR /&gt;+ ' ' + CONVERT(CHAR(8),[From], 108)) as [From] ,&lt;BR /&gt;CONVERT(DATETIME, CONVERT(CHAR(8), Date, 112)&lt;BR /&gt;+ ' ' + CONVERT(CHAR(8),[TO], 108)) as [TOs]&lt;BR /&gt;from #ResultTable cross join [dbo].[GuidlineTable] )T&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;)&lt;BR /&gt;-- VAR T1 = FILTER ( InputTable, [StartDate] + [StartTime] &amp;lt; [To] &amp;amp;&amp;amp; [FinishDate] + [FinishTime] &amp;gt; [From] )&lt;BR /&gt;,cte2 as&lt;BR /&gt;(&lt;BR /&gt;&lt;BR /&gt;select CONVERT(DATETIME, StartDate&lt;BR /&gt;+ ' ' + CONVERT(CHAR(8),StartTime, 108)) as Start ,&lt;BR /&gt;CONVERT(DATETIME,FinishDate&lt;BR /&gt;+ ' ' + CONVERT(CHAR(8),FinishTime, 108)) as Finish&lt;BR /&gt;from InputTable&lt;BR /&gt;&lt;BR /&gt;)&lt;/P&gt;&lt;P&gt;select Date , Shift ,Sum(DATEDIFF(Hour,S,E) )as StopTime&lt;BR /&gt;from (&lt;BR /&gt;select&lt;BR /&gt;&lt;BR /&gt;*, case when Start &amp;gt; = [From] Then Start else [From] End As S&lt;BR /&gt;,case when Finish &amp;lt;= [TO] then Finish else [TO] End as E&lt;BR /&gt;from (&lt;BR /&gt;select *&lt;/P&gt;&lt;P&gt;from cte cross join cte2&lt;BR /&gt;where Start &amp;lt; [TO] and Finish &amp;gt; [From]&lt;BR /&gt;)T)B&lt;/P&gt;&lt;P&gt;group by date ,Shift&lt;BR /&gt;order by date,Shift&lt;/P&gt;&lt;P&gt;-- "StopTime", SUMX ( CURRENTGROUP ( ), DATEDIFF ( MAX ( [From], [Start] ), MIN ( [To], [Finish] ), HOUR ) )&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;&lt;P&gt;&amp;nbsp;&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>Sun, 19 Feb 2023 11:06:13 GMT</pubDate>
    <dc:creator>zfadaie</dc:creator>
    <dc:date>2023-02-19T11:06:13Z</dc:date>
    <item>
      <title>Calculate stoptime with shift in DAX</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-stoptime-with-shift-in-DAX/m-p/3081451#M107412</link>
      <description>&lt;P&gt;I have a table with 5 columns "InputTable", based on "Guideline Table" i want to generate new table named "ResultTable" with 3 columns.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 15 Feb 2023 12:54:51 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-stoptime-with-shift-in-DAX/m-p/3081451#M107412</guid>
      <dc:creator>As9124141636</dc:creator>
      <dc:date>2023-02-15T12:54:51Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate stoptime with shift in DAX</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-stoptime-with-shift-in-DAX/m-p/3081533#M107420</link>
      <description>&lt;P&gt;hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="352493" data-lia-user-login="As9124141636" class="lia-mention lia-mention-user"&gt;As9124141636&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;can you explain the logic behind?&lt;/P&gt;</description>
      <pubDate>Wed, 15 Feb 2023 13:49:05 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-stoptime-with-shift-in-DAX/m-p/3081533#M107420</guid>
      <dc:creator>FreemanZ</dc:creator>
      <dc:date>2023-02-15T13:49:05Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate stoptime with shift in DAX</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-stoptime-with-shift-in-DAX/m-p/3082833#M107532</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="352493" data-lia-user-login="As9124141636" class="lia-mention lia-mention-user"&gt;As9124141636&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;Please refer to attached sample file with the proposed solution. You need to test the performance from your end. Perhaps simpler solutions are possible, however, for now this seems to work.&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;img /&gt;&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;ResultTable = 
GROUPBY ( 
    GENERATE (
        SELECTCOLUMNS (
            CROSSJOIN ( 
                CALENDAR ( MIN ( InputTable[StartDate] ), MAX ( InputTable[FinishDate] ) ),
                GuidlineTable
            ),
            "Date", [Date],
            "Shift", [Shift],
            "From", [From] + [Date],
            "To", [To] + [Date]
        ),
        VAR T1 = FILTER ( InputTable, [StartDate] + [StartTime] &amp;lt; [To] &amp;amp;&amp;amp; [FinishDate] + [FinishTime] &amp;gt; [From] )
        RETURN
            SELECTCOLUMNS ( T1, "Start", [StartDate] + [StartTime], "Finish", [FinishDate] + [FinishTime] )
    ),
    [Date],
    [Shift],
    "StopTime", SUMX ( CURRENTGROUP ( ), DATEDIFF ( MAX ( [From], [Start] ), MIN ( [To], [Finish] ), HOUR ) )
)&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 16 Feb 2023 07:29:18 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-stoptime-with-shift-in-DAX/m-p/3082833#M107532</guid>
      <dc:creator>tamerj1</dc:creator>
      <dc:date>2023-02-16T07:29:18Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate stoptime with shift in DAX</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-stoptime-with-shift-in-DAX/m-p/3084794#M107733</link>
      <description>&lt;P&gt;thanks for your exact solution. is it possible to do with TSQL in ssms?&lt;/P&gt;</description>
      <pubDate>Fri, 17 Feb 2023 10:21:57 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-stoptime-with-shift-in-DAX/m-p/3084794#M107733</guid>
      <dc:creator>As9124141636</dc:creator>
      <dc:date>2023-02-17T10:21:57Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate stoptime with shift in DAX</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-stoptime-with-shift-in-DAX/m-p/3084809#M107736</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="352493" data-lia-user-login="As9124141636" class="lia-mention lia-mention-user"&gt;As9124141636&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;Most probably yes but I'm no expert in TSQL. You can place a question in the desktop forum, I'm pretty sure many heroes&amp;nbsp;can do beautiful&amp;nbsp;TSQL over there.&lt;/P&gt;</description>
      <pubDate>Fri, 17 Feb 2023 10:31:14 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-stoptime-with-shift-in-DAX/m-p/3084809#M107736</guid>
      <dc:creator>tamerj1</dc:creator>
      <dc:date>2023-02-17T10:31:14Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate stoptime with shift in DAX</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-stoptime-with-shift-in-DAX/m-p/3086469#M107924</link>
      <description>&lt;P&gt;TSQL Solution:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Drop Table IF Exists #ResultTable&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Create table #ResultTable ([Date] date)&lt;BR /&gt;Declare @StartResult datetime ,@EndResult datetime&lt;/P&gt;&lt;P&gt;select @StartResult = MIN(StartDate), @EndResult =Max(FinishDate)&lt;BR /&gt;from [dbo].[InputTable]&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;-- Create CALENDAR ( MIN ( InputTable[StartDate] ), MAX ( InputTable[FinishDate] )&lt;BR /&gt;While (@StartResult &amp;lt;&amp;gt; @EndResult)&lt;BR /&gt;Begin&lt;BR /&gt;insert into #ResultTable&lt;BR /&gt;select @StartResult&lt;BR /&gt;Set @StartResult = @StartResult+1&lt;BR /&gt;End&lt;BR /&gt;insert into #ResultTable&lt;BR /&gt;select @EndResult&lt;/P&gt;&lt;P&gt;--"Date", [Date],&lt;BR /&gt;-- "Shift", [Shift],&lt;BR /&gt;-- "From", [From] + [Date],&lt;BR /&gt;-- "To", [To] + [Date]&lt;BR /&gt;GO&lt;/P&gt;&lt;P&gt;With cte as (&lt;/P&gt;&lt;P&gt;select *,case when Shift=2 then DATEADD(day,1,[TOs]) else [Tos] End as [TO]&lt;BR /&gt;&lt;BR /&gt;from (&lt;BR /&gt;select Date,Shift, CONVERT(DATETIME, CONVERT(CHAR(8), Date, 112)&lt;BR /&gt;+ ' ' + CONVERT(CHAR(8),[From], 108)) as [From] ,&lt;BR /&gt;CONVERT(DATETIME, CONVERT(CHAR(8), Date, 112)&lt;BR /&gt;+ ' ' + CONVERT(CHAR(8),[TO], 108)) as [TOs]&lt;BR /&gt;from #ResultTable cross join [dbo].[GuidlineTable] )T&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;)&lt;BR /&gt;-- VAR T1 = FILTER ( InputTable, [StartDate] + [StartTime] &amp;lt; [To] &amp;amp;&amp;amp; [FinishDate] + [FinishTime] &amp;gt; [From] )&lt;BR /&gt;,cte2 as&lt;BR /&gt;(&lt;BR /&gt;&lt;BR /&gt;select CONVERT(DATETIME, StartDate&lt;BR /&gt;+ ' ' + CONVERT(CHAR(8),StartTime, 108)) as Start ,&lt;BR /&gt;CONVERT(DATETIME,FinishDate&lt;BR /&gt;+ ' ' + CONVERT(CHAR(8),FinishTime, 108)) as Finish&lt;BR /&gt;from InputTable&lt;BR /&gt;&lt;BR /&gt;)&lt;/P&gt;&lt;P&gt;select Date , Shift ,Sum(DATEDIFF(Hour,S,E) )as StopTime&lt;BR /&gt;from (&lt;BR /&gt;select&lt;BR /&gt;&lt;BR /&gt;*, case when Start &amp;gt; = [From] Then Start else [From] End As S&lt;BR /&gt;,case when Finish &amp;lt;= [TO] then Finish else [TO] End as E&lt;BR /&gt;from (&lt;BR /&gt;select *&lt;/P&gt;&lt;P&gt;from cte cross join cte2&lt;BR /&gt;where Start &amp;lt; [TO] and Finish &amp;gt; [From]&lt;BR /&gt;)T)B&lt;/P&gt;&lt;P&gt;group by date ,Shift&lt;BR /&gt;order by date,Shift&lt;/P&gt;&lt;P&gt;-- "StopTime", SUMX ( CURRENTGROUP ( ), DATEDIFF ( MAX ( [From], [Start] ), MIN ( [To], [Finish] ), HOUR ) )&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;&lt;P&gt;&amp;nbsp;&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>Sun, 19 Feb 2023 11:06:13 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-stoptime-with-shift-in-DAX/m-p/3086469#M107924</guid>
      <dc:creator>zfadaie</dc:creator>
      <dc:date>2023-02-19T11:06:13Z</dc:date>
    </item>
  </channel>
</rss>

