Calculating Durations in Power Bi - time

I've received a CSV file from a client to import into Power Bi for analytics. It's a list of dates, agents, and then columns of total & average call/talk/hold times, all in durations. I've imported it with Power Query and converted to Duration but can't get PBi to add/average out over the weeks/months per agent. What am I missing?
DATE
AGENT
HANDLE TIME
1/3/23
Bob
0:03:55
1/3/23
Sam
0:19:03
1/4/23
Bob
0:05:01
.....

PowerQuery has the Duration data type, but DAX hasn't. Instead, you have (fractions of) days that you can use for aggregations.
Multiply the result with 24 * 60 *60 to get seconds and then convert it back to a time-like format.

Related

DAX ignore row context in measure. Calculate for a defined set of dates and show the value for all dates in visual

I am trying to create a measure which calculates the average daily revenue per customer, but only using days in a 6 months period prior to a specific date (where some type of conversion happens).
This specific date can be different for each customer.
The intention is to use the measure as a baseline for indexing daily average in the days/months/years after said conversion date.
If I put my current version of the measure in a card it works just fine (circled in green). But I will eventually have to visualize this over time as well. Thus I need the value to stay the same regardless of the row/date context in a table or timeline (circled in orange).
I suspect I need to use one of the ALL/ALLSELECTED/ALLEXCEPT filter modifiers but I can't really get anything to work.
The measure looks like this for now:
Average daily rev before conversion = CALCULATE (
AVERAGEX(
VALUES('Date'[Date]),
[HI & Acc Rev]
),
FILTER('poc vFact_SalesLine','poc vFact_SalesLine'[OrderDate_ID] IN DATESINPERIOD('Date'[Date],FIRSTNONBLANK('poc vDim_Customer'[DSE first conversion date],1),-6,MONTH)))
I've tried adding REMOVEFILTERS('Date'[Date]) just before the filtering of order dates, but that doesn't work. Gives me the exact same values as shown below.
All help is very welcome? Is my approach all wrong?

Howe to count an event by minute in Big Query

Many years ago I knew SQL quite well but apparently it's been so long I lost my skills and knolwedge.
I have a number of tables that each track a given event with additional metadata. One piece of Metadata is a timestamp in UTC format(2021-08-11 17:27:27.916007 UTC).
Now I need to count how many times the event occurred per minute.
Col 1, Col2
EventName, Timestamp in UTC
I am trying to recall my past knowledge and also how to apply that to BQ. Any help is appreciated.
If I'm understanding well, you could transform your Timestamp into minutes and then group by it.
SELECT count(*) AS number_events,
FLOOR(UNIX_SECONDS(your_timestamp)/60) AS minute
FROM your_table
GROUP BY FLOOR(UNIX_SECONDS(your_timestamp)/60)
So it transforms your timestamps to unix_seconds, then divide by 60 to get minutes and floor() to skip decimals after the division.
If you have multiple type of events in the same table, just add the name of the event to the select and to the group by
The first step would be to group by event column.
Then the Timestamp events can be counted.
Select Col2_EventName, count(Timestamp )
group by 1
Depending on your data, some more transformation have to be done. E.g. ignore the seconds in the timestamp and hold only the full minutes, as done in the answer from Javier Montón.

Using Power BI Desktop, how can I present Time Duration in a matrix that converts it to decimal?

I have Begin Date and End Date in my data file (Excel), then in PowerQuery I add a column where I simply subtract the Begin Date from End Date to create a new column "Import Time", change the result's data type to Duration and in PowerQuery, the column correctly shows the difference expressed in Day:Hour:Minute:Seconds. However, in a report, I need to show the average not of an entire column, but of groups of values in a matrix based on the entire table. In other words, I'm looking for a table that shows columns for Category Name, Avg. Duration and Max Duration, with rows showing the results for each unique Category.
The problem is that when I drag a table field in to the Visualizations Value area and change to Average, it expresses the result as a decimal, and I can't figure out how to show the date / time equivalent. I thought I might have to multiply by 3,600 since the result might be shown in milliseconds, but that didn't work. Any thoughts?
So I theorized that perhaps the issue wasn't one with Power BI, but perhaps with how Microsoft expresses date / time computations. I edited my Excel file that contains the source data and computed the "Import Time" by subtracting Begin Time from End Time. By default, it displays in date / time format of "hh:mm:ss.0000". I then asked Google and came across a simple explanation: to express a date / time in numeric format, you need to multiply the date / time by 24 (hours), 60 (minutes) and 60 (seconds) if that's how you wanted to express the result. I then created a Pivot Table summarizing the average of the Import Times and saw the same thing I did in my Power BI report, which is when it clicked it for me.
To solve the issue, I edited my Power Query step to compute the import time and included " * 24 * 60 * 60" in the formula. After updating the report, the results matched what I saw in Excel and I'm good. Hopefully this helps others to deal with this vexing issue.
You wrap your averaging measure in a FORMAT function to convert it to precisely the text format you want to show in your matrix. For example, suppose your raw output is
Group AvgDuration
------------------
A 0.0633
B 0.2733
C 0.0600
These numbers are in the unit of days, so you can convert to hours, minutes, or seconds like this e.g.:
FORMAT ( [AvgDuration] * 24, "0.00h" )
FORMAT ( [AvgDuration] * 24 * 60, "#,0m" )
FORMAT ( [AvgDuration] * 24,* 60 * 60, "#,0s" )
FORMAT ( [AvgDuration], "hh:mm:ss" )
(where [AvgDuration] is your measure you use to calculate the average)
Those last two should look like this:
Group AvgDuration
------------------
A 5,472s
B 23,616s
C 5,184s
and
Group AvgDuration
------------------
A 01:31:12
B 06:33:36
C 01:26:24

I need to calculate difference between two datetime fields, THEN show a percentage based on the first calculation

I need to calculate the difference between the time a patient arrives for their appointment, and the time they check out, also displaying the default length of their appointment, THEN show the percentage of their default appointment time that was actually used.
I've calculated the first part:
,TRUNC(24*MOD(enc.CHECKOUT_TIME - enc.CHECKIN_TIME,1))|| ':' ||TRUNC(MOD(MOD(enc.CHECKOUT_TIME - enc.CHECKIN_TIME,1)*24,1)*60) AS "VISIT LENGTH"
The default appointment duration is in this field: enc.APPT_LENGTH. This is the part I'm not sure how to do - calculate the percentage of the APPT_LENGTH that was actually used (VISIT_LENGTH) as calculated by the above.
Thanks in advance for any assistance.
Your question is not very clear but, to get the actual duration of an appointment in hours (decimal system), you can use:
(enc.CHECKOUT_TIME - enc.CHECKIN_TIME) * 24 AS ACTUAL_DURATION
since using - with 2 dates in Oracle results in the difference in days between the 2 dates.
Now, if APPT_LENGTH contains the expected duration of the appointment in hours too, you can then use
(((enc.CHECKOUT_TIME - enc.CHECKIN_TIME) * 24) / enc.APPT_LENGTH) * 100 AS DURATION_RATIO
to calculate the ratio (in %) between the actual duration and the expected duration

Is there a data type for time format hh:mm:ss in Hive

I am processing the files that contains the call details of different users. In the data file, there is a field call_duration which contains the value in the format hh:mm:ss. eg: 00:49:39, 00:20:00 etc
I would like to calculate the the total call duration of each user per month.
I do not see a data type in hive which can stock the time format in hh:mm:ss. ( Currently I have this data as string in my staging table).
I am thinking of writing a UDF which converts the time into seconds, so that i can do a sum(call_duration) grouping by user.
Did any one face a similar situation? Should I go with writing a UDF for is there a better approach?
Thanks a lot in advance
Storing duration as an Integer number of seconds seems like the best option for efficiency and for being able to do calcuations. I don't think you need a custom UDF to convert from your String to an Int. It can be done by combining existing UDFS:
Select 3600 * hours + 60 * minutes + seconds as duration_seconds
FROM (
Select
cast(substr(duration,1,2) as Int) as hours,
cast(substr(duration,4,2) as Int) as minutes,
cast(substr(duration,7,2) as Int) as seconds
From(
Select "01:02:03" as duration) a
) b;
Hive provides built-in date functions to extract hour, minutes and seconds.
https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF#LanguageManualUDF-DateFunctions
But if these functions doesn't help you directly and you use many combination of builtin function then i would suggest you to write your own UDF (in case this is very frequent utility and you run over large number of rows). You will see query performance difference.
Hope this helps

Resources