Forum Discussion

DanMcLaughlin's avatar
DanMcLaughlin
Qrew Trainee
5 months ago

Running Total with a Max

Have a timesheets table and it helps with accruing Paid time off.

I need to implement a policy that PTO will max out at 160 hours in a running total field. 

The running total field looks like this:

var text QONE = "{23.BF.'" & [Date] & "'}AND {56.EX.'"& [Related Resource] &"'}";
var text QTWO = "{23.EX.'" &[Date] & "'}AND{3.LTE.'" & [Record ID#] & "'}AND {56.EX.'"& [Related Resource] &"'}";

Min(160,Nz(SumValues(GetRecords($QONE),193)+Nz(SumValues(GetRecords($QTWO),193),0)))

THE PROBLEM: When I use the Min function  it doesnt calculate against the 160 value in the formula.  If the balance is 200 hours, any subsequent line entries build off the 200 value although the table above shows 160.  

Here is the table without the Min function:

When I try to change the formula in the PTO in the something like

If([PTO BALANCE]>=160,0,[Hours worked]*[PTO Rate] 

I get an error that I cannot write a formula for a field that is based on itself.

---------------------------------------------------------------------------

I also tried the pipeline approach but since PTO in is a calculated field I hit a road block as well.

 

 

4 Replies

  • I'm not sure I completely understand why you need to queries to get to your totals, but maybe it has something to do with the placement of the brackets. Maybe try this

     

    Min(

    160,Nz(SumValues(GetRecords($QONE),193))+

     

    Nz(SumValues(GetRecords($QTWO),193))

    )

     

  • The problem here is that when someone uses 8 hours of PTO is does not take it into account in the balance field. It still show a balance of 160 instead of 152.

      If there is a better way to do running totals without a query I'm all ears.

     

    • DonLarson's avatar
      DonLarson
      Qrew Deputy

      Dan,

      I have solved a similar problem on Time Off, but attack it differently.  There is a Pipeline running monthly that expires the oldest earned time that exceeds the threshold.  It also checks seniority and will raise the threshold on an individual basis from the new hire level out to five years when employees max out.

    • Mez's avatar
      Mez
      Qrew Captain

      I solved something similar a few years ago using a three table structure, so we could use relationships: 

      1. Employee {Parent},
      2. Yearly (calendar) Hours (child), 
      3. Time Off Requests (child)

       

      On the Yearly Hours table the employee inputs yearly balance record, Year and Starting Balance, so we can subtract as request records are 'approved'. I query to the sibling table based on the Year (Start Date and End Date) values to get the 'approved' hours. Then on the Time Off Request record we can show the estimated hours taken, and remaining balance. 

      If you're comfortable with your query structure, then you can always use report formulas to see where they might be wrong. As Mark pointed out, the inputs/parameters to the Min() seem a bit off, but without knowing your intent it's difficult to say. If you're attempt is to get the first value of query one, and if null, then query two, it should be this: 

      Nz(
          SumValues(
            GetRecords($QONE),
            193
          ),
          SumValues(
            GetRecords($QTWO),
            193
          )
      )

      If not, and you truly want to add the value of these two queries together within the NZ function, where does the trailing zero, 0, come into play?

      edit - I see, you're adding either the value of QTWO or 0 to QONE so long as this result isn't null. Which field is 193?

      Min(
        160,
        Nz(
          SumValues(
            GetRecords($QONE),
            193
          )
          +
          Nz(
            SumValues(
              GetRecords($QTWO),
              193
            ),
            0
          )
        )
      )

      Here are some screenshots of using report formulas, and what the resulting report looks like (just a sample of the report). Because I'm already calculating remaining balance, if I sum it, then it's per record for the query params.