Is there a standard or convenient way to get the current financial period in a custom workflow activity and use it to convert a calendar date field to it's financial date?
Fiscal periods are monthly.
Manually removing months from the date field to get the fiscal date is not really an option.
I have found this set, but it is empty:
XRMServices/2011/OrganizationData.svc/MonthlyFiscalCalendarSet
I found this
<d:FiscalCalendarStart m:type="Edm.DateTime">
2013-10-01T10:59:00Z
</d:FiscalCalendarStart>
in
/XRMServices/2011/OrganizationData.svc/OrganizationSet
This method returns the fiscal period (as month) from a calendar date:
/* returns month, year */
private Tuple<int, int> GetFinancialDate(WorkflowContext context, DateTime? date)
{
var start = context.Linq.OrganizationSet
.FirstOrDefault().FiscalCalendarStart;
if (date.HasValue && start.HasValue)
{
var localDate = date.Value.ToLocalTime();
var month = localDate.Month - start.Value.ToLocalTime().Month + 1; // +1 because not 0 based
var year = (localDate.Month == 1) ? localDate.Year : localDate.Year + 1; // if it is Jan then do not move the year forward - move forward for Feb onward
if (month <= 0) // the date is in the previous financial year, 0 would be the last period of the previous fiscal year
{
--year; // move to the previous year
month = month + 12; // month is negative or 0 before this, so is 12 or less after
}
return Tuple.Create(month, year);
}
return Tuple.Create(0, 0);
}
Related
I have two dates:
Monday 26/12/2022
Sunday 01/01/2023
Take this code:
CString strDate = L"2022-12-26 00:00:00";
COleDateTime datMeeting;
datMeeting.ParseDateTime(strDate);
const auto iPublicTalkWeekNumber = _ttoi(datMeeting.Format(L"%W"));
The value of iPublicTalkWeekNumber is 52.
Now use:
CString strDate = L"2023-01-01 00:00:00";
COleDateTime datMeeting;
datMeeting.ParseDateTime(strDate);
const auto iPublicTalkWeekNumber = _ttoi(datMeeting.Format(L"%W"));
The value of iPublicTalkWeekNumber is 0.
Why? The 1st Jan 2023 is a Sunday, which rolls back to the 26th Dec, 2022. Why is it not returning 52?
As a result this is messing the logic of my software. The documents say the %W returns a value 0 - 53, based on the first Monday being week one.
How do I correctly handle this?
This workaround resolves my issue:
COleDateTime datFirstMonday;
COleDateTimeSpan spanDay;
spanDay.SetDateTimeSpan(1, 0, 0, 0);
// Rewind the start / end dates back until we get to the Monday
datFirstMonday = datMeeting;
while (datFirstMonday.GetDayOfWeek() != 2)
datFirstMonday -= spanDay;
const auto iPublicTalkWeekNumber = _ttoi(datFirstMonday.Format(L"%W"));
The above code is used with the Sunday date (but for users of the software it might be another day). Eitherway, we roll back to the associated Monday for that week. Then, we do the test for week number.
This way, I find the matching week number.
Having a lot of trouble translating the logic below in pandas/python, so I do not even have sample code or a df to work with :x
I run a daily report, that essentially filters for data from Monday thru the day before what 'Today' is. I have a Date column [ in dt.strftime('%#m/%#d/%Y') format] . It will never be longer than a Monday-Sunday scope.
1) Recognize the day it is 'today' when running the report, and recognize what day the closet Monday prior was. Filter the "Date" Column for the Monday-day before today's date [ in dt.strftime('%#m/%#d/%Y') format ]
2) Once the df is filtered for that, take this group of rows that have dates in the logic above, have it check for dates in a new column "Date2". If any dates are before the Monday Date, in Date2, change all of those earlier dates in 'Date2' to the Monday date it the 'Date' column.
3) If 'Today' is a Monday, then filter the scope from the Prior Monday through - Sunday in the "Date" Column. While this is filtered, do the step above [step 2] but also, for any dates in the "Date2" column that are Saturday and Sunday Dates - changes those to the Friday date.
Does this make sense?
Here're the steps:
from datetime import datetime
today = pd.to_datetime(datetime.now().date())
day_of_week = today.dayofweek
last_monday = today - pd.to_timedelta(day_of_week, unit='d')
# if today is Monday, we need to step back another week
if day_of_week == 0:
last_monday -= pd.to_timedelta(7, unit='d')
# filter for last Monday
last_monday_flags = (df.Date == last_mon)
# filter for Date2 < last Monday
date2_flags = (df.Date2 < last_monday)
# update where both flags are true
flags = last_monday_flags & date2_flags
df.loc[flags, 'Date2'] = last_monday
# if today is Monday
if day_of_week == 0:
last_sunday = last_monday + pd.to_timedelta(6, unit='d')
last_sat = last_sunday - pd.to_timedelta(1, unit='d')
last_week_flags = (df.Date >= last_monday) & (df.Date <= next_sunday)
last_sat_flags = (df.Date2 == last_sat)
last_sun_flags = (df.Date2 == last_sun)
# I'm just too lazy and not sure how Sat and Sun relates to Fri
# but i guess just subtract 1 day or 2 depending on which day
...
I need to calculate Projected Premium by Insurance Type.
The formula for calculation is:
Sum(Budget) - Sum(Budget thru current Day) * (1+BudgetVariance) + TotalPremium
BudgetVariance = (Premium - Budget tru current Day) / Budget tru current Day
Basically I got all variables I need (Thanks to Joe), but some of them don't calculate properly:
BudgetMTD =
VAR DaysOfMonth = MAXX(Dates, DAY(EOMONTH(Dates[Date], 0)))
VAR BudgetPerDayForMonth = SUM(BudgetData[Amount]) / DaysOfMonth
VAR DaysInMonthToToday = MAXX(Dates,
IF(Dates[Date] < TODAY(), DAY(Dates[Date]),
IF(Dates[Date] > TODAY(), 0,
DAY(TODAY())
)))
RETURN BudgetPerDayForMonth * DaysInMonthToToday
BudgetMTD should be 25,882,308 but it displays 30,148,763 (which is the total for 12 months).
Also BudgetVarianceMTD displays correctly for each month, but in a card I need it as:
(Premium - Budget tru current Day) / Budget tru current Day
Which is:
28,505,823 - 25,882,308 / 25,882,308 = 0.10136326
just a hint to get from beginning of the year to current day
DATEDIFF(month, '2017/01/01', GETDATE()) AS d
I am writing code that lets users write down dates and times for things they have on. It takes in the date on which it starts, the start time and finish time. It also allows the user to specify if they want it to carry over into multiple weeks (every Monday for a month for example)
I am using a for loop to do this, and because of the different months having different days I obviously want (if the next Monday for example is in the next month) it to have the correct date.
This is my code:
for i in range(0 , times):
day = day
month = month
fulldateadd = datetime.date(year, month, day)
day = day + 7
if month == ( '01' or '03' or '05' or '07' or '10'or '12'):
if day > 31:
print(day)
day = day - 31
print(day)
month = month + 1
elif month == ( '04' or '06'or '09' or '11'):
if day > 30:
print(day)
day = day - 30
print(day)
month = month + 1
elif month == '02':
if day > 29:
print(day)
day = day - 29
print(day)
month = month + 1
When running this and testing to see if it goes correctly into the new month I get the error:
File "C:\Users\dansi\AppData\Local\Programs\Python\Python36-32\gui test 3.py", line 73, in addtimeslot
fulldateadd = datetime.date(year, month, day)
ValueError: day is out of range for month
Where have I gone wrong?
It's hard to be completely accurate without seeing some of the previous code (for example, where do day, month, year, and times come from?), but here's how you might be able to use timedelta in your code:
fulldateadd = datetime.date(year, month, day)
for i in range(times):
fulldateadd = fulldateadd + datetime.timedelta(7)
A timedelta instance represents a period of time, rather than a specific absolute time. By default, a single integer passed to the constructor represents a number of days. So timedelta(7) gives you an object that represents 7 days.
timedelta instances can then be used with datetime or date instances using basic arithmetic. For example, date(2016, 12, 31) + timedelta(1) would give you date(2017, 1, 1) without you needing to do anything special.
I would like to calculte the number of the week in a year. I see this post
In this post the acepted answer use this code:
public static int GetIso8601WeekOfYear(DateTime time)
{
// Seriously cheat. If its Monday, Tuesday or Wednesday, then it'll
// be the same week# as whatever Thursday, Friday or Saturday are,
// and we always get those right
DayOfWeek day = CultureInfo.InvariantCulture.Calendar.GetDayOfWeek(time);
if (day >= DayOfWeek.Monday && day <= DayOfWeek.Wednesday)
{
time = time.AddDays(3);
}
// Return the week of our adjusted day
return CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(time, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
}
However I see a problem. The number of the week is repeated if the last day of a month is in Sunday.
For example, the last day of March of 2013 year is in sunday. This week is the number 13th, is correct. But in April, how C# use always 6 weeks in a month to calculate the number of week, the first week of april has not any day of april, because all the days belong to march because the last day of the week is 30th March. So C# says that the first week of april is th 15th week, but this is incorrect, it has to be 14th.
So I would like to know if there are any way to calculate the number of a week in a right way.
EDIT:
I mean this:
In march, the last week is this:
25 26 27 28 29 30 31
This is the 13th, is correct.
In april, the first week is:
1 2 3 4 5 6 7
And this week is calculated as 15th.
So if I see the march calendar the last week is calculated as 13th and if I see the april calendar the last week of march is caluclated as 14th. This is incorrect.
SOLUTION:
DateTime dtCalendar = Calendar.DisplayDate;
int gridRow = (int)GetValue(Grid.RowProperty);
// Return the week of our adjusted day
int wueekNumber= System.Globalization.CultureInfo.InvariantCulture.Calendar.GetWeekOfYear(dtCalendar, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday);
if (dtCalendar.DayOfWeek == DayOfWeek.Monday)
{
gridRow = gridRow - 1;
}
Text = (weekNumbe r+ gridRow - 1).ToString();
Thanks.
The problem is that you are using the wrong CalendarWeekRule. To get the result you want you should use FirstDay. I have seen various codes in internet saying that you should use FirstFourDayWeek but, after some testing, I realised that the "right one" is FirstDay. I have tested it with your example and it delivers the right result: 14th week.
int targetYear = 2013;
DateTime targetDate = new DateTime(targetYear, 4, 1);
int week = System.Globalization.CultureInfo.CurrentCulture.Calendar.GetWeekOfYear(targetDate, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Monday);