cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
New Member

Why do we need COUNTROW in the DAX formula?

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
Super User I
Super User I

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

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
New Member

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

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
Super User I
Super User I

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

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
Super User III
Super User III

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

Hi @kimjongbun ,

 

 

Have a look at this to understand Count Rows.

 

 

https://dax.guide/countrows/

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
New Member

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

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.

 

kimjongbun_0-1594001486497.png

 

Highlighted
Super User I
Super User I

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

Yes, that's correct.


Thank you,
Antriksh Sharma

Helpful resources

Announcements
Get Ready for Power BI Dev Camp

Get Ready for Power BI Dev Camp

Mark your calendars and join us for our next Power BI Dev Camp!.

Community Conference

Power Platform Community Conference

Check out the on demand sessions that are available now!

secondImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Tech Marathon

Maratón de Soluciones de Negocio Microsoft

Una semana de contenido con +100 sesiones educativas, consultorios, +10 workshops Premium, Hackaton, EXPO, Networking Hall y mucho más!

Top Solution Authors
Top Kudoed Authors