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
kky1
Helper II
Helper II

Help with DAX to find the date for a weekday for the current week of the year

 

Hello, I need help with the syntax for a DAX expression. I am attempting to build an employee schedule on a calendar using the MAQ calendar visual. My data only has a weekday name and a start time and end time. I have a date table also with every day from 2022-2024 which I created a today column in my fact table and linked to the date in the date table. 

 

I also tried creating a new table filtering from my date table with: Table = filter('DateTable', 'DateTable'[Week of Month] = weeknum(today())). It didn't throw an error but there's no data in it.

 

What I want is to dynamically determine the actual date of the day of the week for the current week. I've tried variations of functions (today(), utc now(), date(), weekday() and weeknum but I'm getting errors. I'm truly struggling. Any help would be appreciated. 

 

At the end of the day, I need to take data that is currently stored as:

IDMondayStart timeMondayEndTimeTuesdayStartTimeTuesdayEndTimeWednesdayStartTimeWednesdayEndTimeThursdayStartTimeThursdayEndTimeFridayStartTimeFridayEndTime
17:004:008:005:006:003:007:3016:3010:0020:00
2    5:304:00    
3    8:0014:006:3017:00  
48:304:30    8:0014:006:3017:00

 

and I need to convert it into a new table in Power BI with the dates dynamically updating based on the current week. 

So for week # 33 of this year (2022) it would be:

(which I've gotten to in this format with Unpivot in power query)

IDDateStart timeEnd Time
18/8/20227:004:00
28/8/2022  
38/8/2022  
48/8/20228:304:30
18/9/20228:005:00
28/9/2022  
38/9/2022  
48/9/2022  
18/10/20226:003:00
28/10/20225:304:00
38/10/20228:0014:00
48/10/2022  
18/11/20227:3016:30
28/11/2022  
38/11/20226:3017:00
48/11/20228:0014:00
18/12/202210:0020:00
28/12/2022  
38/12/2022  
48/12/20226:3017:00

 

However, I'm running into an issue with the MAQ Calendar visual in that the null values for the hours on certain dates is populating in the calendar as "all day events" when I want them not to show on the calendar. Does anyone know of a workaround for that?

 

Thank you in advance for any help!

 

1 ACCEPTED SOLUTION
kky1
Helper II
Helper II

I'm sure there is likely to be a better way to work this out, but I was able to solve my issue of dynamically having M-F dates on a calendar with the following steps.

 

1. create today column
Today = Today()

2. create weeknum column for 1st Monday of the week:
Weeknum = WEEKNUM('ID'[Today],2)

3. create First Monday column:
First Monday =
var _mindate=CALCULATE(MIN('Table'[today]),FILTER('Table','Table'[Weeknum]=EARLIER('Table'[Weeknum])))
var _weekday=WEEKDAY(_mindate,2)
Return
IF(_weekday<>1,DATE(YEAR(_mindate)-1,12,31-(_weekday-1)+1),_mindate)

3. create current weekday based on weekday names:
CurrentWkday =
SWITCH(
TRUE(),
Table[DayName]="Monday", Table[First Monday]+0,
Table[DayName]="Tuesday", Table[First Monday]+1,
Table[DayName]="Wednesday", Table[First Monday]+2,
Table[DayName]="Thursday", Table[First Monday]+3,
Table[DayName]="Friday", Table[First Monday]+4,
BLANK()
)

4. Create Date Time:
CurrentDtTime = Table[CurrentWkday]&" "&Table[EndTimes]

 

Last step, I just did a datetime formula to concatenate the dates I'd generated with the time slots in my table to create a datetime for the calendar.

 

Side note: I'm still getting "all day" on the MAQ Calendar visual even though the values are null. If anyone has a solution to that, I'd be most grateful! 

View solution in original post

3 REPLIES 3
kky1
Helper II
Helper II

I'm sure there is likely to be a better way to work this out, but I was able to solve my issue of dynamically having M-F dates on a calendar with the following steps.

 

1. create today column
Today = Today()

2. create weeknum column for 1st Monday of the week:
Weeknum = WEEKNUM('ID'[Today],2)

3. create First Monday column:
First Monday =
var _mindate=CALCULATE(MIN('Table'[today]),FILTER('Table','Table'[Weeknum]=EARLIER('Table'[Weeknum])))
var _weekday=WEEKDAY(_mindate,2)
Return
IF(_weekday<>1,DATE(YEAR(_mindate)-1,12,31-(_weekday-1)+1),_mindate)

3. create current weekday based on weekday names:
CurrentWkday =
SWITCH(
TRUE(),
Table[DayName]="Monday", Table[First Monday]+0,
Table[DayName]="Tuesday", Table[First Monday]+1,
Table[DayName]="Wednesday", Table[First Monday]+2,
Table[DayName]="Thursday", Table[First Monday]+3,
Table[DayName]="Friday", Table[First Monday]+4,
BLANK()
)

4. Create Date Time:
CurrentDtTime = Table[CurrentWkday]&" "&Table[EndTimes]

 

Last step, I just did a datetime formula to concatenate the dates I'd generated with the time slots in my table to create a datetime for the calendar.

 

Side note: I'm still getting "all day" on the MAQ Calendar visual even though the values are null. If anyone has a solution to that, I'd be most grateful! 

kky1
Helper II
Helper II

Hi @littlemojopuppy - thank you so much for the reply. I couldn't attach a file to my post so I uploaded it to a public repository in GitHub - https://github.com/kky1/SampleFiles 

 

Steps so far:

I unpivoted the start times and end times separately into two tables. Then added a column to get the week of year number and the day name of the week. 

 

I then created a date table and a time table using scripts at this web site: https://radacad.com/all-in-one-script-to-create-date-dimension-in-power-bi-using-power-query and https://radacad.com/script-for-creating-time-table-in-power-bi-with-hours-minutes-and-seconds-bucket...

 

I thought I'd try linking the end time table and start time table each to a date and time table from the script above on week of year number and day name. I'm sure there's a more simplistic way to do this though...

 

As far as the MAQ calendar custom visual, I'm not sure why it is showing null or empty date values as "all day" on the calendar but I'm looking into that separately in case it's a bug. 

 

Thank you so much for your insight!

 

littlemojopuppy
Community Champion
Community Champion

Hi @kky1 can you provide some sample data to play with?

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