How to get first month of previous quarter

Hello!
I have a question and I really hope you can help me. I don’t find a solution by my own :frowning:

I need the first month of the last quarter (“yyyy.MM”). For example:
Today is 18th March 21 → 2020.10
Today is 31st Dec 20 → 2020.07
Today is 23rd Sep 20 → 2020.04

Do you have any ideas?
Thanks,
Ani

@Ani_Ma - Please take a look here for the logic…From this, you can get the month instead of date…

@Ani_Ma
have a look here:
grafik

test result:
grafik

In case of the Strings 31st Dec 20… are to handle, then we write some cleansing job removing st,nd, rd and can parse the date string as usually e.g. with DateTime.ParseExact(… method

In case of a more dynamic going back is needed we can do as well

Hi,

Another solution :

First, make the following dictionary.

dict = new Dictionary(Of Int32, Int32())From{{1,{10,-1}},{2,{10,-1}},{3,{10,-1}},{4,{1,0}},{5,{1,0}},{6,{1,0}},{7,{4,0}},{8,{4,0}},{9,{4,0}},{10,{7,0}},{11,{7,0}},{12,{7,-0}}}

Let’s say dateVar as target date var.

Then

(dateVar.Year+dict(dateVar.Month)(1)).ToString+"."+dict(dateVar.Month)(0).ToString

Main.xaml (5.7 KB)

Regards,

2 Likes

If that is based on today date, the amounts of months would change. In April it would be 3.

So it can do sth like this

Int_month = today.Month

Switch with integers (1-12)

For 1: assign int_count = -3
For 2: assign int_count = -4
For 3: assign int_count = -5
For 4: assign int_count = -3
etc.

After that:

Today.AddMonths(int_count).toString(“yyyy.MM”)

Other way:

Int_month = today.Month

If int_month mod 3 =0
Then Today.AddMonths(-5).toString(“yyyy.MM”)
Else
If int_month mod 3 = 2
Then
Today.AddMonths(-4).toString(“yyyy.MM”)
Else
Today.AddMonths(-3).toString(“yyyy.MM”)

Quarterly starting months are standard aren’t they? Shouldn’t we be simply looking at which bracket a current month falls into and take the starting month of that bracket?

If Month (Today) between 1 and 3 then
Q1_Date = Jan 1, 2021
Else if
Blah …

End if

Or is this too simple? Because you can then extend this pseudo-code to get 1st Month of current, past or present quarter?

Thank you all for your replies!
Based on the link from @prasath17 I found a solution:

Var1 = now .AddMonths(-3).Month-1
Var2 = CInt(Math.Floor(Var1 /3)) *3 +1

In my first step I thought there would be a solution in 1 formula and that does not work very well for me. Now I splitted it in 2 variable and it worked.

3 Likes