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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Gusd8
Frequent Visitor

Group like data onto single row

Greetings, thank you for looking at my post.

I am being given data in the following format

DateGroupType1Type2Type3
1/1/24Apple121826
1/1/24Banana91521
2/1/24Apple151736
2/1/24Banana16713

 

I need  to convert the data (preferably in Power Query Transform) to look like the format below

DateApple1Apple2Apple3Banana1Banana2Banana3
1/1/2412182691521
2/1/2415173616713

 

I am drawing a blank in my head how to do this. Any insight or advice would be greatly appreicated. Thank you again for looking at my post.

1 ACCEPTED SOLUTION
AUDISU
Resolver III
Resolver III

Hi @Gusd8 ,

Use following M code.

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31DcyUdJRciwoyEkF0oZGIMICSBiZKcXqIKlwSswDQiDDEqTCFKTCEKzCCMMMkKShOZAwNkNVATfD0AxIgFQYGivFxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Group = _t, Type1 = _t, Type2 = _t, Type3 = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Group", type text}, {"Type1", Int64.Type}, {"Type2", Int64.Type}, {"Type3", Int64.Type}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Date", "Group"}, "Attribute", "Value"),
#"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Attribute"}),
#"Grouped Rows" = Table.Group(#"Removed Columns", {"Date", "Group"}, {{"Count", each _, type table [Date=nullable date, Group=nullable text, Value=number]}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Count], "Index", 1, 1, Int64.Type)),
#"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"Count"}),
#"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns1", "Custom", {"Value", "Index"}, {"Value", "Index"}),
#"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Expanded Custom", {{"Index", type text}}, "en-AU"),{"Group", "Index"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged"),
#"Pivoted Column" = Table.Pivot(#"Merged Columns", List.Distinct(#"Merged Columns"[Merged]), "Merged", "Value", List.Sum)
in
#"Pivoted Column"

 

AUDISU_0-1711394522386.png

Thanks

View solution in original post

4 REPLIES 4
AUDISU
Resolver III
Resolver III

Hi @Gusd8 ,

Use following M code.

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31DcyUdJRciwoyEkF0oZGIMICSBiZKcXqIKlwSswDQiDDEqTCFKTCEKzCCMMMkKShOZAwNkNVATfD0AxIgFQYGivFxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Group = _t, Type1 = _t, Type2 = _t, Type3 = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Group", type text}, {"Type1", Int64.Type}, {"Type2", Int64.Type}, {"Type3", Int64.Type}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Date", "Group"}, "Attribute", "Value"),
#"Removed Columns" = Table.RemoveColumns(#"Unpivoted Other Columns",{"Attribute"}),
#"Grouped Rows" = Table.Group(#"Removed Columns", {"Date", "Group"}, {{"Count", each _, type table [Date=nullable date, Group=nullable text, Value=number]}}),
#"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([Count], "Index", 1, 1, Int64.Type)),
#"Removed Columns1" = Table.RemoveColumns(#"Added Custom",{"Count"}),
#"Expanded Custom" = Table.ExpandTableColumn(#"Removed Columns1", "Custom", {"Value", "Index"}, {"Value", "Index"}),
#"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Expanded Custom", {{"Index", type text}}, "en-AU"),{"Group", "Index"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged"),
#"Pivoted Column" = Table.Pivot(#"Merged Columns", List.Distinct(#"Merged Columns"[Merged]), "Merged", "Value", List.Sum)
in
#"Pivoted Column"

 

AUDISU_0-1711394522386.png

Thanks

Gusd8
Frequent Visitor

Hi @AUDISU I have to apologize, your sloulution did EXACTLY what I asked. My intent was to take the insight I was provided and then engineer it to work with the data I have. The "index" works to provide what I had asked...but sadly I didn't correctly ask what I needed. For this I am sorry. What I am really wanted is listed below. I hope it is more precise and easily achived. Your reply was brilliant and I am excited about another solution you may have for me. Thank you in advance for sharing your knowledge!!

 

DateSourceBatchesDocumentsPages
1/1/24Apple121826
1/1/24Banana91521
2/1/24Apple151736
2/1/24Banana16713

 

and I want it to look like...

DateApple_BatchesApple_DocumentsApple_PagesBanana_BatchesBanana_DocumentsBanana_Pages
1/1/2412182691521
2/1/2415173616713

 

 

Gusd8
Frequent Visitor

@AUDISU You know what...I cheated...I used the index and I renamed the columns as the last step (since the column headers in this instance will always be static). I think it would still be very cool if there was a solution that was dynamic in case teh column headers are not static...but I am marking this solved as I was able to make it work.

 

Thank You VERY MUCH for your help with this!!!

Hi @Gusd8 
Use following M code.

 

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31DcyUdJRciwoyEkF0oZGIMICSBiZKcXqIKlwSswDQiDDEqTCFKTCEKzCCMMMkKShOZAwNkNVATfD0AxIgFQYGivFxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, Source = _t, Batches = _t, Documents = _t, Pages = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Source", type text}, {"Batches", Int64.Type}, {"Documents", Int64.Type}, {"Pages", Int64.Type}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Date", "Source"}, "Attribute", "Value"),
#"Merged Columns" = Table.CombineColumns(#"Unpivoted Other Columns",{"Source", "Attribute"},Combiner.CombineTextByDelimiter("_", QuoteStyle.None),"Merged"),
#"Pivoted Column" = Table.Pivot(#"Merged Columns", List.Distinct(#"Merged Columns"[Merged]), "Merged", "Value", List.Sum)
in
#"Pivoted Column"

 Thanks

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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