dubafek
dubafek

Reputation: 1113

Tableau count values after a GROUP BY in SQL

I'm using Tableau to show some schools data. My data structure gives a table that has all de school classes in the country. The thing is I need to count, for example, how many schools has Primary and Preschool (both).

A simplified version of my table should look like this: Table example

In that table, if I want to know the number needed in the example, the result should be 1, because in only one school exists both Primary and Preschool.

I want to have a multiple filter in Tableau that gives me that information.

I was thinking in the SQL query that should be made and it needs a GROUP BY statement. An example of the consult is here in a fiddle: Database example query

In the SQL query I group by id all the schools that meet either one of the conditions inside de IN(...) and then count how many of them meet both (c=2).

Is there a way to do something like this in Tableau? Either using groups or sets, using advanced filters or programming a RAW SQL calculated fiel?

Thanks! Dubafek

PS: I add a link to my question in Tableu's forum because you can download my testing workbook there: Tableu's forum question

Upvotes: 0

Views: 240

Answers (1)

dubafek
dubafek

Reputation: 1113

I've solved the issue using LODs (specifically INCLUDE and EXCLUDE statements).

I created two calculated fields having the aggregation I needed:

Number of school types selected in the multiple filter

Number of school types per school ID after the filtering

Then I made a calculated field that leaves only the School IDs that matches the number of types they have (according with the filtering) with the number of types selected in the multiple filter (both of the fields shown above):

School IDs that match between both calculated fields

Finally, I used COUNTD([Condition]) to display the amounts of schools matching with at least the School types selected.

Hope this helps someone with similar issue.

PS: If someone wants the Workbook with the solution I've uploaded it in an answer in the Tableau Forum

Upvotes: 0

Related Questions