cancel
Showing results for
Did you mean:

Fabric is Generally Available. Browse Fabric Presentations. Work towards your Fabric certification with the Cloud Skills Challenge.

Helper I

## CountIf PowerBI

Hello all. I'm new to PowerBI, previously only used Tableau, and have been given a dataset with ~100K records. I'm trying to get the count of the number of instances a deal number comes up where the field in my dataset Direction = "Out". In Excel I'd accomplish this using =COUNTIFS, but unfortunately I haven't be able to find a comparable PowerBI solution. I've tried the following solution based on another post I found on the forum, but it's giving me inaccurate totals.

DealCount = CALCULATE(COUNTROWS(Referrals),ALLSELECTED(Referrals),VALUES(Referrals[DimDealNaturalID]), FILTER(Referrals, Referrals[Direction]="Out"))

My dataset is structured as following with the deal count field being what I'd like to calculate:

DimDealNaturalID      Direction      DealCount

12345                          Out              2

12345                          Out              2

12345                          In                 0

56789                          Out              1

57788                          Out              1

Any suggestions would be appreciated. Thanks!

1 ACCEPTED SOLUTION
Community Champion

@walkery

May be

Please see attached file with all these formulas

```DealCount3 =
IF (
FIRSTNONBLANK(  Referrals[Direction],1 ) = "Out",
COUNTROWS (
FILTER (
ALL ( Referrals ),
Referrals[DimDealNaturalID] = VALUES ( Referrals[DimDealNaturalID] )
&& Referrals[Direction] = "Out"
)
),
0
)```

Regards
Zubair

12 REPLIES 12
Community Champion

@walkery

May be this column

```DealCount =
IF (
Referrals[Direction] = "Out",
CALCULATE (
COUNTROWS ( Referrals ),
FILTER (
ALLEXCEPT ( Referrals, Referrals[DimDealNaturalID] ),
Referrals[Direction] = "Out"
)
),
0
)```

Regards
Zubair

Helper I

@Zubair_Muhammad thanks for the quick reply! For some reason it isn't accepting the direction field as an apporporiate field in that spot in the formula. Any idea why? It is definitely a field wtihin the Referrals table.

Community Champion

@walkery

May be you are adding a MEASURE

I gave you a Column formula

Regards
Zubair

Helper I

@Zubair_Muhammad Ah that makes sense. It doesn't look like with my data connection I'm able to add a new column, it's greyed out and only "new Measure" is avaialble to be selelected. Is there a way to accomplish what I'm looking to do with a measure?

Community Champion

@walkery

Try this MEASURE

```DealCount =
IF (
SELECTEDVALUE ( Referrals[Direction] ) = "Out",
CALCULATE (
COUNTROWS ( Referrals ),
FILTER (
ALLEXCEPT ( Referrals, Referrals[DimDealNaturalID] ),
Referrals[Direction] = "Out"
)
),
0
)```

Regards
Zubair

Helper I

@Zubair_Muhammad still getting an error. Maybe I'm using a different version of PowerBI? I don't know that I have SELECTEDVALUE as a function available. Is it perhaps added with a 3rd party package? I have ALLSELECTED available. Would that accomplish the same task?

Community Champion

@walkery

Try Replacing SelectedValue with

VALUES or

MIN or

MAX

Regards
Zubair

Helper I

@Zubair_Muhammad Thanks for the suggestion. I tried it with each of those suggestions and while the measure doesn't give an error, when I try to add the measure into the table it just hangs for an extended period of time and then provides a time-out error. Is there any workaround to this do you think? Or is it too memory intensive of a process and would need to be added in by the DBA on the backend?

Community Champion

Hi @walkery

try this one...May be

```DealCount =
IF (
MAX ( Referrals[Direction] ) = "Out",
COUNTROWS (
FILTER (
ALL ( Referrals ),
Referrals[DimDealNaturalID] = MIN ( Referrals[DimDealNaturalID] )
&& Referrals[Direction] = "Out"
)
),
0
)```

Regards
Zubair

Helper I

@Zubair_Muhammad It was moving faster, so I was hopeful but it resulted in an error with using the Min function. Any other workarounds? Thanks for your continued suggestions!

Community Champion

@walkery

May be

Please see attached file with all these formulas

```DealCount3 =
IF (
FIRSTNONBLANK(  Referrals[Direction],1 ) = "Out",
COUNTROWS (
FILTER (
ALL ( Referrals ),
Referrals[DimDealNaturalID] = VALUES ( Referrals[DimDealNaturalID] )
&& Referrals[Direction] = "Out"
)
),
0
)```

Regards
Zubair

Helper I

@Zubair_Muhammad Thank you! I was able to get it to work finally! Thanks again for your help!

Announcements

#### Power BI Monthly Update - November 2023

Check out the November 2023 Power BI update to learn about new features.

#### Fabric Community News unified experience

Read the latest Fabric Community announcements, including updates on Power BI, Synapse, Data Factory and Data Activator.

#### Exclusive opportunity for Women!

Join us for a free, hands-on Microsoft workshop led by women trainers for women where you will learn how to build a Dashboard in a Day!

#### The largest Power BI and Fabric virtual conference

130+ sessions, 130+ speakers, Product managers, MVPs, and experts. All about Power BI and Fabric. Attend online or watch the recordings.

Top Solution Authors
Top Kudoed Authors