Calculate the difference in minutes of 2 rows - powerbi

I need to obtain the time difference in minutes between a row and the one that follows. I would like to use the field DATE to calculate and maybe create an additional field with the new measurement.
This is the structure of my fields in my database.

You should be able to use the TRANSACTION number to look up the prior row.
Something like this as a calculated column:
Diff = 24 * 60 *
(Table1[DATE] -
LOOKUPVALUE(
Table1[DATE],
Table1[TRANSACTION],
Table1[TRANSACTION] - 1
)
)
Note: The difference has units of days, so the 24 (hours/day) * 60 (minutes/hour) is the conversion you need.

Related

What's DAX To extract employees ID's whose Shifts fall with in certain time slot

I have table contains data col's of EmpID, Shift Start Time, Shift End Time & Resonable Notice Time in case of closing business before business hours. I'm trying to filter Employees whose ShiftStartTime is less than 2 hours of Resonable Notice Time of Closing so they get compensation as per the policy. My syntax does not fit to filter those employees. Please advise.
I also tried calculating time difference by using DATEDIFF as
Step 1
Time Diff = DATEDIFF('Table1'[ShiftStartTime],'Table1'[Resonable Notice Time],MINUTE)
Step 2
Trying to filter only those EmpID's who has less than 120 mins and >= 0 as they many not have had chance to clock in. but not getting right syntax
EmpID's for Compensation = LOOKUPVALUE('Table1'[iEmpID],('Table1'[Time Diff]<120 &&'Table1'[Time Diff]>=0),BLANK()) Need help solving this.
I assume that 'Time_Diff' is a calculated column?
Time_Diff =
DATEDIFF ( 'Table1'[ShiftStartTime], 'Table1'[Resonable Notice Time], MINUTE )
Then I think you should use a table function (such as FILTER or summarize) instead of lookup value which returns a scalar value.
Please test this:
EmpID's for Compensation =
FILTER (
ALL ( 'Table1'[iEmpID], 'Table1'[Time Diff] ),
( 'Table1'[Time Diff] < 120
&& 'Table1'[Time Diff] >= 0 )
)

Create New Column containing value based on conditions on date and hour

I have a table in my power BI with the following fields :
Preview of the data:
The column "platform" has 3 possible values : application, shop, website
"day" is of type Date
"hour" is of type "Date/Time" (same information as "day" + has the hour)
I added a measure to calculate the conversion_rate (orders/visits):
conversion_rate = DIVIDE(SUM(Table[orders]), SUM(Table[visits]))
Then I calculated for every day the conversion_rate from 7 days ago (to be able to compare them):
conversion_rate_7_j = CALCULATE(Table[conversion_rate],
DATEADD(Table[day],-7,DAY)
)
Now my data looks like this:
What I want to do is calculate the conversion rate from 7 days ago but for the same hour.
However I couldn't find a function that substracts field of type Date/Time while taking in consideration the hour.
A solution I thought of is to calculate orders and visits -7 days same hour separately and then divide them to have the conversion rate -7 days same hour:
orders_7_j_hourly =
VAR h = Table[hour] - 7
VAR p = Table[platform]
Return CALCULATE(
MAX(Table[orders]),
Table,
Table[hour] = h,
Table[platform] = p
)
Since my data is grouped by hour (Date/Time) and platform,
And since sometimes for a certain hour I have values for the platform = "application" but not "shop",
My function did not work especially that I am using MAX, this associated the number of orders to the wrong platform.
Can you please help ?
Sample data : https://ufile.io/y1blqgqn
Datetime values are stored in units of days. Thus you can simply shift hour by 7 in your measure.
conversion_rate prev_week =
VAR CurrHour = SELECTEDVALUE ( Table1[hour] )
RETURN
CALCULATE (
[conversion_rate],
ALL ( Table1[day] ),
Table1[hour] = CurrHour - 7
)
Sample results:
Did you try to use HOUR function ??
conversion_rate_7_hour = CALCULATE( [conversion_rate],
FILTER( ALL(Table),
SELECTEDVALUE(Table[day]) - 7 = Table[day]
&& HOUR(SELECTEDVALUE(Table[hour]) - 7) = HOUR( Table[hour])
))
When we put Table[hour] to visualization it should work.
Ps. best pratice => if your refer to measures in your calculations,do not include the table prefix
You can create an additional column called hour in your dataset
Once you have that, you bring the hours in the viz, the following measure can give you what you want
convRate-7 = CALCULATE([convRate],DATEADD('Table'[day],-7,DAY))

Difference between two timestamps in hours

I need to create a DAX measure to calculate the difference between two date time columns in hours
Example:
Column 1 (input): 2021-02-09 19:30:02
Column 2 (input): 2021-02-10 22:00:02
Difference in hours (output): 26.30
The internal representation of DATETIME is like in Excel, using the number of days elapsed. So to get the difference in hours is enough to compute the difference between the datetimes and multiply by 24 hours.
this is an example running on dax.do
DEFINE
TABLE T =
DATATABLE (
"DateTimeStart", DATETIME,
"DateTimeEnd", DATETIME,
{
{ "2021-02-09 19:30:02", "2021-02-10 22:00:02" }
}
)
EVALUATE
ADDCOLUMNS
(
T,
"Difference Hours", (T[DateTimeEnd] - T[DateTimeStart]) * 24
)
and this is the result

Calculate cumulative % based on sum of next row

I want to calculate % based on below formula. Its little bit tricks and I am kind of stuck and not sure how to do it. Thanks if anyone can help.
I have few records in below table which are grouped by Range
Range Count
0-10 50
10-20 12
20-30 9
30-40 0
40-50 0
50-60 1
60-70 4
70-80 45
80-90 16
90-100 7
Other 1
I want to have one more column which has the cumulative % based on sum of next row against total row count (145), something like below
Range Count Cumulative % of Range
0-10 50 34.5% (which is 50/145)
10-20 12 42.7% (which is 62/145)
20-30 9 48.9% (which is 71/145)
30-40 0 48.9% (which is 71/145)
40-50 0 48.9% (which is 71/145)
50-60 1 49.6% (which is 72/145)
60-70 4 52.4% (which is 76/145)
70-80 45 83.4% (which is 121/145)
80-90 16 94.5% (which is 137/145)
90-100 7 99.3% (which is 144/145)
Other 1 100.0% (which is 145/145)
Follow the below steps to get your answer. Please vote and accept the answer, If you find the solution helpful.
1st step - - Create an index column from your range column. I have replaced "Other" Value to 999. You can replace it to much bigger number, which is unlikely to be there in your dataset. Convert this new column into whole number
Sort Column = if(Sickness[Range] = "Other",9999,CONVERT(LEFT(Sickness[Range],SEARCH("-",Sickness[Range],1,LEN(Sickness[Range])+1)-1),INTEGER))
2nd Step - Use the below measure to get value:
Measure =
var RunningTotal = CALCULATE(SUM(Sickness[Count]),FILTER(all(Sickness),Sickness[Sort Column] <= MAX(Sickness[Sort Column])))
var totalSum = CALCULATE(SUM(Sickness[Count]),ALL())
Return
RunningTotal/totalSum
Below is the output that exactly matches your requirement.
For cumulative calculation, a ordering is always required. Anyway, if your given values in column "Range" is real - this will also work for this purpose as an ascending ordering on this filed keep data in expected order. Do this following to get your desired output.
Create the following measure-
count_percentage =
VAR total_of_count =
CALCULATE(
SUM(your_table_name[Count]),
ALL(your_table_name)
)
VAR cumulative_count =
CALCULATE(
SUM(your_table_name[Count]),
FILTER(
ALL(your_table_name),
your_table_name[Range] <= MIN(your_table_name[Range])
)
)
RETURN cumulative_count/total_of_count
Here is the final output-

DAX query for counting values based on another cumulative DAX measure

I have this table:
Id Length(m) Defect Site Date
1 10 1 y 10/1/19
2 60 0 x 09/1/19
3 30 1 y 08/1/19
4 80 1 x 07/1/19
5 20 1 x 06/1/19
I want to count the amount of defects and ids that are in the last 100m of length(sorted by date DESC), whilst maintaining the ability for this to change with additional filters. For example, what are the amount of defects for site x in the last 100m, or what are the amount of defects in the last 100m that have an ID bigger than 1.
For the question 'What are the amount of defects for site x in the last 100m', I would like the result to be 2, as the table should look like this:
Id Length(m) Length Cum. Defect Site Date
4 80 80 1 x 07/1/19
5 20 100 1 x 06/1/19
I believe the issue in creating this query so far has been that I need to create a cumulative DAX query first and then base the counting query off of that DAX query.
Also important to note that the filtering will be undertaken in PowerBI. I don't want to hardcode filters in the DAX query.
Any help is welcome.
Allwright!
I have taken a crack at this. I did assume that the id of the items(?) increments through time, so the oldest item has the lowest id.
You were correct that we need to filter the table based on the cumulative sum of the meters. So I first add a virtual column to the table (CumulativeMeters) which I can then use to filter the table on. I need to break the filter context of the ADDCOLUMNS function to sum up the hours of multiple rows.
Important is to use ALLSELECTED to keep any external filters in place. After this it is pretty straightforward to filter the table on a maximum CumulativeMeters of <= 100 meters and where the row is a defect. Counting the rows in the resulting table gives you the result you are looking for:
# Defects last 100m =
CALCULATE (
COUNTROWS ( Items ),
FILTER (
ADDCOLUMNS (
Items,
"CumulativeMeters", CALCULATE (
SUM ( Items[Length(m)] ),
FILTER (
ALLSELECTED( Items ),
Items[Date] <= EARLIER ( Items[Date] )
&& Items[Id] <= EARLIER ( Items[Id] )
)
)
),
[CumulativeMeters] <= 100
&& Items[Defect] = 1
)
)
Hope that helps,
Jan