Search code examples
excel-formulapowerpivotdax

DAX to find percent of total based on filter (regardless of view)


How do I create DAX that finds the percent of incidents that are Status=Closed based on row view

Total Incidents = Count of CREATED_DATE

I tried following Measure both gave error

`% Closed:=if(Table1[STATUS]="Closed",[Count of CREATED_DATE],0)/[Count of CREATED_DATE]`

and

% Closed2:=DIVIDE(if(Table1[STATUS]="Closed",[Count of CREATED_DATE],0),[Count of CREATED_DATE],0)

Sample data from Power Pivot.

enter image description here

Here is my table

enter image description here

Here is what I wish to create. Please note, the % of Closed Incidents to Total should change based on how I filter the power-pivot, i.e. if I filter to include ONLY closed incidents, then naturally the % should be 100%

enter image description here


Solution

  • What you need is the Calculate function, which allows for calculations under different contexts. Think of it as excel's sumif function but a bit more powerful.

    So for example the below, which would do a count of the BBL rows where the table1 status is closed. Note for count the column has to be a number or date.

    Closed Count:=CALCULATE( COUNT( Table1[BBL]), Table1[STATUS] = "Closed")
    

    Then you can do your divide. Keep in mind that not putting the alternative result, means that in a divide by zero error power pivot will return NULL. The advantage being that if a all measures in a row evaluates to null it will be suppressed in the pivot.

    Total Incidents:=COUNT( Table1[BBL])
    
    Closed %:=DIVIDE( [Closed Count], [Total Incidents])