DAX number of days - dax

Can you please help how I can return the total number of days for each month in a given quarter?
For example, I already have 92 days for December 2025 but how can I show 92 days for October and November as well?

If you want only a count, you can use measure:
QuarterDays = calculate(countrows(VALUES('Calendar'[Date])), FILTER(ALL('Calendar'), selectedvalue('Calendar'[Year]) = 'Calendar'[Year] && selectedvalue('Calendar'[Quarter]) = 'Calendar'[Quarter] ))

Related

Count next X days without Sunday using Laravel carbon

I am developing a subscription based application using Laravel. I want to change the state of the user to expire after 75 days of subscribing to the package. I want to exclude Sundays from these 75 days.
Consider a scenario where user's account is verified today and he has only access to the premium functionalities for 75 days(without Sundays). After the 75 days, the user needs to resubscribe to get access to the premium functionalities of the application.
I will then set up a middleware which will check if the user's subscription is expired or not.
I have two scenarios to check for expiration:
Save the expiration date column in the users table.
Verify each user's request based on verified_at (datetime) column and prevent premium access if the user subscription is over more than 75 days without Sundays.
I want to achieve this using Laravel Carbon or any other alternative library/functionality.
After 75 Days from Today(11 June) is August 25 🙅‍♂️
After 75 Days from Today(11 June - Excluding Sundays) is September 07 👈
Reference: https://getcalc.com/75business-days-after-today.htm
If 75 is fixed, than you could easily calculate the number of Sundays in the period, and so then you just need adding those days to the 75:
$period_in_days = 75;
if($user->verified_at->dayOfWeek > 2)
$period_in_days += 11;
else
$period_in_days += 10;
The point is, in 75 days there could either 10 or 11 Sundays, and so in order to decide whether there are 10 or 11, we need to check which day is the first day.
Let's say is Monday the first day, so the 75 days should looks like this
1 - Monday
2 - Tuesday
...
71 - Monday
72 - Tuesday
73 - Wednesday
74 - Thursday
75 - Friday
Let's say is Tuesday the first day, so the 75 days should looks like this
1 - Tuesday
2 - Wednesday
...
71 - Tuesday
72 - Wednesday
73 - Thursday
74 - Friday
75 - Saturday
Let's say is Wednesday the first day, so the 75 days should looks like this
1 - Wednesday
2 - Thursday
...
71 - Wednesday
72 - Thursday
73 - Friday
74 - Saturday
75 - Sunday
So if the first day is not neither Monday and Tuesday, then there will be 11 Sundays (10 + the 1 that appears in the [71- 75]), otherwise there will be only 10 Sundays
I can count next 75 days excluding Sundays using the following PHP function
function Next75Days($StartingDate){
// Count Next 75 Days excluding Sundays
$Days = 75;
$d = new DateTime($StartingDate);
$t = $d->getTimestamp();
// Loop for 75 days
for($i=0; $i<$Days; $i++){
// Add 1 day to timestamp
$addDay = 86400;
// Get date of next day
$nextDay = date('w', ($t+$addDay));
// if it's Sunday, do $i--1
if($nextDay == 0) {
$i--;
}
// modify timestamp, add 1 day
$t = $t+$addDay;
}
$d->setTimestamp($t);
return $d->format('d M Y');
}

How to create a proper calculation of a due date in a dynamic manner in laravel

I been analyzing the proper calculation of the duedate for my app. I am working with a lending app where I need to display the due date for the borrower.
let say the approved date of their loan is 2019-10-27 and today is 11-5-2019. the payment scheme is dynamic. depending on what the admin set. in this example, the payment scheme weekly so I just need to say;
$duedate = date('M d Y', strtotime($loan_application->date_approval. ' + '.$loan_application->scheme->num_days.' days') );
from my example above the due date is supposedly on Nov 3, 2019. Now how can I make it say that the next due date is on Nov 10, 2019?
I really tried to analyze, here's my thought.
I get the difference between the approval date and NOW()
$diff = Carbon::createFromTimestamp(strtotime($loan_application->date_approval))->diff(Carbon::now())->days;
now I compared the $diff with $loan_application->scheme->num_days. so let say the $diff=9 and $loan_application->scheme->num_days = 7
now I created the condition
if ($diff > $loan_application->scheme->num_days) {
//should display
Nov 10, 2019
//because the current date is already Nov 5
}
and after Nov 10, + 7 days again, and + 7 days again and so on.
here is the complete code I already have;
$dateApproved = Carbon::parse($loan_application->date_approval)->toFormattedDateString();
$now = Carbon::today('M d Y');
$duedate = date('M d Y', strtotime($loan_application->date_approval. ' + '.$loan_application->scheme->num_days.' days') );
$diff = Carbon::createFromTimestamp(strtotime($loan_application->date_approval))->diff(Carbon::now())->days;
$numOfScheme = $loan_application->loanDuration->num_days / $loan_application->scheme->num_days;
if ($diff > $loan_application->scheme->num_days) {
}
I hope you understand my question. Please help. Thank you so much in advance.
this will add number of days from payment scheme. in my example 7 days. but how can I tell my code that today is already 2 days late, add additional 7 days
It took some time but I understood your question.
$date_approval = Carbon::createFromTimestamp(strtotime($loan_application->date_approval));
$scheme_numdays = $loan_application->scheme->num_days;
$days = (intdiv($date_approval->diff(Carbon::now())->days , $scheme_numdays) + 1) * $scheme_numdays
$due_date = $date_approval->addDays($days)->format('M d Y');
intdiv is just php's integer division.

Dateadd() like alternative in Visual Fox pro

How can I add year to Date.
I want to add 65 year to date (12\11\1952).
I have tried "12\11\1952" + 65 ,but it is not giving the required value i.e.
12\11\2017.
please suggest how can i achieve this.
When you add an integer to a Date, you are adding days. ie: Date(1952,11,12)+65 adds 65 days to Nov 12th, 1952.
If you add an integer to a DateTime then you are adding seconds. ie: datetime() + 60*60 adds an hour (60 seconds * 60 mins) to now.
To add a year to a date in VFP, you use GoMonth(). To Add 65 years you use 65 * 12 months:
yearsAdded = GoMonth( Date(1952, 11, 12), 12 * 65 )

Converting a difference of 2 24-hr times into time

Forgive me for my ignorance if its too easy and if its not the right place to post. I have 2 24 hours times strings as 1544 and 1458. Their difference should be 46 minutes but when I subtract them it yields 86 minutes as follows.
1544
-1458
-------
86
Can someone tell me how can I find a time difference of 2 24-hr times?
Hour doesn't have 100 minutes, but unfortunately only 60. You need to do this:
15*60+44
-14*60+58
---------
46
If you are working in C#, you can try to parse them to DateTime using
DateTime date1 = DateTime.ParseExact("1544","HHmm",CultureInfo.InvariantCulture);
DateTime date2 = DateTime.ParseExact("1458","HHmm",CultureInfo.InvariantCulture);
and then subtract them:
TimeSpan diff = date1 - date2

algorithm for calculating a week # from a date with custom start of week? (for iCal)

I can only find algorithm for getting ISO 8601 week (week starts on a Monday).
However, the iCal spec says
A week is defined as a seven day period, starting on the day of the
week defined to be the week start (see WKST). Week number one of the
calendar year is the first week that contains at least four (4) days
in that calendar year.
Therefore, it is more complex than ISO 8601 since the start of week can be any day of the week.
Is there an algorithm to determine what is the week number of a date, with a custom start day of week?
or... is there a function in iCal4j that does this? Determine a weekno from a date?
Thanks!
p.s. Limitation: I'm using a JVM language that cannot extend a Java class, but I can invoke Java methods or instantiate Java classes.
if (input_date < firstDateOfTheYear(WKST, year))
{
return ((isLeapYear(year-1))?53:52);
}
else
{
return ((dayOfYear(input_date) - firstDateOfTheYear(WKST, year).day)/7 + 1);
}
firstDateOfTheYear returns the first calendar date given a start of week(WKST) and the year, e.g. if WKST = Thursday, year = 2012, then it returns Jan 5th.
dayOfYear returns sequencial numerical day of the year, e.g. Feb 1st = 32
Example #1: Jan 18th, 2012, start of week is Monday
dayOfYear(Jan 18th, 2012) = 18
firstDateOfTheYear(Monday, 2012) = Jan 2nd, 2012
(18 - 2)/7 + 1 = 3
Answer Week no. 3
Example #2: Jan 18th, 2012, start of week is Thursday
dayOfYear(Jan 18th, 2012) = 18
firstDateOfTheYear(Thursday, 2012) = Jan 5th, 2012
(18 - 5)/7 + 1 = 2
Answer Week no. 2
Example #3: Jan 1st, 2012, start of week is Monday
firstDateOfTheYear(Monday, 2012) = Jan 2nd, 2012
IsLeapYear(2012-1) = false
Jan 1st, 2012 < Jan 2nd, 2012
Answer Week no. 52
Let daysInFirstWeek be the number of days on the first week of the year that are in January. Week starts on a WKST day. (e.g. if Jan 1st is a WKST day, return 7)
Set dayOfYear to the n-th days of the input date's year (e.g. Feb 1st = 32)
If dayOfYear is less than or equal to daysInFirstWeek
3.1. if daysInFirstWeek is greater than or equal to 4, weekNo is 1, skip to step 5.
3.2. Let daysInFirstWeekOfLastYear be the number of days on the first week of the previous year that are in January. Week starts on a WKST day.
3.3. if daysInFirstWeekOfLastYear is 4 or last year is Leap year and daysInFirstWeekOfLastYear is 5, weekNo is 53, otherwise weekNo is 52, skip to step 5.
Set weekNo to ceiling((dayOfYear - daysInFirstWeek) / 7)
4.1. if daysInFirstWeek greater than or equal to 4, increment weekNo by 1
4.2. if daysInFirstWeek equal 53 and count of days on the first week (starting from WKST) of January in the year of inputDate's year + 1 is greater than or equal to 4, set weekNo to 1
return weekNo

Resources