Forum Discussion

markabdey's avatar
markabdey
Frequent Visitor
5 years ago
Solved

Where Clause for Custom Column

Hi

In Power Query I'm trying to create a Custom Column that is populated by a value from another column in the same table where a flag is set to 'Y'. I can't seem to get the right syntax in M to get it to work. Can anyone help please? Example below. Thanks.

 

Time        Flag     Custom Column

07:30       N         07:45

07:45       Y          07:45

08:00       N         07:45

 

  • Hi markabdey 

    Place the following M code in a blank query to see the steps.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjC3MjY4tEBJR8lPKVYHzDcxBfMjIXwLKwO4fCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Time = _t, Flag = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Time", type time}, {"Flag", type text}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each #"Changed Type"{[Flag="Y"]}[Time], type time)
    in
        #"Added Custom"

     

    Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

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

     

4 Replies

  • markabdey 

    Click the Custom column and enter the following code:

    =if [Flag] = "Y" then [Time] else null

     

  • mahoneypat's avatar
    mahoneypat
    Icon for Microsoft Employee rankMicrosoft Employee

    This would be easier with DAX but it can be done in the query editor too.  Will all rows have the same value in that column (as with your example), or are there other columns and the Y value within a group needs to be found?  If the latter, please update your example data to reflect that so you get what you need.

    Pat

     

    • markabdey's avatar
      markabdey
      Frequent Visitor

      Thanks for your reply. The value will be the same across all the rows.

  • AlB's avatar
    AlB
    Icon for Community Champion rankCommunity Champion

    Hi markabdey 

    Place the following M code in a blank query to see the steps.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjC3MjY4tEBJR8lPKVYHzDcxBfMjIXwLKwO4fCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Time = _t, Flag = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Time", type time}, {"Flag", type text}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each #"Changed Type"{[Flag="Y"]}[Time], type time)
    in
        #"Added Custom"

     

    Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

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