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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Justas4478
Post Patron
Post Patron

Selected columns change to rows in tables

Hi, I have table that has dates as columns and working hours in those columns rows.

Justas4478_0-1674840171431.png

Is it possible to change table to looks like this in the power query editor?

Justas4478_1-1674840276452.png 

Please let me know if it can be done or do I need to change it in the excel file manually?.

Thanks.

 

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

Hi @Justas4478 ,

 

There should be multiple steps for you to transform your data.

Merge [First Name] and [Surname] columns > Remove [Dept/Shift] column > Select new [Name] column and Unpivot other columns > Group [Value] by new [Name] and new [Date] columns with SUM operation.

My Sample:

RicoZhou_0-1675070324704.png

Result is as below.

RicoZhou_1-1675070558051.png

Whole M Code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fc5LCoAgFAXQrYTjDH35KtpAi5AGDRoaUbT//D/6gnId3MNVazZgAavhqle4GlayZTKztLkfW3raA1WLFGwsPQyIS/EjsQKkSBJ6Ie6rQBbeVoWLz+Wnvi77iLqx65Ph4RPR1+Tr4Lt4s3Ik9xX1FfV9fTwB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Dept/Shift" = _t, #"First Name" = _t, Surname = _t, #"2023/01/02" = _t, #"2023/01/03" = _t, #"2023/01/04" = _t, #"2023/01/05" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Dept/Shift", type text}, {"First Name", type text}, {"Surname", type text}, {"2023/01/02", type number}, {"2023/01/03", type number}, {"2023/01/04", type number}, {"2023/01/05", type number}}),
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type","name","Name",Replacer.ReplaceText,{"First Name"}),
    #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","surname","Surname",Replacer.ReplaceText,{"Surname"}),
    #"Merged Columns" = Table.CombineColumns(#"Replaced Value1",{"First Name", "Surname"},Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"Name"),
    #"Removed Columns" = Table.RemoveColumns(#"Merged Columns",{"Dept/Shift"}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Removed Columns", {"Name"}, "Attribute", "Value"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Unpivoted Other Columns",{{"Attribute", type date}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"Attribute", "Date"}}),
    #"Grouped Rows" = Table.Group(#"Renamed Columns", {"Name", "Date"}, {{"Work Hours", each List.Sum([Value]), type number}})
in
    #"Grouped Rows"

 

Best Regards,
Rico Zhou

 

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

2 REPLIES 2
v-rzhou-msft
Community Support
Community Support

Hi @Justas4478 ,

 

There should be multiple steps for you to transform your data.

Merge [First Name] and [Surname] columns > Remove [Dept/Shift] column > Select new [Name] column and Unpivot other columns > Group [Value] by new [Name] and new [Date] columns with SUM operation.

My Sample:

RicoZhou_0-1675070324704.png

Result is as below.

RicoZhou_1-1675070558051.png

Whole M Code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fc5LCoAgFAXQrYTjDH35KtpAi5AGDRoaUbT//D/6gnId3MNVazZgAavhqle4GlayZTKztLkfW3raA1WLFGwsPQyIS/EjsQKkSBJ6Ie6rQBbeVoWLz+Wnvi77iLqx65Ph4RPR1+Tr4Lt4s3Ik9xX1FfV9fTwB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Dept/Shift" = _t, #"First Name" = _t, Surname = _t, #"2023/01/02" = _t, #"2023/01/03" = _t, #"2023/01/04" = _t, #"2023/01/05" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Dept/Shift", type text}, {"First Name", type text}, {"Surname", type text}, {"2023/01/02", type number}, {"2023/01/03", type number}, {"2023/01/04", type number}, {"2023/01/05", type number}}),
    #"Replaced Value" = Table.ReplaceValue(#"Changed Type","name","Name",Replacer.ReplaceText,{"First Name"}),
    #"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","surname","Surname",Replacer.ReplaceText,{"Surname"}),
    #"Merged Columns" = Table.CombineColumns(#"Replaced Value1",{"First Name", "Surname"},Combiner.CombineTextByDelimiter(" ", QuoteStyle.None),"Name"),
    #"Removed Columns" = Table.RemoveColumns(#"Merged Columns",{"Dept/Shift"}),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Removed Columns", {"Name"}, "Attribute", "Value"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Unpivoted Other Columns",{{"Attribute", type date}}),
    #"Renamed Columns" = Table.RenameColumns(#"Changed Type1",{{"Attribute", "Date"}}),
    #"Grouped Rows" = Table.Group(#"Renamed Columns", {"Name", "Date"}, {{"Work Hours", each List.Sum([Value]), type number}})
in
    #"Grouped Rows"

 

Best Regards,
Rico Zhou

 

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

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.