Looking for function to derive prior quarter based on the year and month value passed from parameter

Yaseen
New Contributor III

Looking for function in the business rules to derive prior quarter based on the year and month value passed from parameter?

2 ACCEPTED SOLUTIONS

sameburn
New Contributor III

A good way to derive a time period based on a time variable is to use the TimeDimHelper class. 

You can nest functions like below; to achieve most use cases. 

' Declare timePeriod
Dim timePeriod As String = "2024M1"
' Derive prior quarter based on time passed in
Dim priorQuarterPeriod As String = TimeDimHelper.GetNameFromId(TimeDimHelper.GetQuarterIdFromId(TimeDimHelper.GetLastPeriodInPriorQuarter(TimeDimHelper.GetIdFromName(timePeriod))))
				
' Log
BRApi.ErrorLog.LogMessage(si, "PriorQuarter Logging: ", priorQuarterPeriod)

 

View solution in original post

ah!  TimeDimHelper.  I didn't know that existed. 🙂  
However for me, I would avoid nested method calls:

Dim time as string = "2022M4"
Dim priorQtr as string = GetPriorQtr(time)
BRApi.ErrorLog.LogMessage(si, priorQtr)  ' should print 2022Q1

Public Function GetPriorQtr(time As String) As String
    Dim timeId As Integer = TimeDimHelper.GetIdFromName(time)
    Dim lastPeriodInPriorQtr As Integer = TimeDimHelper.GetLastPeriodInPriorQuarter(timeId)
    Dim priorQtrId As Integer = TimeDimHelper.GetQuarterIdFromId(lastPeriodInPriorQtr)
    Dim priorQtr As String = TimeDimHelper.GetNameFromId(priorQtrId)
	
    Return priorQtr
End Function

 

View solution in original post

4 REPLIES 4

RobbSalzmann
Valued Contributor

Hi @Yaseen 

This will do it:

public string GetPreviousQuarter(string time)
{
  	var parts = time.Split('M');
	int year = 0;
    int month = 0;
	if (int.TryParse(parts[0], out year) && int.TryParse(parts[1], out month))
	{
		// Determine the current quarter
	    int currentQuarter = (month - 1) / 3 + 1;
	
	    // Calculate the previous quarter
	    int previousQuarter = currentQuarter - 1;
	
	    // If the current quarter is Q1, then the previous quarter is Q4 of the previous year
	    if (previousQuarter < 1)
	    {
	        previousQuarter = 4;
	        year -= 1; // Decrement the year as we're moving to the previous year
	    }		
	    	return $"{year}Q{previousQuarter}";
	}
	else
	{
		return  $"{time} is not a valid time";
	}
}
Public Function GetPreviousQuarter(time As String) As String
    Dim parts() As String = time.Split("M"c)
    Dim year As Integer = 0
    Dim month As Integer = 0
    If Integer.TryParse(parts(0), year) AndAlso Integer.TryParse(parts(1), month) Then
        ' Determine the current quarter
        Dim currentQuarter As Integer = (month - 1) \ 3 + 1

        ' Calculate the previous quarter
        Dim previousQuarter As Integer = currentQuarter - 1

        ' If the current quarter is Q1, then the previous quarter is Q4 of the previous year
        If previousQuarter < 1 Then
            previousQuarter = 4
            year -= 1 ' Decrement the year as we're moving to the previous year
        End If
        Return $"{year}Q{previousQuarter}"
    Else
        Return $"{time} is not a valid time"
    End If
End Function

 

Call it using standard OS time like GetPreviousQuarter("2024M4")  will return 2024Q1`

sameburn
New Contributor III

A good way to derive a time period based on a time variable is to use the TimeDimHelper class. 

You can nest functions like below; to achieve most use cases. 

' Declare timePeriod
Dim timePeriod As String = "2024M1"
' Derive prior quarter based on time passed in
Dim priorQuarterPeriod As String = TimeDimHelper.GetNameFromId(TimeDimHelper.GetQuarterIdFromId(TimeDimHelper.GetLastPeriodInPriorQuarter(TimeDimHelper.GetIdFromName(timePeriod))))
				
' Log
BRApi.ErrorLog.LogMessage(si, "PriorQuarter Logging: ", priorQuarterPeriod)

 

ah!  TimeDimHelper.  I didn't know that existed. 🙂  
However for me, I would avoid nested method calls:

Dim time as string = "2022M4"
Dim priorQtr as string = GetPriorQtr(time)
BRApi.ErrorLog.LogMessage(si, priorQtr)  ' should print 2022Q1

Public Function GetPriorQtr(time As String) As String
    Dim timeId As Integer = TimeDimHelper.GetIdFromName(time)
    Dim lastPeriodInPriorQtr As Integer = TimeDimHelper.GetLastPeriodInPriorQuarter(timeId)
    Dim priorQtrId As Integer = TimeDimHelper.GetQuarterIdFromId(lastPeriodInPriorQtr)
    Dim priorQtr As String = TimeDimHelper.GetNameFromId(priorQtrId)
	
    Return priorQtr
End Function

 

Yaseen
New Contributor III

Thank you @sameburn @RobbSalzmann