<?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 total time someone spent playing a game in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-total-time-someone-spent-playing-a-game/m-p/4318704#M171458</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="894275" data-lia-user-login="sknnyftn" class="lia-mention lia-mention-user"&gt;sknnyftn&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just confirming the exact requirements before suggesting a solution:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Do you want to calculate the total time for which at least one game is "active"?&lt;BR /&gt;For example, two games active simultaneously for 5 minutes would give a result of 5 minutes?&lt;/LI&gt;
&lt;LI&gt;Alternatively, do you want to calculate the total time for each game separately and sum?&lt;BR /&gt;For example, two games active simultaneously for 5 minutes would give a result of 10 minutes?&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a plot of "Active Games" based on your sample data.&lt;/P&gt;
&lt;P&gt;Active Games = # games entered so far less # games exited so far.&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Option 1 above would measure the area under the minimum of the blue line and dotted red line.&lt;/LI&gt;
&lt;LI&gt;Option 2 above would measure the area under the blue line.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 08 Dec 2024 21:51:58 GMT</pubDate>
    <dc:creator>OwenAuger</dc:creator>
    <dc:date>2024-12-08T21:51:58Z</dc:date>
    <item>
      <title>Calculate total time someone spent playing a game</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-total-time-someone-spent-playing-a-game/m-p/4318682#M171457</link>
      <description>&lt;P&gt;I am trying to calculate the total time someone has spent playing a particular poker game. The problem I have is that they may be playing multiple games at the same time, or just one, and I have a simple table with chronological entry and exit times as shown below and in the pbx file. In cases where multiples are going at the same time, I don't have a preference on first in first out or last in first out methodology.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried splitting the data out into seperate entry and exit tables using CALCULATETABLE, attempted to add an index column to both with ROWNUMBER, and develop a measure to using DATEDIFF between the two tables, with no luck. I am at a loss on how to get this done at all and would really appreciate any help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A title="Game Data.pbx" href="https://drive.google.com/file/d/1LBTJCnEnhkbzW0ntqtUoQTH5YO44YUfV/view?usp=sharing" target="_self"&gt;Game Data.pbx&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Date/Time&lt;/TD&gt;&lt;TD&gt;Game&lt;/TD&gt;&lt;TD&gt;Type&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12/07/24 6:14 PM&lt;/TD&gt;&lt;TD&gt;Texas Hold'em 0.25/0.5&lt;/TD&gt;&lt;TD&gt;Game Exit&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12/07/24 3:36 PM&lt;/TD&gt;&lt;TD&gt;Texas Hold'em 0.25/0.5&lt;/TD&gt;&lt;TD&gt;Game Enter&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12/07/24 12:28 PM&lt;/TD&gt;&lt;TD&gt;Texas Hold'em 0.25/0.5&lt;/TD&gt;&lt;TD&gt;Game Exit&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12/07/24 10:56 AM&lt;/TD&gt;&lt;TD&gt;Texas Hold'em 0.25/0.5&lt;/TD&gt;&lt;TD&gt;Game Enter&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12/07/24 12:22 AM&lt;/TD&gt;&lt;TD&gt;Texas Hold'em 0.25/0.5&lt;/TD&gt;&lt;TD&gt;Game Exit&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12/07/24 12:21 AM&lt;/TD&gt;&lt;TD&gt;Texas Hold'em 0.25/0.5&lt;/TD&gt;&lt;TD&gt;Game Exit&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12/06/24 9:07 PM&lt;/TD&gt;&lt;TD&gt;Texas Hold'em 0.25/0.5&lt;/TD&gt;&lt;TD&gt;Game Enter&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12/06/24 7:05 PM&lt;/TD&gt;&lt;TD&gt;Texas Hold'em 0.25/0.5&lt;/TD&gt;&lt;TD&gt;Game Enter&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12/06/24 1:02 PM&lt;/TD&gt;&lt;TD&gt;Texas Hold'em 0.25/0.5&lt;/TD&gt;&lt;TD&gt;Game Exit&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;12/06/24 11:32 AM&lt;/TD&gt;&lt;TD&gt;Texas Hold'em 0.25/0.5&lt;/TD&gt;&lt;TD&gt;Game Enter&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Sun, 08 Dec 2024 20:36:36 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-total-time-someone-spent-playing-a-game/m-p/4318682#M171457</guid>
      <dc:creator>sknnyftn</dc:creator>
      <dc:date>2024-12-08T20:36:36Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate total time someone spent playing a game</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-total-time-someone-spent-playing-a-game/m-p/4318704#M171458</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="894275" data-lia-user-login="sknnyftn" class="lia-mention lia-mention-user"&gt;sknnyftn&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Just confirming the exact requirements before suggesting a solution:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Do you want to calculate the total time for which at least one game is "active"?&lt;BR /&gt;For example, two games active simultaneously for 5 minutes would give a result of 5 minutes?&lt;/LI&gt;
&lt;LI&gt;Alternatively, do you want to calculate the total time for each game separately and sum?&lt;BR /&gt;For example, two games active simultaneously for 5 minutes would give a result of 10 minutes?&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's a plot of "Active Games" based on your sample data.&lt;/P&gt;
&lt;P&gt;Active Games = # games entered so far less # games exited so far.&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Option 1 above would measure the area under the minimum of the blue line and dotted red line.&lt;/LI&gt;
&lt;LI&gt;Option 2 above would measure the area under the blue line.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 08 Dec 2024 21:51:58 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-total-time-someone-spent-playing-a-game/m-p/4318704#M171458</guid>
      <dc:creator>OwenAuger</dc:creator>
      <dc:date>2024-12-08T21:51:58Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate total time someone spent playing a game</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-total-time-someone-spent-playing-a-game/m-p/4318741#M171460</link>
      <description>&lt;P&gt;Owen, I am looking to accomplish option 2 and find area under the blue line.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Dec 2024 00:07:59 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-total-time-someone-spent-playing-a-game/m-p/4318741#M171460</guid>
      <dc:creator>sknnyftn</dc:creator>
      <dc:date>2024-12-09T00:07:59Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate total time someone spent playing a game</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-total-time-someone-spent-playing-a-game/m-p/4320150#M171517</link>
      <description>&lt;P&gt;Thanks for that&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="894275" data-lia-user-login="sknnyftn" class="lia-mention lia-mention-user"&gt;sknnyftn&lt;/a&gt;&amp;nbsp; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I have uploaded a PBIX with one possible solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;I added a Date table. This table is disconnected from Data, but the measure created below takes into account the min/max Date filtered and determines the Game Duration intersecting that date range.&lt;/LI&gt;
&lt;LI&gt;The Game Duration measures are shown below:&lt;/LI&gt;
&lt;/UL&gt;
&lt;LI-CODE lang="markup"&gt;Game Duration (days) = 
VAR MinDateTime = MIN ( 'Date'[Date] )
VAR MaxDateTime = MAX ( 'Date'[Date] ) + 1 -- Add 1 to represent exactly 0:00 at the end of MaxDate
VAR Result =
    SUMX (
        Data,
        VAR CurrentType = Data[Type]
        VAR Multiplier = 
            SWITCH (
                CurrentType,
                "Game Enter", 1,
                "Game Exit", -1
            )
        VAR EffectiveDateTime =
            MIN ( MAX ( Data[Date/Time], MinDateTime ), MaxDateTime )
        VAR Contribution =
            ( MaxDateTime - EffectiveDateTime ) * Multiplier
        RETURN
            Contribution
    )
RETURN
    Result&lt;/LI-CODE&gt;&lt;LI-CODE lang="markup"&gt;Game Duration (hours) = 
[Game Duration (days)] * 24&lt;/LI-CODE&gt;&lt;LI-CODE lang="markup"&gt;Game Duration (minutes) = 
[Game Duration (days)] * ( 24 * 60 )&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This appears to give the correct result based on your sample data. Does it work as expected at your end?&lt;/P&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards&lt;/P&gt;</description>
      <pubDate>Mon, 09 Dec 2024 17:30:03 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-total-time-someone-spent-playing-a-game/m-p/4320150#M171517</guid>
      <dc:creator>OwenAuger</dc:creator>
      <dc:date>2024-12-09T17:30:03Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate total time someone spent playing a game</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-total-time-someone-spent-playing-a-game/m-p/4320446#M171533</link>
      <description>&lt;P&gt;Owen, your solution appears to work perfectly. I tried with slightly different data and it tracked as expected. Thank you for the help.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Dec 2024 21:28:40 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-total-time-someone-spent-playing-a-game/m-p/4320446#M171533</guid>
      <dc:creator>sknnyftn</dc:creator>
      <dc:date>2024-12-09T21:28:40Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate total time someone spent playing a game</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-total-time-someone-spent-playing-a-game/m-p/4320669#M171542</link>
      <description>&lt;P&gt;Is there a straightforward way to switch between&amp;nbsp; options 1 and 2?&lt;/P&gt;</description>
      <pubDate>Tue, 10 Dec 2024 02:07:04 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-total-time-someone-spent-playing-a-game/m-p/4320669#M171542</guid>
      <dc:creator>sknnyftn</dc:creator>
      <dc:date>2024-12-10T02:07:04Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate total time someone spent playing a game</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-total-time-someone-spent-playing-a-game/m-p/4321521#M171564</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="894275" data-lia-user-login="sknnyftn" class="lia-mention lia-mention-user"&gt;sknnyftn&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Excellent question!&lt;/P&gt;
&lt;P&gt;I gave this some further thought, and we can write the measures differently in a way that makes switching between methods more straightforward.&lt;/P&gt;
&lt;P&gt;I've attached an updated PBIX.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The idea is:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Construct a list of distinct Date/Time values (Entry &amp;amp; Exit).&lt;/LI&gt;
&lt;LI&gt;For each Date/Time
&lt;OL class="lia-list-style-type-lower-alpha"&gt;
&lt;LI&gt;Determine the "next" Date/Time (I used the OFFSET function for this).&lt;/LI&gt;
&lt;LI&gt;Compute the number of "active games" (cumulative Entries less Exits).&lt;/LI&gt;
&lt;/OL&gt;
&lt;/LI&gt;
&lt;LI&gt;For each Date/Time, multiply the time interval (adjusted to be within the filtered date range) by either:
&lt;OL class="lia-list-style-type-lower-alpha"&gt;
&lt;LI&gt;&lt;STRONG&gt;Option 1:&lt;/STRONG&gt; 1 if there is at least one active game; or&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;Option 2:&lt;/STRONG&gt; The number of active games.&lt;/LI&gt;
&lt;/OL&gt;
&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Below are the measures involved.&lt;/P&gt;
&lt;P&gt;In the two final &lt;STRONG&gt;Game Duration&lt;/STRONG&gt; measures, the only difference is the CountOverlaps variable.&lt;/P&gt;
&lt;P&gt;TRUE corresponds to Option 2 and FALSE corresponds to Option 1.&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Entry Count = 
CALCULATE (
    COUNTROWS ( Data ),
    KEEPFILTERS ( Data[Type] = "Game Enter" )
)&lt;/LI-CODE&gt;&lt;LI-CODE lang="markup"&gt;Exit Count = 
CALCULATE (
    COUNTROWS ( Data ),
    KEEPFILTERS ( Data[Type] = "Game Exit" )
)&lt;/LI-CODE&gt;&lt;LI-CODE lang="markup"&gt;Entry Count Cumulative = 
VAR MaxDateTime = MAX ( Data[Date/Time] )
VAR Result =
    CALCULATE (
        [Entry Count],
        Data[Date/Time] &amp;lt;= MaxDateTime
    )
RETURN
    Result&lt;/LI-CODE&gt;&lt;LI-CODE lang="markup"&gt;Exit Count Cumulative = 
VAR MaxDateTime = MAX ( Data[Date/Time] )
VAR Result =
    CALCULATE (
        [Exit Count],
        Data[Date/Time] &amp;lt;= MaxDateTime
    )
RETURN
    Result&lt;/LI-CODE&gt;&lt;LI-CODE lang="markup"&gt;Active Games = 
[Entry Count Cumulative] - [Exit Count Cumulative]&lt;/LI-CODE&gt;&lt;LI-CODE lang="markup"&gt;Game Duration (days) = 
-- Option 2
VAR CountOverlaps = TRUE
VAR MinDateTime = MIN ( 'Date'[Date] )
VAR MaxDateTime = MAX ( 'Date'[Date] ) + 1 -- Add 1 to represent exactly 0:00 at the end of MaxDate

VAR DateTimes = VALUES ( Data[Date/Time] )
VAR Result =
    SUMX (
        DateTimes,
        VAR NextDateTime =
            OFFSET ( 1, DateTimes, ORDERBY ( Data[Date/Time] ) )
        VAR NextDateTimeNoBlank = COALESCE ( NextDateTime, MaxDateTime ) -- convert blank NextDateTime to MaxDateTime
        VAR EffectiveDateTimeStart =
            MIN ( MAX ( Data[Date/Time], MinDateTime ), MaxDateTime )
        VAR EffectiveDateTimeEnd =
            MIN ( MAX ( NextDateTimeNoBlank, MinDateTime ), MaxDateTime )
        VAR ActiveGames = [Active Games]
        VAR Multiplier =
            IF ( CountOverlaps, ActiveGames, ActiveGames &amp;gt;= 1 )
        VAR Contribution =
            ( EffectiveDateTimeEnd - EffectiveDateTimeStart ) * Multiplier
        RETURN
            Contribution
    )
RETURN
    Result&lt;/LI-CODE&gt;&lt;LI-CODE lang="markup"&gt;Game Duration non-duplicated (days) = 
-- Option 1
VAR CountOverlaps = FALSE
VAR MinDateTime = MIN ( 'Date'[Date] )
VAR MaxDateTime = MAX ( 'Date'[Date] ) + 1 -- Add 1 to represent exactly 0:00 at the end of MaxDate

VAR DateTimes = VALUES ( Data[Date/Time] )
VAR Result =
    SUMX (
        DateTimes,
        VAR NextDateTime =
            OFFSET ( 1, DateTimes, ORDERBY ( Data[Date/Time] ) )
        VAR NextDateTimeNoBlank = COALESCE ( NextDateTime, MaxDateTime ) -- convert blank NextDateTime to MaxDateTime
        VAR EffectiveDateTimeStart =
            MIN ( MAX ( Data[Date/Time], MinDateTime ), MaxDateTime )
        VAR EffectiveDateTimeEnd =
            MIN ( MAX ( NextDateTimeNoBlank, MinDateTime ), MaxDateTime )
        VAR ActiveGames = [Active Games]
        VAR Multiplier =
            IF ( CountOverlaps, ActiveGames, ActiveGames &amp;gt;= 1 )
        VAR Contribution =
            ( EffectiveDateTimeEnd - EffectiveDateTimeStart ) * Multiplier
        RETURN
            Contribution
    )
RETURN
    Result&lt;/LI-CODE&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;Does this work for you?&lt;/P&gt;
&lt;P&gt;This likely requires some testing to confirm performance is acceptable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards&lt;/P&gt;</description>
      <pubDate>Tue, 10 Dec 2024 11:02:28 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-total-time-someone-spent-playing-a-game/m-p/4321521#M171564</guid>
      <dc:creator>OwenAuger</dc:creator>
      <dc:date>2024-12-10T11:02:28Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate total time someone spent playing a game</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-total-time-someone-spent-playing-a-game/m-p/4323167#M171632</link>
      <description>&lt;P&gt;To calculate the total time someone spent playing a game with overlapping sessions, you can try sorting the data by date/time and calculating the time difference between entry and exit using DATEDIFF. You can sum these differences, keeping in mind overlapping sessions by ensuring you account for any concurrent games. If you're using Power BI, the EARLIER function might help with comparing rows for accurate calculation.&lt;/P&gt;&lt;P&gt;By the way, if you're looking for a fun game to relax and unwind, &lt;EM&gt;Car Parking Multiplayer&lt;/EM&gt; is a great option. You can find more details about it at &lt;A href="https://modcarparking.com/" target="_new" rel="noopener"&gt;&lt;SPAN&gt;https&lt;/SPAN&gt;&lt;SPAN&gt;://modcarparking&lt;/SPAN&gt;&lt;SPAN&gt;.com/&lt;/SPAN&gt;&lt;/A&gt;.&lt;/P&gt;</description>
      <pubDate>Wed, 11 Dec 2024 07:10:54 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-total-time-someone-spent-playing-a-game/m-p/4323167#M171632</guid>
      <dc:creator>abuislam</dc:creator>
      <dc:date>2024-12-11T07:10:54Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate total time someone spent playing a game</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-total-time-someone-spent-playing-a-game/m-p/4339200#M172300</link>
      <description>&lt;P&gt;Sorry I got tied up on a different project and didn't respond sooner but this works perfectly.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Dec 2024 22:42:41 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Calculate-total-time-someone-spent-playing-a-game/m-p/4339200#M172300</guid>
      <dc:creator>sknnyftn</dc:creator>
      <dc:date>2024-12-20T22:42:41Z</dc:date>
    </item>
  </channel>
</rss>

