You need to sign in to do that
Don't have an account?
Mathew Andresen 5
year over year changes
Hi,
I have a map/list with year over year sales by quarter for accounts. I want to calculate the change year over year. For example if Q1 prior year is 100 and Q1 current year is 150 then the growth should be 150.
Currently what I'm doing is changing my map into a list and then doing the calculation based on the position in the list, but I'm wondering if there is a better way to do this.
Any thoughts?
I have a map/list with year over year sales by quarter for accounts. I want to calculate the change year over year. For example if Q1 prior year is 100 and Q1 current year is 150 then the growth should be 150.
Currently what I'm doing is changing my map into a list and then doing the calculation based on the position in the list, but I'm wondering if there is a better way to do this.
Any thoughts?
for (PivotTimeSeries_Class.PivotRowWrapper wrapper :partialRowList) { List<decimal> salesList = new List<decimal>(); for (string period:periodYearList) { salesList.add(wrapper.amountMap.get(period)); } map<string, decimal> growthMap = new map<string, decimal>(); decimal Y1Total; decimal Y2Total; decimal q1Growth; decimal q2Growth; decimal q3Growth; decimal q4Growth; decimal YGrowth; Y1Total = salesList[0] + salesList[1] + salesList[2] + salesList[3]; Y2Total = salesList[5] + salesList[6] + salesList[7] + salesList[8]; wrapper.amountMap.put('Y1 Total', Y1Total); wrapper.amountMap.put('Y2 Total', Y2Total); if (salesList[0] != 0 && salesList[0] != NULL && salesList[4] != 0 && salesList[4] != NULL) { q1Growth = salesList[4]/salesList[0]*100 -100; } else { q1Growth = 0; } growthMap.put('P1Growth', q1Growth); if (salesList[1] != 0 && salesList[1] != NULL && salesList[5] != 0 && salesList[5] != NULL) { q2Growth = salesList[5]/salesList[1]*100 -100; } else { q2Growth = 0; } growthMap.put('P2Growth', q2Growth); if (salesList[2] != 0 && salesList[2] != NULL && salesList[6] != 0 && salesList[6] != NULL) { q3Growth = salesList[6]/salesList[2]*100 -100; } else { q3Growth = 0; } growthMap.put('P3Growth', q3Growth); if (salesList[3] != 0 && salesList[3] != NULL && salesList[7] != 0 && salesList[7] != NULL) { q4Growth = salesList[7]/salesList[3]*100 -100; } else { q4Growth = 0; } growthMap.put('P4Growth', q4Growth); if (Y1Total != 0 && Y2Total != 0) { YGrowth = Y2Total / Y1TOtal*100 -100; system.debug ('firing'); } else { YGrowth = 0; } growthMap.put('YoY Growth', YGrowth); system.debug('YGrowth = ' +YGrowth); sortList.add(new SortWrapper(new CompleteRowWrapper(wrapper, growthMap))); } sortList.sort(); for (SortWrapper sorted:sortList) { CompleteRowList.add(sorted.wrapper); }
Then this makes the rest of your code look like
NOTE: This code has not been tested and may contain typographical or logical errors
You could condense it down a bit more, but I think this is a better balance between readability and size. If I were to make any additional changes, I would move you strings ('P1Growth', 'P2Growth', etc) into class level static variables to make it easier to reference and reduce the chance of typos.
All Answers
Then this makes the rest of your code look like
NOTE: This code has not been tested and may contain typographical or logical errors
You could condense it down a bit more, but I think this is a better balance between readability and size. If I were to make any additional changes, I would move you strings ('P1Growth', 'P2Growth', etc) into class level static variables to make it easier to reference and reduce the chance of typos.