Rams_2022
4 months agoNew Contributor II
Business rule didn't give results
Dear OS Experts,
really appreciate your support on this, I have written a business rule for some generic calculations based on the calender months. the rule is working for USD currency but it not working for CAD. Any suggestions on this code corrections?
Namespace OneStream.BusinessRule.Finance.Temp234Calc
Public Class MainClass
#Region "Main - xxx Calculation"
Public Function Main(ByVal si As SessionInfo, ByVal globals As BRGlobals, ByVal api As FinanceRulesApi, ByVal args As FinanceRulesArgs) As Object
Try
Dim timePeriod As String = api.Pov.Time.Name
Dim entityCode As String = api.Pov.Entity.Name
Dim monthNumber As SByte = api.Time.GetPeriodNumFromId
Dim yearNumber As Short = TimeDimHelper.GetSubComponentsFromName(timePeriod).Year
Dim currScenario As String = api.Pov.Scenario.Name
Dim currCons As ViewMember = api.Scenario.GetConsolidationView()
Dim localCurrency As String = api.pov.Cons.Name
Dim conversionMonth As String = args.CustomCalculateArgs.NameValuePairs("ConversionMonth")
Dim conversionYear As String = args.CustomCalculateArgs.NameValuePairs("ConversionYear")
Dim parentEntity As String = args.CustomCalculateArgs.NameValuePairs("LegacyParentEntity")
Dim monthShift As Dictionary(Of String, MonthShiftValues) = CreateMonthShiftDict(localCurrency)
Dim iter As MonthShiftValues = Nothing
monthShift.TryGetValue(timePeriod, iter)
Dim priorPeriodRevenue As Decimal = api.Data.GetDataCell("E#" & parentEntity & ":A#NET_SALES:T#PovPrior1:V#Periodic").CellAmount
Dim currentPeriodRevenue As Decimal = api.Data.GetDataCell("E#" & parentEntity & ":A#NET_SALES:T#Pov:V#Periodic").CellAmount
Dim nextPeriodRevenue As Decimal = api.Data.GetDataCell("E#" & parentEntity & ":A#NET_SALES:T#PovNext1:V#Periodic").CellAmount
Dim priorPeriodValid As Boolean = False
Dim currentPeriodValid As Boolean = False
Dim nextPeriodValid As Boolean = False
If priorPeriodRevenue > 0 Then
priorPeriodValid = True
End If
If currentPeriodRevenue > 0 Then
currentPeriodValid = True
End If
If nextPeriodRevenue > 0 Then
nextPeriodValid = True
End If
If (Not api.Entity.HasChildren()) And currScenario = "Actual" Then
Dim priorPeriodAdj As DataBuffer
Dim currentPeriodAdj As DataBuffer
Dim nextPeriodAdj As DataBuffer
If priorPeriodValid And nextPeriodValid Then
priorPeriodAdj = api.Data.GetDataBufferUsingFormula("DIVIDE(FilterMembers(T#PovPrior1:V#Periodic:U1#Fro_Prog_Mgt_Stats_Rules123, [E#["& entityCode &"]], [A#[INCOME_MGMT].Base]) * "& CStr(iter.prevMonth) &", "& CStr(iter.prevMonthDays) &")")
currentPeriodAdj = api.Data.GetDataBufferUsingFormula("DIVIDE(FilterMembers(T#Pov:V#Periodic:U1#Fro_Prog_Mgt_Stats_Rules123, [E#["& entityCode &"]], [A#[INCOME_MGMT].Base]) * "& CStr(iter.currMonth) &", "& CStr(iter.currMonthDays) &")")
nextPeriodAdj = api.Data.GetDataBufferUsingFormula("DIVIDE(FilterMembers(T#PovNext1:V#Periodic:U1#Fro_Prog_Mgt_Stats_Rules123, [E#["& entityCode &"]], [A#[INCOME_MGMT].Base]) * "& CStr(iter.nextMonth) &", "& CStr(iter.nextMonthDays) &")")
ElseIf (Not priorPeriodValid) And nextPeriodValid Then
priorPeriodAdj = api.Data.GetDataBufferUsingFormula("DIVIDE(FilterMembers(T#Pov:V#Periodic:U1#Fro_Prog_Mgt_Stats_Rules123, [E#["& entityCode &"]], [A#[INCOME_MGMT].Base]) * "& CStr(iter.prevMonth) &", "& CStr(iter.currMonthDays) &")")
currentPeriodAdj = api.Data.GetDataBufferUsingFormula("DIVIDE(FilterMembers(T#Pov:V#Periodic:U1#Fro_Prog_Mgt_Stats_Rules123, [E#["& entityCode &"]], [A#[INCOME_MGMT].Base]) * "& CStr(iter.currMonth) &", "& CStr(iter.currMonthDays) &")")
nextPeriodAdj = api.Data.GetDataBufferUsingFormula("DIVIDE(FilterMembers(T#PovNext1:V#Periodic:U1#Fro_Prog_Mgt_Stats_Rules123, [E#["& entityCode &"]], [A#[INCOME_MGMT].Base]) * "& CStr(iter.nextMonth) &", "& CStr(iter.nextMonthDays) &")")
ElseIf priorPeriodValid And (Not nextPeriodValid) Then
priorPeriodAdj = api.Data.GetDataBufferUsingFormula("DIVIDE(FilterMembers(T#PovPrior1:V#Periodic:U1#Fro_Prog_Mgt_Stats_Rules123, [E#["& entityCode &"]], [A#[INCOME_MGMT].Base]) * "& CStr(iter.prevMonth) &", "& CStr(iter.prevMonthDays) &")")
currentPeriodAdj = api.Data.GetDataBufferUsingFormula("DIVIDE(FilterMembers(T#Pov:V#Periodic:U1#Fro_Prog_Mgt_Stats_Rules123, [E#["& entityCode &"]], [A#[INCOME_MGMT].Base]) * "& CStr(iter.currMonth) &", "& CStr(iter.currMonthDays) &")")
nextPeriodAdj = api.Data.GetDataBufferUsingFormula("DIVIDE(FilterMembers(T#Pov:V#Periodic:U1#Fro_Prog_Mgt_Stats_Rules123, [E#["& entityCode &"]], [A#[INCOME_MGMT].Base]) * "& CStr(iter.nextMonth) &", "& CStr(iter.currMonthDays) &")")
ElseIf (Not priorPeriodValid) And (Not nextPeriodValid) Then
priorPeriodAdj = api.Data.GetDataBufferUsingFormula("DIVIDE(FilterMembers(T#Pov:V#Periodic:U1#Fro_Prog_Mgt_Stats_Rules123, [E#["& entityCode &"]], [A#[INCOME_MGMT].Base]) * "& CStr(iter.prevMonth) &", "& CStr(iter.currMonthDays) &")")
currentPeriodAdj = api.Data.GetDataBufferUsingFormula("DIVIDE(FilterMembers(T#Pov:V#Periodic:U1#Fro_Prog_Mgt_Stats_Rules123, [E#["& entityCode &"]], [A#[INCOME_MGMT].Base]) * "& CStr(iter.currMonth) &", "& CStr(iter.currMonthDays) &")")
nextPeriodAdj = api.Data.GetDataBufferUsingFormula("DIVIDE(FilterMembers(T#Pov:V#Periodic:U1#Fro_Prog_Mgt_Stats_Rules123, [E#["& entityCode &"]], [A#[INCOME_MGMT].Base]) * "& CStr(iter.nextMonth) &", "& CStr(iter.currMonthDays) &")")
End If
api.Data.FormulaVariables.SetDataBufferVariable("priorPeriodAdj", priorPeriodAdj, True)
api.Data.FormulaVariables.SetDataBufferVariable("currentPeriodAdj", currentPeriodAdj, True)
api.Data.FormulaVariables.SetDataBufferVariable("nextPeriodAdj", nextPeriodAdj, True)
Select Case yearNumber
Case = conversionYear
If monthNumber = 1 And conversionMonth = 1 Then
api.Data.Calculate(
"E#" & entityCode & ":V#Periodic:O#Import:U1#123_Adjustment = 0"
)
ElseIf monthNumber > 1 And conversionMonth = 1 Then
api.Data.Calculate(
"E#" & entityCode & ":V#YTD:U1#123_Adjustment = FilterMembers(T#PovPrior1:V#YTD:U1#123_Adjustment, [A#[INCOME_MGMT].Base])"
)
ElseIf monthNumber = 1 And conversionMonth <> 1 And currentPeriodValid Then
api.Data.Calculate("E#" & entityCode & ":V#Periodic:U1#123_Adjustment = $priorPeriodAdj + $currentPeriodAdj + $nextPeriodAdj")
ElseIf monthNumber > 1 And monthNumber < conversionMonth And conversionMonth <> 1 And currentPeriodValid Then
api.Data.Calculate("E#" & entityCode & ":V#YTD:U1#123_Adjustment = FilterMembers(T#PovPrior1:V#YTD:U1#123_Adjustment, [A#[INCOME_MGMT].Base]) + $priorPeriodAdj + $currentPeriodAdj + $nextPeriodAdj")
ElseIf monthNumber >= conversionMonth And conversionMonth <> 1 Then
api.Data.Calculate("E#" & entityCode & ":V#YTD:U1#123_Adjustment = FilterMembers(T#PovPrior1:V#YTD:U1#123_Adjustment, [A#[INCOME_MGMT].Base])")
End If
Case < conversionYear
If monthNumber = 1 And currentPeriodValid Then
api.Data.Calculate("E#" & entityCode & ":V#Periodic:U1#123_Adjustment = $priorPeriodAdj + $currentPeriodAdj + $nextPeriodAdj")
ElseIf monthNumber > 1 And currentPeriodValid Then
api.Data.Calculate("E#" & entityCode & ":V#YTD:U1#123_Adjustment = FilterMembers(T#PovPrior1:V#YTD:U1#123_Adjustment, [A#[INCOME_MGMT].Base]) + $priorPeriodAdj + $currentPeriodAdj + $nextPeriodAdj")
End If
End Select
ElseIf (Not api.Entity.HasChildren()) And currScenario <> "Actual" And currCons.Name = "Periodic" Then
If monthNumber >= 1 Then
api.Data.Calculate(
"E#" & entityCode & ":V#Periodic:U1#123_Adjustment =
DIVIDE(FilterMembers(T#PovPrior1:V#Periodic:U1#Fro_Prog_Mgt_Stats_Rules123, [E#["& entityCode &"]], [A#[INCOME_MGMT].Base]) * "& CStr(iter.prevMonth) &", "& CStr(iter.prevMonthDays) &") +
DIVIDE(FilterMembers(T#Pov:V#Periodic:U1#Fro_Prog_Mgt_Stats_Rules123, [E#["& entityCode &"]], [A#[INCOME_MGMT].Base]) * "& CStr(iter.currMonth) &", "& CStr(iter.currMonthDays) &") +
DIVIDE(FilterMembers(T#PovNext1:V#Periodic:U1#Fro_Prog_Mgt_Stats_Rules123, [E#["& entityCode &"]], [A#[INCOME_MGMT].Base]) * "& CStr(iter.nextMonth) &", "& CStr(iter.nextMonthDays) &")
"
)
End If
ElseIf (Not api.Entity.HasChildren()) And currScenario <> "Actual" And currCons.Name = "YTD" Then
If monthNumber = 1 Then
api.Data.Calculate(
"E#" & entityCode & ":V#Periodic:U1#123_Adjustment =
DIVIDE(FilterMembers(T#PovPrior1:V#Periodic:U1#Fro_Prog_Mgt_Stats_Rules123, [E#["& entityCode &"]], [A#[INCOME_MGMT].Base]) * "& CStr(iter.prevMonth) &", "& CStr(iter.prevMonthDays) &") +
DIVIDE(FilterMembers(T#Pov:V#Periodic:U1#Fro_Prog_Mgt_Stats_Rules123, [E#["& entityCode &"]], [A#[INCOME_MGMT].Base]) * "& CStr(iter.currMonth) &", "& CStr(iter.currMonthDays) &") +
DIVIDE(FilterMembers(T#PovNext1:V#Periodic:U1#Fro_Prog_Mgt_Stats_Rules123, [E#["& entityCode &"]], [A#[INCOME_MGMT].Base]) * "& CStr(iter.nextMonth) &", "& CStr(iter.nextMonthDays) &")
"
)
ElseIf monthNumber > 1 Then
api.Data.Calculate(
"E#" & entityCode & ":V#YTD:U1#123_Adjustment = FilterMembers(T#PovPrior1:V#YTD:U1#123_Adjustment, [A#[INCOME_MGMT].Base]) +
DIVIDE(FilterMembers(T#PovPrior1:V#Periodic:U1#Fro_Prog_Mgt_Stats_Rules123, [E#["& entityCode &"]], [A#[INCOME_MGMT].Base]) * "& CStr(iter.prevMonth) &", "& CStr(iter.prevMonthDays) &") +
DIVIDE(FilterMembers(T#Pov:V#Periodic:U1#Fro_Prog_Mgt_Stats_Rules123, [E#["& entityCode &"]], [A#[INCOME_MGMT].Base]) * "& CStr(iter.currMonth) &", "& CStr(iter.currMonthDays) &") +
DIVIDE(FilterMembers(T#PovNext1:V#Periodic:U1#Fro_Prog_Mgt_Stats_Rules123, [E#["& entityCode &"]], [A#[INCOME_MGMT].Base]) * "& CStr(iter.nextMonth) &", "& CStr(iter.nextMonthDays) &")
"
)
End If
End If
Return Nothing
Catch ex As Exception
Throw ErrorHandler.LogWrite(si, New XFException(si, ex))
End Try
End Function
#End Region
Public Function CreateMonthShiftDict(ByVal Currency As String) As Dictionary(Of String, MonthShiftValues)
If Currency = "USD" Then
Dim monthShift_USD As New Dictionary(Of String, MonthShiftValues)
monthShift_USD.Add("2023M1",New MonthShiftValues(2,-2,0,21,20,23))
monthShift_USD.Add("2023M2",New MonthShiftValues(2,-5,0,20,23,20))
monthShift_USD.Add("2023M3",New MonthShiftValues(5,0,0,23,20,22))
monthShift_USD.Add("2023M4",New MonthShiftValues(0,-2,0,20,22,22))
monthShift_USD.Add("2023M5",New MonthShiftValues(2,-5,0,22,22,20))
monthShift_USD.Add("2023M6",New MonthShiftValues(5,-1,0,22,20,23))
monthShift_USD.Add("2023M7",New MonthShiftValues(1,-4,0,20,23,20))
monthShift_USD.Add("2023M8",New MonthShiftValues(4,-5,0,23,20,22))
monthShift_USD.Add("2023M9",New MonthShiftValues(5,-2,0,20,22,20))
monthShift_USD.Add("2023M10",New MonthShiftValues(2,-4,0,22,20,20))
monthShift_USD.Add("2023M11",New MonthShiftValues(4,-4,0,20,20,22))
monthShift_USD.Add("2023M12",New MonthShiftValues(4,-3,0,20,22,21))
monthShift_USD.Add("2024M1",New MonthShiftValues(3,-4,0,22,21,21))
monthShift_USD.Add("2024M2",New MonthShiftValues(4,-5,0,21,21,22))
monthShift_USD.Add("2024M3",New MonthShiftValues(5,-2,0,21,22,22))
monthShift_USD.Add("2024M4",New MonthShiftValues(2,-4,0,22,22,20))
monthShift_USD.Add("2024M5",New MonthShiftValues(4,-5,0,22,20,22))
monthShift_USD.Add("2024M6",New MonthShiftValues(5,-3,0,20,22,22))
monthShift_USD.Add("2024M7",New MonthShiftValues(3,-5,0,22,22,20))
monthShift_USD.Add("2024M8",New MonthShiftValues(5,-6,0,22,20,23))
monthShift_USD.Add("2024M9",New MonthShiftValues(6,-4,0,20,23,19))
monthShift_USD.Add("2024M10",New MonthShiftValues(4,-3,0,23,19,21))
monthShift_USD.Add("2024M11",New MonthShiftValues(3,-6,0,19,21,22))
monthShift_USD.Add("2024M12",New MonthShiftValues(6,0,0,21,22,20))
Return monthShift_USD
ElseIf Currency = "CAD" Then
Dim monthShift_CAD As New Dictionary(Of String, MonthShiftValues)
monthShift_CAD.Add("2023M1",New MonthShiftValues(2,-2,0,21,20,23))
monthShift_CAD.Add("2023M2",New MonthShiftValues(2,-5,0,20,23,20))
monthShift_CAD.Add("2023M3",New MonthShiftValues(5,0,0,23,20,22))
monthShift_CAD.Add("2023M4",New MonthShiftValues(0,-2,0,20,22,22))
monthShift_CAD.Add("2023M5",New MonthShiftValues(2,-5,0,22,22,20))
monthShift_CAD.Add("2023M6",New MonthShiftValues(5,-1,0,22,20,23))
monthShift_CAD.Add("2023M7",New MonthShiftValues(1,-4,0,20,23,20))
monthShift_CAD.Add("2023M8",New MonthShiftValues(4,-5,0,23,20,22))
monthShift_CAD.Add("2023M9",New MonthShiftValues(5,-2,0,20,22,20))
monthShift_CAD.Add("2023M10",New MonthShiftValues(2,-4,0,22,20,20))
monthShift_CAD.Add("2023M11",New MonthShiftValues(4,-4,0,20,20,22))
monthShift_CAD.Add("2023M12",New MonthShiftValues(4,-3,0,20,22,21))
monthShift_CAD.Add("2024M1",New MonthShiftValues(3,-4,0,22,21,21))
monthShift_CAD.Add("2024M2",New MonthShiftValues(4,-5,0,21,21,22))
monthShift_CAD.Add("2024M3",New MonthShiftValues(5,-2,0,21,22,22))
monthShift_CAD.Add("2024M4",New MonthShiftValues(2,-4,0,22,22,20))
monthShift_CAD.Add("2024M5",New MonthShiftValues(4,-5,0,22,20,22))
monthShift_CAD.Add("2024M6",New MonthShiftValues(5,-3,0,20,22,22))
monthShift_CAD.Add("2024M7",New MonthShiftValues(3,-5,0,22,22,20))
monthShift_CAD.Add("2024M8",New MonthShiftValues(5,-6,0,22,20,23))
monthShift_CAD.Add("2024M9",New MonthShiftValues(6,-4,0,20,23,19))
monthShift_CAD.Add("2024M10",New MonthShiftValues(4,-3,0,23,19,21))
monthShift_CAD.Add("2024M11",New MonthShiftValues(3,-6,0,19,21,22))
monthShift_CAD.Add("2024M12",New MonthShiftValues(6,0,0,21,22,20))
Return monthShift_CAD
End If
End Function
End Class
#Region "MonthShiftClass"
Public Class MonthShiftValues
Public Property prevMonth As SByte
Public Property currMonth As SByte
Public Property nextMonth As SByte
Public Property prevMonthDays As SByte
Public Property currMonthDays As SByte
Public Property nextMonthDays As SByte
Public Sub New(ByVal prevMonth As SByte, ByVal currMonth As SByte, ByVal nextMonth As SByte, ByVal prevMonthDays As SByte, ByVal currMonthDays As SByte, ByVal nextMonthDays As SByte)
Me.prevMonth = prevMonth
Me.currMonth = currMonth
Me.nextMonth = nextMonth
Me.prevMonthDays = prevMonthDays
Me.currMonthDays = currMonthDays
Me.nextMonthDays = nextMonthDays
End Sub
End Class
#End Region
End Namespace