Counting Items for Students

Modified on Mon, Dec 4, 2023 at 3:40 PM

Counting Items for Students

It is possible to count things for students.  This capability requires you to first create Columns for First Row for Student and, usually, Last Row for Student.   Information on how to create those Columns can be found in the Finding the First and Last Rows for a Student article.


The Whole Counting Expression


Let's create a Column called Counter - The "Counter" row will simply count rows for a student:

${ row.firstRowForStudent ? 1 : prevRow.counter+1 }

Let's analyze this Expression by looking at the "Decision" embedded within it:   A ? B : C

The "A" part of the Decision is 'the test':    row.firstRowForStudent ?

 

The "B" part of the Decision is "What to do if this IS the First Row for the Student":    1

The "C" part of the Decision is "What to do if this is the Last Row for the Student:   prevRow.counter+1


The 'A' Part of the Decision

The "A" part of the Decision Expression is the test - something that evaluates to either true or false.   What we want to decide here is whether or not the current row is First Row for the student.   Since the Column 'first Row for Student' was already built (here), and also simply evaluates to either true or false, the 'test' here is very simple:

row.firstRowForStudent ?


The 'B' Part of the Decision

The "B" part of a Decision Expression is What to Do if 'A' is TRUE.   In the Counter Expression, what we want to do is:  Count the first row for the student.   The first row for the student is "row #1" - and so the "B" part of our Decision Expression is simply:

1


This is interpreted as the Integer value 1.    Notice there are no quotes around the 1 – It is just the number 1.  It is interpreted as “the integer value 1”.    If there were quotes around it:  ‘1’, it would be interpreted as “the String 1” and it would be text, not a number.  Numbers and text are stored very differently.


The 'C' Part of the Decision

The "C" part of a Decision Expression is What to Do if 'A' is FALSE.   In the Counter Expression, this means:  what to do for all rows after the first row for the student.  What we want to do is:  Count the current row - up the counter by 1.    To up a counter, you first have to access its previous value.  You cannot add to a value if you cannot access the value.    To up the counter, you have to find out what the value of the counter was in the previous row.   You must know the name of the current Column.  In this case, the name of our current Column (i.e. the name of our Counter) is "counter".  To find the value of the current Column in the previous row, use the "prevRow" selector.   Once you have the previous value, add 1 to it:


prevRow.counter+1


This means that “the value of the counter column in the current row is the value from the previous row upped by 1. “


Note that we can do integer arithmetic directly here - no function calls are necessary.


The Whole Expression

The meaning of the whole expression is:   If this is the first row for a student, start the counter for this student at 1; if this is NOT the first row for a student, continue counting by upping the previous count by 1.


${ row.firstRowForTheStudent ? 1 : prevRow.counter + 1 }


Print the Final Count Only on the Last Line for the Student

Suppose you want to print the final count on the last row for the student.  In all lines prior to the last line, you want to be blank (because there is no total as yet).  To do this you will need yet another column.

Create a new Expression Column.  The Expression you want to put in it must place a blank in it UNTIL you reach the last line for the student.  On the last line for the student, you want to show the final count (or total if you are summing something).

  • The 'print total' Column must appear, in the list of Columns, below both the lastRowForTheStudent Column and the counter Column (or those two values will be undefined in the current row).
  • The Expression to use is:       ${ row.lastRowForTheStudent ? row.counter : ‘ ‘ }     
  • How this works:     
    • The test is       row.lastRowForTheStudent           That Column will be true only when the current row is the last row for the student (the next row below will be the first row for another student).
    • The "true" section is:   row.counter            That is, when lastRowForTheStudent is true - when it is the last row for the Student, print the value of the counter.
    • The "false" section is:  ""                  That is, when lastRowForTheStudent is false - when it is NOT the last row for the Student, print the empty String - nothing.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article