Forum Discussion

Tony2021's avatar
Tony2021
Steel Contributor
Oct 06, 2024

DateDIFF and a Where Clause

Hello Experts,    I am trying to calculate a day count between 2 dates [FundingDate],[LastBdMo] but I need to add a where condition.  I am not sure if I can do this?  I have added the blue below an...
  • arnel_gp's avatar
    arnel_gp
    Oct 08, 2024

    Tony2021 , you replace:

    ..

    Abs(DateDiff("d",[qryDrawsDecliningCumDrawn].[FundingDate],[LastBdMo])) AS DayCountDD

     

    with:

     

    Abs(DateDiff("d",[qryDrawsDecliningCumDrawn].[FundingDate], fncLastBusinessDayThisMo([qryDrawsDecliningCumDrawn].[FundingDate])) As DayCountDD

     

    Also change the function to this:

    Public Function fncLastBusinessDayThisMo(ByVal FundingDate As Date) As Date
    Static dte_static As Date
    Static dte_ans As Date
    Dim dte As Date
    dte = DateSerial(Year([FundingDate]), Month([FundingDate]) + 1, 0)
    If dte = dte_static Then
       fncLastBusinessDayThisMo = dte_ans
       Exit Function
    End If
    dte_static = dte
    Do Until InStr(1, "Sat/Sun", Format$(dte, "ddd")) = 0
       dte = DateAdd("d", -1, dte)
    Loop
    dte_ans = dte
    fncLastBusinessDayThisMo = dte
    End Function

Resources