<?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 Output difference between set expression and if loop in QlikView</title>
    <link>https://community.qlik.com/t5/QlikView/Output-difference-between-set-expression-and-if-loop/m-p/1535551#M439114</link>
    <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;I have the following problem. I try to calculate the average number of secondary diagnoses in the patients, who are still staying in the hospital. Some of them do have a secondary diagnosis or two, some does not have any yet. I have to take all in-patients and all existing secondary diagnoses by these patients into account and calculate a mean value per patient per department.&lt;/P&gt;&lt;P&gt;The data model looks as follows:&lt;/P&gt;&lt;PRE&gt;let vMaxDate = num(today()) - 30;
let vMinDate = $(vMaxDate) – 200;

Dep:
LOAD * INLINE [
    Units
    Surgery
    Angiology
    Traumatology
    Obstetrics
    Nephrology
];

Let vDepCount=FieldValueCount('Units');

Diagnosis:
LOAD * INLINE [
    Code
    J10
    K20
    L30
    M40
    N50
];

let vDiagCount = FieldValueCount('Code');

for i = $(vMinDate) to $(vMaxDate)

	let vAttDate = $(i);
	let vDuration = Round(Rand() * (25 - 1 ) + 1, 1);

	IF rand() &amp;gt; 0.3 THEN
		let vDisDate =$(vAttDate) + $(vDuration);
	ELSE			
		vDisDate =;
		let vDuration = $(vMaxDate) - $(vAttDate);
	END IF
	
	
	for j = 0 to $(vDepCount) - 1
		let vThisDep = Peek('Units', $(j), 'Dep');		
		let vN = $(i)* 1000 + $(j) + 100001; // Patient-ID.
	
		Patients: load '$(vN)' as PATID, date('$(vAttDate)') as AttDate, date('$(vDisDate)') as DisDate, $(vDuration) as Duration, '$(vThisDep)' as DEP
				  autogenerate(1);
	

		IF rand() &amp;gt; 0.5 THEN
			for k = 1 to 3
				let vD = floor(rand() * ($(vDiagCount)));
			ICD:
				load '$(vN)' as PATID, peek('Code', $(vD), 'Diagnosis') as ICDCODE, 'SEC' as ICDTYPE autogenerate (1);
			next k;
		END IF;

	next j;
next i;	


drop tables Dep, Diagnosis; 

vDepCount=;
vDiagCount=; 
vDatesCount=;
i=; 
vAttDate=;
vDuration=;

i=;
j=;
vThisDep=;
vN=;
vD=;
k=;
vDisDate=;
vMaxDate=;
vMinDate=;&lt;/PRE&gt;&lt;P&gt;Now the diagramm to calculate the average:&lt;/P&gt;&lt;P&gt;Dimension is DEP.&lt;/P&gt;&lt;P&gt;This expression works, but it has two „if“ loops:&lt;/P&gt;&lt;PRE&gt;=avg( 
	if(IsNull(DisDate),
		
		
	aggr( 
	count(distinct if(ICDTYPE = 'SEC',  ICDCODE))
	
	, PATID)
	)) &lt;/PRE&gt;&lt;P&gt;This set expression however seems to calculate the average only on the patients who does have at least one secondary diagnosis (thus exclude those who does not have any):&lt;/P&gt;&lt;PRE&gt;= avg( 
	{&amp;lt;PATID = E({1&amp;lt;DisDate={"*"}&amp;gt;} )&amp;gt;}
	aggr( count(
    {&amp;lt; PATID = E({1&amp;lt;DisDate={"*"}&amp;gt;} ),
	   ICDTYPE={'SEC'}
      &amp;gt;}
  			distinct ICDCODE), PATID)
  ) &lt;/PRE&gt;&lt;P&gt;This works:&lt;/P&gt;&lt;PRE&gt;=sum( aggr( count(
    {&amp;lt; PATID = E({1&amp;lt;DisDate={"*"}&amp;gt;} ), ICDTYPE={'SEC'} &amp;gt;}
  distinct ICDCODE),  PATID) )
/
count( {$&amp;lt; PATID = E({1&amp;lt;DisDate={"*"}&amp;gt;} )&amp;gt;} distinct PATID )   &lt;/PRE&gt;&lt;P&gt;However it is not very flexible if you want to calculate a median or fractile, for example.&lt;/P&gt;&lt;P&gt;There is a „compromiss“ solution, that works too:&lt;/P&gt;&lt;PRE&gt;=avg(
	
	{$&amp;lt;PATID = E({1&amp;lt;DisDate={"*"}&amp;gt;})&amp;gt;}

	aggr( 
		count(distinct  if(ICDTYPE = 'SEC',  ICDCODE)), PATID) 
	) &lt;/PRE&gt;&lt;P&gt;That is, outer set expression and inner if loop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;It seems, that the problem is the second set expression (inside the count). It seems to „communicate“ with the outer set expression (inside the avg).&lt;/P&gt;&lt;P&gt;Is there a way to produce the desired result with set expressions only?&lt;/P&gt;&lt;P&gt;(The adding of „&lt;EM&gt;, ICDTYPE=&lt;/EM&gt;“ to the outer set expression does not change the behavior.)&lt;/P&gt;&lt;P&gt;Thank you very much in advance!&lt;/P&gt;</description>
    <pubDate>Sat, 16 Nov 2024 04:39:04 GMT</pubDate>
    <dc:creator>qv_mypath</dc:creator>
    <dc:date>2024-11-16T04:39:04Z</dc:date>
    <item>
      <title>Output difference between set expression and if loop</title>
      <link>https://community.qlik.com/t5/QlikView/Output-difference-between-set-expression-and-if-loop/m-p/1535551#M439114</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;I have the following problem. I try to calculate the average number of secondary diagnoses in the patients, who are still staying in the hospital. Some of them do have a secondary diagnosis or two, some does not have any yet. I have to take all in-patients and all existing secondary diagnoses by these patients into account and calculate a mean value per patient per department.&lt;/P&gt;&lt;P&gt;The data model looks as follows:&lt;/P&gt;&lt;PRE&gt;let vMaxDate = num(today()) - 30;
let vMinDate = $(vMaxDate) – 200;

Dep:
LOAD * INLINE [
    Units
    Surgery
    Angiology
    Traumatology
    Obstetrics
    Nephrology
];

Let vDepCount=FieldValueCount('Units');

Diagnosis:
LOAD * INLINE [
    Code
    J10
    K20
    L30
    M40
    N50
];

let vDiagCount = FieldValueCount('Code');

for i = $(vMinDate) to $(vMaxDate)

	let vAttDate = $(i);
	let vDuration = Round(Rand() * (25 - 1 ) + 1, 1);

	IF rand() &amp;gt; 0.3 THEN
		let vDisDate =$(vAttDate) + $(vDuration);
	ELSE			
		vDisDate =;
		let vDuration = $(vMaxDate) - $(vAttDate);
	END IF
	
	
	for j = 0 to $(vDepCount) - 1
		let vThisDep = Peek('Units', $(j), 'Dep');		
		let vN = $(i)* 1000 + $(j) + 100001; // Patient-ID.
	
		Patients: load '$(vN)' as PATID, date('$(vAttDate)') as AttDate, date('$(vDisDate)') as DisDate, $(vDuration) as Duration, '$(vThisDep)' as DEP
				  autogenerate(1);
	

		IF rand() &amp;gt; 0.5 THEN
			for k = 1 to 3
				let vD = floor(rand() * ($(vDiagCount)));
			ICD:
				load '$(vN)' as PATID, peek('Code', $(vD), 'Diagnosis') as ICDCODE, 'SEC' as ICDTYPE autogenerate (1);
			next k;
		END IF;

	next j;
next i;	


drop tables Dep, Diagnosis; 

vDepCount=;
vDiagCount=; 
vDatesCount=;
i=; 
vAttDate=;
vDuration=;

i=;
j=;
vThisDep=;
vN=;
vD=;
k=;
vDisDate=;
vMaxDate=;
vMinDate=;&lt;/PRE&gt;&lt;P&gt;Now the diagramm to calculate the average:&lt;/P&gt;&lt;P&gt;Dimension is DEP.&lt;/P&gt;&lt;P&gt;This expression works, but it has two „if“ loops:&lt;/P&gt;&lt;PRE&gt;=avg( 
	if(IsNull(DisDate),
		
		
	aggr( 
	count(distinct if(ICDTYPE = 'SEC',  ICDCODE))
	
	, PATID)
	)) &lt;/PRE&gt;&lt;P&gt;This set expression however seems to calculate the average only on the patients who does have at least one secondary diagnosis (thus exclude those who does not have any):&lt;/P&gt;&lt;PRE&gt;= avg( 
	{&amp;lt;PATID = E({1&amp;lt;DisDate={"*"}&amp;gt;} )&amp;gt;}
	aggr( count(
    {&amp;lt; PATID = E({1&amp;lt;DisDate={"*"}&amp;gt;} ),
	   ICDTYPE={'SEC'}
      &amp;gt;}
  			distinct ICDCODE), PATID)
  ) &lt;/PRE&gt;&lt;P&gt;This works:&lt;/P&gt;&lt;PRE&gt;=sum( aggr( count(
    {&amp;lt; PATID = E({1&amp;lt;DisDate={"*"}&amp;gt;} ), ICDTYPE={'SEC'} &amp;gt;}
  distinct ICDCODE),  PATID) )
/
count( {$&amp;lt; PATID = E({1&amp;lt;DisDate={"*"}&amp;gt;} )&amp;gt;} distinct PATID )   &lt;/PRE&gt;&lt;P&gt;However it is not very flexible if you want to calculate a median or fractile, for example.&lt;/P&gt;&lt;P&gt;There is a „compromiss“ solution, that works too:&lt;/P&gt;&lt;PRE&gt;=avg(
	
	{$&amp;lt;PATID = E({1&amp;lt;DisDate={"*"}&amp;gt;})&amp;gt;}

	aggr( 
		count(distinct  if(ICDTYPE = 'SEC',  ICDCODE)), PATID) 
	) &lt;/PRE&gt;&lt;P&gt;That is, outer set expression and inner if loop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;It seems, that the problem is the second set expression (inside the count). It seems to „communicate“ with the outer set expression (inside the avg).&lt;/P&gt;&lt;P&gt;Is there a way to produce the desired result with set expressions only?&lt;/P&gt;&lt;P&gt;(The adding of „&lt;EM&gt;, ICDTYPE=&lt;/EM&gt;“ to the outer set expression does not change the behavior.)&lt;/P&gt;&lt;P&gt;Thank you very much in advance!&lt;/P&gt;</description>
      <pubDate>Sat, 16 Nov 2024 04:39:04 GMT</pubDate>
      <guid>https://community.qlik.com/t5/QlikView/Output-difference-between-set-expression-and-if-loop/m-p/1535551#M439114</guid>
      <dc:creator>qv_mypath</dc:creator>
      <dc:date>2024-11-16T04:39:04Z</dc:date>
    </item>
  </channel>
</rss>

