<?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: Convert String that represents duration to a Numeric Value (number of seconds) in DAX in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Convert-String-that-represents-duration-to-a-Numeric-Value/m-p/4415568#M175339</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="539674" data-lia-user-login="makarama" class="lia-mention lia-mention-user"&gt;makarama&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The best approach to convert a duration string into total seconds in DAX is to systematically extract numeric values preceding time unit words (Day, Hour, Minute, Second) and multiply them by their corresponding values in seconds. The solution must account for both singular and plural forms of the time units while handling missing values gracefully. The optimal DAX formula for this is:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;TotalSeconds =
VAR DurationString = 'Table'[Duration]

VAR Days = 
    IFERROR(
        LOOKUPVALUE( 
            VALUE(LEFT(DurationString, FIND(" Day", DurationString &amp;amp; " Day") - 1)), 
            TRUE, 
            TRUE
        ) * 86400, 
        0
    )

VAR Hours = 
    IFERROR(
        LOOKUPVALUE( 
            VALUE(LEFT(DurationString, FIND(" Hour", DurationString &amp;amp; " Hour") - 1)), 
            TRUE, 
            TRUE
        ) * 3600, 
        0
    )

VAR Minutes = 
    IFERROR(
        LOOKUPVALUE( 
            VALUE(LEFT(DurationString, FIND(" Minute", DurationString &amp;amp; " Minute") - 1)), 
            TRUE, 
            TRUE
        ) * 60, 
        0
    )

VAR Seconds = 
    IFERROR(
        LOOKUPVALUE( 
            VALUE(LEFT(DurationString, FIND(" Second", DurationString &amp;amp; " Second") - 1)), 
            TRUE, 
            TRUE
        ) * 1, 
        0
    )

RETURN
    Days + Hours + Minutes + Seconds
&lt;/LI-CODE&gt;
&lt;P&gt;This formula ensures that each time unit is correctly identified, the number in front of it is extracted, and the result is multiplied by the appropriate conversion factor. The IFERROR function prevents calculation errors when a particular time unit is missing. This method efficiently handles different input variations, such as "2 Days 3 Minutes 1 Second" producing 172981, "1 Hour" returning 3600, and "1 Day 3 Hours 1 Minute 10 Seconds" calculating to 97270. This approach is concise, robust, and flexible for various input formats.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best regards,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 18 Feb 2025 09:54:59 GMT</pubDate>
    <dc:creator>DataNinja777</dc:creator>
    <dc:date>2025-02-18T09:54:59Z</dc:date>
    <item>
      <title>Convert String that represents duration to a Numeric Value (number of seconds) in DAX</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Convert-String-that-represents-duration-to-a-Numeric-Value/m-p/4415487#M175337</link>
      <description>&lt;P&gt;Assuming that I have a string that contains any of the following words:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Day, Days, Hour, Hours, Minute, Minutes, Second, Seconds&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I wanna grab the number in front of every appearing word and&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;depending&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;on the type of measure multiply it with the right number to get the total number of seconds.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example if I have "2 Days 3 Minutes" then the code will calculate:&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;2&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;x 86400 +&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;3&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;x 60 =&amp;nbsp;&lt;SPAN&gt;172980&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;Example Input&lt;/TD&gt;&lt;TD&gt;Desired Outcome&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2 Days 3 Minutes 1 Second&lt;/TD&gt;&lt;TD&gt;&lt;SPAN&gt;172981&lt;/SPAN&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1 Hour&lt;/TD&gt;&lt;TD&gt;3600&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2 Hours 15 Minutes&lt;/TD&gt;&lt;TD&gt;8100&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1 Day 3 Hours 1 Minute 10 Seconds&lt;/TD&gt;&lt;TD&gt;97270&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;TD&gt;&amp;nbsp;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There are two challenges:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1) the string might missing one of the key words. It could be a full string like the last example in the table, but also only one of the words or a combination of those.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;2) Both singular and plural forms of the words appear (Day, Days etc) depending on if it is 1 or 2 or more days.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Feb 2025 09:22:19 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Convert-String-that-represents-duration-to-a-Numeric-Value/m-p/4415487#M175337</guid>
      <dc:creator>makarama</dc:creator>
      <dc:date>2025-02-18T09:22:19Z</dc:date>
    </item>
    <item>
      <title>Re: Convert String that represents duration to a Numeric Value (number of seconds) in DAX</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Convert-String-that-represents-duration-to-a-Numeric-Value/m-p/4415568#M175339</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="539674" data-lia-user-login="makarama" class="lia-mention lia-mention-user"&gt;makarama&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The best approach to convert a duration string into total seconds in DAX is to systematically extract numeric values preceding time unit words (Day, Hour, Minute, Second) and multiply them by their corresponding values in seconds. The solution must account for both singular and plural forms of the time units while handling missing values gracefully. The optimal DAX formula for this is:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;TotalSeconds =
VAR DurationString = 'Table'[Duration]

VAR Days = 
    IFERROR(
        LOOKUPVALUE( 
            VALUE(LEFT(DurationString, FIND(" Day", DurationString &amp;amp; " Day") - 1)), 
            TRUE, 
            TRUE
        ) * 86400, 
        0
    )

VAR Hours = 
    IFERROR(
        LOOKUPVALUE( 
            VALUE(LEFT(DurationString, FIND(" Hour", DurationString &amp;amp; " Hour") - 1)), 
            TRUE, 
            TRUE
        ) * 3600, 
        0
    )

VAR Minutes = 
    IFERROR(
        LOOKUPVALUE( 
            VALUE(LEFT(DurationString, FIND(" Minute", DurationString &amp;amp; " Minute") - 1)), 
            TRUE, 
            TRUE
        ) * 60, 
        0
    )

VAR Seconds = 
    IFERROR(
        LOOKUPVALUE( 
            VALUE(LEFT(DurationString, FIND(" Second", DurationString &amp;amp; " Second") - 1)), 
            TRUE, 
            TRUE
        ) * 1, 
        0
    )

RETURN
    Days + Hours + Minutes + Seconds
&lt;/LI-CODE&gt;
&lt;P&gt;This formula ensures that each time unit is correctly identified, the number in front of it is extracted, and the result is multiplied by the appropriate conversion factor. The IFERROR function prevents calculation errors when a particular time unit is missing. This method efficiently handles different input variations, such as "2 Days 3 Minutes 1 Second" producing 172981, "1 Hour" returning 3600, and "1 Day 3 Hours 1 Minute 10 Seconds" calculating to 97270. This approach is concise, robust, and flexible for various input formats.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best regards,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Feb 2025 09:54:59 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Convert-String-that-represents-duration-to-a-Numeric-Value/m-p/4415568#M175339</guid>
      <dc:creator>DataNinja777</dc:creator>
      <dc:date>2025-02-18T09:54:59Z</dc:date>
    </item>
    <item>
      <title>Re: Convert String that represents duration to a Numeric Value (number of seconds) in DAX</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Convert-String-that-represents-duration-to-a-Numeric-Value/m-p/4416780#M175386</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="539674" data-lia-user-login="makarama" class="lia-mention lia-mention-user"&gt;makarama&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can try this DAX formula:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;TotalSeconds = 
VAR dd = SEARCH ( " Day", [TimeString],, 0 )
VAR Days = IF ( dd = 0, 0, VALUE ( MID ( [TimeString], 1, dd - 1 ) ) )
VAR hh = SEARCH ( " Hour", [TimeString],, 0 )
VAR Hours = IF ( hh = 0, 0, VALUE ( MID ( [TimeString], MAX ( hh - 2, 1 ), 2 ) ) )
VAR mm = SEARCH ( " Minute", [TimeString],, 0 )
VAR Minutes = IF ( mm = 0, 0, VALUE ( MID ( [TimeString], MAX ( mm - 2, 1 ), 2 ) ) )
VAR ss = SEARCH ( " Second", [TimeString],, 0 )
VAR Seconds = IF ( ss = 0, 0, VALUE ( MID ( [TimeString], MAX ( ss - 2, 1 ), 2 ) ) )
RETURN
    Days * 86400 + Hours * 3600 + Minutes * 60 + Seconds
    &lt;/LI-CODE&gt;
&lt;P&gt;&lt;img /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best Regards,&lt;BR /&gt;Jing&lt;BR /&gt;&lt;EM&gt;If this post helps, please Accept it as Solution to help other members find it. Appreciate your Kudos! &lt;/EM&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 19 Feb 2025 02:50:49 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Convert-String-that-represents-duration-to-a-Numeric-Value/m-p/4416780#M175386</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2025-02-19T02:50:49Z</dc:date>
    </item>
  </channel>
</rss>

