Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Display shift schedules in calendar

Hello,

I have the following table which are the individual shift schedules and i am trying to present this in a calendar visualization. But it is only working for some of the names. Sometimes, if there is an overlap in the dates for the same name, it does not give the correct values. The final result should be a table with 3 columns: Date, Name, Value. The Value is either 1 or 0. It should be 1 when the date falls within the range specified in the schedule, otherwise it should be 0.

NameFromUntil

Kacy Jones

04/03/202222/03/2022

Kade Kennedy

06/03/202212/03/2022

Cristian Thompson

09/03/202230/03/2022

Kade Kennedy

06/03/202213/03/2022

Damian Mclellan

09/03/202230/03/2022

Damian Mclellan

14/03/202230/03/2022

Cristian Thompson

18/03/202218/03/2022

Ishan Rigby

14/03/202220/03/2022

Jimmie Harding

09/03/202230/03/2022

Brax Oneill

28/03/202208/04/2022

Kacy Jones

23/03/202230/03/2022

Bear Barry

17/03/202227/03/2022

Jorgie Galloway

21/03/202225/03/2022

Farah Lyon

01/04/202221/04/2022

Salman Goodman

28/03/202215/04/2022

Vincenzo Murray

30/03/202220/04/2022

Jorgie Galloway

12/04/202203/05/2022

Kade Kennedy

10/04/202216/04/2022

Kacy Jones

18/04/202227/04/2022

 

In red below do not give the correct values and therefore do not show correctly or at all in the calendar visualization.

 

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZJdS8MwFIb/Suj1YEnarvV2itPNIah4M3ZxbEMbyIekitZfb7JCPdF18/LAc16e87HbJRuoerK2RnTJLKHZnKZzTjn3BedjsZ8FsBZkI4wRdR/QBUJZjF462b1JMOSptfq1sybwF4hP6f+j0wi9Ah2Ct5USSsG54L80y6bpY9qsxC5lxN92rYcfZPPS/07mcfJaai0FuQFXS9OckV46+CT3RkilQhIWoL7I8N7Q7Xh6IlKAI0tw7iBaYNEiFrWu8aIrUMp+QKA5w3Qe0dfgoCV3/XBgNroNXUj0EZT2q1pZW+vDGaKpWB7Bz9JUwnxZsn137uCAphlWm50yDs84ivg+mk9+GqMIZYvp5bISD1f8gPtv", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, From = _t, Until = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"From", type date}, {"Until", type date}}),
        // create lists with dates 1 or 0 corresponding to each Name/From/Until
    allDates = List.Dates(Date.AddMonths(Date.StartOfMonth(Date.From(DateTime.FixedLocalNow())),-1), Number.From(Date.EndOfMonth(DateTime.FixedLocalNow()))- Number.From(Date.AddMonths(Date.StartOfMonth(DateTime.FixedLocalNow()),-1)) ,#duration(1,0,0,0)),
    textDates = List.Transform(allDates, each Text.From(_)),
    dateCols = List.Generate(
            ()=>[d=List.Transform(allDates, 
                        (L)=> if L >=#"Changed Type"[From]{0} and L <=#"Changed Type"[Until]{0} then 1 else 0),
                        idx=0],
            each [idx]<Table.RowCount(#"Changed Type"),
            each [d=List.Transform(allDates, (L)=>
                        if L >=#"Changed Type"[From]{[idx]+1} and L <= #"Changed Type"[Until]{[idx]+1} then 1 else 0),
                        idx=[idx]+1],
            each Table.ToRecords(#"Changed Type"){[idx]} & Record.FromList([d], textDates)),
    dateTable = Table.FromRecords(dateCols),
    #"Removed Columns1" = Table.RemoveColumns(dateTable,{"From", "Until"}),
    #"Grouped Rows" = Table.Group(#"Removed Columns1", {"Name"}, {
        {"total row", (t)=> let 
            colNames = Table.ColumnNames(t),
            sumDates = List.Generate(
                ()=>[s=t[Name]{0}, idx=0],
                each [idx] < List.Count(colNames),
                each [s = List.Product(Table.Column(t,colNames{[idx]+1})), idx=[idx]+1],
                each [s])
            in sumDates, type list}
        }),
    #"Removed Columns2" = Table.RemoveColumns(#"Grouped Rows",{"Name"}),
    colNames2 = List.Accumulate(#"Removed Columns2"[total row],{"Date"},(state,current)=>
        state & List.Range(current,0,1)),
    data = List.Accumulate(#"Removed Columns2"[total row],{},(state, current)=>state & {List.RemoveFirstN(current,1)}),
    results = Table.FromColumns({allDates} & data, colNames2),
    typeIt = Table.TransformColumnTypes(results, {{"Date", type date}} &
       List.Transform(List.RemoveFirstN(Table.ColumnNames(results),1), each {_, Int64.Type})
),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(typeIt, {"Date"}, "Name", "Value")
in
    #"Unpivoted Other Columns"

 

I hope someone can help troubleshoot this problem or provide a better alternative solution. Any help is much appreciated! 

Here is the download link to my PBIX file (click the download icon at the top-right corner of the window): https://drive.google.com/file/d/15bEGr3tmVlRrSCb3LSnAn-KJgCi8J39c/view?usp=sharing

 

  • See the working here

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZJdS8MwFIb/Suj1YEnarvV2itPNIah4M3ZxbEMbyIekitZfb7JCPdF18/LAc16e87HbJRuoerK2RnTJLKHZnKZzTjn3BedjsZ8FsBZkI4wRdR/QBUJZjF462b1JMOSptfq1sybwF4hP6f+j0wi9Ah2Ct5USSsG54L80y6bpY9qsxC5lxN92rYcfZPPS/07mcfJaai0FuQFXS9OckV46+CT3RkilQhIWoL7I8N7Q7Xh6IlKAI0tw7iBaYNEiFrWu8aIrUMp+QKA5w3Qe0dfgoCV3/XBgNroNXUj0EZT2q1pZW+vDGaKpWB7Bz9JUwnxZsn137uCAphlWm50yDs84ivg+mk9+GqMIZYvp5bISD1f8gPtv", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, From = _t, Until = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"From", type date}, {"Until", type date}}),
        MinDate = List.Min(#"Changed Type"[From]),
        MaxDate = List.Max(#"Changed Type"[Until]),
        ListOfDates = List.Dates(MinDate,Duration.Days(MaxDate-MinDate)+1,#duration(1,0,0,0)),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each List.Dates([From],Duration.Days([Until]-[From])+1,#duration(1,0,0,0))),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"From", "Until"}),
        #"Added Custom1" = Table.AddColumn(#"Removed Columns", "Date", each ListOfDates),
        #"Removed Columns1" = Table.RemoveColumns(#"Added Custom1",{"Custom"}),
        #"Expanded Custom.1" = Table.ExpandListColumn(#"Removed Columns1", "Date"),
        #"Expanded Custom" = Table.ExpandListColumn(#"Removed Columns", "Custom"),
        #"Added Custom2" = Table.AddColumn(#"Expanded Custom", "Value", each 1),
        #"Merged Queries" = Table.NestedJoin(#"Expanded Custom.1", {"Name", "Date"}, #"Added Custom2", {"Name", "Custom"}, "Added Custom2", JoinKind.LeftOuter),
        #"Expanded Added Custom2" = Table.ExpandTableColumn(#"Merged Queries", "Added Custom2", {"Value"}, {"Value"}),
        #"Sorted Rows" = Table.Sort(#"Expanded Added Custom2",{{"Date", Order.Ascending}, {"Name", Order.Ascending}}),
        #"Removed Duplicates" = Table.Distinct(#"Sorted Rows", {"Name", "Date"}),
        #"Replaced Value" = Table.ReplaceValue(#"Removed Duplicates",null,0,Replacer.ReplaceValue,{"Value"}),
        #"Reordered Columns" = Table.ReorderColumns(#"Replaced Value",{"Date", "Name", "Value"})
    in
        #"Reordered Columns"

     

4 Replies

  • Vijay_A_Verma's avatar
    Vijay_A_Verma
    Most Valuable Professional

    See the working here

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jZJdS8MwFIb/Suj1YEnarvV2itPNIah4M3ZxbEMbyIekitZfb7JCPdF18/LAc16e87HbJRuoerK2RnTJLKHZnKZzTjn3BedjsZ8FsBZkI4wRdR/QBUJZjF462b1JMOSptfq1sybwF4hP6f+j0wi9Ah2Ct5USSsG54L80y6bpY9qsxC5lxN92rYcfZPPS/07mcfJaai0FuQFXS9OckV46+CT3RkilQhIWoL7I8N7Q7Xh6IlKAI0tw7iBaYNEiFrWu8aIrUMp+QKA5w3Qe0dfgoCV3/XBgNroNXUj0EZT2q1pZW+vDGaKpWB7Bz9JUwnxZsn137uCAphlWm50yDs84ivg+mk9+GqMIZYvp5bISD1f8gPtv", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, From = _t, Until = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"From", type date}, {"Until", type date}}),
        MinDate = List.Min(#"Changed Type"[From]),
        MaxDate = List.Max(#"Changed Type"[Until]),
        ListOfDates = List.Dates(MinDate,Duration.Days(MaxDate-MinDate)+1,#duration(1,0,0,0)),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each List.Dates([From],Duration.Days([Until]-[From])+1,#duration(1,0,0,0))),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"From", "Until"}),
        #"Added Custom1" = Table.AddColumn(#"Removed Columns", "Date", each ListOfDates),
        #"Removed Columns1" = Table.RemoveColumns(#"Added Custom1",{"Custom"}),
        #"Expanded Custom.1" = Table.ExpandListColumn(#"Removed Columns1", "Date"),
        #"Expanded Custom" = Table.ExpandListColumn(#"Removed Columns", "Custom"),
        #"Added Custom2" = Table.AddColumn(#"Expanded Custom", "Value", each 1),
        #"Merged Queries" = Table.NestedJoin(#"Expanded Custom.1", {"Name", "Date"}, #"Added Custom2", {"Name", "Custom"}, "Added Custom2", JoinKind.LeftOuter),
        #"Expanded Added Custom2" = Table.ExpandTableColumn(#"Merged Queries", "Added Custom2", {"Value"}, {"Value"}),
        #"Sorted Rows" = Table.Sort(#"Expanded Added Custom2",{{"Date", Order.Ascending}, {"Name", Order.Ascending}}),
        #"Removed Duplicates" = Table.Distinct(#"Sorted Rows", {"Name", "Date"}),
        #"Replaced Value" = Table.ReplaceValue(#"Removed Duplicates",null,0,Replacer.ReplaceValue,{"Value"}),
        #"Reordered Columns" = Table.ReorderColumns(#"Replaced Value",{"Date", "Name", "Value"})
    in
        #"Reordered Columns"

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi Vijay_A_Verma Thanks for your reply! I have only replaced the source by referring directly to another table already in Power Query (it has the same 3 columns but the start and end dates are as from 2021 until 2022) and i have used the exact same code as above but i am getting an error at the following step. Any idea how to fix it?

       

      Here is the new table:

      NameFromUntil
      Kacy Jones18/11/202108/12/2021
      Kade Kennedy24/11/202116/12/2021
      Cristian Thompson20/12/202112/12/2021
      Kade Kennedy29/11/202109/12/2021
      Damian Mclellan17/12/202129/12/2021
      Damian Mclellan20/12/202126/12/2021
      Cristian Thompson25/12/202129/12/2021
      Ishan Rigby17/12/202129/12/2021
      Jimmie Harding19/12/202125/12/2021
      Brax Oneill26/12/202131/12/2021
      Kacy Jones26/12/202131/12/2021
      Bear Barry27/12/202102/01/2022
      Jorgie Galloway03/01/202209/01/2022
      Farah Lyon07/01/202226/01/2022
      Salman Goodman20/12/202110/01/2022
      Vincenzo Murray22/12/202111/01/2022
      Jorgie Galloway19/12/202109/01/2022
      Kade Kennedy06/01/202227/01/2022
      Kacy Jones10/01/202216/01/2022
      Kacy Jones16/01/202222/01/2022
      Kade Kennedy27/01/202216/02/2022
      Cristian Thompson23/01/202230/01/2022
      Kade Kennedy28/01/202217/02/2022
      Damian Mclellan04/02/202216/02/2022
      Damian Mclellan31/01/202206/02/2022
      Cristian Thompson26/01/202216/02/2022
      Ishan Rigby31/01/202221/02/2022
      Jimmie Harding31/01/202216/02/2022
      Brax Oneill27/01/202215/02/2022
      Kacy Jones30/11/202130/11/2021
      Bear Barry30/11/202130/11/2021
      Jorgie Galloway09/02/202220/02/2022
      Farah Lyon16/02/202209/03/2022
      Salman Goodman10/02/202227/02/2022
      Vincenzo Murray19/02/202210/03/2022
      Jorgie Galloway20/02/202225/02/2022
      Kade Kennedy21/02/202227/02/2022
      Kacy Jones21/02/202214/03/2022
      Kacy Jones21/02/202213/03/2022
      Kade Kennedy14/02/202220/02/2022
      Cristian Thompson04/03/202222/03/2022
      Kade Kennedy28/02/202206/03/2022
      Damian Mclellan06/03/202212/03/2022
      Damian Mclellan09/03/202230/03/2022
      Cristian Thompson06/03/202213/03/2022
      Ishan Rigby09/03/202230/03/2022
      Jimmie Harding14/03/202230/03/2022
      Brax Oneill11/03/202216/03/2022
      Kacy Jones18/03/202218/03/2022
      Bear Barry14/03/202220/03/2022
      Jorgie Galloway14/03/202204/04/2022
      Farah Lyon09/03/202230/03/2022
      Salman Goodman30/11/202130/11/2021
      Vincenzo Murray28/03/202208/04/2022
      Jorgie Galloway23/03/202230/03/2022
      Kade Kennedy17/03/202227/03/2022
      Kacy Jones21/03/202225/03/2022
      Kacy Jones01/04/202221/04/2022
      Kade Kennedy28/03/202215/04/2022
      Cristian Thompson30/03/202220/04/2022
      Kade Kennedy12/04/202203/05/2022
      Damian Mclellan10/04/202216/04/2022
      Damian Mclellan18/04/202227/04/2022
      Cristian Thompson01/01/202201/01/2022
      • Vijay_A_Verma's avatar
        Vijay_A_Verma
        Most Valuable Professional

        Is there a data where Until < From? Just check it by putting in a custom column following and see if you get True somewhere..(Apply a filter on True)

        =[Until]<[From]