Forum Discussion
countifs based multiple columns criteria
- Anonymous5 years ago
Hi theo ,
I created a sample pbix file(see attachment), please check whether that is what you want.
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("RZDJDUQhDEN74czhZ2OpBdF/G8MLQnOxnMSOA2sVi1LLlAOiB6JTfgcaQCUQnUAvu67ToCPM+nhGo0IUjRYLITHSEt/l9TLN3WjMyeMGBRqS6emZcGN3yzOY23yl4mYoyFzS0ok0e2pHPfIxeWIqCVT9x4g9jzPXvOA9wYkaWKTdX9j7Bw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [col1 = _t, col2 = _t, col3 = _t, col4 = _t, col5 = _t, col6 = _t, col7 = _t, col8 = _t, col9 = _t, col10 = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"col1", Int64.Type}, {"col2", Int64.Type}, {"col3", Int64.Type}, {"col4", Int64.Type}, {"col5", Int64.Type}, {"col6", Int64.Type}, {"col7", Int64.Type}, {"col8", Int64.Type}, {"col9", Int64.Type}, {"col10", Int64.Type}}), MyCols = List.Buffer(List.Select(Table.ColumnNames(#"Changed Type"), each Text.StartsWith(_, "col"))), #"Added Custom" = Table.AddColumn(#"Changed Type" , "Range10", each List.Count(List.Select(Record.ToList(Record.SelectFields(_,MyCols)), each _<11)) ,Int64.Type), #"Added Custom1" = Table.AddColumn(#"Added Custom", "Range20", each List.Count(List.Select(Record.ToList(Record.SelectFields(_,MyCols)), each _<21 and _>=11))), #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Range30", each List.Count(List.Select(Record.ToList(Record.SelectFields(_,MyCols)), each _<31 and _>=20))) in #"Added Custom2"Of course, you can also follow the suggestion by Greg_Deckler , add index column in Power Query Editor and unpivot these value columns. Finally, create 3 measures to get the counts...
Note: This method will destroy the original structure of the table...
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("RZDJDUQhDEN74czhZ2OpBdF/G8MLQnOxnMSOA2sVi1LLlAOiB6JTfgcaQCUQnUAvu67ToCPM+nhGo0IUjRYLITHSEt/l9TLN3WjMyeMGBRqS6emZcGN3yzOY23yl4mYoyFzS0ok0e2pHPfIxeWIqCVT9x4g9jzPXvOA9wYkaWKTdX9j7Bw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [col1 = _t, col2 = _t, col3 = _t, col4 = _t, col5 = _t, col6 = _t, col7 = _t, col8 = _t, col9 = _t, col10 = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"col1", Int64.Type}, {"col2", Int64.Type}, {"col3", Int64.Type}, {"col4", Int64.Type}, {"col5", Int64.Type}, {"col6", Int64.Type}, {"col7", Int64.Type}, {"col8", Int64.Type}, {"col9", Int64.Type}, {"col10", Int64.Type}}), #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type), #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each List.Count(List.Select( #"Added Index"[Index], each _<11))), #"Unpivoted Only Selected Columns" = Table.Unpivot(#"Added Custom", {"col10", "col9", "col8", "col7", "col6", "col5", "col4", "col3", "col2", "col1"}, "Cols", "Value") in #"Unpivoted Only Selected Columns"Range10_1 = CALCULATE(COUNT('7Jun_output (2)'[Cols]),FILTER('7Jun_output (2)','7Jun_output (2)'[Index]=SELECTEDVALUE('7Jun_output (2)'[Index])&&'7Jun_output (2)'[Value]<11))Range20_1 = CALCULATE(COUNT('7Jun_output (2)'[Cols]),FILTER('7Jun_output (2)','7Jun_output (2)'[Index]=SELECTEDVALUE('7Jun_output (2)'[Index])&&'7Jun_output (2)'[Value]<21))Range30_1 = CALCULATE(COUNT('7Jun_output (2)'[Cols]),FILTER('7Jun_output (2)','7Jun_output (2)'[Index]=SELECTEDVALUE('7Jun_output (2)'[Index])&&'7Jun_output (2)'[Value]<31))Best Regards
theo Assuming you have some type of Index column or other identifier for your rows, you can do this:
range10 measure =
VAR __Table = UNION( { MAX([col1]) }, { MAX([col2]) }, { MAX([col3]) }, { MAX([col4]) }, { MAX([col5]) }, { MAX([col6]) }, { MAX([col7]) }, { MAX([col8]) }, { MAX([col9]) }, { MAX([col10]) } )
RETURN
COUNTX(FILTER(__Table,[Value]<11),[Value])+0
That said, are you sure you don't want to unpivot your columns? Makes things soooo much easier. Otherwise it is the MC Aggregations pattern. Multi-Column Aggregations (MC Aggregations) - Microsoft Power BI Community
Greg_Deckler can you share how to unpivot columns? I ran out of memory using the column while for summry, while there's error in visual using measure. Filtering based on group of 60 columns
- Greg_Deckler5 years agoCommunity Champion
theo Sure, let's say you do have an Index column or just add one. Right-click the header of the Index column in Power Query and then choose Unpivot other columns.