Code action DI011
(Improvement) Use ISEMPTY instead of COUNTROWS
Description
When checking if a table is empty, it is more efficient to use the ISEMPTY
function than to count the rows of the table.
Example
Change:
IF(COUNTROWS(Products) = 0, "No products", "Products exist")
To:
IF(ISEMPTY(Products), "No products", "Products exist")
Why is Tabular Editor suggesting this?
When checking if a table is empty, a common anti-pattern in DAX is to use the COUNTROWS
function to count the rows of the table, and then compare the result to zero. However, this pattern is inefficient, as it requires the engine to count all rows of the table, even if the only thing we are interested in is whether the table is empty or not.
By using the ISEMPTY
function, the engine can stop counting rows as soon as it finds the first row, which is much more efficient. The ISEMPTY
function returns TRUE
if the table is empty, and FALSE
otherwise, which makes it a more efficient and readable way to check if a table is empty.