Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
4 years ago
Solved

Complex Conditional Column

I'm trying to make a conditional column based on values from two other columns.  If Column B is "yes" than I want Column C to be "yes" whenever Column A has the same value.  Here's an example of what...
  • Ashish_Mathur's avatar
    4 years ago

    Hi,

    This calculated column formula works

    =if(CALCULATE(COUNTROWS(Table1),FILTER(Table1,Table1[Column A]=EARLIER(Table1[Column A])&&Table1[Column B]="Yes"))>=1,"Yes",BLANK())

    Hope this helps.

  • Vijay_A_Verma's avatar
    4 years ago

    See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test (later on when you use the query on your dataset, you will have to change the source appropriately. If you have columns other than these, then delete Changed type step and do a Changed type for complete table from UI again).

    Note - Replaced value step may not be needed for you as I have used that to generate nulls in place of blank.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("rdC7DcAgDATQXVxTYOMfo0SIMhNkfylARYRCmrRPZ53tUgApimZjVjEIcJwX1LDw1gyZWXL+1TTxh6lTn7X0YtMpm6jTZIatotX4wzz2nC6mcX6DMY0VcVi9AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Column A" = _t, #"Column B" = _t]),
        #"Replaced Value" = Table.ReplaceValue(Source,"",null,Replacer.ReplaceValue,{"Column B"}),
        #"Changed Type" = Table.TransformColumnTypes(#"Replaced Value",{{"Column A", Int64.Type}, {"Column B", type text}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"Column A"}, {{"Temp", each _, type table [Column A=nullable number, Column B=nullable text]}}),
            //Function Start
            fxProcess=(Tbl)=>
            let
                #"Added Custom" = Table.AddColumn(Tbl, "Column C", each if List.Count(List.Select(Tbl[Column B],each _="Yes"))>0 then "Yes" else null)
            in
                #"Added Custom",
        //Function End
        #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each fxProcess([Temp])),
        #"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom", "Custom", {"Column B", "Column C"}, {"Column B", "Column C"}),
        #"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"Temp"})
    in
        #"Removed Columns"