Sum of all rows named ________ from a dataset tag?

So im trying to count all the rows of a certain column in a dataset tag with a specific string name
for example if there is 22 rows in the current dataset
I am trying to make my 3 new tags show that

10 rows have Name 1
5 rows have Name 2
7 rows have Name 3
so i can put them on a dashboard

I was looking at len but it just counted every row

is there a way to use len based on a condition? or some other method to doing this?

This is tricky to do from expressions, unless you delegate to scripting via the runScript function.

Phil Turmel's Simulation Aids module adds a view expression function that would work:
https://www.automation-pros.com/simaids/doc/expression.view.html

2 Likes

If you were to script it:

from collections import Counter
sampleheaders = ['ndx', 'value']
sampledata = [[ 1, 'Name 1'],
              [ 2, 'Name 2'],
              [ 3, 'Name 3'],
              [ 4, 'Name 1'],
              [ 5, 'Name 2'],
              [ 6, 'Name 3'],
              [ 7, 'Name 1'],
              [ 8, 'Name 3'],
              [ 9, 'Name 1'],
              [10, 'Name 2'],
              [11, 'Name 1'],
              [12, 'Name 3'],
              [13, 'Name 1'],
              [14, 'Name 2'],
              [15, 'Name 1'],
              [16, 'Name 3'],
              [17, 'Name 2'],
              [18, 'Name 1'],
              [19, 'Name 3'],
              [20, 'Name 1'],
              [21, 'Name 3'],
              [22, 'Name 1']
             ]

sampleDS = system.dataset.toDataSet(sampleheaders, sampledata)

col = Counter(sampleDS.getColumnAsList(1))

# Sort by keys
print sorted(col.items())
# Sort by most common values
print col.most_common()

Output

>>> 
[(u'Name 1', 10), (u'Name 2', 5), (u'Name 3', 7)]
[(u'Name 1', 10), (u'Name 3', 7), (u'Name 2', 5)]
>>> 
4 Likes