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:
Related
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:
Sample Data
In my data, I have to calculate the current 6 months of usage and prior 6 months of usage. I already have calculations for the current 6 months. Now I need to do the calculations for 6 months Prior to the current 6 months. Can someone have any advice?
Current 6 Months =
CALCULATE (
[Full Price (modified)],
DATESINPERIOD (
'Dates_Jul20-Jun22'[Date],
MAX ( 'Dates_Jul20-Jun22'[Date] ),
-6,
MONTH
)
)
Same with the 3 months prior to the current 3 months.
Current 3 Months =
CALCULATE (
[Full Price (modified)],
DATESINPERIOD (
'Dates_Jul20-Jun22'[Date],
MAX ( 'Dates_Jul20-Jun22'[Date] ),
-3,
MONTH
)
)
Prior n Months =
VAR _priormonths = 12
VAR _currentmonths = 6
VAR _current =
DATESINPERIOD ( 'Table'[Date], MAX ( 'Table'[Date] ), - _currentmonths, MONTH )
VAR _prior =
DATESINPERIOD ( 'Table'[Date], MAX ( 'Table'[Date] ), - _priormonths, MONTH )
RETURN
CALCULATE ( SUM ( 'Table'[Full Price] ), _prior )
- CALCULATE ( SUM ( 'Table'[Full Price] ), _current )
I am new to PowerBi and would need some help figuring out how to make the percentile.exc function work (I cannot use percentile.inc).
I systematically receive the following error: The percentile value should be in the range 1/(N+1)..N/(N+1) inclusive, where N is the number of data values.
The objective of this code is to compute the 3months rolling 90th percentile of a given ratio in another calculated column.
I would be very grateful for any help!
This is what I have come with up so far:
3MonthsRolling_90thPercentile=
VAR current_date = Table1[Date]
VAR k = 0.9
VAR NumValues =
COUNT (Table1[Ratio] )
VAR LowerBound = 1 / ( NumValues + 1 )
VAR UpperBound = NumValues / ( NumValues + 1 )
RETURN
CALCULATE (
IF (AND ( PERCENTILE.EXC ( Table1[Ratio],0.9) >= LowerBound, PERCENTILE.EXC (Table1[Ratio],0.9) <= UpperBound ),PERCENTILE.EXC ( Table1[Ratio],0.9), BLANK()),
FILTER (
ALL (Table1 ),
Table1[Date]
>= DATE ( YEAR ( current_date ), MONTH ( current_date ) - 3, DAY ( current_date ) )
&& Table1[Date] <= current_date
)
)
If NumValues is less than 9 in, then 0.9 is above your upper bound NumValues / ( NumVales + 1 ), so PERCENTILE.EXC ( Table1[Ratio], 0.9 ) will throw an error.
I think your measure would make more sense as follows:
3MonthsRolling_90thPercentile =
VAR current_date = Table1[Date]
VAR DateFiltered =
FILTER (
ALL ( Table1 ),
Table1[Date]
>= DATE (
YEAR ( current_date ),
MONTH ( current_date ) - 3,
DAY ( current_date )
)
&& Table1[Date] <= current_date
)
VAR k = 0.9
VAR NumValues = CALCULATE ( COUNT ( Table1[Ratio] ), DateFiltered )
VAR LowerBound = 1 / ( NumValues + 1 )
VAR UpperBound = NumValues / ( NumValues + 1 )
RETURN
IF (
AND ( k >= LowerBound, k <= UpperBound ),
CALCULATE ( PERCENTILE.EXC ( Table1[Ratio], k ), DateFiltered )
)
Note that I'm comparing k to the bounds rather than the k-percentile.
I have a measure in which i am dividing total number of contractual months from total number of months. I am getting correct result, but the total in the bottom is not correct.
The first column is a unique ID, Third and fourth columns are numerator and denominator, Second column is the result of the division, I want to count those IDs, where the division is between 0.75 and 1.00
Here are my calculations
Var Check=DIVIDE([Month of Engagement],[Months In Contract L30])
RETURN
IF(HASONEVALUE('Fact - TABLE'[ID]),IF(Check>=0.75 && Check<=1.00,DISTINCTCOUNT(ID),0),SUMX('Fact - TABLE',IF(Check>=0.75 && Check<=1.00,DISTINCTCOUNT(ID),0)))
Please let me know, how to solve this.
For Total, you should change 3th parameter of IF to:
CALCULATE (
SUMX (
CALCULATETABLE (
tab,
FILTER (
ALL ( tab ),
VAR __Check =
DIVIDE ( 'Tab'[Month of Engagement], Tab[Months In Contract L30] )
RETURN
__Check < 1
&& __Check > 0.75
)
),
1
)
)
Below my test example (for testing only I have put check variable in first IF
IF ( Check >= 0.75 && Check <= 1.00, Check, 0 ) to see if check calculation is correct):
Measure =
VAR Check =
DIVIDE (
SUM ( 'Tab'[Month of Engagement] ),
SUM ( Tab[Months In Contract L30] )
)
RETURN
IF (
HASONEVALUE ( 'Tab'[ID] ),
IF ( Check >= 0.75 && Check <= 1.00, Check, 0 ),
CALCULATE (
SUMX (
CALCULATETABLE (
tab,
FILTER (
ALL ( tab ),
VAR __Check =
DIVIDE ( 'Tab'[Month of Engagement], Tab[Months In Contract L30] )
RETURN
__Check < 1
&& __Check > 0.75
)
),
1
)
)
)
Measure =
VAR Check =
DIVIDE (
[Month of Engagement] ,
[Months In Contract L30]
)
RETURN
IF (
HASONEVALUE ( 'Tab'[ID] ),
IF ( Check >= 0.75 && Check <= 1.00, Check, 0 ),
CALCULATE (
SUMX (
CALCULATETABLE (
'Tab',
FILTER (
ALL ( 'Tab' ),
VAR __Check =
DIVIDE ( [Month of Engagement], [Months In Contract L30] )
RETURN
__Check >=0.75
&& __Check <=1.00
)
),
1
)
)
)
Here are the brief definitions
Month of Engagement:= CALCULATE(COUNTROWS(
SUMMARIZE('table','table'[MONTH_OF_ENGAGEMENT],"Count",DISTINCT('table'[MONTH_OF_ENGAGEMENT]))),'table'[FREQUENCY_FLAG]="Y")+0
Here MONTH_OF_ENGAGEMENT is value extracted from a date as YYYYMM
Months In Contract L30:=
Var DBRefreshDate30= [Database Refresh Date]-30
RETURN
DATEDIFF(DBRefreshDate30,[Database Refresh Date],MONTH)+1
Now basis on dividing Month of Engagement from Months In Contract L30, i am deriving a percentage, and checking if that percentage lies between 0.75 and 1.00
I have a data structure like this
DateRoll Dataset Date Value Customer
Month Online 1/1/2018 10 Cust1
Month Online 2/1/2018 11 Cust1
Month Online 3/1/2018 12 Cust1
Month Online 4/1/2018 22 Cust1
Quarter Online 1/1/2018 33 Cust1
Quarter Online 4/1/2018 22 Cust1
I have to calculate previous quarter value, I tried different ways but it's not working
1 - Not returning any value.
CALCULATE (
SUM ( 'Data_Rollup_KPI_DNR'[Value] ),
DATEADD ( 'Data_Rollup_KPI_DNR'[Date].[Date], -1, QUARTER ),
FILTER ( Data_Rollup_KPI_DNR, Data_Rollup_KPI_DNR[DateRoll] = "Quarter")
)
2--Nested - Returning overall total
CALCULATE (
CALCULATE (
SUM ( 'Data_Rollup_KPI_DNR'[Value] ),
DATEADD ( 'Data_Rollup_KPI_DNR'[Date].[Date], -1, QUARTER )
),
FILTER ( Data_Rollup_KPI_DNR, Data_Rollup_KPI_DNR[DateRoll] = "Quarter" )
)
3--Nested --Returning overall total
CALCULATE (
CALCULATE (
SUM ( 'Data_Rollup_KPI_DNR'[Value] ),
FILTER ( Data_Rollup_KPI_DNR, Data_Rollup_KPI_DNR[DateRoll] = "Quarter" )
),
DATEADD ( 'Data_Rollup_KPI_DNR'[Date].[Date], -1, MONTH )
)
Tried PREVIOUSQUARTER function too, but its not returning any value.
To take advantage of built in DAX time intelligence functions you will need to to have a contiguous set of dates. I would recommend using a date table. The following code can be used to create a date/calendar table in your model:
Celndar =
Var MinDate = MIN(Data_Rollup_KPI_DNR[Date])
Var MaxDate = MAX(Data_Rollup_KPI_DNR[Date])
Var BaseCalendar = CALENDAR(MinDate, MaxDate)
RETURN
GENERATE (
BaseCalendar,
VAR BaseDate = [Date]
VAR YearDate =
YEAR ( BaseDate )
VAR MonthNumber =
MONTH ( BaseDate )
VAR YrMonth =
100 * YEAR ( BaseDate )
+ MONTH ( BaseDate )
VAR Qtr =
CONCATENATE ( "Q", CEILING ( MONTH ( BaseDate ) / 3, 1 ) )
RETURN
ROW (
"Day", BaseDate,
"Year", YearDate,
"Month Number", MonthNumber,
"Month", FORMAT ( BaseDate, "mmmm" ),
"Year Month", FORMAT ( BaseDate, "mmm yy" ),
"YrMonth", YrMonth,
"Qtr", Qtr
)
)
Once this table exists, mark it as a 'date' table and create a relationship with
Data_Rollup_KPI_DNR[Date]
Then, you can write the following measure to obtain the results you are searching for:
PQSum =
CALCULATE (
SUM ( 'Data_Rollup_KPI_DNR'[Value] ),
PREVIOUSQUARTER ( 'Calendar'[Date] )
)
Hope that helps!
*Edited
You can also create a ranking column to index in a measure:
Rank =
RANKX (
FILTER (
'Data_Rollup_KPI_DNR',
'Data_Rollup_KPI_DNR'[DateRoll] = EARLIER ( 'Data_Rollup_KPI_DNR'[DateRoll] )
),
'Data_Rollup_KPI_DNR'[Date].[Date],
,
ASC
)
Then you can reference a previous quarter using something like the following:
PQSum2 =
CALCULATE (
SUM ( 'Data_Rollup_KPI_DNR'[Value] ),
FILTER (
'Data_Rollup_KPI_DNR',
'Data_Rollup_KPI_DNR'[Rank]
= MAX ( 'Data_Rollup_KPI_DNR'[Rank] ) - 1
),
'Data_Rollup_KPI_DNR'[DateRoll] = "Quarter"
)
but this is hard coded and just plain nasty!
Echoing #steliok that a date dimension is the proper way to handle this; there are plenty of date table templates out there, and a date dimension will work with your data model. If you really really can't add to your data structure for some reason, this should work:
BaseValue = SUM ( 'Data_Rollup_KPI_DNR'[Value] )
PriorQuarter =
VAR CurrentDate = MAX ( 'Data_Rollup_KPI_DNR'[Date] )
VAR CurrentYear = YEAR ( CurrentDate )
VAR CurrentMonth = MONTH ( CurrentDate )
VAR FirstMonthOfCurrentQuarter =
SWITCH (
TRUE (),
CurrentMonth IN {1,2,3}, 1,
CurrentMonth IN {4,5,6}, 4,
CurrentMonth IN {7,8,9}, 7,
CurrentMonth IN {10,11,12}, 10
)
// DATE() does the right thing with negative month args
VAR PriorQuarterDate = DATE ( CurrentYear, FirstMonthOfCurrentQuarter - 3, 1 )
RETURN
CALCULATE (
[BaseValue],
ALL ( 'Data_Rollup_KPI_DNR'[DateRoll], 'Data_Rollup_KPI_DNR'[Date] ),
'Data_Rollup_KPI_DNR'[Date] = PriorQuarterDate,
'Data_Rollup_KPI_DNR'[DateRoll] = "Quarter"
)
This relies on DATE being clever, which it is. DATE ( 2019, -2, 1 ) = DATE ( 2018, 10, 1 ).
Ultimately, my question is why can't you just source the un-rolled up data from the same place that the ETL process is sourcing it?
Date functions are working well when you are using # Day level.
Following link would be helpful to resolve your issue,
https://community.powerbi.com/t5/Desktop/Lead-and-Lag-in-DAX/td-p/649162