<?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: Dynamic Format not working with Durations in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Dynamic-Format-not-working-with-Durations/m-p/4158126#M165232</link>
    <description>&lt;P&gt;Thanks for the update&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="411078" data-lia-user-login="nchr" class="lia-mention lia-mention-user"&gt;nchr&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I see, that's a bit of thorny issue!&lt;/P&gt;
&lt;P&gt;As you've pointed out, it seems that a variant-typed measure returning decimal/time values doesn't allow switching between decimal/time format strings!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The best option I can suggest is to use the formatted-value itself as a literal value (enclosed in double quotes) as the format string in the case of time values.&lt;/P&gt;
&lt;P&gt;There are some situations where this doesn't work (e.g. on the axis of a chart) but I think it should be acceptable in table visuals or similar.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PBIX with dummy example attached.&lt;/P&gt;
&lt;P&gt;This is the format string expression:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;VAR MetricType = SELECTEDVALUE ( Metric[Metric Type] )
VAR MetricFormat =
    SWITCH (
        MetricType,
        "Percentage", "#,0.00%",
        "Decimal", "0.0000",
        "Duration", 
        VAR MetricValue =
            [Metric Value]
        RETURN
            IF (
                MetricValue &amp;gt; 1 / 24,
                """" &amp;amp; FORMAT ( MetricValue, "hh:mm:ss" ) &amp;amp; """",
                """" &amp;amp; FORMAT ( MetricValue, "mm:ss" ) &amp;amp; """"
            )
    )
RETURN
    MetricFormat&lt;/LI-CODE&gt;
&lt;P&gt;Regards&lt;/P&gt;</description>
    <pubDate>Thu, 19 Sep 2024 00:52:04 GMT</pubDate>
    <dc:creator>OwenAuger</dc:creator>
    <dc:date>2024-09-19T00:52:04Z</dc:date>
    <item>
      <title>Dynamic Format not working with Durations</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Dynamic-Format-not-working-with-Durations/m-p/4156138#M165161</link>
      <description>&lt;P&gt;In my reports, I use durations in seconds for various metrics. It is handy to create visuals and make calculations, but when displaying the values, users prefer to see "hh:mm:ss" instead of the number of seconds.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I convert the durations to part-of-day decimal and use FORMAT() to display it as "hh:mm:ss" or "nn:ss" if less than an hour.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But this does not work with &lt;EM&gt;dynamic formating&lt;/EM&gt;: although i return a text string containing the appropriate format, it is handled as text instead of being applied to the decimal value as format stirng.&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;So, how can i use dynamic formatting to apply the appropriate format string to a duration?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The .pbix demonstrates the issue with a simple example (it's in weTransfer, can't upload here yet):&lt;/P&gt;&lt;P&gt;&lt;A href="https://we.tl/t-gdqaD1nf3d" target="_blank" rel="noopener"&gt;https://we.tl/t-gdqaD1nf3d&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Thank you,&lt;BR /&gt;Nikos&lt;/P&gt;</description>
      <pubDate>Wed, 18 Sep 2024 08:26:30 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Dynamic-Format-not-working-with-Durations/m-p/4156138#M165161</guid>
      <dc:creator>nchr</dc:creator>
      <dc:date>2024-09-18T08:26:30Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Format not working with Durations</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Dynamic-Format-not-working-with-Durations/m-p/4156826#M165182</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="411078" data-lia-user-login="nchr" class="lia-mention lia-mention-user"&gt;nchr&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(PBIX attached)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try changing the measure to:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Dynamic Formatted = 
CONVERT ( [Duration] / ( 24 * 60 * 60 ), DATETIME )&lt;/LI-CODE&gt;
&lt;P&gt;and the format string expression to:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;IF ( [Dynamic Formatted] &amp;gt; 1 / 24, "hh:mm:ss", "mm:ss" )&lt;/LI-CODE&gt;
&lt;P&gt;(it appears "mm" is required here).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When Power BI formats a measure in a visual it will only apply datetime format codes to a datetime value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does this work at your end?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Sep 2024 12:44:07 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Dynamic-Format-not-working-with-Durations/m-p/4156826#M165182</guid>
      <dc:creator>OwenAuger</dc:creator>
      <dc:date>2024-09-18T12:44:07Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Format not working with Durations</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Dynamic-Format-not-working-with-Durations/m-p/4157040#M165190</link>
      <description>&lt;P&gt;Hey&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="6799" data-lia-user-login="OwenAuger" class="lia-mention lia-mention-user"&gt;OwenAuger&lt;/a&gt;&amp;nbsp;, great to hear from you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yes, it works for that example, indeed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But my original scenario is more complex. I have a list of several metrics that I track (consider a Dim table with metric name and other attributes) and I use SWITCH() to retrieve the value for each metric's measure. Some are durations, but others are decimals or integers.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;When using diverse data types, the SWITCH() measure is cast into decimal anyway, losing any type I impose with CONVERT. In that case, the dynamic format does not work.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;I created an extra SWITCH measure that returns a value only for duration metrics, CONVERTing to datetime. In this case, i.e. when only blanks and datetimes are returned, it works.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;img /&gt;&lt;/P&gt;&lt;P&gt;So, I guess the issue is now that SWITCH measures returning diverse datatypes are cast into decimals and lose any datatype.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Sep 2024 13:56:27 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Dynamic-Format-not-working-with-Durations/m-p/4157040#M165190</guid>
      <dc:creator>nchr</dc:creator>
      <dc:date>2024-09-18T13:56:27Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Format not working with Durations</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Dynamic-Format-not-working-with-Durations/m-p/4158126#M165232</link>
      <description>&lt;P&gt;Thanks for the update&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="411078" data-lia-user-login="nchr" class="lia-mention lia-mention-user"&gt;nchr&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I see, that's a bit of thorny issue!&lt;/P&gt;
&lt;P&gt;As you've pointed out, it seems that a variant-typed measure returning decimal/time values doesn't allow switching between decimal/time format strings!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The best option I can suggest is to use the formatted-value itself as a literal value (enclosed in double quotes) as the format string in the case of time values.&lt;/P&gt;
&lt;P&gt;There are some situations where this doesn't work (e.g. on the axis of a chart) but I think it should be acceptable in table visuals or similar.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PBIX with dummy example attached.&lt;/P&gt;
&lt;P&gt;This is the format string expression:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;VAR MetricType = SELECTEDVALUE ( Metric[Metric Type] )
VAR MetricFormat =
    SWITCH (
        MetricType,
        "Percentage", "#,0.00%",
        "Decimal", "0.0000",
        "Duration", 
        VAR MetricValue =
            [Metric Value]
        RETURN
            IF (
                MetricValue &amp;gt; 1 / 24,
                """" &amp;amp; FORMAT ( MetricValue, "hh:mm:ss" ) &amp;amp; """",
                """" &amp;amp; FORMAT ( MetricValue, "mm:ss" ) &amp;amp; """"
            )
    )
RETURN
    MetricFormat&lt;/LI-CODE&gt;
&lt;P&gt;Regards&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2024 00:52:04 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Dynamic-Format-not-working-with-Durations/m-p/4158126#M165232</guid>
      <dc:creator>OwenAuger</dc:creator>
      <dc:date>2024-09-19T00:52:04Z</dc:date>
    </item>
    <item>
      <title>Re: Dynamic Format not working with Durations</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Dynamic-Format-not-working-with-Durations/m-p/4158721#M165246</link>
      <description>&lt;P&gt;Hey&amp;nbsp;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="6799" data-lia-user-login="OwenAuger" class="lia-mention lia-mention-user"&gt;OwenAuger&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;That is how I did things before dynamic formatting, with literal values as format strings, but it has its drawbacks as you mentioned.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For tables, it is ok; for graphs, what I am doing is to create another measure that returns the formatted value and use that as data label. It increases the model complexity though, I hoped that dynamic format would save the day!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyway, your help is much appreciated as usual, marking this as solved for the CONVERT tip.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Sep 2024 05:47:11 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Dynamic-Format-not-working-with-Durations/m-p/4158721#M165246</guid>
      <dc:creator>nchr</dc:creator>
      <dc:date>2024-09-19T05:47:11Z</dc:date>
    </item>
  </channel>
</rss>

