Fiscal Year, Fiscal Quarter, Week in Fiscal Quarter
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
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
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
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!
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!
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.
Post the Python code and we’ll try to adapt it.
Thank you!
Test case: today, 2023-2-27 is FY23 Q2 W9
Test case: 2023-10-1 is FY24 Q1 W1
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))
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?
But why not simply running the Python script by using Type: Run?
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?
Could you point me in the right direction to make sure I am compiling it correctly and calling it correctly?