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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
bhaskarpbi999
Helper III
Helper III

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.

bhaskarpbi999_0-1722435707425.png

 

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

 

bhaskarpbi999_2-1722435858913.png

 

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

 

 

1 ACCEPTED SOLUTION
Ahmedx
Super User
Super User

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"

View solution in original post

8 REPLIES 8
dufoq3
Super User
Super User

Hi @bhaskarpbi999, different approach:

 

Result

dufoq3_0-1722601096404.png

 

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

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Hi DUFO, 

 

Many thanks for your support.

 

Regards,

Bhaskar

 

You're welcome 😉


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Ahmedx
Super User
Super User

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"

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.

 

bhaskarpbi999_0-1722513693958.png

 

 

Result screenshot which has issue

bhaskarpbi999_1-1722513784064.png

 

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,

 

 

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

Screenshot_1.pngScreenshot_2.png

Hi Ahmed,

 

I have checked the date column values and it was taking 3 characters so i trimmed it the issue is resolved.

Kudos to you . Accepting your solution.

 

Many thanks to you for extreme support regarding this Post.

 

Regards,

Bhaskar

bhaskarpbi999
Helper III
Helper III

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.

Top Solution Authors