Thanks, this worked for me! The only difference was that I had to subtract 2 from the total row count instead of 1. When I created the csv table (from a get rows in SQL table action), the csv table output had the header row, the data rows, then one extra blank row. Not sure what caused the blank row, but in the end, if I subtracted 2 in the final compose statement, the results were correct.

sub(length(outputs('Compose')), 2)