Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Create multiple rows from single row based on conditions

Hi all,   I would like to turn the following table: Criteria  Amount Month Up 1,000 7 Down 500 3   into the following table: Criteria  Amount Month Up 1,000 7 Up ...
  • AlB's avatar
    AlB
    5 years ago

    Anonymous 

    If you want it in PQ, paste this M code in a blank query to see the steps:

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCi1Q0lEyNDAwAFLmSrE60Uou+eV5QI4pWMhYKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Criteria = _t, Amount = _t, Month = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Criteria", type text}, {"Amount", Int64.Type}, {"Month", Int64.Type}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each List.Numbers(if [Criteria]="Up" then [Month] else 1 , if [Criteria]="Up" then 12-[Month]+1 else [Month] )),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Month"}),
        #"Expanded Custom" = Table.ExpandListColumn(#"Removed Columns", "Custom"),
        #"Renamed Columns" = Table.RenameColumns(#"Expanded Custom",{{"Custom", "Month"}})
    in
        #"Renamed Columns"

     

    Please mark the question solved when done and consider giving kudos if posts are helpful.

    Contact me privately for support with any larger-scale BI needs, tutoring, etc.

    Cheers