Calculate the days between two fields in sugarCRM - sugarcrm

How to calculate number of days between two date fields of same module without counting the weekends (Saturday and Sunday) using the "Formula Builder" in sugarCRM studio.

Preliminary remarks
As far as I'm aware Sugar Logic doesn't have a function to count the days of a date span.
However we can calculate it using existing Sugar functions like this:
add(1,subtract(daysUntil(addDays($date_end,365000)),daysUntil(addDays($date_start,365000))))
This will return the span from $date_start and $date_end, counting all days, including both start and end date,
so the span 2017-01-01 to 2017-01-01 will return 1 rather than 0.
Notes:
This does not handle the special cases of any of those fields being empty.
If you want to display the result of this formula directly, wrap it in floor() to display as an integer without .000000
The solution
Since Sugar Logic also does not seem to provide any modulo function and formula scoped variables either,
the resulting formula for what you want (count only Mo-Fr) is as "compact" as:
floor(add(0.5,
add(
multiply(floor(divide(add(1,subtract(daysUntil(addDays($date_end,365000)),daysUntil(addDays($date_start,365000)))),7)),5)
,
add(
min(
max(0,subtract(6,ifElse(equal(dayofweek($date_start),0),7,dayofweek($date_start))))
,
subtract(
add(1,subtract(daysUntil(addDays($date_end,365000)),daysUntil(addDays($date_start,365000))))
,
multiply(floor(divide(add(1,subtract(daysUntil(addDays($date_end,365000)),daysUntil(addDays($date_start,365000)))),7)),7)
)
)
,
max(
0,
subtract(
subtract(
add(1,subtract(daysUntil(addDays($date_end,365000)),daysUntil(addDays($date_start,365000))))
,
multiply(floor(divide(add(1,subtract(daysUntil(addDays($date_end,365000)),daysUntil(addDays($date_start,365000)))),7)),7)
)
,
subtract(8,ifElse(equal(dayofweek($date_start),0),7,dayofweek($date_start)))
)
)
)
)
))
Notes:
The formulas above do not handle start dates that are after the end date, you could catch such cases using if() and isAfter()
Holidays and such are not considered at all, so this will not only count actual work days

Related

SSRS exact month difference

This may have been asked before but I've not been able to find it having searched! In Oracle SQL there's a function called MONTHS_BETWEEN which returns a fractional value if the two dates you are comparing are not both the first day of the month for example. I need to do something similar in SSRS report builder, I've tried using DateDiff("m",Date1,Date2) however this always returns an integer and I think from what I can tell it just compares the two months from the dates, so when I compare 30/09/20 and 01/04/21 I get 7 months when actually it is much closer to 6.
Is there a function or a fix that can be used in SSRS to get that more accurate value?
Thank you!
For example I would like to get the following result:
Difference between 30/09/20 and 01/04/21 = 6.1
Difference between 01/08/20 and 30/09/20 = 1.9
It doesn't have to super accurate as I will be rounding to the nearest integer but I'm looking for something that will recognise that in the second example nearly 2 months have been covered and in the first example it's only just over 6 months.
If you only need an approximation then you could just calculate the number of days difference and divide by 30.
using the following expression...
=DATEDIFF("d", Fields!startDate.Value, Fields!endDate.Value)/30
I put a few more examples into a table and got the following results.
The following code mimics oracle definition of months_between
Public Function MonthsBetween( d1 As Date, d2 As Date) As Decimal
Dim df As Decimal
df = DateDiff("m", d1, d2)
If Day(d1) <> Date.DaysInMonth(Year(d1), Month(d1)) Or Day(d2) <> Date.DaysInMonth(Year(d2), Month(d2)) Then
df = df + Cdec((Day(d2)-Day(d1))/31)
End If
Return df
End Function
Integer result when both dates are last day of month
Negative result when date1 > date2
Decimal part based on 31 days month
For your expression use something like
=Code.MonthsBetween(Fields!date1.Value , Fields!date2.Value)
UPDATE
The following expression works in the same manner
= Cdec(
DateDiff("m", Fields!date1.Value, Fields!date2.Value)
+
Iif (
Day(Fields!date1.Value) <> Date.DaysInMonth(Year(Fields!date1.Value), Month(Fields!date1.Value)) Or
Day(Fields!date2.Value) <> Date.DaysInMonth(Year(Fields!date2.Value), Month(Fields!date2.Value)) ,
Cdec( (Day(Fields!date2.Value) - Day(Fields!date1.Value))/31),
0.0
)
)

How to pass a date into a ColdFusion collection to be searched

I'm using a ColdFusion collection to search events and I need to pass a date into the collection as a "mmm" so it can be searched. Every time I try I get an error.
custom4="DateFormat(start_date, "mmm")"
Update:
I'm trying to search "month" of the current year
You should use the above code like
custom4=dateFormat(start_date, "mmm")
(Remove the outer double quotes)
Quotes aren't the problem and the suggestion of removing them will actually cause an error. The problem is DateFormat() can't be applied to an entire query column. It's only capable of operating on a single value.
It would help to have more context about what you're trying to achieve, to determine the best approach.
If you want to find items dated in July of a specific year (i.e. July 2019) - then storing the full date, and searching for a date range, is probably a better way to go
If you want to find items dated in July of ANY year, then it's simpler to extract a month number within your SQL query, and store it in the collection. Then you need only search for a number.
Date Range Search
To search for a specific month/year, like July 2019, populate the collection with a timestamp field from your SQL query. Add the suffix _dt to the custom field name so it's treated as a date field
cfindex( query="yourQuery"
, collection="yourCollection"
, action="Update"
, type="Custom"
, Start_Date_dt="yourTimeStampColumn"
, ...
);
In the search criteria, use the date range July 1 through August 1, 2019 (yes - August 1st). Dates must be formatted for Solr, which expects YYYY-MM-DDThh:mm:ssZ. NB: Dates should be in UTC (not local time).
cfsearch (name="searchResults"
, collection="yourCollection"
, criteria=' start_date_dt:[2019-07-01T04:00:00Z TO 2019-08-01T04:00:00Z}'
);
Explanation/Notes
[ and } - Brackets indicates a range (i.e. from dateX TO dateY)
[ - Square bracket means inclusive (i.e. include July 1st)
} - Curly bracket means exclusive (i.e. exclude August 1st)
Field names should be in lower case
Month Number Search
To search for a specific month in any year, like July, extract the month number within your SQL query. (The exact syntax will be DBMS specific. You didn't mention which one you're using, so see your database's documentation on date functions.) Add the suffix _i to the custom field name so it's handled as an integer
cfindex( query="yourQuery"
, collection="yourCollection"
, action="Update"
, type="Custom"
, monthNumber_i="theMonthNumberColumn"
, ...
);
Then simply search for the desired month number, i.e. 7 - July
cfsearch ( name="searchResults"
, collection="yourCollection"
, criteria=' monthnumber_i:7 '
);
Full Example
Sample Query
sampleData = queryNew("MyID,Start_Date,MonthNumber"
, "integer,timestamp,integer"
, [{MyID=10, Start_Date="2019-06-30 12:30:00", MonthNumber=6}
, {MyID=20, Start_Date="2019-07-01 00:00:00", MonthNumber=7}
, {MyID=30, Start_Date="2019-07-01 16:30:00", MonthNumber=7}
, {MyID=40, Start_Date="2019-07-31 23:50:00", MonthNumber=7}
, {MyID=50, Start_Date="2019-08-01 00:00:00", MonthNumber=8}
]);
Create Collection
cfcollection ( action="create", collection="MyCollection");
Update Collection
cfindex( query="sampleData"
, collection="MyCollection"
, action="Update"
, type="Custom"
, key="MyID"
, title="SampleData"
, MonthNumber_i="MonthNumber"
, Start_Date_dt="Start_Date"
, body="MyID"
);
Find July, by month number
cfsearch ( name="monthNumberResults"
, collection="MyCollection"
, criteria=' monthnumber_i:7 '
);
// results
writeDump( var=monthNumberResults, label="Month Number Search" );
Find July 2019, by date range
// Search range: July 1 to August 1, 2019
fromDate = "2019-07-01";
toDate = dateAdd("m", 1, fromDate);
// Format dates for Solr
// Note: DateTimeFormat uses "n" for minutes. Valid in CF2016 Update 3 or higher
fromDate = dateTimeFormat( dateConvert("local2UTC", fromDate), "yyyy-mm-dd'T'HH:nn:ss'Z'");
toDate = dateTimeFormat( dateConvert("local2UTC", toDate), "yyyy-mm-dd'T'HH:nn:ss'Z'");
cfsearch ( name="dateRangeResults"
, collection="MyCollection"
, criteria=' start_date_dt:[#fromDate# TO #toDate#} '
);
// results
writeDump( var=dateRangeResults, label="Date Range Search" );

Create a measure to return the maximum date about the order in a fact table. (SSAS Multidimensional)

I want to create a measure which return the maximum date about Orders but before the actual day
I will write an example :
My tables here
(In my table Calendar i have the year 2016,2017,2019, and in my Order table, i have an order for 2016 and 2019,
I want the last date order but before the actual day (18/05/2017), so i want the Date 01/01/2016).
I have 2 table, a dimension Calendar and a fact table Order.
I was thinking about the function filter, so i search how to use filter in
google, and all the solutions i found use 'With' and 'Select'.
(I can't use 'With' and 'Select' when i create a measure in SSAS multidimensional).
Hope i will see your advice.
Just like this similar case in adv cube?
[max order date] return the maximum date about [Internet Sales Amount]
with member [max order date] AS
tail(NONEMPTY([Date].[Date].[Date],[Measures].[Internet Sales Amount])).item(0).item(0).PROPERTIES( "name" )
select {[max order date] } on 0 from [Adventure Works]
if yes, then you can create a measure in your cube like this:
Create Member CurrentCube.[Measures].[max order date]
As tail(NONEMPTY([Date].[Date].[Date],[Measures].[Internet Sales
Amount])).item(0).item(0).PROPERTIES( "name" );
if only till current day, then(following is refer to adv cube, you need do some code changes per your cube):
Create Member CurrentCube.[max order date] AS
Tail
(
NonEmpty
(
{
Head([Date].[Date].[Date]).Item(0).Item(0)--the first day in your Date dim
:
StrToMember("[Date].[Date].&[" + Format(Now(),"yyyyMMdd") + "]")-- as of current day
}
,[Measures].[Internet Sales Amount]
)
).Item(0).Item(0).Properties("name")
IDE to Write, Analyze, Tuning, Debug MDX efficiently (www.mdx-helper.com)

Tableau - Filter based on parameter

I have a dataset of dates. It has just one column CreatedOnDate and its values are in datetime as shown below.
This dataset has 6 months of values as shown. I have a parameter called Report Type which has possible values Monthly, Weekly, Daily (Screenshot below)
I have created a calculated field (called Created On Date) which converts the date based on Report Type selected. The formula is shown below
CASE [Report Type]
WHEN "Monthly" THEN DATENAME('month', [CreatedOnDate])
WHEN "Weekly" THEN "Week " + STR(DATEPART('week',[CreatedOnDate]))
WHEN "Daily" THEN STR(MONTH([CreatedOnDate])) + "/" + STR(DAY([CreatedOnDate])) + "/" + STR(YEAR([CreatedOnDate]))
END
This works perfectly. The result of the calculated field is shown below.
I now need to incorporate the following logic
IFF Report Type = "Daily" Display only the last 30 days in the dataset
Other cases Show all values
How do I achieve this?
woodhead92, I'd suggest using so called Level of Detail expressions that were introduced in Tableau v8. First create a calculated field that will calculate the most recent (=MAX) date available:
{FIXED : MAX(CreatedOnDate) }
Let's call this MaxDate LOD. Then adding a new calculated field Show/Hide:
IF [Report Type] = "Daily" AND
([CreatedOnDate] >= DATEADD('day', -30, [MaxDate LOD]) THEN 'Show'
ELSEIF [Report Type] = "Weekly" OR [Report Type] = "Monthly" THEN 'Show'
ELSE 'Hide'
END
Add this filter and select 'Show' value only. I am assuming that you want to see all dates when Weekly/Monthly date granularity is selected - if that's not the case, simply add more ELSEIF conditions.
The formula above could be simplified, but I wanted to make it as verbose as possible so that it helps you understand how Level of Detail expressions work.
One thing to keep in mind - FIXED LOD calculation overwrites filters, so if you have a date-range filter available, you will have to make sure it's added to context. More details on filter context are available here in this a bit out-dated, but still excellent blog post.
Create a calculated field for your condition and then place it on the filter shelf to include only rows that evaluate to true.
[Report Date] <> "Daily" or
datediff('day', [CreatedOnDate], { max[CreatedOnDate] } < 30
The Curley braces are significant, an LOD calculation

Quartz .Net - Meaning of BigInt DateTime

we've used sql server as our persisted data store for Quartz.net. I'd like to write some queries looking # the Time values. Specifically - Qrtz_Fired_Triggers.Fired_Time, Qrtz_Triggers.Next_fire_time, Prev_fire_time.
For the life of me, I can't find anything that says what this data is - ticks, milliseconds, microseconds, nanoseconds. I've guessed at a couple of things, but they've all proven wrong.
The best answer would include the math to convert the big int into a datetime and perhaps even a link(s) to the pages/documentation that I should have found - explaining the meaning of the data in those fields.
If you have specific instructions on using Quartz .Net libraries to view this information, that would be appreciated, but, I really have 2 goals - to understand the meaning of the date/time data being stored and to keep this in T-SQL. If I get the one, I can figure out T-SQL or out.
On the SQL side, you can convert from Quartz.NET BIGINT times to a DateTime in UTC time with:
SELECT CAST(NEXT_FIRE_TIME/864000000000.0 - 693595.0 AS DATETIME) FROM QRTZ_TRIGGERS
Numbers Explanation
Values stored in the column are the number of ticks from .NET DateTime.MinValue in UTC time. There are 10000 ticks per millisecond.
The 864000000000.0 represents the number of ticks in a single day. You can verify this with
SELECT DATEDIFF(ms,'19000101','19000102')*10000.0
Now, if we take March 13, 2013 at midnight, .NET returns 634987296000000000 as the number of ticks.
var ticks = new DateTime(2013, 3, 13).Ticks;
To get a floating point number where whole numbers represent days and decimal numbers represent time, we take the ticks and divide by the number of ticks per day (giving us 734939.0 in our example)
SELECT 634987296000000000/(DATEDIFF(ms,'19000101','19000102')*10000.0)
If we get put the date in SQL and convert to a float, we get a different number: 41344.0
SELECT CAST(CAST('March 13, 2013 0:00' AS DATETIME) AS FLOAT)
So, we need to generate a conversion factor for the .NET-to-SQL days. SQL minimum date is January 1, 1900 0:00, so the correction factor can be calculated by taking the number of ticks for that time (599266080000000000) and dividing by the ticks per day, giving us 693595.0
SELECT 599266080000000000/(DATEDIFF(ms,'19000101','19000102')*10000.0)
So, to calculate the DateTime of a Quartz.NET date:
take the value in the column
divide by the number of ticks per day
subtract out the correction factor
convert to a DATETIME
SELECT CAST([Column]/864000000000.0 - 693595.0 AS DATETIME)
The value stored in database is the DateTime.Ticks value. From MSDN:
A single tick represents one hundred
nanoseconds or one ten-millionth of a
second. There are 10,000 ticks in a
millisecond.
The value of this property represents
the number of 100-nanosecond intervals
that have elapsed since 12:00:00
midnight, January 1, 0001, which
represents DateTime.MinValue. It does
not include the number of ticks that
are attributable to leap seconds.
So, unless I missed something and am making this too complicated, I couldn't get the dateadd functions in Ms Sql Server 2008 to handle such large values and I kept getting overflow errors. The approach I took in Ms Sql Server was this:
a) find a date closer to now than 0001.01.01 & its ticks value
b) use a function to give me a DateTime value.
Notes:
* for my application - seconds was good enough.
* I've not tested this extensively, but so far, it has acted pretty well for me.
The function:
CREATE FUNCTION [dbo].[net_ticks_to_date_time]
(
#net_ticks BIGINT
)
RETURNS DATETIME
AS
BEGIN
DECLARE
#dt_2010_11_01 AS DATETIME = '2010-11-01'
, #bi_ticks_for_2010_11_01 AS BIGINT = 634241664000000000
, #bi_ticks_in_a_second AS BIGINT = 10000000
RETURN
(
DATEADD(SECOND , ( ( #net_ticks - #bi_ticks_for_2010_11_01 ) / #bi_ticks_in_a_second ) , #dt_2010_11_01)
);
END
GO
Here is how I came up with the # of ticks to some recent date:
DECLARE
#dt2_dot_net_min AS DATETIME2 = '01/01/0001'
, #dt2_first_date AS DATETIME2
, #dt2_next_date AS DATETIME2
, #bi_seconds_since_0101001 BIGINT = 0
SET #dt2_first_date = #dt2_dot_net_min;
SET #dt2_next_date = DATEADD ( DAY, 1, #dt2_first_date )
WHILE ( #dt2_first_date < '11/01/2010' )
BEGIN
SELECT #bi_seconds_since_0101001 = DATEDIFF(SECOND, #dt2_first_date, #dt2_next_date ) + #bi_seconds_since_0101001
PRINT 'seconds 01/01/0001 to ' + CONVERT ( VARCHAR, #dt2_next_date, 101) + ' = ' + CONVERT ( VARCHAR, CAST ( #bi_seconds_since_0101001 AS MONEY ), 1)
SET #dt2_first_date = DATEADD ( DAY, 1, #dt2_first_date );
SET #dt2_next_date = DATEADD ( DAY, 1, #dt2_first_date )
END