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
derickson091
Frequent Visitor

How can I create a one to many relationship from DateKey to table with weeks?

Hello,

I have a DateKey table shown below that I'd like to connect to a table that shows weekly inventory levels. I need to create a one-to-many relationship between the two tables, but am unable to do so (cannot join on week number because the DateKey table has multiple entries for each week).

 

Any ideas how to get around this?

 

Thank you!

 

DateKeyDateKeyWeekly inventory tableWeekly inventory table

1 ACCEPTED SOLUTION

Assuming a couple of items

  1. Your weeks are all 7 days, starting on Monday except for the first week of the year which start on the 1st and runs to Sunday.
  2. You can have more than 52 weeks in a year.

Give this measure a try as a calculated column in your week inventory table.

Date of Week = 
VAR YearStart = DATE(VALUE(LEFT('Weekly Inventory'[WeekNum],4)),1,1)
VAR YearStartDay = WEEKDAY(YearStart,2)
VAR WeekStartDate = YearStart + VALUE(RIGHT('Weekly Inventory'[WeekNum],LEN('Weekly Inventory'[WeekNum])-4)) * 7 - 7 - (YearStartDay-1)

RETURN MAX(YearStart,WeekStartDate)

Here is how the table looks in my test.  Each year starts on the 1st but after that all weeks start on the Monday.

DateOfWeek.jpg

View solution in original post

4 REPLIES 4
jdbuchanan71
Super User
Super User

On your weekly inventory table add a column for [Week Date] which would be the date of the first day of that week. 1/1/2019 for wk1-2019, 1/8/2019 for wk2-2019 etc.  The you can join the [Week Date] column to your date table.

Is there a convenient way to do this in Power BI?

Assuming a couple of items

  1. Your weeks are all 7 days, starting on Monday except for the first week of the year which start on the 1st and runs to Sunday.
  2. You can have more than 52 weeks in a year.

Give this measure a try as a calculated column in your week inventory table.

Date of Week = 
VAR YearStart = DATE(VALUE(LEFT('Weekly Inventory'[WeekNum],4)),1,1)
VAR YearStartDay = WEEKDAY(YearStart,2)
VAR WeekStartDate = YearStart + VALUE(RIGHT('Weekly Inventory'[WeekNum],LEN('Weekly Inventory'[WeekNum])-4)) * 7 - 7 - (YearStartDay-1)

RETURN MAX(YearStart,WeekStartDate)

Here is how the table looks in my test.  Each year starts on the 1st but after that all weeks start on the Monday.

DateOfWeek.jpg

Awesome! Works perfectly. Thank you very much @jdbuchanan71 

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.