How to subtract values between two particular dates in Powerbi? - powerbi

I have a table like this:
DeviceID
SignalID
Value
DateTime
1
22
1032223
01.10.2022 00:00:00
2
24
1923892
01.10.2022 00:00:00
3
33
3434342
01.10.2022 00:00:00
4
33
3232323
01.10.2022 00:00:00
.......
......
......
......
1
33
4155151
05.12.2022 15:36:38
I have a few devices with each device monitoring signals. The signal value here represents some counter value. The counter are running continously but I need their relative value as in from 01.10.2022.
What I want to calculate :
The value of the signalID x of device y = (value now) - (value on 01.10.2022 00:00:00).
How can I acheive this for all the value of the signalID of all the devices?

You can test this, and let me know if It solves your problem!
15Seconds_Cardinality =
VAR NowTime =
NOW ()
VAR FixedTime =
DATE ( 2022, 10, 01 ) + TIME ( 0, 0, 0 )
VAR Value_On_FixedDate =
CALCULATE (
MIN ( Signals[Value] ),
YEAR ( Signals[DateTime] ) = YEAR ( FixedTime ),
MONTH ( Signals[DateTime] ) = MONTH ( FixedTime ),
DAY ( Signals[DateTime] ) = DAY ( FixedTime ),
HOUR ( Signals[DateTime] ) = HOUR ( FixedTime ),
MINUTE ( Signals[DateTime] ) = MINUTE ( FixedTime ),
ROUND ( SECOND ( Signals[DateTime] ) / 15, 0 )
= ROUND ( SECOND ( FixedTime ) / 15, 0 )
)
VAR Value_On_Now =
CALCULATE (
MAX ( Signals[Value] ),
YEAR ( Signals[DateTime] ) = YEAR ( NowTime ),
MONTH ( Signals[DateTime] ) = MONTH ( NowTime ),
DAY ( Signals[DateTime] ) = DAY ( NowTime ),
HOUR ( Signals[DateTime] ) = HOUR ( NowTime ),
MINUTE ( Signals[DateTime] ) = MINUTE ( NowTime ),
ROUND ( SECOND ( Signals[DateTime] ) / 15, 0 )
= ROUND ( SECOND ( NowTime ) / 15, 0 )
)
RETURN
Value_On_Now - Value_On_FixedDate
Same above code with image file:

Related

Sum of values in hourly range

I have the following table
Date
Hour
Value
2022-11-17
15:21:00
1
2022-11-17
22:19:00
2
2022-11-18
00:44:00
2
2022-11-18
04:27:00
3
2022-11-18
13:21:00
2
2022-11-18
23:55:00
1
2022-11-19
09:01:00
4
Now I would like to sum the values based on the day and the specified hour range
It should sum from hour 6:00 of the previous day to hour 6:00 of today.
For example: So the first four rows should be the sum of 2022-11-17 and so on.
Date (6-6)
Sum of values
2022-11-17
8
2022-11-18
3
2022-11-19
4
At the moment I can sum values between hours, but on the same day
Usage =
CALCULATE(
SUM(Table[Value]),
TIME(
HOUR(Table[Hour]),
MINUTE(Table[Hour]),
SECOND(Table[Hour])
) >= TIMEVALUE("02:00:00"),
TIME(
HOUR(Table[Hour]),
MINUTE(Table[Hour]),
SECOND(Table[Hour])
) <= TIMEVALUE("14:00:00")
)
`
You can try this measure:
Usage =
VAR _min_time = SELECTEDVALUE ( 'Table'[Date] ) + TIME ( 6, 0, 0 )
VAR _max_time = SELECTEDVALUE ( 'Table'[Date] ) + 1 + TIME ( 6, 0, 0 )
RETURN
IF (
ISINSCOPE ( 'Table'[Date] ) ,
CALCULATE (
SUM ( 'Table'[Value] ) ,
FILTER (
ALL ( 'Table' ) ,
VAR _time = [Date] + [Hour]
RETURN _min_time < _time && _time <= _max_time
)
)
)
Calculates your desired result:

PowerBI RANKX is not continious

I want to show the TOP10 difference in a measure.
The difference is calculated YTD actual + Rest of the year forecast - Full year budget values.
The normal measure looks like this:
VAR _Year =
SELECTEDVALUE ( 'Calendar'[Year] )
RETURN
(
CALCULATE (
SELECTEDMEASURE (),
DATESYTD ( Calendar[Dates] ),
CRDB[Scenario] = "Actual",
ALL ( CRDB[ForecastTypeFinal] )
)
+ CALCULATE (
SELECTEDMEASURE (),
CRDB[Scenario] = "Forecast",
'Calendar'[Dates] >= DATE ( _Year, 1, 1 )
&& 'Calendar'[Dates] <= DATE ( _Year, 12, 31 )
)
)
- CALCULATE (
SELECTEDMEASURE (),
Calendar[Dates] >= DATE ( _Year, 1, 1 )
&& Calendar[Dates] <= DATE ( _Year, 12, 31 ),
CRDB[Scenario] = "Budget",
ALL ( CRDB[ForecastTypeFinal] )
)
I would like to rank by project, so I made this ranking measure:
RANKX (
ALL ( CRDB[Project ID - Project ID Level 01 (Text)] ),
(
CALCULATE (
SELECTEDMEASURE (),
DATESYTD ( Calendar[Dates] ),
CRDB[Scenario] = "Actual",
ALL ( CRDB[ForecastTypeFinal] )
)
+ CALCULATE (
SELECTEDMEASURE (),
CRDB[Scenario] = "Forecast",
'Calendar'[Dates] >= DATE ( SELECTEDVALUE ( 'Calendar'[Year] ), 1, 1 )
&& 'Calendar'[Dates] <= DATE ( SELECTEDVALUE ( 'Calendar'[Year] ), 12, 31 )
)
)
- CALCULATE (
SELECTEDMEASURE (),
Calendar[Dates] >= DATE ( SELECTEDVALUE ( 'Calendar'[Year] ), 1, 1 )
&& Calendar[Dates] <= DATE ( SELECTEDVALUE ( 'Calendar'[Year] ), 12, 31 ),
CRDB[Scenario] = "Budget",
ALL ( CRDB[ForecastTypeFinal] )
),
,
DESC
))
The ranking gets me the correct projects, but when I look at the rank values, they are not right:
ranking values
The values are not the same, so it is no reason for skipping places.
What do I do wrong?
Thank you for your help in advance.

DAX PBI Current month minus Previous month

I have a measure:
_Forecast, % =
VAR ForecastMinimumPercent = 10
VAR ForecastIncreasePercent = 15
VAR sM =
MAX ( T1[PlanDate] ) //Plan Date
VAR fM =
MAX ( '_Date2'[Date] ) //fact Date
VAR sC =
FIRSTNONBLANK ( T1[ProjectCode], 1 ) //Current project code
VAR tP =
//Current forecast cumulative percent
CALCULATE (
SUM ( T1[ForecastPercent] ),
FILTER (
ALLSELECTED ( 'T1' ),
T1[PlanDate] <= fM
&& T1[PlanDate] <= sM
&& T1[ProjectCode] = sC
),
REMOVEFILTERS ( T1[PlanDate] )
)
VAR mM =
// Current Project first Date
CALCULATE (
MIN ( T1[PlanDate] ),
FILTER ( ALLSELECTED ( 'T1' ), T1[ProjectCode] = sC ),
REMOVEFILTERS ( T1[PlanDate] )
)
VAR nD =
ROUNDUP ( DIVIDE ( 100 - tP, ForecastIncreasePercent, 0 ), 0 ) // count of forecast columns
VAR fD =
DATEDIFF ( sM, fM, MONTH ) //dates offsets, '_Date2'[Date] - T1[PlanDate]
VAR P1 = tP + ForecastIncreasePercent * fD //forecasted percent
VAR P2 =
IF ( P1 > 100, 100, P1 ) //does not show values over 100, for example 90+15=105 but show 100
VAR fP =
SWITCH (
TRUE (),
tP < ForecastMinimumPercent, BLANK (),
// if percent <10, then hide
fM < mM, BLANK (),
//hide values if T1[PlanDate] <> '_Date2'[Date]
MAX ( T1[ForecastPercent] ) = 0, BLANK (),
// hide empty percent rows
fM
> DATE ( YEAR ( sM ), MONTH ( sM ) + nD + 1, DAY ( sM ) ), BLANK (),
//does not show 100 more than once
fM <= sM, IF ( HASONEVALUE ( T1[PlanDate] ), BLANK (), tP ),
//if collapsed show first fact values
P2
)
RETURN
fP
This measure give me current forecast percent
But i need get difference current month - previous
How to write right syntax?

Find daily slope OR previous minus current week difference

My data:
week
sale
20
50
20
20
21
10
21
10
22
5
22
5
Desired result:
week
sale
change
20
50
any
20
20
any
21
10
-50
21
10
-50
22
5
-10
22
5
-10
Where:
Week 20 total = 70,
Week 21 total = 20,
Week 22 total = 10
Diff for week 21 = 20-70 = -50
Diff for week 22 = 10-20 = -10
Note: "any" = can be anything (i.e. 0 or null)
Calculated column
Column 2 =
VAR _1 =
CALCULATE ( MAX ( 'Table'[week] ) ) - 1
VAR _2 =
SUMX ( FILTER ( ALL ( 'Table' ), 'Table'[week] = _1 ), 'Table'[sale] )
VAR _3 =
CALCULATE ( SUM ( 'Table'[sale] ), ALLEXCEPT ( 'Table', 'Table'[week] ) )
VAR _4 =
IF ( ISBLANK ( _2 ) = TRUE (), BLANK (), _3 )
RETURN
_4
Measure
currentWeekTotal:= SUM('Table'[sale])
prevWeekTotal:=
VAR _1 = MAX('Table'[week])-1
VAR _2 = SUMX(FILTER(ALL('Table'),'Table'[week]=_1),'Table'[sale])
RETURN _2
change:= IF([prevWeekTotal]=BLANK(),BLANK(),[currentWeekTotal]-[prevWeekTotal])
Here is a simple code for a calculated column :
change =
VAR _firstweek = MIN ( 'Table'[week] )
VAR _thisweek = 'Table'[week]
VAR _thisweeksales = CALCULATE ( SUM ( 'Table'[sale] ) , ALLEXCEPT ( 'Table' , 'Table'[week] ) )
VAR _lastweeksales = CALCULATE ( SUM ( 'Table'[sale] ) , ALL ( 'Table' ) , 'Table'[week] = _thisweek - 1 )
VAR _diff = IF ( _thisweek <> _firstweek , _thisweeksales - _lastweeksales )
RETURN _diff
This also handles skipped weeks that are not the first week.

Tool recommendation for data transform

I have large amounts of raw fault data in Power BI.
code time status
x123 2019-04-22T23:57:00 ok
x123 2019-04-23T01:00:00 faulty
x123 2019-04-23T02:00:00 ok
x123 2019-04-23T23:00:00 faulty
x123 2019-04-24T01:00:00 ok
I need to transform this to show how long an item has been in a faulty state on a given day. So on the 23rd, the item was in a faulty state between 1 and 2a.m and then again between 11pm until past midnight.
code day % of day faulty
x123 23/04/2019 8.30% (2 hours)
Can I do this easily in Power BI or should I use another tool such as Azure Data Factory?
Add the following Calculated Columns to your table:
Report Date = Table1[time].[Date]
Fault Duration =
VAR CurrentTime = Table1[time]
VAR CurrentCode = Table1[code]
VAR PreviousTime =
CALCULATE (
MAX ( Table1[time] ),
FILTER (
Table1,
Table1[time] < CurrentTime &&
Table1[code] = CurrentCode
)
)
VAR NextTime =
CALCULATE (
MIN ( Table1[time] ),
FILTER (
Table1,
Table1[time] > CurrentTime &&
Table1[code] = CurrentCode
)
)
VAR FaultyFrom =
IF(
Table1[status] = "faulty",
Table1[time],
IF (
DAY(PreviousTime) = DAY(Table1[time]),
BLANK(),
Table1[time].[Date]
)
)
VAR FaultyTo =
IF (
Table1[status] = "ok",
Table1[time],
IF (
DAY(NextTime) = DAY(Table1[time]),
NextTime,
Table1[time].[Date] + 1
)
)
RETURN
IF(
ISBLANK ( PreviousTime ) || ISBLANK ( NextTime ) || ISBLANK ( FaultyFrom ),
BLANK(),
FaultyTo - FaultyFrom
)
Now create measures:
Faulty Hours = SUM ( Table1[Fault Duration] )
Faulty % Day =
IF (
HASONEVALUE ( Table1[Report Date] ),
DIVIDE (
[Faulty Hours],
DISTINCTCOUNT ( Table1[code] ),
BLANK()
),
BLANK()
)
Output:
See https://pwrbi.com/so_55825688/ for a worked example PBIX file