How to calculate total time in hour using DAX? - time

I have to table, start and end with time as the data, I want to calculate the total hour from start to end.
I tried this but it does not return hour, it returns date and the date also same in the column:
Column = SUM('Data'[Finish Date])-SUM('Data'[Start Date])*24-12
Anyone can give me idea please. Thank you so much

Read the docs: https://learn.microsoft.com/en-us/dax/datediff-function-dax
Column = DATEDIFF('Data'[Start Date], 'Data'[Finish Date], HOUR)

Related

Filter records by interval date in Laravel

I'm creating a filter to get expenses on a Credit Card, to generate a invoice.
But I only made it work filtering by month and year. I need get the records on days interval, and than, month and year.
For example:
Table: credit_cards
Column: close_invoice (day 25)
How do I get all expenses between day 25 of previous month and the next day 25?
This is my query filtering by month:
$thisMonth = $now->format('m');
$thisYear = $now->format('Y');
$cc_expenses = Expense::where([
['bank_id', $bank->id],
['payment_method', 1],
['parcels', NULL]
])
->whereYear('date', $thisYear)
->whereMonth('date', $thisMonth)
->get();
But this way I don't get the records between day 25 and the last day of the previous month.
Thanks in advance!
You can use
$previousMonthLastDay = now()->subMonth()->setDay(25)->setHour(0)->setMinute(0)->setSecond(0);
->whereBetween('date', [$previousMonthLastDay, now()])
I think nobody need to set future time from now, but if you need, you can do like $previousMonthLastDay without subMonth()
if your now date is 2022-04-13 12:22:10 , $previousMonthLastDay will be like 2022-04-25 00:00:00

Laravel Eloquent - Get a record every hour

I have a table that stores statistics every 3 minutes with a cron job.
I want to display a chart with this data but I want the chart to have an interval of 1 hour otherwise it looks ugly as hell and is too much resource demanding.
The table has the created_at and updated_at columns.
How can I do this with eloquent?
EDIT: I want to query the records from the last 24 hours but that gives me around 480 records which is too much for a chart. I'd like to have only 24 records instead (one for every hour).
Thanks for your help!
Thanks Tim!
For anyone reading through this later, here is the solution: https://laracasts.com/discuss/channels/laravel/count-rows-grouped-by-hours-of-the-day
Model::where('created_at', '>=', Carbon::now()->subDay())->get()->groupBy(function($date) {
return Carbon::parse($date->created_at)->format('h');
});
This will allow you to get data 1 hours ago base on current time.
//Get all data for the day
$all_data = Model::where('created_at','>=',Carbon::today()->get());
//Recursive to groupBy hours
$i=1;
while ($all_data->last() != null)
{
$hourly_data = Model::where('created_at','>=',Carbon::today()->addHours($i))->get();
$all_data= $all_data->merge($hourly_data);
$i++
}
return $all_data;

Ruby on Rails 3 convert time to DateTime in specific timezone

I have column which stores only time. I want to convert to it into datetime and then to a specific timezone. When I do it on console using only the attribute, it shows correct conversion value. But when I use it in query the query shows the current time in UTC. Suppose I have time "05:15" stored in Central time. Now when I want to fetch records for a interval of 30 minutes plus and minus of this time, I do following,
day = Time.now
# departure_time_from _db below is the name of column in table which store time
departure = departure_time_from _db.change(:day => date.day, :month => date.month, :year => date.year)
departure = departure.in_time_zone("Central Time (US & Canada)")
When I execute above code on console and see the value, it shows correct converted value. But when I use this variable in below query, current time gets used instead of the departure time.
Model.where("column_name > ? and "column_name < ?, departure - 30.minutes, departure + 30.minutes)
The time used in above query should be the time I want i.e Central time zone, but in query output, it shows UTC being used.
Please let me know what i am missing.
It shouldn't matter what time zone the query uses in the where clause as long as it's the same time representation. The time you're seeing in your query output from Rails is most likely the UTC equivalent of the ruby DateTime object being passed to the where statement. So if departure is 12:00 noon (UTC -6) in your Ruby code then the time shown in the SQL query will be 18:00, which corresponds to noon CST.

How can I group on the created_at field and return only the Date and hour

I run the following in Rails:
coe = trackers.where(:trackable_type => "Mailing").count(:group => 'DATE(created_at)')
which returns a hash with the items nicely grouped.
However I want to group not by date only by date but by the hour, but not the minutes or seconds.
How can I run this query so all the heavy lifting is done by Postgresql and not convert after I get the data set back?
My mistake I want to group by Date and Hour. Sorry for the confusion.
It sounds like you want the date_trunc() function:
http://www.postgresql.org/docs/current/interactive/functions-datetime.html#FUNCTIONS-DATETIME-TABLE
I want to group not by date but by the hour, but not the minutes or
seconds.
Bold emphasis mine.
So date_trunc()is probably not what you want, but EXTRACT or date_part():
coe = trackers.where(:trackable_type => "Mailing").count(:group => 'EXTRACT (hour FROM created_at)')

Oracle DateAdd() of 2 different columns

I'm trying to figure out a DateAdd() equivalent in Oracle that is actually the difference in seconds between 2 columns in the same table:
SELECT
DISTINCT p.packet_id,
p.launch_dt,
r.route_duration,
s.completion_date,
DATEADD(SS, r.route_duration, p.launch_dt) AS tempDate
FROM
tdc_arc_apprpkt_def p
JOIN tdc_arc_inpr_route_def r
ON p.packet_id = r.packet_id
JOIN tdc_arc_inpr_route_step_detai s
ON p.packet_id = s.packet_id
AND s.completion_date > DATEADD(SS, r.route_duration, p.launch_dt)
Any help would be greatly appreciated!
In addition to being able to do date arithmetic using fractions of days as Tony demonstrates, assuming you are using 9i or later, you can also use interval functions (or, even better, define the ROUTE_DURATION column as an interval) and add intervals to dates. In your case, you can do
p.launch_dt + numtodsinterval( r.route_duration, 'SECOND' )
to add route_duration seconds to launch_dt.
If you were to define the route_duration column as an INTERVAL DAY TO SECOND rather than a NUMBER, you could simply add it to a date
p.launch_dt + r.route_duration
If I understand you correctly, you want to add r.route_duration seconds to p.launch_dt? In that case the expression is:
p.launch_dt + (r.route_duration/24/60/60)
Oracle DATE arithmetic works in days, so the divisions by 24, 60 and 60 convert the route_duration value from seconds to days.

Resources