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
RichardP
Helper I
Helper I

Creating a column to help pivot data

Hi all,

 

Does anyone have advice on how to go about pivoting some data?  My current data looks like this:

 

Order NoLine ItemIndex
0001Red Book1
0002Blue Book2
0002Green Book3
0003Blue Book4
0004Yellow Book5
0004Purple Book6
0004Green Book7
0004Blue Book8

 

What I'm trying to pivot to is this structure:

 

Order NoLine Item 1Line Item 2Line Item 3Line Item 4
0001Red Book   
0002Blue BookGreen Book  
0003Blue Book   
0004Yellow BookPurple BookGreen BookBlue Book

 

My problem is that there are varying numbers of Line Items per Order, so how do I create a column in the source data to use as the "Values column" to create the new columns?

 

Thanks in advance for your help 🙂

 

Richard

1 ACCEPTED SOLUTION
PattemManohar
Community Champion
Community Champion

@RichardP  Please try this in Power Query Editor

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjAwMFTSUQpKTVFwys/PBjINlWJ1wOJGQI5TTmkqTMIIWcK9KDU1DyZjDJMxRtNiApMwAXIiU3Ny8sthUqbIUgGlRQU5cF1myFIoFpkjyyBbZKEUGwsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Order No" = _t, LineItem = _t, Index = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Order No", Int64.Type}, {"LineItem", type text}, {"Index", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Order No"}, {{"AllRows", each _, type table}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "LineItem", each [AllRows][LineItem]),
    #"Extracted Values" = Table.TransformColumns(#"Added Custom", {"LineItem", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Extracted Values", "LineItem", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"LineItem.1", "LineItem.2", "LineItem.3", "LineItem.4"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"LineItem.1", type text}, {"LineItem.2", type text}, {"LineItem.3", type text}, {"LineItem.4", type text}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"AllRows"})
in
    #"Removed Columns"

For Reference, Here is the overview of the steps that are implemented as above.

 

image.pngimage.pngimage.pngimage.pngimage.png





Did I answer your question? Mark my post as a solution!

Proud to be a PBI Community Champion




View solution in original post

3 REPLIES 3
PattemManohar
Community Champion
Community Champion

@RichardP  Please try this in Power Query Editor

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjAwMFTSUQpKTVFwys/PBjINlWJ1wOJGQI5TTmkqTMIIWcK9KDU1DyZjDJMxRtNiApMwAXIiU3Ny8sthUqbIUgGlRQU5cF1myFIoFpkjyyBbZKEUGwsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Order No" = _t, LineItem = _t, Index = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Order No", Int64.Type}, {"LineItem", type text}, {"Index", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Order No"}, {{"AllRows", each _, type table}}),
    #"Added Custom" = Table.AddColumn(#"Grouped Rows", "LineItem", each [AllRows][LineItem]),
    #"Extracted Values" = Table.TransformColumns(#"Added Custom", {"LineItem", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Extracted Values", "LineItem", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"LineItem.1", "LineItem.2", "LineItem.3", "LineItem.4"}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"LineItem.1", type text}, {"LineItem.2", type text}, {"LineItem.3", type text}, {"LineItem.4", type text}}),
    #"Removed Columns" = Table.RemoveColumns(#"Changed Type1",{"AllRows"})
in
    #"Removed Columns"

For Reference, Here is the overview of the steps that are implemented as above.

 

image.pngimage.pngimage.pngimage.pngimage.png





Did I answer your question? Mark my post as a solution!

Proud to be a PBI Community Champion




Anonymous
Not applicable

another way using the pivot function with an appropriate aggregation function

 

 

 

let
  Source = Table.FromRows(
    Json.Document(
      Binary.Decompress(
        Binary.FromText(
          "i45WMjAwMFTSUQpKTVFwys/PBjINlWJ1wOJGQI5TTmkqTMIIWcK9KDU1DyZjDJMxRtNiApMwAXIiU3Ny8sthUqbIUgGlRQU5cF1myFIoFpkjyyBbZKEUGwsA", 
          BinaryEncoding.Base64
        ), 
        Compression.Deflate
      )
    ), 
    let
      _t = ((type text) meta [Serialized.Text = true])
    in
      type table[#"Order No" = _t, #"Line Item" = _t, Index = _t]
  ),
  #"Changed Type" = Table.TransformColumnTypes(
    Source, 
    {{"Order No", Int64.Type}, {"Line Item", type text}, {"Index", Int64.Type}}
  ),
  #"Removed Columns" = Table.RemoveColumns(#"Changed Type", {"Index"}),
  #"Pivoted Column" = Table.Pivot(
    Table.TransformColumnTypes(#"Removed Columns", {{"Order No", type text}}, "it-IT"), 
    List.Distinct(
      Table.TransformColumnTypes(#"Removed Columns", {{"Order No", type text}}, "it-IT")[#"Order No"]
    ), 
    "Order No", 
    "Line Item", 
    (t) => Text.Combine(t, "#")
  ),
  #"Demoted Headers" = Table.DemoteHeaders(#"Pivoted Column"),
  #"Transposed Table" = Table.Transpose(#"Demoted Headers"),
  #"Split Column by Delimiter" = Table.SplitColumn(
    #"Transposed Table", 
    "Column2", 
    Splitter.SplitTextByDelimiter("#", QuoteStyle.Csv), 
    {"Column2.1", "Column2.2", "Column2.3", "Column2.4"}
  )
in
  #"Split Column by Delimiter"

 

 

jthomson
Solution Sage
Solution Sage

Try searching for ranking within a group to get you started

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.