To get last date, use sortedGroup.lastOption. 

If there are no transactions then, lastOption will return None. In that case, don't write anything to output file.