Fiscal Year, Fiscal Quarter, Week in Fiscal Quarter

Share your favorite FastKeys commands
Post Reply
TokyoMike
Posts: 35
Joined: May 10th, ’18, 07:01

Post by TokyoMike » Feb 23rd, ’23, 03:42

Hi - I was previously using aText which lets you run Python scripts natively. My company has locked down out machines and aText will no longer update - I think because it comes from the Microsoft Store. Fastkeys, on the other hand, is my savior and I am glad to come back to it.

I am hoping you can help me with 2 scripts though that I ran in aText via Python.

1. Our company fiscal year starts in October. I would like to be able, based on any day I run the shortcut, to print what Fiscal Year and Quarter I am in.
2. I would like to display the WEEK NUMBER within that fiscal quarter

Since it is a bit harder to use Python with Fastkeys (I have never gotten compiling Python and running it in Fastkeys to work) I am hoping my request is simpler.

Can you give me some direction? I would like a shortcut to display this as a result: Report for {Fiscal Year} {Fiscal Quarter} Week {Week in Fiscal Quarter} - as an example if I run it today, 2/Feb/2023: FY23 Q2 Week 8
User avatar
Tom
Posts: 791
Joined: Nov 24th, ’15, 23:39

Post by Tom » Feb 23rd, ’23, 20:56

Initial idea, not fully tested:

Code: Select all

q:=[2,2,2,3,3,3,4,4,4,1,1,1]
w:=[274,1,91,182]
Quarter:=q[A_MM]
shift:=-w[Quarter]
date:=A_Now
date += %shift%, days
FormatTime, YearWeek, %date%, YWeek
Week:=SubStr(YearWeek,5)
Send, % "Report for FY" A_YYYY " Q" Quarter " Week " Week
TokyoMike
Posts: 35
Joined: May 10th, ’18, 07:01

Post by TokyoMike » Feb 25th, ’23, 04:42

Thanks! This seems to work. Let me see if I have the logic:

Quarters: you are shifting them one quarter out in the q array, so Jan/Feb/Mar instead of Q1 is shifted to Q2, and so on.

Weeks: you are shifting these manually in the same fashion shifting a number of days out - an entire quarter out. Now - what I am wondering is this - will this be the week of the fiscal year, or will it be the week of the current quarter - so for Q3 I would expect the first week of April to be week 1.

For the year I am not sure how this is being shifted. For instance in October of 2023 the Fiscal Year should be FY24.

This is a neat way to approach the problem - if it covers the cases above - wow!
TokyoMike
Posts: 35
Joined: May 10th, ’18, 07:01

Post by TokyoMike » Feb 25th, ’23, 04:50

After some tinkering and testing I can confirm that the Quarter works great. The weeks are also "shifted" - but for the YEAR not the week in the quarter.
User avatar
Tom
Posts: 791
Joined: Nov 24th, ’15, 23:39

Post by Tom » Feb 25th, ’23, 17:59

Post the Python code and we’ll try to adapt it.
TokyoMike
Posts: 35
Joined: May 10th, ’18, 07:01

Post by TokyoMike » Feb 27th, ’23, 00:14

Thank you!

Test case: today, 2023-2-27 is FY23 Q2 W9

Test case: 2023-10-1 is FY24 Q1 W1

Code: Select all

from datetime import date, datetime, timedelta
import typing

from dateutil import relativedelta

FY_START_MONTH = 10
Q_MAP = [2, 3, 4, 1]

# Define a "delta" object that represents the time from a given date to the next Monday
NEXT_MONDAY = relativedelta.relativedelta(weekday=relativedelta.MO)
# Define a "delta" object that represents the time from a given date to the most recent Monday
LAST_MONDAY = relativedelta.relativedelta(weekday=relativedelta.MO(-1))
# Define a "timedelta" object that represents one week
ONE_WEEK = timedelta(weeks=1)


def week_in_quarter(dt: datetime) -> typing.Tuple[str, int, int]:
    """Return the fiscal year, fiscal quarter, and week number for a given date."""
    d: date = dt.date()
    year = d.year

    # Determine the fiscal quarter of the date
    quarter = (d.month - 1) // 3
    # Determine the first day of the quarter
    quarter_start = date(year, (quarter * 3) + 1, 1)
    # Determine the Monday of the second week of the quarter
    quarter_week_2_monday = quarter_start + NEXT_MONDAY

    # If the date is before the second Monday of the quarter, it is in the first week
    if d < quarter_week_2_monday:
        week = 1
    else:
        # Determine the most recent Monday before the date
        cur_week_monday = d + LAST_MONDAY
        # Determine the number of weeks between the second week of the quarter and the current week
        week = ((cur_week_monday - quarter_week_2_monday) // ONE_WEEK) + 1

    # Map the quarter to its fiscal quarter
    quarter_fy = Q_MAP[quarter]
    # Map the year to its fiscal year
    if d.month >= FY_START_MONTH:
        year += 1

    # Return the fiscal year, fiscal quarter, and week number
    return str(year)[2:], quarter_fy, week


today = datetime.today()

# today = datetime(2023, 4, 1)

year, quarter, week_count = week_in_quarter(today)

print("FY{} Q{} W{}".format(year, quarter, week_count))



User avatar
Tom
Posts: 791
Joined: Nov 24th, ’15, 23:39

Post by Tom » Mar 1st, ’23, 20:43

My script was calculating the week of the fiscal year (not month). Maybe someone can help with adapting the Python script.

But why not simply running the Python script by using Type: Run?
TokyoMike
Posts: 35
Joined: May 10th, ’18, 07:01

Post by TokyoMike » Mar 11th, ’23, 07:18

I’ve not had a lot of luck compiling Python into an exe and then running it.

Could you point me in the right direction to make sure I am compiling it correctly and calling it correctly?
TokyoMike
Posts: 35
Joined: May 10th, ’18, 07:01

Post by TokyoMike » Mar 11th, ’23, 07:22

Also - have you seen the app aText? It lets you paste in scripts or various languages and as long as you point it at the right compiler in the settings it runs the script and gives you the output. Would be an awesome feature for Fastkeys.
Post Reply