5 Replies Latest reply on Feb 15, 2016 11:45 AM by Joe Oppelt

    Count Females and Males in a Table

    Ramin Melikov

      Hi,

       

      I have a table and I want to use a table calculation to count the number of females and the number of males in the table. How do I do that? I came up with

       

      WINDOW_COUNT(ATTR([Gender]) = "Female")

       

      but this give the value of the number of records in the table.

       

      Thanks,

       

      Ramin

        • 1. Re: Count Females and Males in a Table
          Joe Oppelt

          You don't need to use a table calc.

           

          Create a calc [Female]:

           

           

          If [Gender] = "Female" then 1 else 0 end

           

          Sum([Female]) on your sheet is the number of females.  Sun([Number of Records]) - SUM([Female]) is your number of males.

          • 2. Re: Count Females and Males in a Table
            Ramin Melikov

            Thanks for your reply, Joe. It is required that I use a table calc.

            • 3. Re: Count Females and Males in a Table
              Joe Oppelt

              Wish I could understand why it's required.  Depending on what's going on, I can help you run that table calc, but whatever is requiring a table calc is probably also impacting how it will be set up.

               

              At a minimum you could do WINDOW_SUM(SUM([Female]))

               

              or

               

              WINDOW_SUM(SUM(IF attr([gender])="Female" then 1 else 0 end))

              • 4. Re: Count Females and Males in a Table
                Ramin Melikov

                Joe,

                 

                I slightly adjusted your code to make it work as i was getting an error. Instead of

                 

                WINDOW_SUM(SUM(IF attr([gender])="Female" then 1 else 0 end))

                 

                I did this

                 

                WINDOW_SUM(IF attr([gender])="Female" then 1 else 0 end).

                 

                Thank you very much!

                 

                Now, I guess I owe you an explanation why I needed a table calc. The calculation is used inside of the title and not inside of the sheet. I put the table calc in the marks card and then I can put into the title something like this...

                 

                – Incidents range from 9/10/2013 to 2/11/2016 for selected dates

                – This report contains 24 unique falls: 9 (38%) involve females and 15 (63%) involve males

                – On average, there are 0.03 falls per day between first and last dates in this list

                 

                ...and so on.

                 

                Thanks again.

                • 5. Re: Count Females and Males in a Table
                  Joe Oppelt

                  Yup.  I just typed that off the top of my head.  I always let the calc editor tell me when I make a coding error like that.

                   

                  There is a lot of power in what you can do inside the function parentheses in tableau.