I am trying to calculate the time it takes in minutes for an action to be completed. Using a simple datediff dax would be my go to, but run into an issue where an action takes longer than one day.
Code i'm trying
Time Taken = DATEDIFF(Sheet1[Start Time],Sheet1[End Time],MINUTE)
And here is an example of the outcome.
enter image description here
In the last cell, you can see that the action takes a day to complete so the result is providing an answer of -149 minutes which i dont want.
What can I do to get this right?
A calculated column of:
Time Taken =
DATEDIFF (
'Table'[Start Date] + 'Table'[Start Time],
'Table'[End Date] + 'Table'[End Time],
MINUTE
)
Gives:
Time Taken =
DATEDIFF(Sheet1[Start Time],Sheet1[End Time],MINUTE)+
DATEDIFF(Sheet1[Start Date],Sheet1[End Date],DAY)*60*24
-- DATEDIFF(Sheet1[Start Date],Sheet1[End Date],MINUTE) --?
Related
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 )
)
This is the Sample Data what i want to do is for each document (field "REQUIREMENT_DESCRIPTION") calculate time difference by subtracting the time in "ACTION_DATE" n row from n+1 row.
And then calculate the mean and standard deviation of each document validation time.
So after searching online i found this formula which calculates the difference in time row by row.
First i split the column ACTION_DATE by "space" delimeter to separate time from date and than named the time field "ACTION_TIME".
Diff in Seconds = DATEDIFF(
Table[ACTION_TIME],
CALCULATE(
MIN([ACTION_TIME]),
FILTER(ALL(Table), Table[ACTION_TIME] > EARLIER(Table[ACTION_TIME]))
),
SECOND
)
I'm working with Power BI. I wanna know from the Datestamp column see how much minute difference it's between the previous value and the last one.
I tried this formula below but got totally different value.
Datediff =
DATEDIFF(
DataView[CommTimestamp];
TODAY();
minute
)
The date-stamp column look like YYYY-MM-DD-HOUR-MINUTE-SECOND
The code you have written compares the [CommTimestamp] column with the TODAY() function. TODAY() returns YYYY-MM-DD-00:00:00 when it's calculated (different for Calculated columns and measures).
Therefore what you will see is the number of minutes from [dateTime] until today:
NB. That the [DiffAdd] column which is calculated like: [DateTime]+[DateDiffToday]/1440 doesn't take the seconds into account.
So without more info about your data table and your expected results, it's difficult to help you.
If you need to know difference in minutes you cannot use TODAY() as Oscar stated, it only provides 'Day' grannularity. You ca try using NOW() instead:
Datediff =
DATEDIFF(
DataView[CommTimestamp];
NOW();
minute
)
This issue seemed to be easy at the first glance but I have been trying to solve it for a while.
enter image description here
I would like to dynamically sum the previous period sales in a power pivot measure. The thing is that my period column is a integer value not a date.
I manage to calculate the previous period but I did not manage to set it up as a filter value:
Max(Table1[Period])-1 --> this gives me the previous value of the period field
However when I want to add this as a filter of a calculated measure it doesn't work: --> Calculate(
Sum(table1[Sales]), Filter(table1,Max(table1[Period])=Max(table1[Period])
)
I tried simply this one as well: Calculate(Sum(table1[Sales]), table1[Period] = table1[Period] -1 )
but neither of them are working. I though that I do it with calculated column however I would rather do it with measure.
Can you please help me?
Expected result:
Create measure:
Previous Sales:=
CALCULATE( SUM(Table1[Sales]),
FILTER( ALL(Table1), Table1[Period] = MAX(Table1[Period]) - 1))
It will give you dynamic previous sales. Please note: it relies on the fact that periods increment by 1.
If you need to summarize Previous Sales, create a second measure:
Total Previous Sales:=
SUMX( VALUES(Table1[Period]), [Previous Sales])
I have following scenario which has been simplified a little:
Costs fact table:
date, project_key, costs €
Project dimension:
project_key, name, starting date, ending date
Date dimension:
date, years, months, weeks, etc
I would need to create a measure which would tell project duration of days using starting and ending dates from project dimension. The first challenge is that there isn't transactions for all days in the fact table. Project starting date might be 1st of January but first cost transaction is on fact table like 15th on January. So we still need to calculate the days between starting and ending date if on filter context.
So the second challenge is the filter context. User might want to view only February. So it project starting date is 1.6.2016 and ending date is 1.11.2016 and user wants to view only September it should display only 30 days.
The third challenge is to view days for multiple projects. So if user selects only single day it should view count for all of the projects in progress.
I'm thankful for any help which could lead towards the solution. So don't hesitate to ask more details if needed.
edit: Here is a picture to explain this better:
Update 7.2.2017
Still trying to create a single measure for this solution. Measure which user could use with only dates, projects or as it is. Separate calculated column for ongoing project counts per day would be easy solution but it would only filter by date table.
Update 9.2.2017
Thank you all for your efforts. As an end result I'm confident that calculations not based on fact table are quite tricky. For this specific case I ended up doing new table with CROSS JOIN on dates and project ids to fulfill all requirements. One option also was to add starting and ending dates as own lines to fact table with zero costs. The real solution also have more dimensions we need to take into consideration.
To get the expected result you have to create a calculated column and a measure, the calculated column lets count the number of projects in dates where projects were executed and the measure to count the number of days elapsed from [starting_date] and [ending_date] in each project taking in account filters.
The calculated column have to be created in the dim_date table using this expression:
Count of Projects =
SUMX (
FILTER (
project_dim,
[starting_date] <= EARLIER ( date_dim[date] )
&& [ending_date] >= EARLIER ( date_dim[date] )
),
1
)
The measure should be created in the project_dim table using this expression:
Duration (Days) =
DATEDIFF (
MAX ( MIN ( [starting_date] ), MIN ( date_dim[date] ) ),
MIN ( MAX ( [ending_date] ), MAX ( date_dim[date] ) ),
DAY
)
+ 1
The result you will get is something like this:
And this if you filter the week using an slicer or a filter on dim_date table
Update
Support for SSAS 2014 - DATEDIFF() is available in SSAS 2016.
First of all, it is important you realize you are measuring two different things but you want only one measure visible to your users. In the first Expected result you want to get the number of projects running in each date while in the Expected results 2 and 3 (in the OP) you want the days elapsed in each project taking in account filters on date_dim.
You can create a measure to wrap both measures in one and use HASONEFILTER to determine the context where each measure should run. Before continue with the wrapping measure check the below measure that replaces the measure posted above using DATEDIFF function which doesn't work in your environment.
After creating the previous calculated column that is required to determine the number of projects in each date, create a measure called Duration Measure, this measure won't be used by your users but lets us calculate the final measure.
Duration Measure = SUMX(FILTER (
date_dim,
date_dim[date] >= MIN ( project_dim[starting_date] )
&& date_dim[date] <= MAX ( project_dim[ending_date] )
),1
)
Now the final measure which your users should interact can be written like this:
Duration (Days) =
IF (
HASONEFILTER ( date_dim[date] ),
SUM ( date_dim[Count of Projects] ),
[Duration Measure]
)
This measure will determine the context and will return the right measure for the given context. So you can add the same measure for both tables and it will return the desired result.
Despite this solution is demonstrated in Power BI it works in Power Pivot too.
First I would create 2 relationships:
project_dim[project_key] => costs_fact[project_key]
date_dim[date] => costs_fact[date]
The Costs measure would be just: SUM ( costs_fact[costs] )
The Duration (days) measure needs a CALCULATE to change the filter context on the Date dimension. This is effectively calculating a relationship between project_dim and date_dim on the fly, based on the selected rows from both tables.
Duration (days) =
CALCULATE (
COUNTROWS ( date_dim ),
FILTER (
date_dim,
date_dim[date] >= MIN ( project_dim[starting_date] )
&& date_dim[date] <= MAX ( project_dim[ending_date] )
)
)
I suggest you to separate the measure Duration (days) into different calculated column/measure as they don't actually have the same meaning under different contexts.
First of all, create a one-to-many relationship between dates/costs and projects/costs. (Note the single cross filter direction or the filter context will be wrongly applied during calculation)
For the Expected result 1, I've created a calculated column in the date dimension called Project (days). It counts how many projects are in progress for a given day.
Project (days) =
COUNTROWS(
FILTER(
projects,
dates[date] >= projects[starting_date] &&
dates[date] <= projects[ending_date]
)
)
P.S. If you want to have aggregated results on weekly/monthly basis, you can further create a measure and aggregate Project (days).
For Expected result 2 and 3, the measure Duration (days) is as follows:
Duration (days) =
COUNTROWS(
FILTER(
dates,
dates[date] >= FIRSTDATE(projects[starting_date]) &&
dates[date] <= FIRSTDATE(projects[ending_date])
)
)
The result will be as expected: