Date Posted:
Product: TIBCO Spotfire®
Product: TIBCO Spotfire®
Problem:
How to count values across multiple columns in Spotfire similar to CountIf function
Solution:
Suppose you have four columns of Year (Year1, Year2, Year3, Year4) and you want to count the occurrence of Year1 column's value that is present in all four columns. Take the example data:
Region | Year1 | Year2 | Year3 | Year4 |
MW | 2013 | 2014 | 2015 | 2016 |
NE | 2013 | 2014 | 2015 | 2016 |
SE | 2016 | 2017 | 2018 | 2019 |
WE | 2016 | 2017 | 2018 | 2019 |
WE | 2016 | 2017 | 2018 | 2019 |
WE | 2016 | 2017 | 2018 | 2019 |
WE | 2016 | 2017 | 2018 | 2019 |
WE | 2016 | 2017 | 2018 | 2019 |
WE | 2016 | 2017 | 2018 | 2019 |
WE | 2016 | 2017 | 2018 | 2019 |
WE | 2016 | 2017 | 2018 | 2019 |
WE | 2016 | 2017 | 2018 | 2019 |
WE | 2016 | 2017 | 2018 | 2019 |
WE | 2014 | 2015 | 2016 | 2017 |
WE | 2014 | 2015 | 2016 | 2017 |
WE | 2014 | 2015 | 2016 | 2017 |
WE | 2014 | 2015 | 2016 | 2017 |
WE | 2014 | 2015 | 2016 | 2017 |
WE | 2014 | 2015 | 2016 | 2017 |
WE | 2014 | 2015 | 2016 | 2017 |
WE | 2013 | 2014 | 2015 | 2016 |
WE | 2013 | 2014 | 2015 | 2016 |
NE | 2013 | 2014 | 2015 | 2016 |
SE | 2013 | 2014 | 2015 | 2016 |
NE | 2013 | 2014 | 2015 | 2016 |
SE | 2013 | 2014 | 2015 | 2016 |
NE | 2013 | 2014 | 2015 | 2016 |
SE | 2013 | 2014 | 2015 | 2016 |
NE | 2013 | 2014 | 2015 | 2016 |
SE | 2016 | 2017 | 2018 | 2019 |
NE | 2016 | 2017 | 2018 | 2019 |
SE | 2016 | 2017 | 2018 | 2019 |
Option 1) Use an unpivot transformation and transform the 4 year columns to one column:
Select Insert > Transformations... Added transformations Transformation name: Unpivot Columns to pass through: Region Columns to transform: Year1 Year2 Year3 Year4 Category column name: Year Category column data type: String Value column name: Value Value column data type: Integer Empty values included: NoThen then calculate the value for each year value using an OVER function in an expression like:
Count() over ([Value])
Option 2) Use a regular expression function like RXReplace() in a calculated column:
Len(RXReplace(RXReplace(Concatenate([Year1]),String([Year1]),"$","g"),"[^$]","","g")) + Len(RXReplace(RXReplace(Concatenate([Year2]),String([Year1]),"$","g"),"[^$]","","g")) + Len(RXReplace(RXReplace(Concatenate([Year3]),String([Year1]),"$","g"),"[^$]","","g")) + Len(RXReplace(RXReplace(Concatenate([Year4]),String([Year1]),"$","g"),"[^$]","","g"))Explanation: First concatenate the values in each year column, then search for the Year1 value in that concatenated set. If the Year1 value exists then replace it with a $ character (or other unique single character not present in the values) for all occurrences. Then perform the replace operation again where everything except $ is replaced with the empty set "". At this point the string is only left with $ characters, one for each occurrence, which can be easily counted. Use the Len() function to sum it up across each column to get total year value occurrence. Doc: Text functions
Comments
0 comments
Article is closed for comments.