<?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: Rows multiply when using Datediff in App Development</title>
    <link>https://community.qlik.com/t5/App-Development/Rows-multiply-when-using-Datediff/m-p/2116216#M91082</link>
    <description>&lt;P&gt;Seems like Period Duration should be a measure, not a dimension that's pre-calculated at the SQL level? Likewise you would want to sum Vacation_Days as a measure, and other fields may need to be either removed or converted to a measure. &lt;/P&gt;</description>
    <pubDate>Tue, 12 Sep 2023 19:29:26 GMT</pubDate>
    <dc:creator>Or</dc:creator>
    <dc:date>2023-09-12T19:29:26Z</dc:date>
    <item>
      <title>Rows multiply when using Datediff</title>
      <link>https://community.qlik.com/t5/App-Development/Rows-multiply-when-using-Datediff/m-p/2116191#M91080</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I’m making a report that calculates treatment days in different departments.&lt;/P&gt;
&lt;P&gt;My current SQL query is this (except that I limited the departments to one to simplify my post):&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;SELECT
CONCAT(TP.OWNER, '#', TP.PART) AS UniqueID,
TP.STARTED AS PERIOD_STARTED,
CASE WHEN TP.ENDED IS NULL THEN GETDATE()
ELSE TP.ENDED END AS PERIOD_ENDED,
D.NAME AS DEPARTMENT_NAME,
TP.AREA_OF_SERVICE,
TPV.STARTED AS VACATION_STARTED,
CASE WHEN TPV.STARTED IS NOT NULL AND TPV.ENDED IS NULL THEN GETDATE()
ELSE TPV.ENDED END AS VACATION_ENDED,
DATEDIFF("DAY", TP.STARTED, ISNULL(TP.ENDED, CURRENT_TIMESTAMP)) + (
CASE
WHEN TP.DELETED = 'N' AND TP.STARTED = TP.ENDED THEN 1
ELSE 0
END
) AS PERIOD_DURATION,
DATEDIFF
(
DAY,
TPV.STARTED,
TPV.ENDED
) AS VACATION_DAYS

FROM TREATMENTPERIODS TP

LEFT JOIN TREATMENTPERIODVACATIONS TPV
ON TP.OWNER = TPV.OWNER
AND TP.PART = TPV.PART
AND (
TPV.STARTED &amp;lt; TP.ENDED
or TP.ENDED is null
) AND (
TP.STARTED &amp;lt; TPV.ENDED
or TPV.ENDED is null
) AND TPV.DELETED = 'N'

INNER JOIN PATIENTS P
on TP.OWNER = P.OWNER

JOIN DEPARTMENTS D
ON D.OWNER = TP.DEPARTMENT

WHERE (
TP.ENDED &amp;gt;= '2023-01-01'
OR TP.ENDED IS NULL
)
AND TP.DELETED IN ('N', 'M')
AND TP.STARTED &amp;lt;= GETDATE()
AND TP.STARTED &amp;gt; '2022-01-01'
AND P.TEST_PATIENT = 'N'
AND D.NAME = 'DEP16';&lt;/LI-CODE&gt;
&lt;P&gt;&lt;BR /&gt;And the table looks like this:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-center" image-alt="Treatment Periods Table" style="width: 999px;"&gt;&lt;img src="https://community.qlik.com/t5/image/serverpage/image-id/116154i4BF3B618D3424320/image-size/large?v=v2&amp;amp;px=999" role="button" title="Example Table.JPG" alt="Treatment Periods Table" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Treatment Periods Table&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My problem is that because, for example, UniqueID 14568#1000020 has 4 different vacations within the same treatment period 2023/03/14 – 2023/06/11 (99 days), query gives 4 rows for UniqueID 14568#1000020 (1 per vacation date range) so period duration multiplies by 4.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Is there a way to make this so that every other row than the first for distinct UniqueID gives 0 to PERIOD_DURATION? Or is there any other way to do something similar so that PERIOD_DURATION would come only once per UniqueID? Or something similar to that?&lt;/P&gt;
&lt;P&gt;Thanks in advance!&lt;/P&gt;</description>
      <pubDate>Tue, 12 Sep 2023 18:16:31 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Rows-multiply-when-using-Datediff/m-p/2116191#M91080</guid>
      <dc:creator>megasupermoon</dc:creator>
      <dc:date>2023-09-12T18:16:31Z</dc:date>
    </item>
    <item>
      <title>Re: Rows multiply when using Datediff</title>
      <link>https://community.qlik.com/t5/App-Development/Rows-multiply-when-using-Datediff/m-p/2116216#M91082</link>
      <description>&lt;P&gt;Seems like Period Duration should be a measure, not a dimension that's pre-calculated at the SQL level? Likewise you would want to sum Vacation_Days as a measure, and other fields may need to be either removed or converted to a measure. &lt;/P&gt;</description>
      <pubDate>Tue, 12 Sep 2023 19:29:26 GMT</pubDate>
      <guid>https://community.qlik.com/t5/App-Development/Rows-multiply-when-using-Datediff/m-p/2116216#M91082</guid>
      <dc:creator>Or</dc:creator>
      <dc:date>2023-09-12T19:29:26Z</dc:date>
    </item>
  </channel>
</rss>

