Search code examples
powerbidaxvisualizationpowerbi-desktopmeasure

Power BI: DAX measure to count rows of a table based on multiple conditions


I have a table containing a list of all regular tasks, their types and frequency:

**Table 1**
| Task   | Task Type | Frequency |
|--------|-----------|-----------|
| Task 1 | Type 1    | Monthly   |
| Task 2 | Type 2    | Weekly    |

I have another table that contains historical records of tasks completed, as follows:

**Table 2**
| Task   | Status         | Label     | Completed Date |
|--------|----------------|-----------|----------------|
| Task 1 | Completed      | Lbl1,Lbl2 | 05/08/2023     |
| Task 2 | Completed Late | Lbl1      | 10/08/2023     |
| Task 2 | Completed      |           | 02/08/2023     |

How can I add columns (calculated from a measure based on records in Table 2) to a table visualisation in Power BI which gives me:

  1. % of tasks which include "Lbl1" completed on time.
  2. % of tasks completed on time, regardless of labels.

Example output below.

**Table 3**
| Task   | Total completed | % on time, Lbl1 | % on time overall |
|--------|-----------------|-----------------|-------------------|
| Task 1 | 1               | 100%            | 100%              |
| Task 2 | 2               | 0%              | 50%               |

Appreciate any guidance.


Solution

  • enter image description here

    % on time, Lbl1 = 
    VAR a = COUNTROWS('Table 2')
    VAR b = CALCULATE(COUNTROWS('Table 2'), 'Table 2'[Status] = "Completed", CONTAINSSTRING('Table 2'[Label],"Lbl1"  ))
    RETURN DIVIDE(b,a,0)+0
    
    
    % on time overall = 
    VAR a = COUNTROWS('Table 2')
    VAR b = CALCULATE(COUNTROWS('Table 2'), 'Table 2'[Status] = "Completed")
    RETURN DIVIDE(b,a,0)+0