SQL/VBA: How to group by a fiscal year starting from a day other than 1st day of month

后端 未结 2 383
清酒与你
清酒与你 2021-01-29 14:19

I’m trying (using MS Access) to group some data by a fiscal year, which can be different to the calendar year.

If every fiscal year always started on the 1st of a given

2条回答
  •  野的像风
    2021-01-29 15:01

    This is actually quite simple, and you don't need any additional table or fields.

    The method is to offset the financial dates to match the calendar year. For example:

    FinancialYearFirstMonth = 9
    FinancialYearFirstDay = 2 
    

    First, toalign to primo of the month:

    DateMonth = DateAdd("d", 1 - FinancialYearFirstDay, SomeDate)
    

    Next, expand to also align to primo of the year:

    DateYearMonth = DateAdd("m", 1 - FinancialYearFirstMonth, DateAdd("d", 1 - FinancialYearFirstDay, SomeDate))
    

    If the financial year is not lagging but leading the calendar year, add 12 months:

    DateYearMonth = DateAdd("m", 1 - FinancialYearFirstMonth + 12, DateAdd("d", 1 - FinancialYearFirstDay, SomeDate))
    

    Now you can have the financial year and month and (with some limitations for the ultimo date(s) of a month) day using the normal functions:

    FinancialYear = Year(DateYearMonth)
    FinancialMonth = Month(DateYearMonth)
    FinancialDay = Day(DateYearMonth)
    

    Of course, month names will not fit. If these are needed, use MonthName of the original date.

提交回复
热议问题