Get column unique values

from System import Array
from Spotfire.Dxp.Data import IndexSet
from Spotfire.Dxp.Data import DataValueCursor

#Property controls
tableName = 'Sales'
columnName = 'Store'

#A Get rows from a data table called 'Sales'
myTable = Document.Data.Tables[tableName]
rowCount = myTable.RowCount
rowsToInclude = IndexSet (rowCount,True)

#B create a cursor to the 'Store' column we wish to get values from
cursor1 = DataValueCursor.CreateFormatted(myTable.Columns[columnName])

#C loop through all rows, retrieve value for specific column, and add value into dictionary
keys=dict()
for row in myTable.GetRows(rowsToInclude,cursor1):
 value1 = cursor1.CurrentValue
 for tag in value1.split(', '):
  keys[tag]=1

#D show unique values
for key in keys: print key

#json style
print keys

#E Optionally convert unique values from dict to array
strArray = Array.CreateInstance(str, len(keys))
i=0
for key in keys:
 strArray[i] = key
 i=i+1

#print array
print strArray

#print array (old school by looping elements) 
for element in strArray: print element

You can always get the same results by getting values from a filter with less code
Previous
Next Post »