you can break this into 2 parts, the first to get the amounts then the seecond as the sum. I think you were trying to use 2 params, one as a end date and the other as a total # of days...
if date ({TPr_InsPayments;1.DatePayment}) in dateadd('d',{?Period A},
{?@todate} to
{?@todate} and {TPr_InsPayments;1.DollarsUnallocated}>0 then ({TPr_InsPayments;1.DollarsUnallocated} else 0
Now you can insert the SUm of that formula at any group level you want
SUM(@formula, groupfield)