Forum Discussion

manojs's avatar
manojs
Frequent Visitor
6 years ago
Solved

Counting across column for conditional values

Hello, I have a table with various columns to include: name, country1, country2, country3 and so forth Each county1-countryX can have several values from a fixed defined choice. Example: Pass, Fail,...
  • ryan_mayu's avatar
    6 years ago

    manojs 

    you can try to use DAX to create columns, However, if the number of country is large , that will be complicated.

    CountPass = 
    VAR c1=if('Table'[C1]="Pass",1,0)
    VAR c2=if('Table'[C2]="Pass",1,0)
    VAR c3=if('Table'[C3]="Pass",1,0)
    VAR c4=if('Table'[C4]="Pass",1,0)
    RETURN c1+c2+c3+c4
    
    CountFail = 
    VAR c1=if('Table'[C1]="Fail",1,0)
    VAR c2=if('Table'[C2]="Fail",1,0)
    VAR c3=if('Table'[C3]="Fail",1,0)
    VAR c4=if('Table'[C4]="Fail",1,0)
    RETURN c1+c2+c3+c4

  • parry2k's avatar
    parry2k
    6 years ago

    manojs although I would recommend to unpivot your table but the code you posted is not working because when you add the second custom column, it sees the first custom column you added as a number because the type of that column by default will be any

     

    There are two ways to handle it, either change type of column to text after your add first custom column and then add a second custom column or change expression as below 

     

    List.Count(List.Select(Record.FieldValues(_),(x)=>Text.Contains(Text.From(x), "Fail"))))

     

    Again, as a best practice, unpivot should be your approach.

     

    I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos whoever helped to solve your problem. It is a token of appreciation!

    Visit us at https://perytus.com, your one-stop shop for Power BI related projects/training/consultancy.

     

     

  • dax's avatar
    6 years ago

    Hi manojs , 

    You could refer to above suggestions, you also could refer to my M code

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUQpILC5GUG6JmTkwKlYnWslJAbsckhJn7FIwFbEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Name = _t, #" C1" = _t, C2 = _t, C3 = _t, C4 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {" C1", type text}, {"C2", type text}, {"C3", type text}, {"C4", type text}}),
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"Name"}, "Attribute", "Value"),
        #"Grouped Rows" = Table.Group(#"Unpivoted Other Columns", {"Name", "Value"}, {{"Count", each Table.RowCount(_), type number}}),
        #"Pivoted Column" = Table.Pivot(#"Grouped Rows", List.Distinct(#"Grouped Rows"[Value]), "Value", "Count", List.Sum),
        #"Merged Queries" = Table.NestedJoin(#"Pivoted Column", {"Name"}, #"Changed Type", {"Name"}, "Pivoted Column", JoinKind.LeftOuter),
        #"Expanded Pivoted Column" = Table.ExpandTableColumn(#"Merged Queries", "Pivoted Column", {" C1", "C2", "C3", "C4"}, {" C1", "C2", "C3", "C4"})
    in
        #"Expanded Pivoted Column"

    Best Regards,
    Zoe Zhi

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