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
strArr=[]

for k in keys:arr.append(k)


#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

No comments: