Forum Discussion
Power Bi Calendar with Shifts assigned
- Anonymous1 year ago
Hi sandeepsai,
For your question, we have made a variety of attempts, we have found a way is to shift into two categories, two and two combinations of combinations with the partter ID, can ensure that we carry out a dynamic filtering, and finally use if statements to achieve your needs.Shift Measure = VAR SelectedPattern = SELECTEDVALUE('Table'[PatternID], 1) -- Gets the selected PatternID (default to 1 if none is selected) VAR CurrentWeek = MAX('calendar'[WeekOfCycle]) -- Gets the current week of cycle VAR CurrentDay = FORMAT(MAX('calendar'[Day]), "ddd") -- Gets the current day in abbreviated format (e.g., "Mon", "Tue") VAR CurrentHour = HOUR(MAX('calendar'[Timestamp])) -- Gets the current hour from the Timestamp RETURN IF( CurrentHour >= 6 && CurrentHour < 18, -- Day shift hours (6:00 to 18:00) CALCULATE( SELECTEDVALUE('Table'[Shift 1(combine 1 and 3)]), -- Day Shift for Shift 1 'Table'[PatternID] = SelectedPattern, 'Table'[Week] = CurrentWeek, LEFT('Table'[Day], 3) = CurrentDay, 'Table'[Shift 1(combine 1 and 3)]="D"||'Table'[Shift 1(combine 1 and 3)]="X" ), IF( CurrentHour < 6 , -- Night shift hours (18:00 to 6:00) CALCULATE( SELECTEDVALUE('Table'[Shift 3 (combine 2 and 4)]), -- Night Shift for Shift 3 'Table'[PatternID] = SelectedPattern, 'Table'[Week] = CurrentWeek, LEFT('Table'[Day], 3) = CurrentDay, 'Table'[Shift 3 (combine 2 and 4)]="D"||'Table'[Shift 3 (combine 2 and 4)]="N" ), IF(CurrentHour>=18, CALCULATE( SELECTEDVALUE('Table'[Shift 3 (combine 2 and 4)]), -- Night Shift for Shift 3 'Table'[PatternID] = SelectedPattern, 'Table'[Week] = CurrentWeek, LEFT('Table'[Day], 3) = CurrentDay, 'Table'[Shift 3 (combine 2 and 4)]="D"||'Table'[Shift 3 (combine 2 and 4)]="N" ), BLANK() ) ) )If you have questions you can check my pbix file, if you have further needs, you can change my pbix file on your own to realize your own latest needs, I hope my thinking can solve your problem, if I can help you solve the problem I will feel honored!
Hope it helps!
Best regards,
Community Support Team_ Tom ShenIf this post helps then please consider Accept it as the solution to help the other members find it more quickly.
I've created a shift table manually for your 4-week pattern. For example, using your pattern:
- N = Night Shift (18:30 - 06:30)
- D = Day Shift (06:30 - 18:30)
- X = Day Off
Assuming a simple fixed start date, like November 4, 2024, you can create a basic shifts table in Power BI:
- Click on "New Table".
- Use DAX formula below to create a shifts table:
Shifts =
UNION(
ROW("Date", DATE(2024, 11, 4), "Shift", "N"),
ROW("Date", DATE(2024, 11, 5), "Shift", "N"),
ROW("Date", DATE(2024, 11, 6), "Shift", "X"),
ROW("Date", DATE(2024, 11, 7), "Shift", "X"),
ROW("Date", DATE(2024, 11, 8), "Shift", "N"),
ROW("Date", DATE(2024, 11, 9), "Shift", "N"),
ROW("Date", DATE(2024, 11, 10), "Shift", "N"),
ROW("Date", DATE(2024, 11, 11), "Shift", "X"),
ROW("Date", DATE(2024, 11, 12), "Shift", "X"),
ROW("Date", DATE(2024, 11, 13), "Shift", "N"),
ROW("Date", DATE(2024, 11, 14), "Shift", "N"),
ROW("Date", DATE(2024, 11, 15), "Shift", "X"),
ROW("Date", DATE(2024, 11, 16), "Shift", "X"),
ROW("Date", DATE(2024, 11, 17), "Shift", "D"),
ROW("Date", DATE(2024, 11, 18), "Shift", "D"),
ROW("Date", DATE(2024, 11, 19), "Shift", "X"),
ROW("Date", DATE(2024, 11, 20), "Shift", "D"),
ROW("Date", DATE(2024, 11, 21), "Shift", "D"),
ROW("Date", DATE(2024, 11, 22), "Shift", "D"),
ROW("Date", DATE(2024, 11, 23), "Shift", "X"),
ROW("Date", DATE(2024, 11, 24), "Shift", "D"),
ROW("Date", DATE(2024, 11, 25), "Shift", "D"),
ROW("Date", DATE(2024, 11, 26), "Shift", "X"),
ROW("Date", DATE(2024, 11, 27), "Shift", "X"),
ROW("Date", DATE(2024, 11, 28), "Shift", "N"),
ROW("Date", DATE(2024, 11, 29), "Shift", "N"),
ROW("Date", DATE(2024, 11, 30), "Shift", "X"),
ROW("Date", DATE(2024, 12, 1), "Shift", "X"),
ROW("Date", DATE(2024, 12, 2), "Shift", "N"),
ROW("Date", DATE(2024, 12, 3), "Shift", "N"),
ROW("Date", DATE(2024, 12, 4), "Shift", "N"),
ROW("Date", DATE(2024, 12, 5), "Shift", "X"),
ROW("Date", DATE(2024, 12, 6), "Shift", "D"),
ROW("Date", DATE(2024, 12, 7), "Shift", "D")
) Next, let's add a new column for the shift timings:
- With the shifts table selected, add a new column with the following DAX:
ShiftTime =
SWITCH(
[Shift],
"N", "18:30 - 06:30",
"D", "06:30 - 18:30",
"X", "OFF"
)Create Your Report
- Create a Table visual.
- Drag the “Date”, “Shift”, and “ShiftTime” fields into the table.
By applying the above steps you can achieve your functionality.
- With the shifts table selected, add a new column with the following DAX: