Time bucket in power bi - powerbi

Time bucket
column dax query:
Column = if(Table1[TransactionDate].[Date]=Table1[COBProcessDate].[Date],
SWITCH (
TRUE (),
HOUR ( Table1[COBProcessDate] )
+ MINUTE ( Table1[COBProcessDate]) / 60
>= 6
&& HOUR ( Table1[COBProcessDate] )
+ MINUTE ( Table1[COBProcessDate] ) / 60
< 8.5, "6:00 a.m. - 8:30 a.m.",
HOUR ( Table1[COBProcessDate])
+ MINUTE ( Table1[COBProcessDate]) / 60
>= 8.5
&& HOUR ( Table1[COBProcessDate])
+ MINUTE ( Table1[COBProcessDate]) / 60
< 9.5, "8:30 a.m. - 9:30 a.m",
HOUR ( Table1[COBProcessDate].[Date] )
+ MINUTE ( Table1[COBProcessDate]) / 60
>= 9.5
&& HOUR ( Table1[COBProcessDate])
+ MINUTE ( Table1[COBProcessDate] ) / 60
< 12, "9:30 a.m. to noon",
HOUR ( Table1[COBProcessDate] )
+ MINUTE ( Table1[COBProcessDate] ) / 60
>= 12
&& HOUR ( Table1[COBProcessDate])
+ MINUTE ( Table1[COBProcessDate]) / 60
< 15.5, "noon to 3:30 p.m.",
HOUR ( Table1[COBProcessDate].[Date] )
+ MINUTE ( Table1[COBProcessDate] ) / 60
>= 15.5
&& HOUR ( Table1[COBProcessDate] )
+ MINUTE ( Table1[COBProcessDate] ) / 60
< 18, "3:30 p.m. to 6:00 p.m.",
"6:00 p.m. - 6:00 a.m."),"Next Day")
I am not able to arrange time in correct series . like 6:30a.m-8:30a.m then 8:30a.m to 9:30a.m then noon to 6:30p.m then 6:30pm to 11:59 pm then next day.
screen shot is attached.
thanks in advance.

by default the column values are alphabetically ordered, if you want or have to change this, you have to complete the following steps:
Create another calculated column "column index"
The column values will be used to order the column values of your 1st column.
The result will look something like this
... | column | column index
... | 6:00 a.m. - 8:30 a.m. | 1
... | 8:30 a.m. - 9:30 a.m. | 2
...
You can use the exact same DAX formula, but instead returning a string you will return a numeric value that represents the sort index
Sort "Column" by "Sort Column"
In the "Data-View" mark the column you want to order, switch to the "Modeling" ribbon and select "Sort by Column" - select the column that contains the numeric values.
Hide the index column
To avoid clutter in the Fields pane I always hide my index columns.
Hopefully this is what you are looking for
Regards
Tom

Related

Need a DAX expression to calculate Rate of Success

I have a table called Payment_Methods in PBI with the following columns:
timestamp, weekDay, Hour, eventType, SuccessEvents, FailedEvents, addPymntCardInstance.
weekDay is ['Sunday', 'Monday', ..., 'Friday', 'Saturday']
Hour is [0, 1, 2, 3, ..., 21, 22, 23]
I want to create a PymntSuccessRate using the DAX expression below, but the results are always the same all the way down across the column PymntSuccessRate. How can I fix this. Appreciate if you can give me some clue. Thanks.
PymntSuccessRate =
DIVIDE(
SUMX(
FILTER(
GROUPBY(
Payment_Methods,
Payment_Methods[timestamp], Payment_Methods[weekDay], Payment_Methods[Hour]
),
Payment_Methods[eventType] = "Success"
),
SUM( Payment_Methods[addPymntCardInstance] )
),
SUMX(
GROUPBY(
Payment_Methods,
Payment_Methods[timestamp], Payment_Methods[weekDay], Payment_Methods[Hour]
),
SUM( Payment_Methods[addPymntCardInstance] )
)
)
timestamp
weekDay
Hour
SuccessEvents
FailedEvents
SuccessRate
2023-01-20
Friday
20
2
8
0.20
2023-01-20
Friday
19
121
111
0.52
2023-01-17
Tuesday
6
31
8
0.79
2023-01-17
Tuesday
5
19
14
0.57
based on your sample you don't need to use SUMX and GROUPBY (visual will do the grouping), this should be enough (used as a measure, NOT a calculated column):
PymntSuccessRate =
DIVIDE (
CALCULATE (
SUM ( Payment_Methods[addPymntCardInstance] ),
Payment_Methods[eventType] = "Success"
),
SUM ( Payment_Methods[addPymntCardInstance] )
)

Powerbi: How to calculate average value per hour from value in minutes?

So I am building a report that shows the time spent on a job and the income the job has generated. My boss wants to see the average income of a job in hours.
Let's say three jobs have been completed:
Job A: Time: 12 minutes & income 450 euro
Job B: Time: 24 minutes & income 600 euro
Job C: Time: 38 minutes & income 950 euro
Job D: Time: 82 minutes & income 1800 euro
How do i calculate the average income per hour in PowerBI/DAX?
If you want to do it in a structured way:
DEFINE
TABLE Payroll = SELECTCOLUMNS ({
("Job-A",FORMAT(TIME(00,12,00),"HH:MM:SS"),450),
("Job-B",FORMAT(TIME(00,24,00),"HH:MM:SS"),600),
("Job-C",FORMAT(TIME(00,38,00),"HH:MM:SS"),950),
("Job-D",FORMAT(TIME(00,82,00),"HH:MM:SS"),1800)
},"Type",[Value1],"Duration",[Value2],"Income",[Value3])
EVALUATE
Payroll
Final Code:
EVALUATE
ROW (
"AVG_Earnings",
FORMAT (
ROUND (
AVERAGEX (
ADDCOLUMNS (
ADDCOLUMNS (
Payroll,
"Hour", HOUR ( Payroll[Duration] ),
"Minute", MINUTE ( Payroll[Duration] ),
"Seconds", SECOND ( Payroll[Duration] )
),
"Total",
ROUND ( [Hour] + DIVIDE ( [Minute], 60 ) + DIVIDE ( [Seconds], 3600 ), 4 )
),
DIVIDE ( [Income], [Total] )
),
2
),
"Currency",
"de-DE"
)
)

Determine if 2 date columns are <= today and <= end of the next month in Power BI / DAX measure with IF statement

I have 2 columns that includes 2 different dates.
What i'm trying to do is to create a calculated column that determines whether column "governance" is <= TODAY and the "contract issue" column is between the start and end of the next month( only March dates). with either "yes" or "no" in the if statement.
i've tried the following measure:
IF ( 'Table'[Governance] <= TODAY() && 'Table'[Contract Issue] <= EOMONTH ( Today(), 1 ), "yes", "No" )
but that returns all contract issues before end of the next month, what do i use to show the contract issue date between start of the next month and end of the next month?
Sample table below
Governance
Contract Issue
14/01/2022
04/03/2022
04/02/2022
11/03/2022
Much thanks in advance
It seems you just need to compare the date to the start of the month.
mymeasure = IF (
'Table'[Governance] <= TODAY ()
&& 'Table'[Contract Issue] <= EOMONTH ( TODAY (), 1 )
&& 'Table'[Contract Issue]
>= DATE ( YEAR ( 'Table'[today] )
+ IF ( MONTH ( 'Table'[today] ) = 12, 1, 0 ), MONTH ( 'Table'[today] )
+ IF ( MONTH ( 'Table'[today] ) = 12, -11, 1 ), 1 ),
"yes",
"No"
)
You'll need a "today" column, too. Do that in Power Query using M:
Date.From(DateTime.LocalNow())

PowerBI calculate overflow backlog in measure

My data is as follows:
Factory ID
Week
CAPACITY
Request
1
21
1000
500
1
22
1000
1200
1
23
1500
1600
1
24
1500
1100
2
21
1000
500
2
22
2000
1900
2
23
2000
1000
2
24
2000
2500
3
21
1000
200
3
22
1000
900
3
23
1000
1300
3
24
1000
800
I want to calculate backlog in a measure or any other way to have backlog be dynamic based on the factories I select. Backlog is calculated as follows:
Backlog = Capacity - (Request + Previous week backlog); where we have backlog when requests + pre. week backlog exceeds capacity or else it is 0. I cannot move capacity from future weeks, so the backlog would always accumulate going forward
Eg. If I select Factory 1, my backlog should look as follows:
Factory Selected: 1
Week
Backlog
21
0
22
-200
23
-300
24
-100
Factory Selected: 1,2
Week
Backlog
21
0
22
-100
23
0
24
-400
Factory Selected: 1,3
Week
Backlog
21
0
22
-100
23
-500
24
0
I have been trying to find a solution since the last 2 days. Let me know if you need any additional details. Any help will be greatly appreciated.
In DAX there are no loops and no recursion, this means that we must write some very ugly loop unrolling dax code. This is a minimal implementation of a working measure, but to make it working in the general case the BacklogWeekNN variables must be added until reaching the maximum possible week depth in the model.
Backlog =
VAR MinWeek = CALCULATE(MIN( T[Week] ), REMOVEFILTERS( T ) )
VAR MaxWeek = MAX( T[Week] )
VAR TAggregated =
ADDCOLUMNS(
CALCULATETABLE( VALUES( T[Week] ), T[Week] <= MaxWeek, REMOVEFILTERS( T ) ),
"#Capacity", CALCULATE( SUM( T[CAPACITY] ), ALLEXCEPT( T, T[Week], T[Factory ID] ) ),
"#Request", CALCULATE( SUM( T[Request] ), ALLEXCEPT( T, T[Week], T[Factory ID] ) )
)
VAR BacklogWeek00 = SUMX( FILTER( TAggregated, T[Week] = MinWeek ), [#Capacity] - [#Request] ) + 0
VAR BacklogWeek01 = SUMX( FILTER( TAggregated, T[Week] = MinWeek + 1), [#Capacity] - [#Request] ) + IF(BacklogWeek00 > 0, 0, BacklogWeek00)
VAR BacklogWeek02 = SUMX( FILTER( TAggregated, T[Week] = MinWeek + 2), [#Capacity] - [#Request] ) + IF(BacklogWeek01 > 0, 0, BacklogWeek01)
VAR BacklogWeek03 = SUMX( FILTER( TAggregated, T[Week] = MinWeek + 3), [#Capacity] - [#Request] ) + IF(BacklogWeek02 > 0, 0, BacklogWeek02)
VAR Result = IF(BacklogWeek03 > 0, 0, BacklogWeek03)
RETURN Result
This way we can obtain the desired resulting matrix using a slicer to select the factories (I'm afraid there are a few errors in the expected result samples int the question)
Edit: I used 'FILTER( TAggregated, T[Week] = MinWeek )' instead of the equivalent CALCULATE/CALCULATTABLE DAX code to avoid context transition happening and because the weeks table has very few rows to be iterated (I can imagine a maximum of a few tens or at maximum hundreds of rows if keeping a few years history)

Show total Minute Utilization for An hour in PowerBI report

I am trying to do log(ssrs execution log) analysis is powerbi. Requirement here is to show how many minutes utilized for a particular hour based on the request start & end time. Below is example for 4 requests Start & end time with expected result.
1st request 12:00 AM - 12:15 AM
2nd request 12:05 AM - 12:10 AM
3rd request 12:40 AM - 12:42 AM
4th request 12:41 AM - 12:48 AM
So total minute utilization for 12 AM hour should be 15mins(as first two requests overlap with each other) + 8mins (as last two also overlap for some mins) = 23 mins of total utilization at 12 AM.
Any help will be greatly appreciated.
I'd recommend splitting up the hour into 60 minutes and counting how many of the minutes are within the time frame of one of the requests.
Something like this logic for a calculated column:
Utilization =
VAR CurrentHour = HOUR ( Requests[Start] )
VAR Minutes =
GENERATESERIES (
TIME ( CurrentHour, 0, 0 ),
TIME ( CurrentHour + 1, 0, 0 ),
TIME ( 0, 1, 0 )
) /*This generates a column named [Value] with 61 rows
starting from the beginning of the hour.*/
RETURN
SUMX (
Minutes,
IF (
COUNTROWS (
FILTER (
Requests,
HOUR ( Requests[Start] ) = CurrentHour
&& Requests[Start] < [Value]
&& Requests[End] > [Value]
)
) > 0,
1,
0
)
)