Jump to content

How to select or filter the latest date or current date in Listboxfilter with reverse sort order through Iron python script in Spotfire 7.8


Maddy Keyan

Recommended Posts

The following script reads the values in a column and then sets the last value in the column to a document proeprty. See if this helps

from System import Array

from Spotfire.Dxp.Data import IndexSet

from Spotfire.Dxp.Data import DataValueCursor

import datetime

# Get table

tbl= Document.Data.Tables["Data Table"]

 

# Get access to the Column populating the choice values

# of the drop-down list control

choicesCol = tbl.Columns["Column 1"]

 

rowCount = tbl.RowCount

rowsToInclude = IndexSet(rowCount,True)

 

# Create a cursor to the Column we wish to get the values from

cursor1 = DataValueCursor.CreateFormatted(choicesCol)

 

# Create Array object that will hold values

strArray = Array.CreateInstance(str,rowCount)

 

# Loop through all rows, retrieve value for specific column,

# and add value into array

for row in tbl.GetRows(rowsToInclude,cursor1):

rowIndex = row.Index

value1 = cursor1.CurrentValue

strArray[rowIndex-1] = value1

 

# Get only unique Values, cast to a list

uniqueVal = list(set(strArray))

 

 

#sorted values

uniqueVal.sort(key = lambda date: datetime.datetime.strptime(date,"%m/%d/%Y"),reverse=True)

 

# Set document property to first value found in the list

Document.Properties["test"] = uniqueVal[0]

Link to comment
Share on other sites

Peddi - Thanks for your response. I truly appreciate your way of approach would give a try to implement above given script. However, i was wondering if we could tweak little bit with the below script (got date reverse column sorted) with any minor changes as a result we could also get "select recent/latest date" from the same column filter.here's the script below:from System.Reflection import Assemblyfrom Spotfire.Dxp.Data.Collections import *from System.Runtime.Serialization import ISerializablefrom System.Collections import IComparerfrom System.Collections.Generic import IComparercolumn = Document.Data.Tables['Table_Name'].Columns['Column_Name']values = column.RowValues.GetEnumerator()myValues = []for val in values:    if val.HasValidValue:  #exclude empty values        myValues.Add(val.ValidValue)myValues.sort(reverse=True)column.Properties.SetCustomSortOrder(myValues)

 

Link to comment
Share on other sites

Peddi - The script worked really fine i was very close to my result but the issue would be addition of one more filter which makes crowded in text area. I was wondering is there any other option where we can embed the same script within already existing filter of text area
Link to comment
Share on other sites

Peddi - I have found the answer to my question hope this helps for other users as well if they ever come across such scenario. Please find the below Ironpython script for reverse sort date order(descending order)that has to select latest date in listbox filter.

from System.Reflection import Assembly

 

from Spotfire.Dxp.Data.Collections import *

 

from System.Runtime.Serialization import ISerializable

 

from System.Collections import IComparer

 

from System.Collections.Generic import IComparer

 

import Spotfire.Dxp.Application.Filters as filters

 

import Spotfire.Dxp.Application.Filters.ListBoxFilter

 

from Spotfire.Dxp.Application.Filters import FilterTypeIdentifiers

 

column = Document.Data.Tables['TABLE_NAME'].Columns['COLUMN_NAME']

 

values = column.RowValues.GetEnumerator()

 

#Val is a value inside the Column specified in the GetFilter

 

#print values

myValues = []

 

for Val in values:

 

if Val.HasValidValue: #exclude empty values

myValues.Add(Val.ValidValue)

print Val.ValidValue

 

myValues.sort(reverse=True)

 

column.Properties.SetCustomSortOrder(myValues)

 

myPanel = Document.ActivePageReference.FilterPanel

myFilter= myPanel.TableGroups[0].GetFilter("COLUMN_NAME")

 

#ListBoxFilter

#myFilter.FilterReference.TypeId = FilterTypeIdentifiers.ListBoxFilter

listBoxFilter = myFilter.FilterReference.As[filters.ListBoxFilter]()

 

listBoxFilter.IncludeAllValues=False

listBoxFilter.SetSelection(myValues[0])

 

#uncheck all

#ListBoxFilter.IncludeEmpty = False #make sure to clear the empty values

#for value in ListBoxFilter.Values:

# ListBoxFilter.Uncheck(value)

 

#ListBoxFilter.Check(myValues)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...