Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
frano72
Helper IV
Helper IV

Find current time between two dates (on same row) to enable a MTD calculation

Hi,

 

First let me give some context to the problem.

I have a roster table which specifies the details about 12 hr rostered shifts.  start time (in local and UTC), finish time, whether day shift/night shift and so on.  The business views a completed day for reporting purposes as when the day shift AND the night shift completes.  Note that the night shift finishes on the morning of the next day.

 

So i'm trying to create the calculated column syntax that can take the current time when the dataset refreshes (eg UTCNOW) and from that time value work out where you are in that roster table by comparing that time to the shift start and end dates.

 

Once i can do that successfully then it enables a few columns to be created for things like :

isListShift : identifies that last completed shift compared to dateset refresh time

isLastDay : identifies the last completed "Day" compared to dataset refresh time

isMTD : identifies the rows that contribute to MTD calculation*

 

* MTD : the nuance here is that on the first of the month the MTD is the prior month as you are currently in a day that hasn't completed yet.  (I can't use the relative date filtering in the pbi service as that works on UTC and as AU is ahead in time, the new month starts on UTC Jul 31 20:00 and finishes on UTC Aug 1 8:00 so when it clicks over to the 1st the relative date filter of "is in this month" switches to the new month.  It needs to b epinned to Aug and not switch to Sep until local Day 2 of the month starts.

 

I know this sounds like a christopher nolan time travel film....

 

I have somethign working with layered if statements but i'm sure there is a smarter pattern.

 

The table is used as a "date" table via relationship to fact tables.

 

A subset of the roster table is pasted below.

 

My current calculated column is also below.

isCurrentMonth =

// due to the powerbi service relative filtering only doing UTC, the relative filters for MTD don't work.
// this column identifies the current month for the purposes of filtering for MTD calcs. You use it in combination with isPriorDays
// the trick with this column is that when it is being run at a time that equates to Day 1 - the month to day is last month. Not unti you are in day 2 does MTD start back at 1

var UTCOffset=10/24
var CurrentTime=UTCNOW()+UTCOffset

// TEST MIDDLE OF YEAR
//var CurrentTime="8/25/2020 9:00:00 PM" + UTCOffset // middle of month
// var CurrentTime="8/31/2020 9:00:00 PM" + UTCOffset // day shift on first of the month
// var CurrentTime="8/31/2020 7:00:00 PM" + UTCOffset // day shift on first of the month
// var CurrentTime="9/1/2020 7:00:00 AM" + UTCOffset // day shift on first of the month
// var CurrentTime="9/1/2020 9:00:00 AM" + UTCOffset // night shift of the 1st sets
//var CurrentTime="9/2/2020 9:00:00 AM" + UTCOffset // next day

// TEST AROUND END OF YEAR
//var CurrentTime="12/25/2020 9:00:00 PM" + UTCOffset // middle of month Y
//var CurrentTime="12/31/2020 9:00:00 PM" + UTCOffset // day shift on first of the month - Y
//var CurrentTime="1/1/2021 7:00:00 AM" + UTCOffset // day shift on first of the month - WORKS
//var CurrentTime="1/1/2021 9:00:00 AM" + UTCOffset // night shift of the 1st sets WORKS
//var CurrentTime="1/5/2021 9:00:00 PM" + UTCOffset // middle of month Y

var CurrentMonth=Month(CurrentTime)
var CurrentYear=YEAR(CurrentTime)
var CurrentDay=day(CurrentTime)
var CompareNowtoDate=day(CurrentTime)=day('COP - Shift Roster'[Date])&&month(CurrentTime)=month('COP - Shift Roster'[Date])&&year('COP - Shift Roster'[Date])

// slightly different logic if its january
var compareFirstDaytoPriorMonth=if(month(CurrentTime)=1,
day(CurrentTime)=1 && month(CurrentTime)=month('COP - Shift Roster'[Date])-11 && year(CurrentTime)=year('COP - Shift Roster'[Date])+1,
day(CurrentTime)=1 && month(CurrentTime)=month('COP - Shift Roster'[Date])+1 && year(CurrentTime)=year('COP - Shift Roster'[Date])
)

return

// first handle where all rows in roster are the first of the month
if(day('COP - Shift Roster'[Date])=1,
// does the current shift day equal the first of the month, because if it does - it needs to be set to 0 as MTD when on the 1st is the full prior month
if(day(CurrentTime)=1 && month(CurrentTime)=month('COP - Shift Roster'[Date]) && year(CurrentTime)=year('COP - Shift Roster'[Date]),
"0",
// if current shift day is 1 but its the prior month then need to set to 1
if(compareFirstDaytoPriorMonth,
"1",
// if current time is not on the 1st of the month, then its fine for the 1st to be set to 1
if(day(CurrentTime)<>1 && month(CurrentTime)=month('COP - Shift Roster'[Date]) && year(CurrentTime)=year('COP - Shift Roster'[Date]),
"1",
"0"
)
)
),
 
// else what to do for all other days [Date] = 2 to end of month
if(compareFirstDaytoPriorMonth,
// want to set to 0 as when its the first 2 shifts of the month the MTD is the prior month
"1",
// if current time is not on the 1st of the month, then its fine for the 1st to be set to 1
if(day(CurrentTime)<>1 && month(CurrentTime)=month('COP - Shift Roster'[Date]) && year(CurrentTime)=year('COP - Shift Roster'[Date]),
"1",
"0"
)
)
)


 

 

idShiftIndexDateDateCodeShiftUTC_Shift_StartShift_StartUTC_Shift_EndShift_End
3251820200724124/07/202020200724Day23/07/2020 20:0024/07/2020 6:0024/07/2020 8:0024/07/2020 18:00
3251920200724224/07/202020200724Night24/07/2020 8:0024/07/2020 18:0024/07/2020 20:0025/07/2020 6:00
3252020200725125/07/202020200725Day24/07/2020 20:0025/07/2020 6:0025/07/2020 8:0025/07/2020 18:00
3252120200725225/07/202020200725Night25/07/2020 8:0025/07/2020 18:0025/07/2020 20:0026/07/2020 6:00
3252220200726126/07/202020200726Day25/07/2020 20:0026/07/2020 6:0026/07/2020 8:0026/07/2020 18:00
3252320200726226/07/202020200726Night26/07/2020 8:0026/07/2020 18:0026/07/2020 20:0027/07/2020 6:00
3252420200727127/07/202020200727Day26/07/2020 20:0027/07/2020 6:0027/07/2020 8:0027/07/2020 18:00
3252520200727227/07/202020200727Night27/07/2020 8:0027/07/2020 18:0027/07/2020 20:0028/07/2020 6:00
3252620200728128/07/202020200728Day27/07/2020 20:0028/07/2020 6:0028/07/2020 8:0028/07/2020 18:00
3252720200728228/07/202020200728Night28/07/2020 8:0028/07/2020 18:0028/07/2020 20:0029/07/2020 6:00
3252820200729129/07/202020200729Day28/07/2020 20:0029/07/2020 6:0029/07/2020 8:0029/07/2020 18:00
3252920200729229/07/202020200729Night29/07/2020 8:0029/07/2020 18:0029/07/2020 20:0030/07/2020 6:00
3253020200730130/07/202020200730Day29/07/2020 20:0030/07/2020 6:0030/07/2020 8:0030/07/2020 18:00
3253120200730230/07/202020200730Night30/07/2020 8:0030/07/2020 18:0030/07/2020 20:0031/07/2020 6:00
3253220200731131/07/202020200731Day30/07/2020 20:0031/07/2020 6:0031/07/2020 8:0031/07/2020 18:00
3253320200731231/07/202020200731Night31/07/2020 8:0031/07/2020 18:0031/07/2020 20:001/08/2020 6:00
325342020080111/08/202020200801Day31/07/2020 20:001/08/2020 6:001/08/2020 8:001/08/2020 18:00
325352020080121/08/202020200801Night1/08/2020 8:001/08/2020 18:001/08/2020 20:002/08/2020 6:00
325362020080212/08/202020200802Day1/08/2020 20:002/08/2020 6:002/08/2020 8:002/08/2020 18:00
325372020080222/08/202020200802Night2/08/2020 8:002/08/2020 18:002/08/2020 20:003/08/2020 6:00
325382020080313/08/202020200803Day2/08/2020 20:003/08/2020 6:003/08/2020 8:003/08/2020 18:00
325392020080323/08/202020200803Night3/08/2020 8:003/08/2020 18:003/08/2020 20:004/08/2020 6:00

 

2 REPLIES 2
Greg_Deckler
Super User
Super User

@frano72 - I'm not certain what you want as your end result. Given your sample data, what would your end result look like? If you are using nested IF statements, a better pattern is SWITCH(TRUE()...)


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Hi @Greg_Deckler,

 

I'm basically after a column that has a 1 or a 0 in it which I can then use as a filter and could be called CurrenReportingMonth

 

The simplistic logic is :

 

if the dataset refreshed at anytime during the 2 x 12 hour shifts that are considered the first of the month (eg Aug 1st) - then each row for July has a 1 in it, 0's every where else. As soon as we exit Aug 1st and are in Aug 2, then there are 1s for all rows for August.

 

I need to be able to compare refresh time (UTCNOW) with the two shiftstart, shiftend columns in the roster table to determine the current day/month combo and then use that to then calculate for each row whether its a 1 or 0.

 

So first step to sovle is how to find UTCNOW between two columns of dates and return a value from another column for that row.

 

thanks !

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors