

Forum Newbie
Group: General Forum Members
Last Login: Tuesday, April 6, 2010 11:10 PM
Points: 5,
Visits: 44


I have been asked to create a weighted distribution calcuation for a SRS report for the Business Development department. I have never done anything like this using TSQL and really could use some advice. The issue I am having is figuring out how to code this thing so that when the query runs it knows what data needs to be calculated when and with what percentage (margin * margin pct). Being a bell curve calculation over an equal distribution calc the margin % will vary depending on when the project starts and how long the term is.
Example... you have a project that totals $1M in revenue with a $250,000 margin and it is 6 months long. The project say is already a month under way and the bell curve calc for that range is m1 (10%), m2 (15%), m3 (15%), m4 (25%), m5 (25%), m6 (10%).
The output should appear something like this:
Project  Description  Revenue  Margin  Sept 2009  Oct 2009  Nov 2009  Dec 2009  Jan 2010  Feb 2010  Project Name  DESC  $1,000,000  $250,000  $37,500  $37,500  $62,500  $62,500  $25,000  $0
Notice how the first month which is forecasted at 10% in the bell curve calc doesnt appear, that is because the project is already a month in so therefore that month doesnt appear on the current forecast report. It has to begin its calc in month 2. That is also why 6 months out (Feb 2010) is 0 because the project will be complete then.
Anyone have a clue how to do this?




SSCEnthusiastic
Group: General Forum Members
Last Login: Tuesday, February 9, 2010 2:41 AM
Points: 140,
Visits: 1,019





Forum Newbie
Group: General Forum Members
Last Login: Tuesday, April 6, 2010 11:10 PM
Points: 5,
Visits: 44


Thanks for your reply. It's an example of what the bell curve calculations are. Obviously equal distribution would be a matter of dividing the margin equally over the course of the term (i.e. $250K / 9)... With the bell curve calculation its different in that the calculations for each month vary... 10% of margin one month followed by say 20% the following and so on....
I havent ever written anything like this before and havent seen any examples anywhere on the net. I was thinking maybe a stored procedure or CURSOR may be an option but was hoping someone on here has coded something like this before and could offer a suggestion.




SSCrazy
Group: General Forum Members
Last Login: Sunday, August 17, 2014 3:10 PM
Points: 2,787,
Visits: 6,098


I will suggest that you give us an example using sample tables and data, to accompany your picture of what the expected output should be. Simple text explanations in the absence of source data lead to long sessions of questionandanswer that can be often be shortened dramatically with just a little effort to create some data.
For examples of how to set up a problem to get tested solutions faster, read this article.
Thanks
__________________________________________________
Against stupidity the gods themselves contend in vain.  Friedrich Schiller Stop, children, what's that sound?  Stephen Stills




Forum Newbie
Group: General Forum Members
Last Login: Tuesday, April 6, 2010 11:10 PM
Points: 5,
Visits: 44


Bob  Thank you as well for your reply... I apologize for not explaining this as well as possible :)
Here is a small example of the coding used for the equal monthly distribution I was reffering to in my original post: (I shortened it to only show you two months of calculation obviously to save space here. So the completed version goes out 24 months, not 2.)
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO
CREATE PROCEDURE usp_WeightedDistCalc WITH ENCRYPTION
AS BEGIN  SET NOCOUNT ON added to prevent extra result sets from  interfering with SELECT statements. SET NOCOUNT ON;
 Insert statements for procedure here SELECT OpportunityBase.Name, OpportunityBase.Description, SystemUserBase.LastName, SystemUserBase.FirstName, OpportunityBase.CloseProbability, OpportunityBase.EstimatedValue, OpportunityExtensionBase.New_Margin, OpportunityExtensionBase.New_ConstructionStartDate, OpportunityExtensionBase.New_OpportunityReport, OpportunityExtensionBase.New_ConstructionTerm, OpportunityExtensionBase.New_RFP, DATEADD(MONTH,+1,(DATEADD(MONTH,OpportunityExtensionBase.New_ConstructionTerm, OpportunityExtensionBase.New_ConstructionStartDate)))  DAY(DATEADD(MONTH,OpportunityExtensionBase.New_ConstructionTerm, OpportunityExtensionBase.New_ConstructionStartDate))AS EndDate,
Month1 = case when GetDate() between OpportunityExtensionBase.New_ConstructionStartDate and DAteadd(month,+1,(DateAdd(month,OpportunityExtensionBase.New_ConstructionTerm, OpportunityExtensionBase.New_ConstructionStartDate)))  day(DateAdd(month,OpportunityExtensionBase.New_ConstructionTerm, OpportunityExtensionBase.New_ConstructionStartDate)) then (case ISNULL(New_ConstructionTerm, 0) when 0 then 0 else ((ISNULL(New_Margin, 0)) / (case ISNULL(New_ConstructionTerm, 1) when 0 then 1 else ISNULL(New_ConstructionTerm, 1) end)) end) else 0 end, Month2 = case when dateadd(month,+1,GetDate()) between OpportunityExtensionBase.New_ConstructionStartDate and DAteadd(month,+1,(DateAdd(month,OpportunityExtensionBase.New_ConstructionTerm, OpportunityExtensionBase.New_ConstructionStartDate)))  day(DateAdd(month,OpportunityExtensionBase.New_ConstructionTerm, OpportunityExtensionBase.New_ConstructionStartDate)) then (case ISNULL(New_ConstructionTerm, 0) when 0 then 0 else ((ISNULL(New_Margin, 0)) / (case ISNULL(New_ConstructionTerm, 1) when 0 then 1 else ISNULL(New_ConstructionTerm, 1) end)) end) else 0 end,
FROM OpportunityBase INNER JOIN OpportunityExtensionBase ON OpportunityBase.OpportunityId = OpportunityExtensionBase.OpportunityId INNER JOIN SystemUserBase ON OpportunityBase.OwningUser = SystemUserBase.SystemUserId
END GO
________________________________________________________________
As you can see the calculation for the above query is a simple division of (Margin / Term). Also creating the months out I need to go (24) by using the DATEADD function. I need to achive pretty much the same layout except now instead of equally diving the margin by the term (in months) I need to have the calculations vary based off of the bell curve calculation provided. See below:
So  for example, if you look at the chart above. Say a project term is 9 months, the monthly calculations have to be as the chart above states... I hope this clears up some of the mess :)




Ten Centuries
Group: General Forum Members
Last Login: Today @ 2:46 PM
Points: 1,327,
Visits: 19,280


So you're forcing your project projection to show the calculated values based on the percentages provided? You're not calculating the curve based on actual values?
If that's the case, and your distribution will never change, just build a table with your percentages for each possible project length. Then when you need to show the calculation, take the budget, determine how many terms it has, what term you're in, and display the result.
I'll work on a sample in a few...
[editing to add sample]
Someone may show you how to do this cleaner, but since you have a finite distribution, I just used a loop. Change your Startdate, Enddate, budget as required, only displays the months used with the final dollars applied.
CREATE TABLE #bellcurve ( term INT , month1 DECIMAL(2, 2) , month2 DECIMAL(2, 2) , month3 DECIMAL(2, 2) , month4 DECIMAL(2, 2) , month5 DECIMAL(2, 2) , month6 DECIMAL(2, 2) , month7 DECIMAL(2, 2) , month8 DECIMAL(2, 2) , month9 DECIMAL(2, 2) , month10 DECIMAL(2, 2) , month11 DECIMAL(2, 2) , month12 DECIMAL(2, 2) , month13 DECIMAL(2, 2) , month14 DECIMAL(2, 2) , month15 DECIMAL(2, 2) , month16 DECIMAL(2, 2) , month17 DECIMAL(2, 2) , month18 DECIMAL(2, 2) )
INSERT INTO #bellcurve SELECT 3 , .2 , .5 , .3 , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL UNION ALL SELECT 4 , .15 , .3 , .3 , .25 , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL UNION ALL SELECT 5 , .12 , .25 , .25 , .23 , .15 , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL UNION ALL SELECT 6 , .1 , .2 , .2 , .2 , .2 , .1 , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL UNION ALL SELECT 18 , .03 , .03 , .03 , .03 , .05 , .07 , .08 , .09 , .08 , .08 , .08 , .08 , .07 , .05 , .05 , .05 , .03 , .02
SELECT * FROM #bellcurve
DECLARE @budget MONEY , @startdate DATETIME , @enddate DATETIME , @terms INT , @sql VARCHAR(2000) , @i INT  loop counter
SET @budget = 1000000 SET @startdate = '1/1/2009' SET @enddate = '7/1/2010' SET @terms = DATEDIFF(mm, @startdate, @enddate) SET @i = 1 SET @sql = 'SELECT '
SELECT 'Budget = $' + CONVERT(VARCHAR, @budget) + CHAR(13) + 'Project starting on: ' + CONVERT(VARCHAR, @startdate, 101) + CHAR(13) + 'Project ending on: ' + CONVERT(VARCHAR, @enddate, 101) + CHAR(13) + 'Project has ' + CONVERT(VARCHAR, @terms) + ' terms.'
WHILE @i <= @terms BEGIN SET @sql = @sql + CONVERT(VARCHAR, @budget) + ' * month' + CONVERT(VARCHAR, @i) + ' AS ' + LEFT(DATENAME(mm, DATEADD(mm, @i  1, @startdate)), 3) + DATENAME(yyyy, DATEADD(mm, @i  1, @startdate)) + ',' SET @i = @i + 1 PRINT @sql END
SET @sql = LEFT(@sql, LEN(@sql)  1) + ' FROM #bellcurve AS b WHERE b.term = ' + CONVERT(VARCHAR, @terms)
PRINT (@sql) EXEC(@sql)
 How best to post your question How to post performance problems Tally Table:What it is and how it replaces a loop
"stewsterl 80804 (10/16/2009)I guess when you stop and try to understand the solution provided you not only learn, but save yourself some headaches when you need to make any slight changes."




Forum Newbie
Group: General Forum Members
Last Login: Tuesday, April 6, 2010 11:10 PM
Points: 5,
Visits: 44


Cool! That does help! I figured the same thing last night so I already created the table... I just seemed to be having an issue with the start dates and the calculations happening when they are supposed to. I will go through your example again and see if that can help me fix the issue!
I'll let you know if that does it!
Thanks again!!



