Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

How do I split the source data week ranges into days according to the date range and hours per day?

I have this source data, the start date is always a Monday and the end date is a Sunday. The columns Mon to Sun represent scheduled hours per day for each week in the start and end range. Note that the range can be 1 week to 300 weeks.

1. Source Data        
ScheduleStartEndMonTueWedThuFriSatSun
Schedule111/21/202211/27/20228888800
Schedule212/5/202212/25/20237777700
Schedule31/2/20231/15/20233.503.53.5003.5

 

I would like to split the columns into rows by day so it becomes:

2. Desired Result  
ScheduleStartEndHours
Schedule111/21/202211/21/20228
Schedule111/22/202211/22/20228
Schedule111/23/202211/23/20228
Schedule111/24/202211/24/20228
Schedule111/25/202211/25/20228
Schedule111/26/202211/26/20220
Schedule111/27/202211/27/20220
Schedule212/5/202212/5/20227
Schedule212/6/202212/6/20227
Schedule212/7/202212/7/20227
Schedule212/8/202212/8/20227
Schedule212/9/202212/9/20227
Schedule212/10/202212/10/20220
Schedule212/11/202212/11/20220
Schedule212/12/202212/12/20227
Schedule212/13/202212/13/20227
Schedule212/14/202212/14/20227
Schedule212/15/202212/15/20227
Schedule212/16/202212/16/20227
Schedule212/17/202212/17/20220
Schedule212/18/202212/18/20220
Schedule212/19/202212/19/20227
Schedule212/20/202212/20/20227
Schedule212/21/202212/21/20227
Schedule212/22/202212/22/20227
Schedule212/23/202212/23/20227
Schedule212/24/202212/24/20220
Schedule212/25/202212/25/20220
Schedule31/2/20231/2/20233.5
Schedule31/3/20231/3/20230
Schedule31/4/20231/4/20233.5
Schedule31/5/20231/5/20233.5
Schedule31/6/20231/6/20230
Schedule31/7/20231/7/20230
Schedule31/8/20231/8/20233.5
Schedule31/9/20231/9/20233.5
Schedule31/10/20231/10/20230
Schedule31/11/20231/11/20233.5
Schedule31/12/20231/12/20233.5
Schedule31/13/20231/13/20230
Schedule31/14/20231/14/20230
Schedule31/15/20231/15/20233.5

 

Ultimately I'm trying to create a planner that can be viewed by day(s) for any specified date range. I guess the desired result is what I think I need to achieve this.

 

How can I do this by either using Power Query or DAX? Thanks in advance.

  • Anonymous's avatar
    Anonymous
    3 years ago

    Hi Anonymous ,

    Please refer to my steps.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCk7OSE0pzUk1VNJRMjTUNwIiAyMjKMccxrHAgg3AOFYHYQZYm5G+KdwII30jOMccC8Y0whikTd8IpAnCNDSFsY31TKFaICwEHyYWGwsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Schedule = _t, Start = _t, End = _t, Mon = _t, Tue = _t, Wed = _t, Thu = _t, Fri = _t, Sat = _t, Sun = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Schedule", type text}, {"Start", type date}, {"End", type date}, {"Mon", type number}, {"Tue", Int64.Type}, {"Wed", type number}, {"Thu", type number}, {"Fri", Int64.Type}, {"Sat", Int64.Type}, {"Sun", type number}}),
        #"Changed Type1" = Table.TransformColumnTypes(#"Changed Type",{{"Start", Int64.Type}, {"End", Int64.Type}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type1", "Custom", each {[Start]..[End]}),
        #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),
        #"Changed Type2" = Table.TransformColumnTypes(#"Expanded Custom",{{"Start", type date}, {"End", type date}, {"Custom", type date}}),
        #"Reordered Columns" = Table.ReorderColumns(#"Changed Type2",{"Schedule", "Start", "End", "Custom", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}),
        #"Removed Columns" = Table.RemoveColumns(#"Reordered Columns",{"End"}),
        #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom", "end"}}),
        #"Added Custom1" = Table.AddColumn(#"Renamed Columns", "Custom", each Date.DayOfWeek([end],Day.Sunday)),
        #"Reordered Columns1" = Table.ReorderColumns(#"Added Custom1",{"Schedule", "Start", "end", "Custom", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}),
        #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Reordered Columns1", {"Schedule", "Start", "end", "Custom"}, "Attribute", "Value"),
        #"Added Conditional Column" = Table.AddColumn(#"Unpivoted Columns", "Custom.1", 
    each if [Schedule] = "Schedule1" and List.Contains({1..5},[Custom]) then 8 
    else if [Schedule] = "Schedule2" and List.Contains({1..5},[Custom]) then 7 
    else if [Schedule] = "Schedule3" and List.Contains({0,1,3,4},[Custom]) then 3.5 
    else 0),
        #"Removed Columns1" = Table.RemoveColumns(#"Added Conditional Column",{"Attribute", "Value", "Custom"}),
        #"Removed Duplicates" = Table.Distinct(#"Removed Columns1")
    in
        #"Removed Duplicates"

    Best Regards

    Community Support Team _ Polly

     

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

4 Replies

  • Try this

    Click here to download solution 

     

    How it works ...

     

    Remove the enddate column, because you dont need it and less in more.

     

    Rename Mon, Tue, Wed , Thu, etc columns  to 1,2,3,4,5,6,7

     

     

    Select the schedule and start column and unpivot the other columns

     

    add a new customer column for the date = start + dayofweek

    remove the uneeded columns, change the data types and column names and volia!  You now have fact table.

     

    Now add a calandar table with contiguous dates and relate it to your fact table.

    You need calandar table with contiguous  dates because you face table may have missing days.

    Click here to learn about power bi calendar tables 

     

    Thanks for reaching out for help.

    I put in a lot of effort to help you, now please quickly help me by giving kudos.

    Remember we are unpaid volunteers and here to coach you with Power BI and DAX skills and techniques, not do the users job for them. So please click the thumbs up and accept as solution button. 

    If you give someone a fish then you only give them one meal, but if you teach them how to fish then they can feed themselves and teach others for a lifetime.  I prefer to teach members on this forum techniques rather give full solutions and do their job. You can then adapt the technique for your solution, learn some DAX skills for next time and soon become a Power BI Super User like me. 

    One question per ticket please. If you need to extend your request then please raise a new ticket.

    You will get a quicker response and each volunteer solver will get the kudos they deserve. Thank you ! 

     

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi, I appreciate the quick response and the effort. However the end date is important because the schedules are not just a week in length; schedule 1 is one week, but schedule 2 is three weeks and schedule 3 is two weeks. The respective Mon to Sun hours split should then be duplicated in each of these weeks. Some of our project schedules are even longer. That's the bit that I'm really struggling with. Advice gratefully appreciated.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous ,

    Please refer to my steps.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCk7OSE0pzUk1VNJRMjTUNwIiAyMjKMccxrHAgg3AOFYHYQZYm5G+KdwII30jOMccC8Y0whikTd8IpAnCNDSFsY31TKFaICwEHyYWGwsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Schedule = _t, Start = _t, End = _t, Mon = _t, Tue = _t, Wed = _t, Thu = _t, Fri = _t, Sat = _t, Sun = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Schedule", type text}, {"Start", type date}, {"End", type date}, {"Mon", type number}, {"Tue", Int64.Type}, {"Wed", type number}, {"Thu", type number}, {"Fri", Int64.Type}, {"Sat", Int64.Type}, {"Sun", type number}}),
        #"Changed Type1" = Table.TransformColumnTypes(#"Changed Type",{{"Start", Int64.Type}, {"End", Int64.Type}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type1", "Custom", each {[Start]..[End]}),
        #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),
        #"Changed Type2" = Table.TransformColumnTypes(#"Expanded Custom",{{"Start", type date}, {"End", type date}, {"Custom", type date}}),
        #"Reordered Columns" = Table.ReorderColumns(#"Changed Type2",{"Schedule", "Start", "End", "Custom", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}),
        #"Removed Columns" = Table.RemoveColumns(#"Reordered Columns",{"End"}),
        #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom", "end"}}),
        #"Added Custom1" = Table.AddColumn(#"Renamed Columns", "Custom", each Date.DayOfWeek([end],Day.Sunday)),
        #"Reordered Columns1" = Table.ReorderColumns(#"Added Custom1",{"Schedule", "Start", "end", "Custom", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}),
        #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Reordered Columns1", {"Schedule", "Start", "end", "Custom"}, "Attribute", "Value"),
        #"Added Conditional Column" = Table.AddColumn(#"Unpivoted Columns", "Custom.1", 
    each if [Schedule] = "Schedule1" and List.Contains({1..5},[Custom]) then 8 
    else if [Schedule] = "Schedule2" and List.Contains({1..5},[Custom]) then 7 
    else if [Schedule] = "Schedule3" and List.Contains({0,1,3,4},[Custom]) then 3.5 
    else 0),
        #"Removed Columns1" = Table.RemoveColumns(#"Added Conditional Column",{"Attribute", "Value", "Custom"}),
        #"Removed Duplicates" = Table.Distinct(#"Removed Columns1")
    in
        #"Removed Duplicates"

    Best Regards

    Community Support Team _ Polly

     

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

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Polly, thank you so much, this is much smarter than the multi step workaround that I had come up with. Thanks again.