Forum Discussion
Counting across column for conditional values
- 6 years ago
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 - 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.⚡
- 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 ZhiIf this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
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- manojs6 years agoFrequent Visitor
Thx Ryan,
This is good but would mean that it would rely on hard coding the column names, I was looking for an automated way and hence the two line examples in my original post: though to get any information about that constructed that was posted is almost impossible on searching in google - looks like magic
- parry2k6 years ago
Super User
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.⚡