Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

Set Analysis and If-then and Aggr

Ladies & Gents:

I have been reading all of the posts on if-then and set analysis and aggr with regards to all the syntax permutations and performance etc (thanks to everyone). Perhaps I am confusing the two in my attempts to get the syntax right on either! I have a combo chart with cyclical group on X-axis (Month, Week, Qtr). Pseudocode for the expression is basically the sum of Sold, for all Jobs in the current year the max date row values for Sold within each period (as there may be multiple rows per period per Job).

I have inserted my (ahem) attempts at both:



// IF statements

//sum( if(Year= (vTodaysYear), [Sold]) ) // displays but not sophisticated enough

//sum( if(Year= (vTodaysYear), if( Date= max(Date, JobID), [Sold]) ) ) // no data to display

//sum( if(Year= (vTodaysYear), if( Date= max(Date, JobID, $(C_JobSoldCalendar)), [Sold]) ) ) // no data to display

// Set analysis

//sum( {$< Year= {$(#vTodaysYear)} >} [Sold] ) // displays but not sophisticated enough

//sum( {$< Year= {$(#vTodaysYear)}, Date= {"= max(Date)"} >} [Sold] ) // displays same as above, 2nd part seems to be ignored?

sum( {$< Year= {$(#vTodaysYear)}, Date= {$( aggr( max(Date, JobID)))} >} [Sold] ) // no data to display






I imagine there is probably some fundamental flaws in my approach! Below is a test snippet of data.

JobIDDateMonthWeekYearSold
9603621428/31/2009836200943
9603621428/28/2009835200973
1405785658/17/20098342009293
9603621428/1/2009831200973
9603621427/30/2009731200995
1405785657/1/2009727200981
9603621427/1/20097272009137
1405785658/15/20088332008305
1405785658/1/2008831200850
9603621428/1/20088312008129
1405785657/25/2008730200864
1405785657/14/2008729200862
1405785657/1/2008727200878
9603621427/1/20087272008150


An aditional wrinkle beyond my initial struggles is that in some shorter periods, eg. week, there may be no avaible row so the last known Sold values should be included. I'd appreciate any advice.

Sincerely - Robt

1 Solution

Accepted Solutions
Oleg_Troyansky
Partner Ambassador/MVP
Partner Ambassador/MVP

Robt,

you are pretty close in most of your attempts, with a few syntax issues. For example:

Max(Date, JobID) is invalid syntax. You can use sum(total <JobID> Date) tto get the maximum date for the JobID.

Similarly, aggr(Max(Date, JobID)) is invalid. Correct syntax is aggr(Max(Date), JobID)

Within the Set Analysis expression, you can use calculations like max(date), but only within a $-sign expansion:

$(=max(Date))

Another option for you to pick the latest value is using a function FirstSortedValue() - that's perhaps the most elegant way...

cheers,

Oleg

View solution in original post

3 Replies
Oleg_Troyansky
Partner Ambassador/MVP
Partner Ambassador/MVP

Robt,

you are pretty close in most of your attempts, with a few syntax issues. For example:

Max(Date, JobID) is invalid syntax. You can use sum(total <JobID> Date) tto get the maximum date for the JobID.

Similarly, aggr(Max(Date, JobID)) is invalid. Correct syntax is aggr(Max(Date), JobID)

Within the Set Analysis expression, you can use calculations like max(date), but only within a $-sign expansion:

$(=max(Date))

Another option for you to pick the latest value is using a function FirstSortedValue() - that's perhaps the most elegant way...

cheers,

Oleg

Not applicable
Author

Thanks Oleg. Strangely, the 'Error in expression' message was not showing for each of the example attempts so I thought that maybe the syntax was somehow partially good despite the graph not showing? I did look at FirstSortedValue() but those attempts probably suffered the same issues as you already highlighted and were nuked before the final C&P. Looking forward to making the mods and verifying.

Cheers - Robt

Not applicable
Author

Oleg:

So with your help I've progessed to the following example which displays correctly in the chart (also taking into account a cyclic dimension and flags):

sum( if( SoldDaysDate= aggr( nodistinct max( SoldDaysDate), JobID, $(= getcurrentfield([C_JobSoldCalendar]))), JSDcyF*[TotalSold]) ) // finally done !!!!!

Cheers - Robt