Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago
Solved

if Function in Custom Column returns inconsistent result - what am I doing wrong ?

Hello everyone, I'm new to Power BI and having trouble with what should be a fairly simple if statement, so would really appreciate some help.

 

I need to create 2 new columns Admin and Meetings to split out the General Minutes data based on this criteria:

General, where TaskId is blank and Type = 14.   

Meetings where TaskId is blank and Type is either 1, 2 or 8.

 

I got this far:

= Table.AddColumn(#"Added Conditional Column2", "General", each if [TaskId] = null and [Type] = 14 then [Minutes] else "")

This doesn't return anything.

 

= Table.AddColumn(#"Added Custom", "Meetings", each if [TaskId] = null and [Type] = 1 or [Type] = 2 or [Type] = 8 then [Minutes] else "")

This strangely returns [Minutes] for types 2 and 8 but not 1, so I'm confused.

 

TaskIdMinutesTypeTask MinutesGeneral MinutesAdminMeetings
23437830430   
234378241224   
252970656   
252503636   
23437818013180   
23437830330   
NULL6014 60  
NULL901 90  
NULL6014 60  
NULL4508 450 450
NULL4508 450 450
NULL902 90 90
NULL4808 480 480
NULL4808 480 480

 

Any help would be very welcome, thanks

Andy

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi Anonymous 

    Based on the sample data you have offered,  you need to make sure that:

    The data type of the taskid is whole number,  and the minutes type should also be whole number, it should be like the following piture

     

    Then you can try the following code in custom column.

    General:

     

    if [TaskId]=null and [Type] = 14 then [Minutes] else null

     

    Meetings:

     

    if [TaskId] = null and ([Type] = 1 or [Type] = 2 or [Type] =8 ) then [Minutes] else null

     

    Output

    And you can refer to the following code in Advanced Editor in power query.

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jY5REgARCIavsuPZAxVyFuP+11i17DA8eCjl+/+qFANImNhYg64lGsWjUe0kAIEeRjUpAuQkptgi9HfBwWH/xh2P+Z5lhse/PGj0NtyPlKnqHpdFN7OsrKPsbm0UpOXOvu4O6g44LiRefHwD6ws=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [TaskId = _t, Minutes = _t, Type = _t, #"Task Minutes" = _t, #"General Minutes" = _t, Admin = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"TaskId", Int64.Type}, {"Minutes", Int64.Type}, {"Type", Int64.Type}, {"Task Minutes", Int64.Type}, {"General Minutes", Int64.Type}, {"Admin", type text}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "General", each if [TaskId]=null and [Type] = 14 then [Minutes] else null),
        #"Added Custom1" = Table.AddColumn(#"Added Custom", "Meetings", each if [TaskId] = null and ([Type] = 1 or [Type] = 2 or [Type] =  then [Minutes] else null),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Admin"})
    in
        #"Removed Columns"

     

    Best Regards!

    Yolo Zhu

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

  • Anonymous's avatar
    Anonymous
    2 years ago

    Great, that works fine, thank you for your help Yolo !

2 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous 

    Based on the sample data you have offered,  you need to make sure that:

    The data type of the taskid is whole number,  and the minutes type should also be whole number, it should be like the following piture

     

    Then you can try the following code in custom column.

    General:

     

    if [TaskId]=null and [Type] = 14 then [Minutes] else null

     

    Meetings:

     

    if [TaskId] = null and ([Type] = 1 or [Type] = 2 or [Type] =8 ) then [Minutes] else null

     

    Output

    And you can refer to the following code in Advanced Editor in power query.

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jY5REgARCIavsuPZAxVyFuP+11i17DA8eCjl+/+qFANImNhYg64lGsWjUe0kAIEeRjUpAuQkptgi9HfBwWH/xh2P+Z5lhse/PGj0NtyPlKnqHpdFN7OsrKPsbm0UpOXOvu4O6g44LiRefHwD6ws=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [TaskId = _t, Minutes = _t, Type = _t, #"Task Minutes" = _t, #"General Minutes" = _t, Admin = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"TaskId", Int64.Type}, {"Minutes", Int64.Type}, {"Type", Int64.Type}, {"Task Minutes", Int64.Type}, {"General Minutes", Int64.Type}, {"Admin", type text}}),
        #"Added Custom" = Table.AddColumn(#"Changed Type", "General", each if [TaskId]=null and [Type] = 14 then [Minutes] else null),
        #"Added Custom1" = Table.AddColumn(#"Added Custom", "Meetings", each if [TaskId] = null and ([Type] = 1 or [Type] = 2 or [Type] =  then [Minutes] else null),
        #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Admin"})
    in
        #"Removed Columns"

     

    Best Regards!

    Yolo Zhu

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

    • Anonymous's avatar
      Anonymous
      Not applicable

      Great, that works fine, thank you for your help Yolo !