Given the following table I want to create another table with the total of video_duration and total duration of videos that have events
video can have numerous events, but the duration is of course the same per video file, hence I can have rows of the same video with different event but the video duration stay the same.
input
filename
event
video_duration
A
RUN
20
A
WALK
20
B
FIGHT
10
B
RUN
10
C
30
D
WALK
25
D
FALL
25
E
15
desired output
total videos duration
videos with events duration
100
55
what I've tried:
I created a calculated field
C_total_videos_duration = sum(max({video_duration, [filename]))
which gave me the desired output (100). But, for gods sake, I can't figure out how to get the "videos without events duration".
I have tried:
sumIf(max({video_duration}, [{filename}]), isNotNull({event})) ERROR: the calculation operated on LAC agg experssions is not valid
sum(maxIf({video_duration}, isNotNull({event})), [{filename}])
ERROR: Nesting of aggregate functions like NESTED_sum and NESTED_SUM(MAX(CASE WHEN "id" IS NOT NULL THEN video_duration ELSE NULL END), filename) is not allowed
ifelse(isNotNull({event}), sum(max({vide_duration}, [{filename}])), 0) ERROR: Mismatched aggregation. Custom aggregations can't contain both aggregate SUM and non-aggregated fields SUM(NESTEDMAX(video_duration, filename)) in any combination
The only thing that partially work is
sumOver(maxIf({video_duration},isNotNull(id)), [filename],POST_AGG_FILTER)
but here I get:
filename
total_videos_duration
videos_with_events_duration
A
20
20
B
10
10
C
30
D
25
25
E
15
Total
100
55
I don't this output because I have A LOT of videos, I just want to get the total durations
thank you!
I figure it out just now!
I did sum(max(ifelse(isNotNull(id),{video_duration}, 0), [filename])) and it worked.
Thank you stack!
Related
I need a little help with this one that seems very simple but I cant write the right DAX for it.
Context
I have a table of insurance claims and the days they were assigned and unassigned to adjusters, and the duration of this assignments in days.
ClaimID
Another header
A header
Another header
1
10/31/2022
11/30/2022
30
1
1/1/2023
1/4/2023
3
2
10/29/2022
12/28/2022
60
2
12/28/2022
1/6/2023
9
I need a measure (CycleTime) that calculates a monthly cumulative duration for each claim, and then take an average. All this based on the UnAssignedDate.
Desired output.
The measure will be plotted by month-year and this is how it needs to calculate CycleTime:
November 2022 : We only have one unassigned claim (1), so the cycletime equals to that single duration (30).
December 2022 : Again, we only have one unassigned claim (2), so the cycletime equals to that single duration (60).
January 2022 : For this month, both claims were unassigned, so we need to calculate the cumulative duration for each one and then take the average:
Claim 1 : 30 + 3 = 33
Claim 2 : 60+9 = 69
CycleTime = (33 + 69)/2 = 51
The measure should work for multiple claims and multiple unassignments per claim.
Any help would be greatly appreciated. Thank you for reading!
I should be able to make a report concerning a relationship between sick leaves (days) and man-years. Data is on monthly level, consists of four years and looks like this (there is also own columns for year and business unit):
Month Sick leaves (days) Man-years
January 35 1,5
February 0 1,63
March 87 1,63
April 60 2,4
May 44 2,6
June 0 1,8
July 0 1,4
August 51 1,7
September 22 1,6
October 64 1,9
November 70 2,2
December 55 2
It has to be possible for the user to filter year, month, as well as business unit and get information about sick leave days during the filtered time period (and in selected business unit) compared to the total sum of man-years in the same period (and unit). Calculated from the test data above, the desired result should be 488/22.36 = 21.82
However, I have not managed to do what I want. The main problem is, that calculation takes into account only those months with nonzero sick leave days and ignores man-years of those months with zero days of sick leaves (in example data: February, June, July). I have tried several alternative functions (all, allselected, filter…), but results remain poor. So all information about a better solution will be highly appreciated.
It sounds like this has to do with the way DAX handles blanks (https://www.sqlbi.com/articles/blank-handling-in-dax/). Your context is probably filtering out the rows with blank values for "Sick-days". How to resolve this depends on how your data are structured, but you could try using variables to change your filter context or use "IF ( ISBLANK ( ... ) )" to make sure you're counting the blank rows.
I have a dataset OvertimeHours with EMPLID, checkdate and NumberOfHours (and other fields). I need a running total NumberOfHours for each employee by checkdate. I tried using the Quick Measure option but that only allows for a single column and I have two. I do not want the measure to recalculate when filters are applied. Ultimately what I am trying to do is identify the records for the first 6 hours of overtime worked on each check so that they can get a category of OCB and all overtime over the first 6 hours is OTP and it does not have to be exact (as demonstrated in the output below). I have only been working with Power BI for about a month and this is a pretty complex (for me) formula to figure out...
EMPLID CheckDate WkDate NumberOfHours RunningTotal Category
124 1/1/19 12/20/18 5 5 OCB
124 1/1/19 12/21/18 9 14 OTP
125 1/1/19 12/20/18 3 3 OCB
125 1/1/19 12/20/18 2 5 OCB
125 1/1/19 12/22/18 2 7 OTP
124 1/15/19 1/8/19 3 3 OCB
*Edited to add the WkDate.
Edit:
I have tweaked my query so that I have the running total and a sequential counter now:
Using the first 12 records, I am looking to get the following results:
I can either do it in a query if that is the easiest way or if there is a way to use DAX in PowerBI with this dataset now that I have the sequential piece, I can do that too.
I got it in the query:
select r.CheckDate,
r.EMPLID,
case
when PayrollRunningOTHours <= 6
then PayrollRunningOTHours
else 6
end as OCBHours,
case
when PayRollRunningOTHours > 6
then PayRollRunningOTHours - 6
end as OTPHours
from #rollingtotal r
inner
join lastone l
on r.CheckDate = l.CheckDate
and r.EMPLID = l.EMPLID
and r.OTCounter = l.lastRec
order by r.emplid,
r.CheckDate,
r.OTCounter
I have dataset which shows how people spent their 30 minutes in 10-minute interval.
Person cumulative_time Activity
A 10 Game
A 30 Eat
B 10 Sleep
B 20 Game
B 30 Sleep
which means person A did gaming during the first 10 minutes,
and eating during the next 20 minutes,
and person B was sleeping for the first 10 min,
gaming for the next 10 min, and sleeping for the last 10 mins.
I want to restructure the dataset. Each row will be each unique person.
Then, each column will be each time interval like this.
Person time10 time20 time30
A Game Eat Eat
B Sleep Game Sleep
I know I can use "collapse" to make person unique but I don't know how this can be used for my purpose. The "reshape" command does something similar but again I cannot figure out how to use it to do what I want to do.
Reshape is the way to solve this problem. Something like this may accomplish what you need.
clear
input str1 Person int cumulative_time str8 Activity
A 10 Game
A 30 Eat
B 10 Sleep
B 20 Game
B 30 Sleep
end
rename Activity time
reshape wide time, i(Person) j(cumulative_time)
replace time20 = time10 if missing(time20)
replace time30 = time20 if missing(time30)
list, clean
If your problem had many cumulative_time values, not just three, I would solve the problem of missing values in a different way.
In addition to the William Lisowski answer, here is an approach using tsset and tsfill commands:
clear
input str1 Person int cumulative_time str8 Activity
A 10 Game
A 30 Eat
B 10 Sleep
B 20 Game
B 30 Sleep
end
rename Activity time
egen id = group(Person)
tsset id cumulative_time, delta(10)
tsfill, full
bysort id : replace Person = Person[_n-1] if Person==""
bysort id : replace time= time[_n+1] if time==""
drop id
reshape wide time, i(Person) j(cumulative_time)
list, clean
Which outputs:
Person time10 time20 time30
1. A Game Eat Eat
2. B Sleep Game Sleep
I have intra-day price data for stock trades and need to write a code to determine the instances in which the following condition is met: Price should go up at least for 10 consecutive trades.
Here is a sample of my data (time is number of minutes in the day, if it's 1 am my time will be 60, if it's 2 am, my time will be 120 etc.):
Obs Time Symbol Price
1 288 AA 36.2800
2 304 AA 36.2800
3 305 AA 36.3400
4 307 AA 36.2800
5 311 AA 36.1500
6 337 AA 36.2000
How can I write this code? Probably a loop is necessary but I can not figure it out. Thank you.
Assuming no missing values, something like:
data want ;
set have ;
lagPrice=lag(Price) ;
if Price>lagPrice and not missing(lagPrice) then Increasing ++ 1 ;
else Increasing=0 ;
if Increasing > 10 then Trend=1 ;
run ;
That will flag the 10th record of an increasing trend, and all those after. Is that what you want? Or are you looking for a ways to flag all records involved in the trend? Or something else??