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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
Anonymous
Not applicable

Create date range column for weeks in DAX

So I have a list of dates (from Monday, December 10, 2018 onwards) in a table. It's a table originally created in the Data View, so is not a query and cannot be edited in the Query Editor.

 

I want to assign each of these dates to calendar weeks starting on Monday, and have a calculated column that displays the first date in the week and the last date in the week, e.g.:

 

Monday, December 10, 201810/12/2018-16/12/2018
Tuesday, December 11, 201810/12/2018-16/12/2018
Wednesday. December 12, 201810/12/2018-16/12/2018
Thursday, December 13, 201810/12/2018-16/12/2018
Friday, December 14, 201810/12/2018-16/12/2018
Saturday, December 15, 201810/12/2018-16/12/2018
Sunday, December 16, 201810/12/2018-16/12/2018
Monday, December 17, 201817/12/2018-23/12/2018
Tuesday, December 18, 201817/12/2018-23/12/2018

 

Any ideas on how I could write a formula to achieve this?

1 ACCEPTED SOLUTION
v-yuta-msft
Community Support
Community Support

@Anonymous,

 

You may try DAX like pattern below(maybe some mistakes but hope it can guide you):

 

Result = 
VAR seperator =
    FIND ( "-", SUBSTITUTE ( Table1[Date], "-", "," ) )
VAR weekday =
    LEFT ( Table1[Date], seperator - 1 )
VAR _date =
    RIGHT ( Table1[Date], LEN ( Table1[Date] ) - seperator )
RETURN
    SWITCH (
        weekday,
        "Monday", CONCATENATE ( _date, CONCATENATE ( "~", _date + 6 ) ),
        "Tuesday", CONCATENATE ( _date - 1, CONCATENATE ( "~", _date + 5 ) ),
        "Wednesday", CONCATENATE ( _date - 2, CONCATENATE ( "~", _date + 4 ) ),
        "Thursday", CONCATENATE ( _date - 3, CONCATENATE ( "~", _date + 3 ) ),
        "Friday", CONCATENATE ( _date - 4, CONCATENATE ( "~", _date + 2 ) ),
        "Saturday", CONCATENATE ( _date - 5, CONCATENATE ( "~", _date + 1 ) ),
        "Sunday", CONCATENATE ( _date - 6, CONCATENATE ( "~", _date ) )
    )

Community Support Team _ Jimmy Tao

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
Grozeille
New Member

Hello, this is very useful, but what if you year does not begin with Monday ? For instance 2023 starts on a Sunday. Thus, with this formula we have Week 1 = 12/26/22 - 1/1/2023 and 1/2/2023 - 1/8/2023. How could we manage this case please?

v-yuta-msft
Community Support
Community Support

@Anonymous,

 

You may try DAX like pattern below(maybe some mistakes but hope it can guide you):

 

Result = 
VAR seperator =
    FIND ( "-", SUBSTITUTE ( Table1[Date], "-", "," ) )
VAR weekday =
    LEFT ( Table1[Date], seperator - 1 )
VAR _date =
    RIGHT ( Table1[Date], LEN ( Table1[Date] ) - seperator )
RETURN
    SWITCH (
        weekday,
        "Monday", CONCATENATE ( _date, CONCATENATE ( "~", _date + 6 ) ),
        "Tuesday", CONCATENATE ( _date - 1, CONCATENATE ( "~", _date + 5 ) ),
        "Wednesday", CONCATENATE ( _date - 2, CONCATENATE ( "~", _date + 4 ) ),
        "Thursday", CONCATENATE ( _date - 3, CONCATENATE ( "~", _date + 3 ) ),
        "Friday", CONCATENATE ( _date - 4, CONCATENATE ( "~", _date + 2 ) ),
        "Saturday", CONCATENATE ( _date - 5, CONCATENATE ( "~", _date + 1 ) ),
        "Sunday", CONCATENATE ( _date - 6, CONCATENATE ( "~", _date ) )
    )

Community Support Team _ Jimmy Tao

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

Hi Jimmy,

 

This worked for me great, slightly tweaked as follows:

 

Calendar Week Date Range = 
VAR separator = 
    FIND("-",SUBSTITUTE(FORMAT('6 Date Table'[Date],"Long Date"),", ","-"))
VAR weekday =
    LEFT(FORMAT('6 Date Table'[Date],"Long Date"), separator - 1)
VAR _date =
    RIGHT(FORMAT('6 Date Table'[Date],"Long Date"),LEN(FORMAT('6 Date Table'[Date],"Long Date")) - separator)
RETURN
    SWITCH (
        weekday,
        "Monday",CONCATENATE(FORMAT(_date + 0,"dd/mm/yyyy"),CONCATENATE("-",FORMAT(_date + 6,"dd/mm/yyyy"))),
        "Tuesday",CONCATENATE(FORMAT( _date - 1,"dd/mm/yyyy"),CONCATENATE("-",FORMAT(_date + 5,"dd/mm/yyyy"))),
        "Wednesday",CONCATENATE(FORMAT(_date - 2,"dd/mm/yyyy"),CONCATENATE("-",FORMAT(_date + 4,"dd/mm/yyyy"))),
        "Thursday",CONCATENATE(FORMAT(_date - 3,"dd/mm/yyyy"),CONCATENATE("-",FORMAT(_date + 3,"dd/mm/yyyy"))),
        "Friday",CONCATENATE(FORMAT(_date - 4,"dd/mm/yyyy"),CONCATENATE("-",FORMAT(_date + 2,"dd/mm/yyyy"))),
        "Saturday",CONCATENATE(FORMAT(_date - 5,"dd/mm/yyyy"),CONCATENATE("-",FORMAT(_date + 1,"dd/mm/yyyy"))),
        "Sunday",CONCATENATE(FORMAT(_date - 6,"dd/mm/yyyy"),CONCATENATE("-",FORMAT(_date + 0,"dd/mm/yyyy")))
    )

This was a great learning exercise for me! Thanks so much for your help!

 

This was a great thread and helped me preparing a report I was looking forward to, but I just wanted to know if there is a way we can only keep the date range from Monday till Friday, please suggest. Thank you!

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Power BI Carousel June 2024

Power BI Monthly Update - June 2024

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

PBI_Carousel_NL_June

Fabric Community Update - June 2024

Get the latest Fabric updates from Build 2024, key Skills Challenge voucher deadlines, top blogs, forum posts, and product ideas.

Top Solution Authors