Forum Discussion

bhaskarpbi999's avatar
2 years ago
Solved

Split Weekday time values in to differetn columns for each unque Index ID

Hi Team,

I have data like below where each unique index has 7 weedkdays with 2 time values for each day.

 

I need output like below where each time value should be in different columss.

 

 

Sample data below 

IndexDateTime
1MO09:00-14:00
1MO16:00-18:00
1TU09:00-14:00
1TU16:00-18:00
1WE09:00-14:00
1WE16:00-18:00
1TH09:00-14:00
1TH16:00-18:00
1FR09:00-14:00
1FR16:00-18:00
2MO09:00-14:00
2MO16:00-18:00
2TU09:00-14:00
2TU16:00-18:00
2WE09:00-14:00
2WE16:00-18:00
2TH09:00-14:00
2TH16:00-18:00
2FR09:00-14:00
2FR16:00-18:00
3MO09:30-14:00
3MO18:00-21:00
3TU09:30-14:00
3TU18:00-21:00
3WE09:30-14:00
3WE18:00-21:00
3TH09:30-14:00
3TH18:00-21:00
3FR09:30-14:00
3FR18:00-21:00
3SA09:30-14:00
3SA18:00-21:00
4MO07:00-22:00
4TU07:00-22:00
4WE07:00-22:00
4TH07:00-22:00
4FR07:00-22:00
4SA07:00-22:00
4SU07:00-22:00
5MO09:00-13:30
5MO16:30-20:00
5TU09:00-13:30
5TU16:30-20:00
5WE09:00-13:30
5WE16:30-20:00
5TH09:00-13:30
5TH16:30-20:00
5FR09:00-13:30
5FR16:30-20:00
5SA10:00-14:00

 

Thanks for your help in advance

 

Regards,

Bhaskar

 

 

  • pls try this

    let
    
        DayName = (shortDay as text) as text =>
          [
            days = [
                MO = "Monday",
                TU = "Tuesday",
                WE = "Wednesday",
                TH = "Thursday",
                FR = "Friday",
                SA = "Saturday",
                SU = "Sunday"
            ],
            fullDayName = Record.FieldOrDefault(days, Text.Upper(shortDay), "Unknown day")
          ][fullDayName],
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dZO7DoMwDEX/JTNIiaHPrQOVl6pSH+qA+P/**bleep**QryEl8lyyHI3OCWdeQwhAez3zEyzXGMc35DNtgQDoKOFvw+QJDgGf8FmAIcGcwmsHAuL+AIaA1CJUTKidUTqicUDmhckLlhMoJlRMqn0z5ZI0dyLMjJQtKeWdouWOU8s7Qcm8GoxkMjFLeGVruGO8bMAS0xrzf1UkAWaBX4gAt9wwGQDscoK/rAW/4oVnqKWfWIC9DLqdoDbvUlVGWujPsUldGWep+BqMZDAy71JVRlroz9AtG8xtsfw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Index = _t, Date = _t, Time = _t]),
        #"Added Custom" = Table.AddColumn(Source, "Custom", each DayName([Date])& " Open/Close"),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Date"}),
        #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom", "Date"}}),
        #"Grouped Rows" = Table.Group(#"Renamed Columns", {"Index", "Date"}, {{"tmp", 
    (x)=> [ a = Table.AddIndexColumn(x, "Id", 1, 1, Int64.Type),
    b = Table.CombineColumns(Table.TransformColumnTypes(a, {{"Id", type text}}),{"Date", "Id"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged"),
    c = Table.Pivot(b, List.Distinct(b[Merged]), "Merged", "Time")
    ][c]
    
    }}),
        #"Grouped Rows1" = Table.Group(#"Grouped Rows", {"Index"}, {{"final", 
    (x)=> [
        a=  Table.Combine(x[tmp]),
        b = Table.FirstN( Table.FillUp(a,Table.ColumnNames(a)),1)][b]
    
    }}),
        #"Removed Other Columns" = Table.Combine( Table.SelectColumns(#"Grouped Rows1",{"final"})[final])
    in
        #"Removed Other Columns"

8 Replies

  • dufoq3's avatar
    dufoq3
    Community Champion

    Hi bhaskarpbi999, different approach:

     

    Result

     

    v1 (empty days included)

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dZO7DoMwDEX/JTNIiaHPrQOVl6pSH+qA+P/**bleep**QryEl8lyyHI3OCWdeQwhAez3zEyzXGMc35DNtgQDoKOFvw+QJDgGf8FmAIcGcwmsHAuL+AIaA1CJUTKidUTqicUDmhckLlhMoJlRMqn0z5ZI0dyLMjJQtKeWdouWOU8s7Qcm8GoxkMjFLeGVruGO8bMAS0xrzf1UkAWaBX4gAt9wwGQDscoK/rAW/4oVnqKWfWIC9DLqdoDbvUlVGWujPsUldGWep+BqMZDAy71JVRlroz9AtG8xtsfw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Index = _t, Date = _t, Time = _t]),
        DayList = List.Buffer({{"MO", "Monday"}, {"TU", "Tuesday"}, {"WE", "Wednesday"}, {"TH", "Thursday"}, {"FR", "Friday"}, {"SA", "Saturday"}, {"SU", "Sunday"}}),
        ReplacedDate = Table.TransformColumns(Source, {{"Date", each 
            [ a = List.Select(DayList, (x)=> List.Contains(x, _)){0}?,
              b = Text.Replace(_, a{0}, a{1})
            ][b], type text }}),
    
        fnTransform = 
            (myTable as table)=>
            let
                // _Detail = GroupedRows{[Index="1"]}[All],
                _Detail = myTable,
                _ChangedType = Table.TransformColumnTypes(_Detail,{{"Index", Int64.Type}}),
                _Buffered = Table.Buffer(_ChangedType),
                _Accumulated = List.Accumulate(
                    List.Transform(DayList, (x)=> x{1}),
                    #table(type table[Index=Int64.Type], {{_Buffered{0}?[Index]?}}),
                    (state, current)=> [ a = Table.SelectRows(_Buffered, (x)=> x[Date] = current),
                                        b = List.Accumulate( {1..(if Table.IsEmpty(a) then 2 else Table.RowCount(a))}, state,
                                                    (innerState, innerCurrent)=> Table.AddColumn(innerState, current & " Open / Close " & Text.From(innerCurrent), (y)=> if Table.IsEmpty(a) then null else a{innerCurrent-1}?[Time]?, type text))
                                    ][b] )
            in _Accumulated,
    
        GroupedRows = Table.Group(ReplacedDate, {"Index"}, {{"fn", fnTransform, type table}}),
        Combined = Table.Combine(GroupedRows[fn])
    in
        Combined

     

     

    v2 (empty days excluded)

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dZO7DoMwDEX/JTNIiaHPrQOVl6pSH+qA+P/**bleep**QryEl8lyyHI3OCWdeQwhAez3zEyzXGMc35DNtgQDoKOFvw+QJDgGf8FmAIcGcwmsHAuL+AIaA1CJUTKidUTqicUDmhckLlhMoJlRMqn0z5ZI0dyLMjJQtKeWdouWOU8s7Qcm8GoxkMjFLeGVruGO8bMAS0xrzf1UkAWaBX4gAt9wwGQDscoK/rAW/4oVnqKWfWIC9DLqdoDbvUlVGWujPsUldGWep+BqMZDAy71JVRlroz9AtG8xtsfw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Index = _t, Date = _t, Time = _t]),
        DayList = List.Buffer({{"MO", "Monday"}, {"TU", "Tuesday"}, {"WE", "Wednesday"}, {"TH", "Thursday"}, {"FR", "Friday"}, {"SA", "Saturday"}, {"SU", "Sunday"}}),
        ReplacedDate = Table.TransformColumns(Source, {{"Date", each 
            [ a = List.Select(DayList, (x)=> List.Contains(x, _)){0}?,
              b = Text.Replace(_, a{0}, a{1})
            ][b], type text }}),
        GroupedRows = Table.Group(ReplacedDate, {"Index", "Date"}, {{"All", each 
            [ a = Table.AddIndexColumn(_, "IndexHelper", 1, 1),
              b = Table.TransformColumnTypes(a,{{"IndexHelper", type text}}), 
              c = Table.CombineColumns(b, {"Date", "IndexHelper"},Combiner.CombineTextByDelimiter(" Open / Close ", QuoteStyle.None),"Open / Close")
            ][c], type table}}),
        Combined = Table.Combine(GroupedRows[All]),
        PivotedColumn = Table.Pivot(Combined, List.Distinct(Combined[#"Open / Close"]), "Open / Close", "Time")
    in
        PivotedColumn
  • pls try this

    let
    
        DayName = (shortDay as text) as text =>
          [
            days = [
                MO = "Monday",
                TU = "Tuesday",
                WE = "Wednesday",
                TH = "Thursday",
                FR = "Friday",
                SA = "Saturday",
                SU = "Sunday"
            ],
            fullDayName = Record.FieldOrDefault(days, Text.Upper(shortDay), "Unknown day")
          ][fullDayName],
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("dZO7DoMwDEX/JTNIiaHPrQOVl6pSH+qA+P/**bleep**QryEl8lyyHI3OCWdeQwhAez3zEyzXGMc35DNtgQDoKOFvw+QJDgGf8FmAIcGcwmsHAuL+AIaA1CJUTKidUTqicUDmhckLlhMoJlRMqn0z5ZI0dyLMjJQtKeWdouWOU8s7Qcm8GoxkMjFLeGVruGO8bMAS0xrzf1UkAWaBX4gAt9wwGQDscoK/rAW/4oVnqKWfWIC9DLqdoDbvUlVGWujPsUldGWep+BqMZDAy71JVRlroz9AtG8xtsfw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Index = _t, Date = _t, Time = _t]),
        #"Added Custom" = Table.AddColumn(Source, "Custom", each DayName([Date])& " Open/Close"),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Date"}),
        #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom", "Date"}}),
        #"Grouped Rows" = Table.Group(#"Renamed Columns", {"Index", "Date"}, {{"tmp", 
    (x)=> [ a = Table.AddIndexColumn(x, "Id", 1, 1, Int64.Type),
    b = Table.CombineColumns(Table.TransformColumnTypes(a, {{"Id", type text}}),{"Date", "Id"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged"),
    c = Table.Pivot(b, List.Distinct(b[Merged]), "Merged", "Time")
    ][c]
    
    }}),
        #"Grouped Rows1" = Table.Group(#"Grouped Rows", {"Index"}, {{"final", 
    (x)=> [
        a=  Table.Combine(x[tmp]),
        b = Table.FirstN( Table.FillUp(a,Table.ColumnNames(a)),1)][b]
    
    }}),
        #"Removed Other Columns" = Table.Combine( Table.SelectColumns(#"Grouped Rows1",{"final"})[final])
    in
        #"Removed Other Columns"
    • bhaskarpbi999's avatar
      bhaskarpbi999
      Helper V

      Hi Ahmed,

      Thanks for your supportt. I am using  the above code but i am getting "unknownday" Result.

       

      Sample data in source query DHL_Open time.

       

       

       

      Result screenshot which has issue

       

      Code which i am using 

       

      let

      DayName = (shortDay as text) as text =>
      [
      days = [
      MO = "Monday",
      TU = "Tuesday",
      WE = "Wednesday",
      TH = "Thursday",
      FR = "Friday",
      SA = "Saturday",
      SU = "Sunday"
      ],
      fullDayName = Record.FieldOrDefault(days, Text.Upper(shortDay), "Unknown day")
      ][fullDayName],
      Source = DHL_OpenTime,
      #"Added Custom" = Table.AddColumn(Source, "Custom", each DayName([Date]) & " Open/Close"),
      #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Date"}),
      #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Custom", "Date"}}),
      #"Grouped Rows" = Table.Group(#"Renamed Columns", {"Index", "Date"}, {{"tmp",
      (x)=> [ a = Table.AddIndexColumn(x, "Id", 1, 1, Int64.Type),
      b = Table.CombineColumns(Table.TransformColumnTypes(a, {{"Id", type text}}),{"Date", "Id"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged"),
      c = Table.Pivot(b, List.Distinct(b[Merged]), "Merged", "Time")
      ][c]
      }}),
      #"Grouped Rows1" = Table.Group(#"Grouped Rows", {"Index"}, {{"final",
      (x)=> [
      a= Table.Combine(x[tmp]),
      b = Table.FirstN( Table.FillUp(a,Table.ColumnNames(a)),1)][b]

      }}),
      #"Removed Other Columns" = Table.Combine( Table.SelectColumns(#"Grouped Rows1",{"final"})[final])
      in
      #"Removed Other Columns"

       

      Many thanks for your support. could you please assist me on this issue,

       

       

      • Ahmedx's avatar
        Ahmedx
        Super User

        I can't say why you have it like that, most likely you have unprinted characters there.
        check the line, it should be two
        to do this, add a column and write