Damo
Damo

Reputation: 2070

New Column, IF statement not performing correct evaluation on Measures

I have a Power BI report with a large dataset. I perform some calculations on that dataset, creating measures. The measures created are timestamps.

I extracted the hour (hourhigh,hourlow) from the timestamp to evaluate if one INT is greater than the other. This evaluation is performed as a new calculated column called 'valid' hourhigh & hourlow are measures.

For some reason, my evaluation (valid) fails, and I don't understand why. It marks each as 'Good', but this is not correct.

HourHigh

hourhigh = 
var h = HOUR([timestamp_high])
RETURN h

HourLow

hourlow = 
var l = Hour([timestamp_low])
RETURN l

Valid

valid = IF([hourhigh]> [hourlow],"Bad","Good")

My desired output is to correctly identify in the valid column on a per row basis if one value is higher than the other, and mark it as "Good" or "Bad".

Actual Data Output

+------------------+----------+--------------+-------------+------------------+------------------+----------+---------+-------+
|       Date       | deviceid | voltage_high | voltage_low |  timestamp_high  |  timestamp_low   | hourhigh | hourlow | valid |
+------------------+----------+--------------+-------------+------------------+------------------+----------+---------+-------+
| 01/11/2023 00:00 |       63 | 28.41        | 23.03       | 01/11/2023 22:42 | 01/11/2023 19:55 |       22 |      19 | Good  |
| 02/11/2023 00:00 |       63 | 28.43        | 22.89       | 02/11/2023 23:36 | 02/11/2023 20:41 |       23 |      20 | Good  |
| 03/11/2023 00:00 |       63 | 28.41        | 22.99       | 03/11/2023 23:07 | 03/11/2023 20:33 |       23 |      20 | Good  |
| 04/11/2023 00:00 |       63 | 26.93        | 22.41       | 04/11/2023 05:07 | 04/11/2023 23:54 |        5 |      23 | Good  |
| 05/11/2023 00:00 |       63 | 28.41        | 23.01       | 05/11/2023 23:22 | 05/11/2023 20:48 |       23 |      20 | Good  |
| 06/11/2023 00:00 |       63 | 27.01        | 22.55       | 06/11/2023 03:12 | 06/11/2023 23:50 |        3 |      23 | Good  |
| 07/11/2023 00:00 |       63 | 28.76        | 21.02       | 07/11/2023 19:16 | 07/11/2023 04:40 |       19 |       4 | Good  |
| 08/11/2023 00:00 |       63 | 28.41        | 22.95       | 08/11/2023 23:35 | 08/11/2023 20:51 |       23 |      20 | Good  |
| 09/11/2023 00:00 |       63 | 28.41        | 23.01       | 09/11/2023 23:53 | 09/11/2023 20:53 |       23 |      20 | Good  |
| 10/11/2023 00:00 |       63 | 28.51        | 23.08       | 10/11/2023 23:26 | 10/11/2023 20:56 |       23 |      20 | Good  |
| 11/11/2023 00:00 |       63 | 27.16        | 22.81       | 11/11/2023 01:26 | 11/11/2023 21:21 |        1 |      21 | Good  |
| 12/11/2023 00:00 |       63 | 28.13        | 22.26       | 12/11/2023 23:55 | 12/11/2023 21:44 |       23 |      21 | Good  |
| 13/11/2023 00:00 |       63 | 28.51        | 23.08       | 13/11/2023 05:47 | 13/11/2023 20:46 |        5 |      20 | Good  |
| 14/11/2023 00:00 |       63 | 28.17        | 22.65       | 14/11/2023 00:06 | 14/11/2023 20:51 |        0 |      20 | Good  |
| 15/11/2023 00:00 |       63 | 28.41        | 22.81       | 15/11/2023 23:43 | 15/11/2023 20:53 |       23 |      20 | Good  |
| 16/11/2023 00:00 |       63 | 28.41        | 22.92       | 16/11/2023 23:23 | 16/11/2023 20:43 |       23 |      20 | Good  |
| 17/11/2023 00:00 |       63 | 28.57        | 22.91       | 17/11/2023 23:24 | 17/11/2023 20:17 |       23 |      20 | Good  |
| 18/11/2023 00:00 |       63 | 27.54        | 22.76       | 18/11/2023 23:51 | 18/11/2023 22:39 |       23 |      22 | Good  |
| 19/11/2023 00:00 |       63 | 28.44        | 22.96       | 19/11/2023 01:31 | 19/11/2023 21:15 |        1 |      21 | Good  |
| 20/11/2023 00:00 |       63 | 28.41        | 22.75       | 20/11/2023 23:37 | 20/11/2023 20:55 |       23 |      20 | Good  |
| 21/11/2023 00:00 |       63 | 28.41        | 22.8        | 21/11/2023 23:33 | 21/11/2023 20:49 |       23 |      20 | Good  |
| 22/11/2023 00:00 |       63 | 28.41        | 22.7        | 22/11/2023 23:50 | 22/11/2023 20:40 |       23 |      20 | Good  |
| 23/11/2023 00:00 |       63 | 28.31        | 22.94       | 23/11/2023 23:27 | 23/11/2023 20:47 |       23 |      20 | Good  |
| 24/11/2023 00:00 |       63 | 28.25        | 22.82       | 24/11/2023 00:07 | 24/11/2023 20:27 |        0 |      20 | Good  |
| 25/11/2023 00:00 |       63 | 28.24        | 21.02       | 25/11/2023 23:58 | 25/11/2023 12:54 |       23 |      12 | Good  |
| 26/11/2023 00:00 |       63 | 28.44        | 22.7        | 26/11/2023 00:48 | 26/11/2023 20:45 |        0 |      20 | Good  |
| 27/11/2023 00:00 |       63 | 28.11        | 22.63       | 27/11/2023 23:44 | 27/11/2023 21:14 |       23 |      21 | Good  |
| 28/11/2023 00:00 |       63 | 28.52        | 22.83       | 28/11/2023 23:40 | 28/11/2023 20:44 |       23 |      20 | Good  |
| 29/11/2023 00:00 |       63 | 28.41        | 22.86       | 29/11/2023 23:34 | 29/11/2023 20:46 |       23 |      20 | Good  |
| 30/11/2023 00:00 |       63 | 28.36        | 22.8        | 30/11/2023 23:31 | 30/11/2023 20:31 |       23 |      20 | Good  |
+------------------+----------+--------------+-------------+------------------+------------------+----------+---------+-------+

Upvotes: 1

Views: 147

Answers (3)

k13
k13

Reputation: 723

You can force a context transition using CALCULATE as shown below.

Valid = CALCULATE(
var h = min(HOUR([timestamp_high]))
var l = min(Hour([timestamp_low]))
return  IF(h > l,"Bad","Good")
)

Upvotes: 0

Sam Nseir
Sam Nseir

Reputation: 12111

Additional info to why it may not be working as expected. When using measures at a row level, you need to give it row-context and this is done by using CALCULATE(...).

So try:

valid = IF(CALCULATE([hourhigh]) > CALCULATE([hourlow]), "Bad", "Good")

Upvotes: 0

davidebacci
davidebacci

Reputation: 30304

I think you're referring to a measure instead of a column. Try correcting to

hourhigh = 
var h = HOUR('Your Table'[timestamp_high])
RETURN h

Incidentally, your code seems to work perfectly for me.

enter image description here

Upvotes: 1

Related Questions