Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Removing duplicates from a column, creating new columns for each distinct value

Sorry, I'm confident there is an answer to this question somewhere on the community but I'm new to Power BI and am struggling to express my problem in the right termanology.

 

Essentially, in the Power Query Editor I am looking to go from a table like this:

 

IDValue
1A
2

A

3A
3B
3C
4A
4B

 

IDABC
1A  
2A  
3ABC
4AB 

 

Or something to that affect. The problem arises when I am creating a table in a report. When I add the Value column it creates another row with the same ID, whereas I want the rows to be a unique list of IDs with all Values having their own seperate column.

 

Hope that makes sense and thank you for any help.

 

Alex

  • You could duplicate [Value] and pivot the duplicated column with no aggregation and values from [Value]

7 Replies

  • Smauro's avatar
    Smauro
    Icon for Solution Sage rankSolution Sage

    You could duplicate [Value] and pivot the duplicated column with no aggregation and values from [Value]

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thank you for that!

  • Anonymous's avatar
    Anonymous
    Not applicable
    et
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXJUitWJVjKCs4xRWE5wljOYZQKXNYHIxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ID = _t, Value = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Value", type text}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"lst", each _[Value]}}),
        #"Extracted Values" = Table.TransformColumns(#"Grouped Rows", {"lst", each Text.Combine(List.Transform(_, Text.From), ","), type text}),
        #"Split Column by Delimiter" = Table.SplitColumn(#"Extracted Values", "lst", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"lst.1", "lst.2", "lst.3"},"")
    in
        #"Split Column by Delimiter"
  • chrschmi's avatar
    chrschmi
    Icon for Microsoft Employee rankMicrosoft Employee

    Will creating a matrix instead of a table work for you? In the visualizations pane simply select the visual next to the table one. Then add the "Value" column in your example under the Columns and then drag/drop again under the Values section, like this:

     

     

    • Anonymous's avatar
      Anonymous
      Not applicable
      let
          Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXJUitWJVjKCs4xRWE5wljOYZQKXNYHIxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ID = _t, Value = _t]),
          #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Value", type text}}),
          #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"lst", each Table.Transpose(_[[Value]])}}),
          #"Expanded lst" = Table.ExpandTableColumn(#"Grouped Rows", "lst", {"Column1", "Column2", "Column3"}, {"A", "B", "C"})
      in
          #"Expanded lst"
  • Anonymous's avatar
    Anonymous
    Not applicable

    Thank you for your help everyone, much appreciated!

  • Anonymous's avatar
    Anonymous
    Not applicable

    yet antother way, via record functions

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXJUitWJVjKCs4xRWE5wljOYZQKXNYHIxgIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [ID = _t, Value = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Value", type text}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"lst", each Record.FromList(_[Value],_[Value])}}),
        #"Expanded lst" = Table.ExpandRecordColumn(#"Grouped Rows", "lst", List.Distinct(Source[Value]))
    in
        #"Expanded lst"