Dax code for multiplay rows - powerpivot

It is possible to write dax code for the next request:
If at "status_new" column appears "active" take "testerName" cell data--> remove the last two characters "G1" (Always G1) and search for the same date at "MTDate" column (I always have two identical dates per tester name one time is for "XXXXG1" testers and second one is for "XXXX" testers),---> After we found this row check tester status if it "Active" leave the cell empty if it something else paste "Active".
MTDate TesterName Day TesterHostname TesterFamily status G1_STATUS MASTER_STATUS Master_new1 STATUS_NEW Status_updated
4/27/2017 20:00 CMT0441 27 HA04TCMT0441W7 CMT Active Active Active
4/27/2017 20:00 CMT0441G1 27 HA04TCMT0441G7 CMT Active Active Active Active
4/27/2017 19:45 CMT0441G1 27 HA04TCMT0441G7 CMT Active Active Active Active
4/27/2017 19:45 CMT0441 27 HA04TCMT0441W7 CMT Active Active Active
4/27/2017 19:30 CMT0441 27 HA04TCMT0441W7 CMT Inactive Inactive Inactive
4/27/2017 19:30 CMT0441G1 27 HA04TCMT0441G7 CMT Inactive Inactive
4/27/2017 19:15 CMT0441 27 HA04TCMT0441W7 CMT Inactive Inactive Inactive
4/27/2017 19:15 CMT0441G1 27 HA04TCMT0441G7 CMT Inactive Inactive
4/27/2017 19:00 CMT0441 27 HA04TCMT0441W7 CMT Inactive Inactive Inactive
4/27/2017 19:00 CMT0441G1 27 HA04TCMT0441G7 CMT Inactive Inactive
4/27/2017 18:45 CMT0441 27 HA04TCMT0441W7 CMT Inactive Inactive Inactive
4/27/2017 18:45 CMT0441G1 27 HA04TCMT0441G7 CMT Inactive Inactive
4/27/2017 18:30 CMT0441 27 HA04TCMT0441W7 CMT Inactive Inactive Inactive
4/27/2017 18:30 CMT0441G1 27 HA04TCMT0441G7 CMT Inactive Inactive
4/27/2017 18:15 CMT0441 27 HA04TCMT0441W7 CMT Active Active Active
4/27/2017 18:15 CMT0441G1 27 HA04TCMT0441G7 CMT Inactive Inactive
4/27/2017 18:00 CMT0441 27 HA04TCMT0441W7 CMT Active Active Active
4/27/2017 18:00 CMT0441G1 27 HA04TCMT0441G7 CMT Inactive Inactive
4/27/2017 17:45 CMT0441 27 HA04TCMT0441W7 CMT Active Active Active
4/27/2017 17:45 CMT0441G1 27 HA04TCMT0441G7 CMT Inactive Inactive
4/27/2017 17:30 CMT0441G1 27 HA04TCMT0441G7 CMT Inactive Inactive
4/27/2017 17:30 CMT0441 27 HA04TCMT0441W7 CMT Active Active Active
4/27/2017 17:15 CMT0441G1 27 HA04TCMT0441G7 CMT Inactive Inactive
4/27/2017 17:15 CMT0441 27 HA04TCMT0441W7 CMT Active Active Active
I tried to do it with "IF" command but I found deadlock time after time.

Related

Getting wrong(?) average when calculating values in a time range

I am working with AWS Redshift / PostgreSQL. I have two tables that can be joined on the interval_date (DATE data_type) and interval_time_utc (VARCHAR data type) and/or the status and price_source columns. Source A is equivalent to the Y status and Source B is equivalent to the N status. I am trying to get the average price and the sum of mw_power for a given hour for each status / price_source. An hour is the timestamps from XX:05 to XX:00 so for 15:00, the values should be from the 14:05 to the 15:00 timestamps. Even if for an hour interval where all status are one value, I still need to calculate the average price for both price_source values, but the sum of mw_power would be 0. I am passing in the date and time intervals through my application code. I am seeing a different average price for the 15:00 hour than I expect so either I am bad at math or there is a bug in my query I can't determine. The 14:00 and 16:00 hour results come back as expected.
power_table
interval_date
interval_time_utc
mw_power
status
2022-05-09
13:00
92.25
N
2022-05-09
13:05
90.75
N
2022-05-09
13:10
91.25
N
2022-05-09
13:15
92.00
N
2022-05-09
13:20
92.00
N
2022-05-09
13:25
90.00
N
2022-05-09
13:30
93.00
N
2022-05-09
13:35
91.75
N
2022-05-09
13:40
90.25
N
2022-05-09
13:45
93.00
N
2022-05-09
13:50
91.00
N
2022-05-09
13:55
94.00
N
2022-05-09
14:00
91.00
N
2022-05-09
14:05
91.00
N
2022-05-09
14:10
94.00
N
2022-05-09
14:15
92.00
N
2022-05-09
14:20
91.00
N
2022-05-09
14:25
94.00
Y
2022-05-09
14:30
92.00
Y
2022-05-09
14:35
91.75
Y
2022-05-09
14:40
92.25
Y
2022-05-09
14:45
91.00
Y
2022-05-09
14:50
92.00
Y
2022-05-09
14:55
93.00
Y
2022-05-09
15:00
90.00
Y
price_table
interval_date
interval_time_utc
price
price_source
2022-05-09
13:00
54.20
Source A
2022-05-09
13:05
54.20
Source A
2022-05-09
13:10
54.20
Source A
2022-05-09
13:00
54.20
Source B
2022-05-09
13:05
54.20
Source B
2022-05-09
13:10
54.20
Source B
2022-05-09
13:15
34.11
Source A
2022-05-09
13:20
34.11
Source A
2022-05-09
13:25
34.11
Source A
2022-05-09
13:15
39.61
Source B
2022-05-09
13:20
39.61
Source B
2022-05-09
13:25
39.61
Source B
2022-05-09
13:30
2.81
Source A
2022-05-09
13:35
2.81
Source A
2022-05-09
13:40
2.81
Source A
2022-05-09
13:30
17.13
Source B
2022-05-09
13:35
17.13
Source B
2022-05-09
13:40
17.13
Source B
2022-05-09
13:45
1.58
Source A
2022-05-09
13:50
1.58
Source A
2022-05-09
13:55
1.58
Source A
2022-05-09
13:45
15.98
Source B
2022-05-09
13:50
15.98
Source B
2022-05-09
13:55
15.98
Source B
2022-05-09
14:00
4.60
Source A
2022-05-09
14:05
4.60
Source A
2022-05-09
14:10
4.60
Source A
2022-05-09
14:00
18.09
Source B
2022-05-09
14:05
18.09
Source B
2022-05-09
14:10
18.09
Source B
2022-05-09
14:15
2.46
Source A
2022-05-09
14:20
2.46
Source A
2022-05-09
14:25
2.46
Source A
2022-05-09
14:15
16.66
Source B
2022-05-09
14:20
16.66
Source B
2022-05-09
14:25
16.66
Source B
2022-05-09
14:30
3.36
Source A
2022-05-09
14:35
3.36
Source A
2022-05-09
14:40
3.36
Source A
2022-05-09
14:30
21.52
Source B
2022-05-09
14:35
21.52
Source B
2022-05-09
14:40
21.52
Source B
2022-05-09
14:45
4.55
Source A
2022-05-09
14:50
4.55
Source A
2022-05-09
14:55
4.55
Source A
2022-05-09
14:45
16.30
Source B
2022-05-09
14:50
16.30
Source B
2022-05-09
14:55
16.30
Source B
2022-05-09
15:00
-21.87
Source A
2022-05-09
15:00
4.96
Source B
-- query that i am using to get hourly values
SELECT pricet.price_source,
COALESCE(powert.volume, 0),
pricet.price,
powert.status
FROM (SELECT status,
SUM(mw_power) volume
FROM power_table
WHERE (interval_date || ' ' || interval_time_utc)::timestamp BETWEEN '2022-05-09 14:05:00.0' AND '2022-05-09 15:00:00.0'
GROUP BY status) powert
RIGHT JOIN (SELECT price_source,
AVG(price) price
FROM price_table
WHERE (interval_date || ' ' || interval_time_utc)::timestamp BETWEEN '2022-05-09 14:05:00.0' AND '2022-05-09 15:00:00.0'
GROUP BY price_source) pricet
ON pricet.price_source = CASE WHEN powert.status = 'Y' THEN 'Source A'
ELSE 'Source B'
END;
I am looking to get an expected output of the following for the 15:00 hour:
price_source
volume
price
status
Source A
736.00
0.54
Y
Source B
368.00
17.38
N
Result that I'm getting from query:
price_source
volume
price
status
Source A
736.00
1.54
Y
Source B
368.00
17.05
N
db fiddle link of tables and query and results: https://dbfiddle.uk/?rdbms=postgres_14&fiddle=474b009c5cf5366961751a61c0f96c6c
I think you made a calculator error. I changed your fiddle to add a rolling sum and rolling average for the second part of your query. To get an average of .54 (Source A) your sum would need to be 12 less than the total of the values for this hour. 12 is the count of values for the hour so a possible slip in subtracting 12 before dividing by 12?
The other source (B) the total would need to be off by 4m (an addition of 4 to the sum). Not sure how this could have happened but ...
Anyway the fiddle is at https://dbfiddle.uk/?rdbms=postgres_14&fiddle=e65c38677f3ab92607bbff778bc0f69e

Calculate difference between time over midnight and condition

When I calculate the difference between two time, I must also pay attention to what happens after midnight.
select *
,datediff(second,
cast([schedule_deptime]as time),
cast([prev_announce_time]as time))
as kpi1_delta
id
date_event
schedule_deptime
prev_announce_time
kpi1_delta
79643204
2021-02-11 19:55:52.000
19:15
2021-02-11 19:39:01.000
1441
79569510
2021-02-11 16:51:05.000
16:50
2021-02-11 16:48:17.000
-103
106160161
2021-01-21 20:43:44.000
20:28
2021-01-21 01:03:41.000
-69859
106216877
2021-01-21 23:50:10.000
23:45
2021-01-21 00:06:52.000
-85088
79703534
2021-02-11 23:58:01.000
00:04
2021-02-11 00:03:01.000
-59
I should get for my third and fourth row:
id
date_event
schedule_deptime
prev_announce_time
kpi1_delta
106160161
2021-01-21 20:43:44.000
20:28
2021-01-21 01:03:41.000
16541
106216877
2021-01-21 23:50:10.000
23:45
2021-01-21 00:06:52.000
1312

SQL Server : update query doesn't change anything, shows no error

I am trying to update the table with the values from the same table.
What I want is to change the connection setup in the rows where the worker and client are same and that the changed row Connection setup started in 5mins after the other connection (with the same worker and client) ended.
I first created a SELECT query that returned me all the rows that needed to be changed
SELECT t.*
FROM Table1 t
WHERE EXISTS (SELECT 1 FROM Table1
WHERE worker = t.worker
AND client = t.client
AND t.SessionNo != SessionNo
AND t.[Connection setup] <= DATEADD(mi, 5, [Connection end])
AND t.[Connection setup] >= [Connection end])
Then I tried to import this query inside of an UPDATE query, but it didn't change anything :/ and it doesn't show me any errors.
UPDATE t
SET t.Start = t2.Start
FROM Table1 t
INNER JOIN Table1 t2 ON (t.SessionNo = t2.SessionNo)
WHERE t.worker = t2.worker
AND t.client = t2.client
AND t2.SessionNo <> t.SessionNo
AND t.[Connection setup] <= DATEADD(mi, 5, t2.[Connection end])
AND t.[Connection setup] >= t2.[Connection end]
Example:
The first table are the rows that should be changed. As you can see there is a column "right time" that shows what value should they have after the update.
SessionNo worker Tag Start Ende Dauer Connection setup Connection end client right_time
1 424568 mh 09.01.2020 00:00:00 13:45 13:49 00:04 09.01.2020 13:45:00 09.01.2020 13:49:00 OBENAT1D0209 13:44
2 269650 mg 09.03.2020 00:00:00 10:25 10:47 00:21 09.03.2020 10:25:00 09.03.2020 10:47:00 OBENAT1D0117 10:24
3 280892 mg 09.03.2020 00:00:00 12:19 12:22 00:03 09.03.2020 12:19:00 09.03.2020 12:22:00 OBENAT1D0117 12:19
4 175250 mg 09.03.2020 00:00:00 13:12 13:13 00:01 09.03.2020 13:12:00 09.03.2020 13:13:00 ORTNERAT1D0001 13:04
5 332684 dg 09.05.2020 00:00:00 16:05 16:33 00:28 09.05.2020 16:05:00 09.05.2020 16:33:00 KILLYAT3D0102 15:57
but as you can see here Start column is still the same.
SessionNo worker Tag Start Ende Dauer Connection setup Connection end client right_time
1 317045 mh 09.01.2020 00:00:00 09:29 09:38 00:09 09.01.2020 09:29:00 09.01.2020 09:38:00 AUMAAT1D0124 09:29
2 144035 sb 09.01.2020 00:00:00 11:09 11:27 00:18 09.01.2020 11:09:00 09.01.2020 11:27:00 OBENAT1D0231 11:09
3 437704 mh 09.01.2020 00:00:00 13:44 13:44 00:00 09.01.2020 13:44:00 09.01.2020 13:44:00 OBENAT1D0209 13:44
4 424568 mh 09.01.2020 00:00:00 13:45 13:49 00:04 09.01.2020 13:45:00 09.01.2020 13:49:00 OBENAT1D0209 13:44
5 219640 mh 09.01.2020 00:00:00 15:16 15:26 00:10 09.01.2020 15:16:00 09.01.2020 15:26:00 OBENAT1D0209 15:16
6 201023 mh 09.01.2020 00:00:00 16:29 16:35 00:06 09.01.2020 16:29:00 09.01.2020 16:35:00 OBENAT1D0209 16:29
7 236114 mg 09.03.2020 00:00:00 08:55 09:08 00:12 09.03.2020 08:55:00 09.03.2020 09:08:00 NULL NULL
8 271379 mg 09.03.2020 00:00:00 10:24 10:25 00:00 09.03.2020 10:24:00 09.03.2020 10:25:00 OBENAT1D0117 10:24
9 269650 mg 09.03.2020 00:00:00 10:25 10:47 00:21 09.03.2020 10:25:00 09.03.2020 10:47:00 OBENAT1D0117 10:24
10 290765 mg 09.03.2020 00:00:00 12:19 12:19 00:00 09.03.2020 12:19:00 09.03.2020 12:19:00 OBENAT1D0117 12:19
11 280892 mg 09.03.2020 00:00:00 12:19 12:22 00:03 09.03.2020 12:19:00 09.03.2020 12:22:00 OBENAT1D0117 12:19
12 538583 mg 09.03.2020 00:00:00 12:30 12:58 00:28 09.03.2020 12:30:00 09.03.2020 12:58:00 RATTAYAT1D0107 NULL
13 697202 mg 09.03.2020 00:00:00 13:04 13:08 00:04 09.03.2020 13:04:00 09.03.2020 13:08:00 ORTNERAT1D0001 13:04
14 175250 mg 09.03.2020 00:00:00 13:12 13:13 00:01 09.03.2020 13:12:00 09.03.2020 13:13:00 ORTNERAT1D0001 13:04
15 330580 dg 09.05.2020 00:00:00 15:57 16:05 00:08 09.05.2020 15:57:00 09.05.2020 16:05:00 KILLYAT3D0102 15:57
16 332684 dg 09.05.2020 00:00:00 16:05 16:33 00:28 09.05.2020 16:05:00 09.05.2020 16:33:00 KILLYAT3D0102 15:57
NOTE : In this case, in order to test the values I am changing the Start column instead of the connection startup.
You are updating zero rows, because of:
ON (t.SessionNo = t2.SessionNo)
...
AND t2.SessionNo <> t.SessionNo
You want to find rows with another session number, but you have t.SessionNo = t2.SessionNo, so this is exactly what you don't want.
You seem to think that a join needs a comparision with = on a single column, but this is not true. A join condition can be any boolean expression.
This may work for you:
UPDATE t
SET t.Start = t2.Start
FROM Table1 t
INNER JOIN Table1 t2 ON t.worker = t2.worker
AND t.client = t2.client
AND t.SessionNo <> t2.SessionNo
AND t.[Connection setup] <= DATEADD(mi, 5, t2.[Connection end])
AND t.[Connection setup] >= t2.[Connection end];

Create an event log from an excel file by turning columns into repeated rows

I have an Excel sheet like the following:
ID Arrival Passed Berthing Date UnBerthing Date Departure Passed
1 13/05/2017 15:30 13/05/2017 16:00 31/05/2017 20:44 31/05/2017
2 15/05/2017 16:56 15/05/2017 17:15 16/05/2017 00:00 16/05/2017
3 20/05/2017 09:54 20/05/2017 10:26 20/05/2017 18:07 20/05/2017
4 24/05/2017 16:09 24/05/2017 16:35 25/05/2017 01:03 25/05/2017
5 29/05/2017 10:30 29/05/2017 10:45 29/05/2017 17:33 29/05/2017
I need this in the following format:
ID Event Time
1 Arrival 13/05/2017 15:30
1 Berth 13/05/2017 16:00
1 UnBerth 31/05/2017 20:44
1 Departure 31/05/2017 20:58
2 Arrival 15/05/2017 16:56
2 Berth 15/05/2017 17:15
2 UnBerth 16/05/2017 00:00
2 Departure 16/05/2017 00:04
etc
I've searched the web and this site(youtube...), but with no right answer, i've tried the transpose function and pivot table, but i couldn't make it.
Any help would be appreciated.
Thanks you.
Assuming that your dataset is in range A2:E6.
For getting ID:
=INDEX($A$2:$E$6,CEILING(ROWS($A$1:A1)/4,1),1)
For getting Event:
=CHOOSE(MOD(ROWS($A$1:A1)-1,4)+1,"Arrival","Berth","Unberth","Departure")
For getting Time:
=INDEX($A$2:$E$6,CEILING(ROWS($A$1:A1)/4,1),MOD(ROWS($A$1:A1)-1,4)+2)
and then copy down until you get error.

SQL Query - Hide lines

in my environment we use Altiris to control our asset and daily we had a policy that set status retired to computers that stay more than 45 days offline and turn back to active if this computer appers online on network.
The problem is sometimes (not for all devices) when the policy change the status it write on database two lines:
first with the current status and and another with the new status:
The same occur when the device go back to active status so when i try to SUM data to know how many devices was changed to retired or active by month the number doesn't make sense because for some devices we have two lines with 2 different status on same dataChanged
eg:
ComputerName Date Changed Status
001PROJNEW-VM 13/01/2015 17:33 Active
002PROJNEW-VM 11/09/2014 11:58 Retired
002PROJNEW-VM 07/10/2014 21:10 Retired
002PROJNEW-VM 07/10/2014 21:10 Active
003PROJNEW-VM 11/09/2014 11:58 Retired
003PROJNEW-VM 13/11/2014 03:27 Retired
003PROJNEW-VM 13/11/2014 03:27 Active
004PROJNEW-VM 06/04/2015 20:00 Retired
005PROJNEW-VM 11/09/2014 11:58 Retired
005PROJNEW-VM 09/10/2014 21:09 Retired
005PROJNEW-VM 09/10/2014 21:09 Active
005PROJNEW-VM 06/04/2015 20:00 Retired
006PROJNEW-VM 26/12/2014 20:00 Retired
006PROJNEW-VM 31/12/2014 05:34 Retired
006PROJNEW-VM 31/12/2014 05:34 Active
006PROJNEW-VM 06/01/2015 20:00 Retired
007PROJNEW-VM 11/09/2014 11:58 Retired
007PROJNEW-VM 27/12/2014 05:38 Retired
007PROJNEW-VM 27/12/2014 05:38 Active
007PROJNEW-VM 12/04/2015 19:50 Retired
008PROJNEW-VM 11/09/2014 11:58 Retired
008PROJNEW-VM 29/10/2014 05:44 Retired
008PROJNEW-VM 29/10/2014 05:44 Active
008PROJNEW-VM 06/04/2015 20:00 Retired
009PROJNEW-VM 11/09/2014 11:58 Retired
009PROJNEW-VM 17/09/2014 20:33 Retired
009PROJNEW-VM 17/09/2014 20:33 Active
009PROJNEW-VM 19/02/2015 20:00 Retired
010PROJNEW-VM 11/09/2014 11:58 Retired
010PROJNEW-VM 29/10/2014 05:44 Retired
010PROJNEW-VM 29/10/2014 05:44 Active
010PROJNEW-VM 06/04/2015 20:00 Retired
011PROJNEW-VM 05/04/2015 20:00 Retired
013PROJNEW-VM 20/02/2015 20:00 Retired
014PROJNEW-VM 06/04/2015 20:00 Retired
Basically what i need and actually i can't do is: if the hostname has two equal 'Date Changed' and two different 'status' bring in query result the last line for this Hostname and 'Date Changed'...
The result eg:
Nome do computador Date Changed Status
001PROJNEW-VM 13/01/2015 17:33 Active
002PROJNEW-VM 11/09/2014 11:58 Retired
002PROJNEW-VM 07/10/2014 21:10 Active
003PROJNEW-VM 11/09/2014 11:58 Retired
003PROJNEW-VM 13/11/2014 03:27 Active
004PROJNEW-VM 06/04/2015 20:00 Retired
005PROJNEW-VM 11/09/2014 11:58 Retired
005PROJNEW-VM 09/10/2014 21:09 Active
005PROJNEW-VM 06/04/2015 20:00 Retired
006PROJNEW-VM 26/12/2014 20:00 Retired
006PROJNEW-VM 31/12/2014 05:34 Active
006PROJNEW-VM 06/01/2015 20:00 Retired
007PROJNEW-VM 11/09/2014 11:58 Retired
007PROJNEW-VM 27/12/2014 05:38 Active
007PROJNEW-VM 12/04/2015 19:50 Retired
008PROJNEW-VM 11/09/2014 11:58 Retired
008PROJNEW-VM 29/10/2014 05:44 Active
008PROJNEW-VM 06/04/2015 20:00 Retired
009PROJNEW-VM 11/09/2014 11:58 Retired
009PROJNEW-VM 17/09/2014 20:33 Active
009PROJNEW-VM 19/02/2015 20:00 Retired
010PROJNEW-VM 11/09/2014 11:58 Retired
010PROJNEW-VM 29/10/2014 05:44 Active
010PROJNEW-VM 06/04/2015 20:00 Retired
011PROJNEW-VM 05/04/2015 20:00 Retired
013PROJNEW-VM 20/02/2015 20:00 Retired
014PROJNEW-VM 06/04/2015 20:00 Retired
If the dates are the same, there is no first or last record, so assuming you only have those 2 statuses, and you want the status always to be Active when 2 rows exist at the same time, you can just do it with min:
select
ComputerName
DateChanged
min(Status) as Status
from
YourTable
group by
ComputerName
DateChanged
If it's more complex, you can do similar things with row_number and ordering by the desired order of statuses.
Assuming you have, say, an id column that specifies the ordering, then you can use row_number():
select t.*
from (select t.*,
row_number() over (partition by computername, datechanged
order by id desc) as seqnum
from table t
) t
where seqnum = 1;
In your particular example, all the duplicates seem to be active. If that is the case, then:
select computername, datechanged,
(case when min(status) = max(status) then min(status)
when sum(case when status = 'Active' then 1 else 0 end) > 0
then 'Active'
else '***Unknown***'
end) as status
from table t
group by computername, datechanged;