Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- Microsoft Power BI Community
- Forums
- Get Help with Power BI
- DAX Commands and Tips
- Re: Why do we need COUNTROW in the DAX formula?

Topic Options

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

Highlighted
##

kimjongbun

New Member

Why do we need COUNTROW in the DAX formula?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-03-2020
06:46 PM

Hi all,

I couldn't visualize what is PowerBI doing with the COUNTROW function behind the below DAX formula. If I don't include COUNTROW, the formula would return an error as it was unable to return a scalar value.

My understanding with the formula is, Power BI will return a table with a set of unique customer names, run the filter on whether the customers matches the two criteria, and finally return the total sales for each customer in the table if it matches the criteria. I couldn't visualize what COUNTROW is doing or returning in this case.

Outlier Sales = CALCULATE( [Total Sales] ,

FILTER( VALUES( Customers[Customer Names] ),

COUNTROWS(

FILTER( 'Outlier Detection Logic' ,

[Profit Margin] >= 'Outlier Detection Logic'[Profit Margins Min] &&

[Total Sales] >= 'Outlier Detection Logic'[Profit Margins Min] )) >0 ))

6 REPLIES 6

Highlighted
##

Thank you,

Antriksh Sharma

AntrikshSharma

Super User II

Re: Why do we need COUNTROW in the DAX formula?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-04-2020
02:28 AM

COUNTROWS is checking if the FITLER inside is returning any row and if COUNTROWS is 0 then that ROW isn't returned by FILTER for the CALCULATE, in simple words, FILTER ( VALUES ( Customer[Customer Name] ) .....) construct iterates for each customer and checks wheter the count of rows returned by the inner FILTER is greater than 0 and if it is greater than 0 then keep that customer otherwise remove it from the list once everything is computed outer FILTER returns only CustomerNames that have atleast 1 row in Outlier table and then CALCULATE pushes this list into the filter context and then evaluates [Total Sales]

Thank you,

Antriksh Sharma

Highlighted
##

kimjongbun

New Member

Re: Why do we need COUNTROW in the DAX formula?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-05-2020
05:49 AM

Hi @AntrikshSharma ,

Thank you so much for your clear explanation. Could you enlighten me why the formula is unable to return a scalar value if I don't include COUNTROW in the formula? Why would there be multiple columns?

Highlighted
##

Thank you,

Antriksh Sharma

AntrikshSharma

Super User II

Re: Why do we need COUNTROW in the DAX formula?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-05-2020
06:38 AM

Because when you remove COUNTROWS you are removing aggregation and the second part of FILTER is for specifying conditions/boolean operations because of which FILTER is able reduce the number of rows returned by it. Also, I just realised that you should insert CALCULATE before COUNTROWS so that the Outliers related to the customers can be counted

```
Outlier Sales =
CALCULATE (
[Total Sales],
FILTER (
VALUES ( Customers[Customer Names] ),
CALCULATE (
COUNTROWS ( 'Outlier Detection Logic' ),
FILTER (
'Outlier Detection Logic',
[Profit Margin] >= 'Outlier Detection Logic'[Profit Margins Min]
&& [Total Sales] >= 'Outlier Detection Logic'[Profit Margins Min]
)
) > 0
)
)
```

Thank you,

Antriksh Sharma

Highlighted
##

harshnathani

Super User V

Re: Why do we need COUNTROW in the DAX formula?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-05-2020
07:59 AM

Hi @kimjongbun ,

Have a look at this to understand Count Rows.

https://curbal.com/blog/glossary/countrows

https://info.enterprisedna.co/dax-function-guide/countrows/

Regards,

Harsh Nathani

**Appreciate with a Kudos!! (Click the Thumbs Up Button)**

**Did I answer your question? Mark my post as a solution!**

Highlighted
##

kimjongbun

New Member

Re: Why do we need COUNTROW in the DAX formula?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-05-2020
07:12 PM

Hi @AntrikshSharma ,

Am I correct to say that DAX is using the following sequence to get to the solution? Bold and Italic are the booleans / measure, and the number in the blue circle is the sequence in which DAX is following.

Highlighted
##

Thank you,

Antriksh Sharma

AntrikshSharma

Super User II

Re: Why do we need COUNTROW in the DAX formula?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-05-2020
10:12 PM

Yes, that's correct.

Thank you,

Antriksh Sharma

Top Solution Authors

User | Count |
---|---|

53 | |

50 | |

18 | |

13 | |

11 |

Top Kudoed Authors

User | Count |
---|---|

80 | |

64 | |

31 | |

28 | |

23 |