Products | Versions |
---|---|
Spotfire Analyst | All |
Very often it is required to exclude duplicate rows from a calculation in order to get an accurate result. You can identify duplicates within a specified group using a custom expression that uses the Rank() function, to which you also pass the columns you want to use for grouping. The 'duplicate' values can be determined based on any number of specified columns (i.e., if 3 columns are unique, consider this a duplicate). You can use this method to include or exclude duplicates, depending upon your requirements.
If(Rank(Baserowid(),"asc",[myGroup1])=1,"First row of group","Duplicate")
If(Rank(Baserowid(),"asc",[myGroup1],[myGroup2],[myGroup3])=1,"First row of group","Duplicate")
You can also use the If() and Rank() logic shown above directly within calculations to include or exclude duplicates, as required:
Avg(if(Rank(baserowid(),"asc",[myGroup1])=1,[Value],Null))
Rank(baserowid(),"asc",[myGroup1])=1
myGroup | Value |
Yellow | 6 |
Yellow | 6 |
Yellow | 6 |
Blue | 3 |
Red | 9 |
Red | 9 |
(6+6+6+3+9+9)/6 = 6.5
if(Rank(baserowid(),"asc",[myGroup])=1,"First row of group","Duplicate")
myGroup | Value | Duplicate? |
Yellow | 6 | First row of group |
Yellow | 6 | Duplicate |
Yellow | 6 | Duplicate |
Blue | 3 | First row of group |
Red | 9 | First row of group |
Red | 9 | Duplicate |
Using this technique you can perform an average that will only look at a single value per grouping. For example:
myGroup | Value |
Yellow | 6 |
Blue | 3 |
Red | 9 |
And the average you would like to calculate would be:
(6+3+9)/3 = 6
Avg(if(Rank(baserowid(),"asc",[myGroup])=1,[Value],Null))
(6+3+9)/3 = 6