Flatline: How to calculate days between two dates - date

I want to add a calculated field in a BigML datasheet with the result of days between two dates.
I'm trying to figure out how to calculate the number of days between two date fields with Flatline language but I don't know how to do it even reading the doc.
Any clue about how create this calculated field?
PS: Somebody with enough reputation could create and add tags "bigml" and "flatline"?

Currently, the only way to subtract dates is by first transforming them to an
epoch (number of milliseconds since 1970) and then computing the difference:
(- (epoch "12/03/1990") (epoch "01/01/1988"))
That will give you the number of milliseconds between the two dates, which then
can be transformed to other units. What that won't give you is of course the
difference in calendar days: we don't have yet in Flatline a way of subtracting
calendar dates. But it shouldn't be too difficult to add them if it's a feature
you need :)

Related

How is it possible to calculate value for the same day last week in tableau for every day?

I would like to calculate values for yesterday for every date in a table.
Firstly I have simply calculated the number of orders for each date using COUNTD function.
But afterwards I encountered some problems trying to calculate values for "yesterdays".
Please refer to the image
example
For example for 12th of April I would like to obtain the value for 11th of April.
On the internet there are a lot of examples using today() or max() or table functions but they do not give the required result because a would like to filter for example 12th April but still see the value for 11 April.
Could you please help, how is it possible to do this?

Why are my values multiplying when I apply Month/Year to my values?

When I apply Month/Year to Cases or Deaths from my data, the values explode. For Cases it goes from approximately 48 million to over 1 billion, and for Deaths it goes from about 700 thousand to over 22 million. However, when I try the same thing with Initial Claims or the Stringency Index, my values remain correct. I'm trying to find the month over month percentage change by the way. And I'm using the Date column. I only select 2020 and 2021 in the filter for Year.
What I'm asking about is Sheet 21.
Link to workbook: https://public.tableau.com/app/profile/nilajah.rivers/viz/CoronaVirusProject_16323687296770/Sheet21
Your problem is that the data points are daily cumulative deaths. If you change the date aggregation to anything other than days, Tableau will default to summing the numbers for all the days in the month. This will give the wrong result, obviously.
If you want to show the correct total deaths or cases regardless of the time aggregation (months, days, weeks etc.) then you could use the New Case or New Death numbers plus a running sum table calculation. This will always give the correct total for the time period.
Table calculations will also allow automatic calculation of the period to period % change from the same data fields.
This is a common problem when working with datasets that offer pre-calculated aggregations. Tableau doesn't need that as it can dynamically calculate the aggregation of a field over any given time period but it is easy to forget which field has pre-aggregated data and which has raw data. Pre-aggregated fields assume a particular time period and can't be used for different time periods without disentangling that assumption (which is unnecessary if you also have the raw data (in this case daily new deaths/cases).

Power BI - Calculating a price/cost until a variable end date

Let's say I have a table of projects/programs/subscriptions/etc, it doesn't really matter as long as there is a price or cost per some amount of time. My table includes at least the following columns:
[ProjectName]
[StartDate]
[EndDate]
[CostPerDay]
I'm trying to allow the user to choose another date (slicer I assume?) and display the cost of each and all projects up to that date. Is this possible?
Edit: After the first responses I realize the original question was very poorly worded. Sorry about that. I've reworded it and I'll explain more here:
I am not trying to filter the programs by end date. I'm trying to sum a cost up until the end date OR slicer date, whichever is earlier.
Here's a short example table:
So we can also think of it as a Gantt chart like this:
Now imagine sliding a vertical line along that chart. I want to see the total cost up to that date.
I'm sure it will have to do with counting days between start date and the slicer date, then multiplying by cost. But how do we not include days after the end date of each project? Or it may be easier to do a range slicer with a min and max date, but again not counting days before or after each project.
To word it differently: can I input a date range, count the days that each project has in common with that range, and (the simple part) multiply days by cost?
It looks like what you need is a table visualization with a slicer for End date. Once the table and slicer are created, you can click on the small down facing arrow in the slicer and choose "Before". If you want both start date and end date in the equation, then you would have to add one more slicer for "Start date". If this is not what you are looking for kindly provide additional details so that we can help you out.

Formula to add days to a Gregorian date

I was looking at Tomohiko Sakamoto's weekday calculator. It's a formula to calculate the day-of-week directly given year, month, day. That made me wonder what other neat date calculation shortcuts exist.
In particular, given an input date as (in_year, in_month, in_day) and a number of days N to add, what's a formula for returning the output (out_year, out_month, out_day)? Is there a well-known trick like the algorithm above?
One way would be to convert the input to a Julian day (a count of days since 4713 BC), add N to it, and then convert back. There are formulas for conversion in both directions. But the combined formula would be quite unwieldy. Is there a simplified version?
Perhaps there is even a formula to move forward or back by a certain number of weekdays.
This question isn't "how do I do date arithmetic in my favourite programming language?" I know how to call the date library to perform these operations. It's more curiosity and the hope of starting a collection of cool date algorithms.
Some of the answers in Algorithm to add or subtract days from a date? will be relevant to this. In particular http://howardhinnant.github.io/date_algorithms.html gives code to convert y,m,d to a count of days and back again. Those two routines run back to back would be pretty fast.

Calculating the difference between two dates using age and extract gives differing results in Postgresql

I'm using Postgresql (on Amazon Redshift), and I need to calculate the difference between two dates and then use that value in a formula to compute a ratio, so the date difference needs to be translated to a numeric value, preferably a float or double precision.
I have two dates: 1/1/2017 and 1/1/2014. I need to find the difference between these two dates in number of days.
When I use the age function I get 1080 days:
select age('2017-01-01','2014-01-01')
However, since age returns an interval and I need to work with a numeric result, I am using EXTRACT to convert the final value. I chose epoch since I wasn't able to find any other value for EXTRACT that would yield the number of time units between the two dates. This formula yields 1095.75 days (the divisor is the number of seconds in a day):
select extract(epoch from age('2017-01-01','2014-01-01'))/86400
Why am I getting a difference of 19.75 days when using age vs using extract?
Did you try
select '2017-01-01'::date - '2014-01-01'::date;
The difference between two dates is number of days in integer
1080 is the figure you would get if every month was 30 days long (36 months by 30 days equals 1080), as it would be if you used justify_days (either explicitly or if the DBMS called it implicitly). You don't say how you're getting this 1080 figure since I believe the duration would normally just print out something like 3 years, but that seems the most likely case
1095.75 seems the more correct figure, being 365.25 days multiplied by three years.
Out of those two, I would go with the latter method.
Although, as pointed out at http://www.postgresql.org/docs/8.1/static/functions-datetime.html#FUNCTIONS-DATETIME-EXTRACT, calculating the difference between two date types should yield the number of days:
select dtend - dtstart from somewhere
Redshift release notes say they recently released a months between function which looks similar to oracles months between function if that's what you're looking for. http://docs.aws.amazon.com/redshift/latest/dg/r_MONTHS_BETWEEN_function.html