Forum Discussion

hackfifi's avatar
hackfifi
Helper V
6 years ago
Solved

Duplicating Rows based on Value in Column

Hi All - I need support via Power Query to transfrom my data as below: Essentially Duplicated the Rows, and convert the "count" to 1   Also, data table is added below the screenshot.   Cheers!! ...
  • dax's avatar
    dax
    6 years ago

    Hi hackfifi , 

    You could try below M code 

    let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTKEYiMwjtWJVnKC8yDYECzqjKTWGIxjYwE=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Type = _t, #"Jan-20" = _t, #"Feb-20" = _t, #"Mar-20" = _t, #"Apr-20" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Type", type text}, {"Jan-20", Int64.Type}, {"Feb-20", Int64.Type}, {"Mar-20", Int64.Type}, {"Apr-20", Int64.Type}}),
    #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Type"}, "Attribute", "Value"),
        #"Added Custom1" = Table.AddColumn(#"Unpivoted Columns", "Custom", each {Number.From(1)..Number.From([Value])}),
        #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom1", "Custom"),
        #"Pivoted Column" = Table.Pivot(#"Expanded Custom", List.Distinct(#"Expanded Custom"[Attribute]), "Attribute", "Value", List.Count),
        #"Removed Columns" = Table.RemoveColumns(#"Pivoted Column",{"Custom"}),
        #"Replaced Value" = Table.ReplaceValue(#"Removed Columns",0,null,Replacer.ReplaceValue,{"Jan-20", "Feb-20", "Mar-20", "Apr-20"})
    in
        #"Replaced Value"

    Best Regards,
    Zoe Zhi

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