Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
kimjongbun
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
harshnathani
Community Champion
Community Champion

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!

AntrikshSharma
Community Champion
Community Champion

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]

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? 

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
    )
)

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

 

Yes, that's correct.

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors