How to get seconds time difference from a DateTime Column in Dax? - dax

I have a table which has a DateTime column, status column and an index column, I am trying to get a column that shows the time difference in seconds between the previous row and the current row and for it to ignore the previous row if status is blank.
This is what the table looks like, currently:
Time ID status. timesinceprev(seconds) index
22.1.21 04:02:04 12 low 0 1
22.1.21 04:24:07 12 low 1320 2
22.1.21 04:26:04 12 medium 120 3
22.1.21 04:29:04 12 180 4
22.1.21 04:30:05 12 low 61 5
22.1.21 04:31:07 12 62 6
I want my timesinceprev column to display a 0 in the row if status has nothing there
timesinceprev =
var values = IF(DATEDIFF(LOOKUPVALUE('Table'[Time],'Table'[index],'Table'[index]-1),'Table'[Time],MINUTE)>720,
0,
IF(ISBLANK(FILTER('Table','Table'[index]=EARLIER('Table[index])-1)),
0,
DATEDIFF(LOOKUPVALUE('Table'[Time],'Table'[index],'Table'[index]-1),'Table'[Time],MINUTE)
* 60))
return values

Related

Amazon QuickSight - Running Difference

I have the following table and want to add a column with running difference.
time_gap
amounts
0
150
0.5
19
1.5
2
6
1
7
4
my desired out is
time_gap
amounts
diff
0
150
150
0.5
19
131
1.5
2
129
6
10
119
7
4
115
What I've tried:
I duplicate the amounts column and used table calculation, difference, but got the difference between two consecutive rows instead:
time_gap
amounts
diff
0
150
0.5
19
-131
1.5
2
-17
6
10
8
7
4
-6
I tried some calculated fields formulas but that didn't work either.
thank you!

How can I get the max value of the sum value in a N x M matrix, each column and each rows can be chosen only once

Example input:
3 4
10 20 30
40 10 30
20 10 0
5 15 5
Example output:
1 3
2 1
4 2
85
30 + 40 + 15 = 85
I need get the result of a matrix with thousand rows and columns

TiBCO Spotfire - How to Calculate only the last 3 columns in a Data - see descr

Week Sales
1 100
2 250
3 350
4 145
5 987
6 26
7 32
8 156
I wanted to calculate the sales only for the last 3 weeks so the total will be 156+32+26.
If new weeks are added it should automatically calculate only the data from the last 3 rows.
Tried this formula but it is returning an incorrect sum
sum(sales) over (lastperiod(3(week))
https://i.stack.imgur.com/6Y7h7.jpg
If you want only the last 3 weeks sum in calculated column you can use a simple if calculation.
If([week]>(Max([week]) - 3),Sum([sales]),0)
If you need 3 weeks calculation throughout table use below one.
sum([sales]) OVER (LastPeriods(3,[week]))

SAS Dataset - Assign incremental value for each change in variable - sort by timestamp

Similar to
How to assign an ID to a group of variables
My dataset is sorted by ID, then timestamp. I need to create an 'order' variable, incrementing on each change in say Status, but my sort must remain time stamp, so I think I am correct in suggesting that by BY (group) will not work. The order field below illustrates what I seek...
ID Status Timestamp Order
188 3 12:15 1
188 4 12:45 2
188 4 13:10 2
188 3 14:20 3
189 10 11:00 1
189 11 13:00 2
189 10 13:30 3
189 10 13:35 3
The first and second '3's are separate, likewise the first and subsequent '10's.
You can use the NOTSORTED option to have SAS automatically set the FIRST.STATUS flag for you.
data want ;
set have ;
by id status notsorted;
if first.id then order=0;
order + first.status;
run;
As you mentioned, it is very similar to that other question. The trick here is to set the order of the first observation in each by group to zero.
data temp;
input ID $ Status $ Timestamp $;
datalines;
188 3 12:15
188 4 12:45
188 4 13:10
188 3 14:20
189 10 11:00
189 11 13:00
189 10 13:30
189 10 13:35
;
run;
data temp2;
set temp;
by id;
if first.id then order = 0;
if status ~= lag(status) then order + 1;
run;

Group sequential numbers into min and max group pairs

I have a table with a list of numbers. Each number belongs to an entity.
Entity Number
1 1
1 2
1 3
1 4
...
1 20
2 21
2 22
2 23
1 24
2 25
2 26
2 30
2 31
2 32
2 33
The goal is to list the numbers, grouped by the entities as ranges (min-max pairs).
I need to find a way to group the above table as:
Entity Min Max
1 1 20
2 21 23
1 24 24
2 25 26
2 30 33
I've succesfully done this in my education, but I always found it hard and can't remember how the algorithm was done
This looks similar to SQL Data Range Min Max category
and TSQL Select Min & Max row when grouping

Resources