Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

Add a conditional column with multiple conditions

Hello all,
I have a dataset with a top material number, in another column the component number that goes into the top material and a calculation number.
I would like to generate a new column in PowerQuery. This column should basically reflect the top material. However, if the top material corresponds to the component, the value in the new column for this calculation number should remain empty or zero.

In this case, the value zero would have to be displayed in two rows, because both rows have the same Calc number.

Top MaterialKompCalc NrExpected Column 
00002211000000221100000105911604null
0000221100 000105911604null
0923148340502 0001075700640923148340502
0923148340502 0001075700640923148340502
0923148340502 0001075700640923148340502
09231483405020923-00160001075700640923148340502
092314834050200880092000001075700640923148340502
0923148340502 0001075700640923148340502
  • Anonymous's avatar
    Anonymous
    3 years ago

    Hi Anonymous ,

     

    Please refer to the following steps.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMgACIyNDQwMDJR0MjqGBqaWhoZmBiVKsDppSBawqLI2MDU0sjE0MTA2MkBWZm5obGJjRWhGIrwtUZEaUYgMLCwOgIMKvpNkfCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Top Material" = _t, Komp = _t, #"Calc Nr" = _t]),
        #"Grouped Rows" = Table.Group(Source, {"Calc Nr"}, {{"Data", each _}}),
        Custom1 = Table.TransformColumns(#"Grouped Rows",{"Data",each Table.SelectRows(_,each [Komp]=[Top Material])}),
        #"Expanded Data" = Table.ExpandTableColumn(Custom1, "Data", {"Calc Nr"}, {"Calc Nr.1"}),
        #"Merged Queries" = Table.NestedJoin(Source, {"Calc Nr"}, #"Expanded Data", {"Calc Nr.1"}, "Expanded Data", JoinKind.LeftOuter),
        #"Expanded Expanded Data" = Table.ExpandTableColumn(#"Merged Queries", "Expanded Data", {"Calc Nr.1"}, {"Calc Nr.1"}),
        #"Added Custom" = Table.AddColumn(#"Expanded Expanded Data", "Expected Column", each if [Calc Nr.1] <> null then null else [Top Material]),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Calc Nr.1"})
    in
        #"Removed Columns"

    Best Regards,
    Gao

    Community Support Team

     

    If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

    How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

2 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hello all,
    I have a dataset with a top material number, in another column the component number that goes into the top material and a calculation number.
    I would like to generate a new column in PowerQuery. This column should basically reflect the top material. However, if the top material corresponds to the component, the value in the new column for this calculation number should remain empty or zero.

    In this case, the value zero would have to be displayed in two rows, because both rows have the same Calc number.

    Top MaterialKomp         Calc NrExpected New Column Material
    00002211000000221100000105911604null
    0000221100 000105911604null
    0965022511302 0001108707450965022511302
    0965022511302 0001108707450965022511302
    0965022511302 0001108707450965022511302
    0965022511302 0001108707450965022511302
    0965022511302 0001108707450965022511302
    0965022511302 0001108707450965022511302
    096502251130200802411000001108707450965022511302
    0965022511302 0001108707450965022511302

     

     

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous ,

     

    Please refer to the following steps.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMgACIyNDQwMDJR0MjqGBqaWhoZmBiVKsDppSBawqLI2MDU0sjE0MTA2MkBWZm5obGJjRWhGIrwtUZEaUYgMLCwOgIMKvpNkfCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Top Material" = _t, Komp = _t, #"Calc Nr" = _t]),
        #"Grouped Rows" = Table.Group(Source, {"Calc Nr"}, {{"Data", each _}}),
        Custom1 = Table.TransformColumns(#"Grouped Rows",{"Data",each Table.SelectRows(_,each [Komp]=[Top Material])}),
        #"Expanded Data" = Table.ExpandTableColumn(Custom1, "Data", {"Calc Nr"}, {"Calc Nr.1"}),
        #"Merged Queries" = Table.NestedJoin(Source, {"Calc Nr"}, #"Expanded Data", {"Calc Nr.1"}, "Expanded Data", JoinKind.LeftOuter),
        #"Expanded Expanded Data" = Table.ExpandTableColumn(#"Merged Queries", "Expanded Data", {"Calc Nr.1"}, {"Calc Nr.1"}),
        #"Added Custom" = Table.AddColumn(#"Expanded Expanded Data", "Expected Column", each if [Calc Nr.1] <> null then null else [Top Material]),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Calc Nr.1"})
    in
        #"Removed Columns"

    Best Regards,
    Gao

    Community Support Team

     

    If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

    How to get your questions answered quickly --  How to provide sample data in the Power BI Forum