cancel
Showing results for
Did you mean:
Frequent Visitor

## Trouble with Nested "If" Statements

I'm trying to calculate an Average Headcount by department.  This is done by summing the starting and ending headcounts and dividing by 2.  However, if either the starting or ending headcount are 0 (blank), then I want the average to use the other number.

It is working correctly 1-way, but not the other way.  See the table below for the results I'm getting.  If STARTING HEADCOUNT is blank, then it's calculating correctly and using the ENDING HEADCOUNT for the average.  But if ENDING HEADCOUNT is blank, it's still trying to calculate (STARTING + ENDING)/2 which is resulting in the wrong number given the blank.

I'm using nested IF statement in DAX like this:

Any help is much appreciated

 Department Starting Headcount Ending Headcount Average Headcount (Dynamic) Sales 12 12 Manufacturing 11 15 13 Marketing 21 10.5

1 ACCEPTED SOLUTION

Accepted Solutions
Community Support Team

## Re: Trouble with Nested "If" Statements

Modify the calculate column like below:

```Average Headcount (Dynamic) =
IF (
&& [Ending Headcount] = BLANK (),
BLANK (),
IF (
&& [Ending Headcount] <> BLANK (),
IF (
&& [Starting Headcount] <> BLANK (),
)
)
)```

Community Support Team _ Jimmy Tao

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

4 REPLIES 4
Member

## Re: Trouble with Nested "If" Statements

I would try pivoting your data so your actual data is describing the departments (your departments become your columns).  That way you can use the AVERAGE function or at least be able to use your data in a column structure rather than crossing rows.

Super User II

## Re: Trouble with Nested "If" Statements

The idea with pivoting is very elegant

But if you couldn't use it, try following:

```AVG =
VAR SUM_OF = TestTable[Column2] + TestTable[Column3]
VAR CNT_OF =
( NOT ISBLANK ( TestTable[Column2] ) ) + ( NOT ISBLANK ( TestTable[Column3] ) )
RETURN
DIVIDE ( SUM_OF; CNT_OF; 0 )```
Community Support Team

## Re: Trouble with Nested "If" Statements

Modify the calculate column like below:

```Average Headcount (Dynamic) =
IF (
&& [Ending Headcount] = BLANK (),
BLANK (),
IF (
&& [Ending Headcount] <> BLANK (),
IF (
&& [Starting Headcount] <> BLANK (),
)
)
)```

Community Support Team _ Jimmy Tao

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Frequent Visitor

## Re: Trouble with Nested "If" Statements

Thank you!  This worked well.

Announcements

#### Meet the 2020 Season 1 Power BI Super Users!

It’s the start of a new Super User season! Learn all about the new Super Users and brand-new tiered recognition system.

#### Super User Challenge: Can You Solve These?

We're celebrating the start of the New Super User season with our first ever Super User 'Can You Solve These?' challenge.

#### Power BI Desktop Update - February 2020

We are super excited for our update this month, as we are releasing two of our top community requests!