How to count rows with or without conditions in Power BI?

Here’s a common question in data analysis (details vary per industry), which i’ll answer in this tutorial: “I have a large HR dataset in Power BI, and need to create a simple dashboard showing to count the total number of employees as well as employees meeting some specific criteria. How can I create measures to count rows with and without conditions?”

For this tutorial, we’ll assume a single table named “Employees”. The table contains several fields: EmployeeID (whole number), Name (text), Department (text), Salary (decimal number), and HireDate (date).

Counting All Rows

To count all rows in the table, create a measure using the COUNTROWS function:

Total Employees = COUNTROWS(Employees)

This measure will return the total number of rows in the Employees table, representing your total workforce.

Counting Rows with a Condition

To count rows that meet specific criteria, use the CALCULATE function along with COUNTROWS:

IT Employees = CALCULATE(COUNTROWS(Employees), Employees[Department] = "IT")

This measure counts employees in the IT department. You can modify the condition to suit your needs:

High Earners = CALCULATE(COUNTROWS(Employees), Employees[Salary] > 75000)

Count with Multiple Conditions

For more complex scenarios, combine multiple conditions:

Senior IT Staff = 
CALCULATE(
    COUNTROWS(Employees),
    Employees[Department] = "IT",
    Employees[Salary] > 75000,
    Employees[HireDate] < DATE(2020, 1, 1)
)

This measure counts IT employees with high salaries who were hired before 2020.

Dynamic Conditions

For flexible analysis, create parameters that users can adjust:

  1. Create a “Salary Threshold” parameter using a What-if parameter.
  2. Create a measure for dynamic high earners:
Dynamic High Earners = 
CALCULATE(
    COUNTROWS(Employees),
    Employees[Salary] > [Salary Threshold]
)

Note: Feel free to adjust the salary threshold interactively.

Real world implementation idea

  1. Create a card visual to display “Total Employees”.
  2. Add a multi-row card to show various employee counts.
  3. Create a slicer for the “Department” field to enable filtering.
  4. Add a line chart showing employee counts over time using HireDate.

Count rows in Power BI do not work

Common issues when counting rows include:

  1. Blank results: Ensure your table has data and check for typos in table/column names.
  2. Incorrect counts: Verify your data model for unwanted relationships or duplicates.
  3. Slow performance: For large datasets, consider using calculated columns instead of measures for static counts.
  4. Inconsistent results: Check for proper date formatting and data type consistency.