So I appreciate this is an old post now, however I’ve found this last night and it was actually really helpful. The solution posted by Harsh does work for the specific scenario stated, however, mine was slightly modified from this and as a result found a few issues with the measures Harsh provided.
I thought it might be helpful for anyone else who comes across this post to share my development based on what Harsh originally provided.
For context I’m trying to achieve the same sort of outcome, however I wanted to split down to an early, day, evening and night period. This allows for consideration of the night rate potentially changing to a different day such as on a weekend where the rate could change.
I also found that shifts which spanned a period, such as one that started at 0700 in the morning and finished at 1900 in the evening, would be picked up in the evening totals and the new early totals I’d added in but not in the daytime totals as it ignores it due to it spanning the set time period.
If you feel this would be helpful then I recommend you download what Harsh provided and just tweak the measures slightly. I’ve not needed to change his total measures at all and I just added in two new measures for the early and early totals, with the early totals being a duplicate of the regular totals pointed to the early calculations.
For reference, my table is called Calendar Data 2 instead of sales, and the column Date Time Start is just Start-Time and Date Time End is End-Time, so you’ll need to change this if you drop it directly into Harsh’s model. I also reference a Start-Date and End-Date in there so it will be helpful if you have this too as this isn’t used in Harsh’s model.
This isn’t a straight drag and drop - I’m sharing more so you have the logic required to point to your own data.
Hope this helps someone!
11. Early Hours - Lookup =
IF(
SELECTEDVALUE( 'Calendar Data 2'[Start-Time] ) < SELECTEDVALUE( 'Calendar Data 2'[End-Time] ) ,
IF(
SELECTEDVALUE( 'Calendar Data 2'[Start-Time] ) >= TIME( 0 , 0 , 0 ) &&
SELECTEDVALUE( 'Calendar Data 2'[End-Time] ) <= TIME( 8 , 0 , 0 ) ,
SELECTEDVALUE( 'Calendar Data 2'[Start-Time] ) - SELECTEDVALUE( 'Calendar Data 2'[End-Time] ) ,
IF(
SELECTEDVALUE( 'Calendar Data 2'[Start-Time] ) >= TIME( 0 , 0 , 0 ) &&
SELECTEDVALUE( 'Calendar Data 2'[Start-Time] ) <= TIME( 8 , 0 , 0 ) ,
SELECTEDVALUE( 'Calendar Data 2'[Start-Time] ) - TIME( 8 , 0 , 0 ) ,
IF(
SELECTEDVALUE( 'Calendar Data 2'[End-Time] ) >= TIME( 0 , 0 , 0 ) &&
SELECTEDVALUE( 'Calendar Data 2'[End-Time] ) <= TIME( 8 , 0 , 0 ) ,
TIME( 0 , 0 , 0 ) - SELECTEDVALUE( 'Calendar Data 2'[End-Time] ) ) ) ) )
1. Regular Hours - Lookup =
IF(
SELECTEDVALUE( 'Calendar Data 2'[Start-Time] ) <= TIME( 8 , 0 , 0 ) &&
SELECTEDVALUE( 'Calendar Data 2'[End-Time] ) >= TIME( 18 , 0 , 0 ) ,
TIME( 18 , 0 , 0 ) - TIME( 8 , 0 , 0 ),
IF(
SELECTEDVALUE( 'Calendar Data 2'[Start-Time] ) <= TIME( 18 , 0 , 0 ) &&
SELECTEDVALUE( 'Calendar Data 2'[End-Time] ) >= TIME( 0 , 0 , 0 ) &&
SELECTEDVALUE('Calendar Data 2'[End-Date]) <> SELECTEDVALUE('Calendar Data 2'[Start-Date]),
TIME( 18 , 0 , 0 ) - SELECTEDVALUE( 'Calendar Data 2'[Start-Time]),
IF(
SELECTEDVALUE( 'Calendar Data 2'[Start-Time] ) < SELECTEDVALUE( 'Calendar Data 2'[End-Time] ) ,
IF(
SELECTEDVALUE( 'Calendar Data 2'[Start-Time] ) >= TIME( 8 , 0 , 0 ) &&
SELECTEDVALUE( 'Calendar Data 2'[End-Time] ) <= TIME( 18 , 0 , 0 ) ,
SELECTEDVALUE( 'Calendar Data 2'[Start-Time] ) - SELECTEDVALUE( 'Calendar Data 2'[End-Time] ) ,
IF(
SELECTEDVALUE( 'Calendar Data 2'[Start-Time] ) >= TIME( 8 , 0 , 0 ) &&
SELECTEDVALUE( 'Calendar Data 2'[Start-Time] ) <= TIME( 18 , 0 , 0 ) ,
SELECTEDVALUE( 'Calendar Data 2'[Start-Time] ) - TIME( 18 , 0 , 0 ) ,
IF(
SELECTEDVALUE( 'Calendar Data 2'[End-Time] ) >= TIME( 8 , 0 , 0 ) &&
SELECTEDVALUE( 'Calendar Data 2'[End-Time] ) <= TIME( 18 , 0 , 0 ) ,
TIME( 8 , 0 , 0 ) - SELECTEDVALUE( 'Calendar Data 2'[End-Time]) ) ) ) ) ) )
3. Evening Hours - Lookup =
IF(
SELECTEDVALUE( 'Calendar Data 2'[Start-Time] ) <= TIME( 18 , 0 , 0 ) &&
SELECTEDVALUE( 'Calendar Data 2'[End-Time] ) >= TIME( 0 , 0 , 0 ) &&
SELECTEDVALUE('Calendar Data 2'[End-Date]) <> SELECTEDVALUE('Calendar Data 2'[Start-Date]),
( TIME( 23 , 59 , 59 ) + TIME( 0 , 0 , 1 ) ) - TIME( 18 , 0 , 0 ),
IF(
SELECTEDVALUE( 'Calendar Data 2'[Start-Time] ) >= TIME( 18 , 0 , 0 ) &&
SELECTEDVALUE( 'Calendar Data 2'[Start-Time] ) <= ( TIME( 23 , 59 , 59 ) + TIME( 0 , 0 , 1 ) ) ,
SELECTEDVALUE( 'Calendar Data 2'[Start-Time] ) - ( TIME( 23 , 59 , 59 ) + TIME( 0 , 0 , 1 ) ) ,
IF(
SELECTEDVALUE( 'Calendar Data 2'[End-Time] ) >= TIME( 18 , 0 , 0 ) &&
SELECTEDVALUE( 'Calendar Data 2'[End-Time] ) <= ( TIME( 23 , 59 , 59 ) + TIME( 0 , 0 , 1 ) ) ,
SELECTEDVALUE( 'Calendar Data 2'[End-Time] ) - TIME( 18 , 0 , 0 ) ) ) )
5. Night Hours - Lookup =
IF(
SELECTEDVALUE( 'Calendar Data 2' [Start-Time] ) >= TIME( 0 , 0 , 0 ) &&
SELECTEDVALUE( 'Calendar Data 2' [Start-Time] ) < TIME( 8 , 0 , 0 ) &&
SELECTEDVALUE('Calendar Data 2'[End-Date]) <> SELECTEDVALUE('Calendar Data 2'[Start-Date]),
SELECTEDVALUE( 'Calendar Data 2' [Start-Time] ) - TIME( 8 , 0 , 0 ) ,
IF(
SELECTEDVALUE( 'Calendar Data 2' [End-Time] ) >= TIME( 0 , 0 , 0 ) &&
SELECTEDVALUE( 'Calendar Data 2' [End-Time] ) < TIME( 8 , 0 , 0 ) &&
SELECTEDVALUE('Calendar Data 2'[End-Date]) <> SELECTEDVALUE('Calendar Data 2'[Start-Date]),
SELECTEDVALUE( 'Calendar Data 2' [End-Time] ) - TIME( 0 , 0 , 0 ) ) )