Business hours calculation 8AM to 8PM AST business hours in the format of days: hours : minutes its - salesforce

Need to exclude weekends and before business hours and after business hours
Below code not working accurately.
Please suggest any other solution too
<TEXT(FLOOR( ROUND(720*((5*FLOOR((DATEVALUE(ClosedDate)-DATE(1996,01,01))/7) +
MIN(5,
MOD(DATEVALUE(ClosedDate)-DATE(1996,01,01), 7) +
MIN(1, 24/12*(MOD(ClosedDate-DATETIMEVALUE('1996-01-01 16:00:00'), 1)))
))-(5*FLOOR((DATEVALUE(CreatedDate)-DATE(1996,01,01))/7) +MIN(5,
MOD(DATEVALUE(CreatedDate)-DATE(1996,01,01), 7) +
MIN(1, 24/12*(MOD(CreatedDate-DATETIMEVALUE('1996-01-01 16:00:00'), 1)))
))), 0) / 720)) & " Day(s) " & TEXT( FLOOR( MOD(ROUND(720*((5*FLOOR((DATEVALUE(ClosedDate)-DATE(1996,01,01))/7) +
MIN(5,
MOD(DATEVALUE(ClosedDate)-DATE(1996,01,01), 7) +
MIN(1, 24/12*(MOD(ClosedDate-DATETIMEVALUE('1996-01-01 16:00:00'), 1)))
))-(5*FLOOR((DATEVALUE(CreatedDate)-DATE(1996,01,01))/7) +MIN(5,
MOD(DATEVALUE(CreatedDate)-DATE(1996,01,01), 7) +
MIN(1, 24/12*(MOD(CreatedDate-DATETIMEVALUE('1996-01-01 16:00:00'), 1)))
))), 0),720 ) / 60)) &' Hour(s)'&
TEXT(FLOOR(MOD( MOD(ROUND(720*((5*FLOOR((DATEVALUE(ClosedDate)-DATE(1996,01,01))/7) +
MIN(5,
MOD(DATEVALUE(ClosedDate)-DATE(1996,01,01), 7) +
MIN(1, 24/12*(MOD(ClosedDate-DATETIMEVALUE('1996-01-01 16:00:00'), 1)))
))-(5*FLOOR((DATEVALUE(CreatedDate)-DATE(1996,01,01))/7) +MIN(5,
MOD(DATEVALUE(CreatedDate)-DATE(1996,01,01), 7) +
MIN(1, 24/12*(MOD(CreatedDate-DATETIMEVALUE('1996-01-01 16:00:00'), 1)))
))), 0),720),60)))& "Minute(s)"),' ')/>

Related

Compute daily returns from cumulative returns

I have a dataframe with cumulative stock returns from 1 to 5 days:
1dReturn 2dReturn 3dReturn 4dReturn 5dReturn
Ticker
LUNA -3.077 -3.077 -6.923 -6.915 -6.615
YTEN -2.139 -2.139 -18.182 -16.043 -16.578
I would like to compute the daily returns. Is there a function for that?
Code below creates the table above:
df = pd.DataFrame({'1dReturn': [-3.077, -2.139],
'2dReturn': [-3.077, -2.139],
'3dReturn': [-6.923, -18.182],
'4dReturn': [-6.915, -16.043],
'5dReturn': [-6.615, -16.578],},
index=['LUNA', 'YTEN'])
The formula to arrive at the daily returns works as follows:
daily returns day 2: cD2/d1
daily returns day 3: cD4/(d1*d2)
daily returns day 4: cD5/(d1*d2*d3)
daily returns day 5: cD5/(d1*d2*d3*d4)
where cD1 is the cum return of day 1 and d1 is the daily return for d1 etc.
np.exp(np.log(cumReturn + 1.0).diff()) - 1
cumReturn is the cumulative return series in Pandas.
R_cum_i = (1 + R_daily_i) * (1 + R_daily_i-1) ... - 1
R_cum_i-1 = (1 + R_daily_i-1) * (1 + R_daily_i-2) ... - 1
so
R_cum_i =(R_cum_i-1 + 1) * (1 + R_daily_i-1) - 1
1 + R_daily_i-1 = (R_cum_i + 1) / (R_cum_i-1 + 1)
1 + R_daily_i-1 = exp(log((R_cum_i + 1) / (R_cum_i-1 + 1)))
1 + R_daily_i-1 = exp(log(R_cum_i + 1) - log(R_cum_i-1 + 1))
1 + R_daily_i-1 = exp(log(R_cum_i + 1).diff())
then
R_daily_i-1 = exp(log(R_cum_i + 1).diff()) - 1

typescript date formatting not working

I am trying to get a date for mat like "2018-05-17T08:09:02", but when I tried below code I get "2018-05-17T8:9:2"
can some one help to get "2018-05-17T08:09:02" , this format
let d = new Date();
console.log("date>> "+d.getFullYear() + "-" + ((d.getMonth() + 1) < 10 ? '0' : '') +
(d.getMonth() + 1) + "-" + d.getDate() + "T" +( d.getHours() )+ ":"+ d.getMinutes() + ":"+ d.getSeconds());
According to How to format numbers by prepending 0 to single-digit numbers?
Your desirable answer is
let d = new Date();
console.log("date>> "+d.getFullYear() + "-" + ((d.getMonth() + 1) < 10 ? '0' : '') +
(d.getMonth() + 1) + "-" + d.getDate() + "T" +("0" + d.getHours()).slice(-2)+ ":"+ ("0" + d.getMinutes()).slice(-2) + ":"+ ("0" + d.getSeconds()).slice(-2));
But as #Aleksey Solovey already mentioned in the above, I also recommand to use d.toISOString().slice(0,-5).

SQL Server 2008 View Automatically Renaming Aliases to Expr1, Expr2, etc

My aliases keep getting renamed to Expr1, Expr2, etc. after saving my view. I have multiple CASE WHEN THEN statements in the view that have the same aliases in each case statement (X0,Y0 and Z0.) Essentially all case statements after the first case statement replaces my aliases with this Expr1, Expr2.
Any help would be extremely appreciated. I also tried putting square brackets around the aliases but that did not work.
I should also note that when I go to save, I get a warning from SQL Server Management Studio about my order by clause. It's probably unrelated but you never know.
SELECT
TOP (100) PERCENT
s.dtmEvaluation AS SurgeryDate,
p.idPatient,
d.strLead AS LeadType,
ds.strDBSSite AS Target,
CASE
WHEN intSide = 0 THEN 'L'
ELSE 'R'
END AS Side,
ROUND(s.dblACX, 2, 1) AS ACX,
ROUND(s.dblACZ, 2, 1) AS ACY,
ROUND(s.dblACZ, 2, 1) AS ACZ,
ROUND(s.dblPCX, 2, 1) AS PCX,
ROUND(s.dblPCY, 2, 1) AS PCY,
ROUND(s.dblPCZ, 2, 1) AS PCZ,
ROUND(s.dblInitX, 2, 1) AS InitialX,
ROUND(s.dblInitY, 2, 1) AS [Initial Y],
ROUND(s.dblInitZ, 2, 1) AS [Initial Z],
s.dblACPCAngle AS InitialACPCAngle,
s.dblCenterAngle AS InitialCLangle,
s.dblMicroPasses AS MicroPasses,
s.dblMacroPasses AS MacroPasses,
ROUND(s.dblFinalX, 2, 1) AS FinalX,
ROUND(s.dblFinalY, 2, 1) AS FinalY,
ROUND(s.dblFinalZ, 2, 1) AS FinalZ,
ROUND(s.dblMeasuredX, 2, 1) AS MeasX,
ROUND(s.dblMeasuredY, 2, 1) AS MeasY,
ROUND(s.dblMeasuredZ, 2, 1) AS MeasZ,
s.dblMeasuredACPCAngle,
s.dblMeasuredCtrAngle,
ROUND(SQRT(POWER(s.dblMeasuredX - s.dblFinalX, 2) + POWER(s.dblMeasuredY - s.dblFinalY, 2) + POWER(s.dblMeasuredZ - s.dblFinalZ,2)), 2, 1) AS Delta,
CASE
WHEN s.intSide = 1 THEN s.dblMeasuredx + ((dblelectrodelength - 1) + 0 * (dblelectrodespacing + dblelectrodelength) + (dblelectrodelength / 2)) * SIN(RADIANS(dblMeasuredCtrAngle))
ELSE dblMeasuredx - ((dblelectrodelength - 1) + 0 * (dblelectrodespacing + dblelectrodelength) + (dblelectrodelength / 2)) * SIN(RADIANS(dblMeasuredCtrAngle))
END AS X0,
s.dblMeasuredY +
(((d.dblElectrodeLength - 1)
+ 0 * (d.dblElectrodeSpacing + d.dblElectrodeLength)) +
d.dblElectrodeLength / 2) * COS(RADIANS(s.dblMeasuredACPCAngle))
* COS(RADIANS(s.dblMeasuredCtrAngle)) AS Y0,
s.dblMeasuredZ + (((d.dblElectrodeLength - 1) + 0 *
(d.dblElectrodeSpacing + d.dblElectrodeLength))
+ d.dblElectrodeLength / 2) *
SIN(RADIANS(s.dblMeasuredACPCAngle)) *
COS(RADIANS(s.dblMeasuredCtrAngle)) AS Z0,
CASE
WHEN intSide = 1 THEN dblMeasuredx +
((dblelectrodelength - 1) + 1 * (dblelectrodespacing +
dblelectrodelength) + (dblelectrodelength / 2))
* SIN(RADIANS(dblMeasuredCtrAngle))
ELSE dblMeasuredx -
((dblelectrodelength - 1) + 1 * (dblelectrodespacing +
dblelectrodelength) + (dblelectrodelength / 2))
* SIN(RADIANS(dblMeasuredCtrAngle))
END AS Expr1,
s.dblMeasuredY + (((d.dblElectrodeLength - 1) + 1 *
(d.dblElectrodeSpacing + d.dblElectrodeLength))
+ d.dblElectrodeLength / 2) *
COS(RADIANS(s.dblMeasuredACPCAngle)) *
COS(RADIANS(s.dblMeasuredCtrAngle)) AS Expr2,
s.dblMeasuredZ + (((d.dblElectrodeLength - 1) + 1 *
(d.dblElectrodeSpacing + d.dblElectrodeLength)) + d.dblElectrodeLength /
2)
* SIN(RADIANS(s.dblMeasuredACPCAngle)) *
COS(RADIANS(s.dblMeasuredCtrAngle)) AS Expr3,
CASE
WHEN intSide = 1 THEN dblMeasuredx + ((dblelectrodelength - 1)
+ 2 * (dblelectrodespacing + dblelectrodelength) +
(dblelectrodelength / 2)) * SIN(RADIANS(dblMeasuredCtrAngle))
ELSE dblMeasuredx - ((dblelectrodelength - 1)
+ 2 * (dblelectrodespacing + dblelectrodelength) +
(dblelectrodelength / 2)) * SIN(RADIANS(dblMeasuredCtrAngle))
END AS
Expr4,
s.dblMeasuredY + (((d.dblElectrodeLength - 1) + 2 *
(d.dblElectrodeSpacing + d.dblElectrodeLength)) + d.dblElectrodeLength
/ 2)
* COS(RADIANS(s.dblMeasuredACPCAngle)) *
COS(RADIANS(s.dblMeasuredCtrAngle)) AS Expr5,
s.dblMeasuredZ +
(((d.dblElectrodeLength - 1)
+ 2 * (d.dblElectrodeSpacing + d.dblElectrodeLength)) +
d.dblElectrodeLength / 2) * SIN(RADIANS(s.dblMeasuredACPCAngle)) *
COS(RADIANS(s.dblMeasuredCtrAngle))
AS Expr6,
CASE
WHEN intSide = 1 THEN dblMeasuredx +
((dblelectrodelength - 1) + 3 * (dblelectrodespacing +
dblelectrodelength) + (dblelectrodelength / 2))
* SIN(RADIANS(dblMeasuredCtrAngle))
ELSE dblMeasuredx -
((dblelectrodelength - 1) + 3 * (dblelectrodespacing +
dblelectrodelength) + (dblelectrodelength / 2))
* SIN(RADIANS(dblMeasuredCtrAngle))
END AS Expr7,
s.dblMeasuredY + (((d.dblElectrodeLength - 1) + 3 *
(d.dblElectrodeSpacing + d.dblElectrodeLength))
+ d.dblElectrodeLength / 2) *
COS(RADIANS(s.dblMeasuredACPCAngle)) *
COS(RADIANS(s.dblMeasuredCtrAngle)) AS Expr8,
s.dblMeasuredZ + (((d.dblElectrodeLength - 1) + 3 *
(d.dblElectrodeSpacing + d.dblElectrodeLength)) + d.dblElectrodeLength /
2)
* SIN(RADIANS(s.dblMeasuredACPCAngle)) *
COS(RADIANS(s.dblMeasuredCtrAngle)) AS Expr9,
s.dblAtlasScaleX,
s.dblAtlasScaleY,
s.dblAtlasScaleZ,
s.dblAtlasMovementX,
s.dblAtlasMovementY,
s.dblAtlasMovementZ,
s.dblAtlasRotationX,
s.dblAtlasRotationY,
s.dblAtlasRotationZ
FROM dbo.tblDBSSurgery AS s
INNER JOIN dbo.tblPatientDemographics AS p
ON p.idPatient =
s.idPatient
LEFT OUTER JOIN dbo.tblLookupLeads AS d
ON d.idLead = s.intLeadType
LEFT OUTER JOIN dbo.tblLookupDBSSites AS ds
ON ds.idDBSSite = s.intSite
WHERE (s.intProcedure = 0
OR s.intProcedure = 2)
AND (s.blnOutside = 0)
AND (NOT
(p.strMRN = '09999999'))
AND (NOT (p.strMRN = '08888888'))
ORDER BY surgerydate

SSIS: IF else in Derived Column expressions

I am trying to convert the way time is written in the flat file source so it would actually look like time.(If that makes any sense).
Right now I have it as 1215, 630, 10, 1 ,.. etc. So it can be 4-1 character long, so I need to convert it depending on the length of the column.
I am doing this in Derived Column with an expression, however I can't seem to make it work, not sure if my syntax is wrong or what, the expression looks like this, however I'm getting errors:
(LEN([TIME OCC]) == 4) ? (SUBSTRING([TIME OCC],1,2) + ":" + SUBSTRING([TIME OCC],3,2)) : (LEN([TIME OCC]) == 3) ? (SUBSTRING([TIME OCC],1,1) + ":" + SUBSTRING([TIME OCC],2,2)) : (LEN([TIME OCC]) == 2) ? (SUBSTRING([TIME OCC],1,2) + ":00") : (LEN([TIME OCC]) == 1) ? (SUBSTRING([TIME OCC],1,1) + ":00")
When I do it with only two values like below it seems to work perfectly:
LEN([TIME OCC]) == 3 ? SUBSTRING([TIME OCC],1,1) + ":" + SUBSTRING([TIME OCC],2,2) : SUBSTRING([TIME OCC],1,2) + ":" + SUBSTRING([TIME OCC],3,2)
I would appreciate any help, thank you!
All these answers are too complicated. Way too complicated. To me this is far less complicated:
LEFT(RIGHT(("0000" + [TIME OCC]),4),2) +
":" +
RIGHT(RIGHT(("0000" + [TIME OCC]),4),2)
You are ending your expression with an incomplete ternary expression. I am adding some line breaks and indentation to make it more readable:
(LEN([TIME OCC]) == 4) ?
(SUBSTRING([TIME OCC],1,2) + ":" + SUBSTRING([TIME OCC],3,2)) :
(LEN([TIME OCC]) == 3) ?
(SUBSTRING([TIME OCC],1,1) + ":" + SUBSTRING([TIME OCC],2,2)) :
(LEN([TIME OCC]) == 2) ?
(SUBSTRING([TIME OCC],1,2) + ":00") :
(LEN([TIME OCC]) == 1) ?
(SUBSTRING([TIME OCC],1,1) + ":00") <-- there needs to be a : with an ELSE condition here
I don't know if it's necessary, but I would also put a set of parenthesis around each of the nested ternary expressions.
As Tab says, missing the final alternative, try this:
LEN([TIME OCC]) == 4 ? (DT_WSTR,2)SUBSTRING([TIME OCC],1,2) + ":" + (DT_WSTR,2)SUBSTRING([TIME OCC],3,2)
: LEN([TIME OCC]) == 3 ?(DT_WSTR,1)SUBSTRING([TIME OCC],1,1) + ":" + (DT_WSTR,2)SUBSTRING([TIME OCC],2,2)
: LEN([TIME OCC]) == 2 ? (DT_WSTR,2)SUBSTRING([TIME OCC],1,2) + ":00"
: (DT_WSTR,1)SUBSTRING([TIME OCC],1,1) + ":00"
assuming the last alternative with a length 1.
I hope this help.
I don't think that Derrived column is the best way to solve your issue (it may be more complicated). you can add a Script component and mark your column as input, create a new output Column (Type DT_STR) and use the following code inside the OutpoutBuffer0_ProcessInputRow method:
Assuming that TIMEOCC and outColumn are your input and output column
If Not row.TIMEOCC_IsNull AndAslo _
Not String.IsnullorEmpty(Row.TIMEOCC.trim) Then
Select Case Row.TIMEOCC.Trim.Length
Case 1
Row.OutColumn = Row.TIMEOCC & ":00"
Case 2
Row.OutColumn = Row.TIMEOCC & ":00"
Case 3
Row.OutColumn = Row.TIMEOCC.Substring(0,1) & ":" & Row.TIMEOCC.Substring(1,2)
Case 4
Row.OutColumn = Row.TIMEOCC.Substring(0,2) & ":" & Row.TIMEOCC.Substring(2,2)
Case Else
Row.OutColumn = "00:00"
End Select
Else
Row.OutColumn = "00:00"
End If
If you want your solution by derrived column only
i think this is what you are looking for:
ISNULL([TIME OCC]) ? "00:00"
: (LEN([TIME OCC]) == 4 ? ((DT_WSTR,2)SUBSTRING([TIME OCC],1,2) + ":" + (DT_WSTR,2)SUBSTRING([TIME OCC],3,2))
: (LEN([TIME OCC]) == 3 ?((DT_WSTR,2)SUBSTRING([TIME OCC],1,1) + ":" + (DT_WSTR,2)SUBSTRING([TIME OCC],2,2))
: (LEN([TIME OCC]) == 2 ? ((DT_WSTR,2)SUBSTRING([TIME OCC],1,2) + ":00")
: (LEN([TIME OCC]) == 1 ? ((DT_WSTR,2)SUBSTRING([TIME OCC],1,1) + ":00")
: "00:00"))))
References
https://social.msdn.microsoft.com/Forums/sqlserver/en-US/48372e18-598b-4005-9ed8-7ccc8bba30ae/how-to-write-case-statement-in-ssis-expression-using-derived-column-transformation-available-in-bi?forum=sqlintegrationservices
https://www.sqlservercentral.com/Forums/Topic1456697-148-1.aspx
How to write SSIS switch/case expression?

Salesforce - Calculate case age in business days based on whether it is closed or in a specific state

I am trying to create a formula that can tell me the age of a case in business days if it's open, closed, or in a specific status. This is what I've got so far:
IF(OR(IsNull(ClosedDate), IsNull(Waiting_Confirmation_Date__c)),
(
5*FLOOR((TODAY()-DATE(1996,01,01))/7) +
MIN(5, MOD(TODAY()-DATE(1996,01,01), 7))
) - (
5*FLOOR((DATEVALUE(CreatedDate)-DATE(1996,01,01))/7) +
MIN(5, MOD(DATEVALUE(CreatedDate)-DATE(1996,01,01), 7))
),
IF(NOT(IsNull(Waiting_Confirmation_Date__c)),
(
5*FLOOR((Waiting_Confirmation_Date__c - DATE(1996,01,01))/7) +
MIN(5, MOD(Waiting_Confirmation_Date__c - DATE(1996,01,01), 7))
) - (
5*FLOOR((DATEVALUE(CreatedDate)-DATE(1996,01,01))/7) +
MIN(5, MOD(DATEVALUE(CreatedDate)-DATE(1996,01,01), 7))
),
(
5*FLOOR((DATEVALUE(ClosedDate)-DATE(1996,01,01))/7) +
MIN(5, MOD(DATEVALUE(ClosedDate)-DATE(1996,01,01), 7))
) - (
5*FLOOR((DATEVALUE(CreatedDate)-DATE(1996,01,01))/7) +
MIN(5, MOD(DATEVALUE(CreatedDate)-DATE(1996,01,01), 7))
))
)
I set the Waiting_Confirmation_Date__c with a workflow that specifies today's date when a user sets the case to a specific status. Basically what I am trying to do is this:
If the case is not closed, or not in Awaiting Confirmation (where the "Waiting_Confirmation_Date__c" would be set) to set the business age to the age from when it was created to today.
Else if the "Waiting_Confirmation_Date__c" is set, make the age time between when it was created to the confirmation date.
Lastly if it is closed set it to the age from when it was created to when it was closed.
I think just my if/else is wrong somehow, but I am going cross-eyed trying to figure it out.
Thanks!
The solution was this, thanks guys:
IF(AND(IsClosed),
(
5*FLOOR((DATEVALUE(ClosedDate)-DATE(1996,01,01))/7) +
MIN(5, MOD(DATEVALUE(ClosedDate)-DATE(1996,01,01), 7))
) - (
5*FLOOR((DATEVALUE(CreatedDate)-DATE(1996,01,01))/7) +
MIN(5, MOD(DATEVALUE(CreatedDate)-DATE(1996,01,01), 7))
),
IF(NOT(IsNull(Waiting_Confirmation_Date__c)),
(
5*FLOOR((Waiting_Confirmation_Date__c - DATE(1996,01,01))/7) +
MIN(5, MOD(Waiting_Confirmation_Date__c - DATE(1996,01,01), 7))
) - (
5*FLOOR((DATEVALUE(CreatedDate)-DATE(1996,01,01))/7) +
MIN(5, MOD(DATEVALUE(CreatedDate)-DATE(1996,01,01), 7))
),
(
5*FLOOR((TODAY()-DATE(1996,01,01))/7) +
MIN(5, MOD(TODAY()-DATE(1996,01,01), 7))
) - (
5*FLOOR((DATEVALUE(CreatedDate)-DATE(1996,01,01))/7) +
MIN(5, MOD(DATEVALUE(CreatedDate)-DATE(1996,01,01), 7))
)))

Resources