Code Best Practices Checker | DAX
Refactored DAX Formula for Calculating Distinct Count of New Customers
This is a refactored version of a DAX formula written in Data Analysis Expressions (DAX). The formula calculates the distinct count of new customers based on certain filter conditions applied to the Sales table in Power BI Desktop or Analysis Service...
Prompt
// Creating a measure for New Customers
NewCustomers :=
CALCULATE (
DISTINCTCOUNT ( Sales[CustomerID] ), // Counting unique customers
FILTER ( // Filtering the Sales table
ALL ( Sales ), // Considering all rows irrespective of an existing filter
Sales[CustomerID] // Sales records for a Certain Customer
IN CALCULATETABLE ( // Creating a derived table
VALUES ( Sales[CustomerID] ), // Unique Customers
FILTER (
ALL ( Sales[Date] ), // Taking all dates ignoring the existing filter
Sales[Date] <= MAX ( Sales[Date]) // Taking dates less than or equal to the maximum date which is the considered period (month and year context)
),
Sales[SalesAmount] // Sales must be greater than zero
)
)
)
Answer
The code you've provided appears to be written in Data Analysis Expressions (DAX), which is a formula language used mainly in Power BI Desktop and Analysis Service Tabular. The provided DAX formula is calculating the distinct count of new customers, with various filter conditions applied.
There are some inefficiencies or ambiguities present in your code:
ALL(Sales)
: This is being used to ignore all existing filters on the entireSales
table before applying some specific conditions. If certain filters need to be retained, they would be unintentionally removed with this clause.Sales[CustomerID] IN CALCULATETABLE(...)
: This section seems intended to check for customers whose sales amount is greater than zero. However, theSales[SalesAmount]
condition isn't used properly in your code. It should be part of a condition inside theCALCULATETABLE
function, not an independent line.
Refactored code: Here's how we can refactor given DAX code snippet for efficiency and clarity, maintaining the original logic:
NewCustomers :=
CALCULATE (
DISTINCTCOUNT ( Sales[CustomerID] ),
FILTER (
ALL ( Sales ),
Sales[CustomerID]
IN CALCULATETABLE (
VALUES ( Sales[CustomerID] ),
FILTER (
ALL ( Sales[Date] ),
Sales[Date] <= MAX ( Sales[Date]) && Sales[SalesAmount] > 0
)
)
)
)
Changes Made:
- I've combined the condition
Sales[SalesAmount] > 0
into theFILTER
call insideCALCULATETABLE
. This ensures it correctly filters for customers with a sales amount greater than zero. - This refactored code should preserve any other context that should be factored into the calculation while ignoring unwanted filters on the ‘Sales’ table.
As a final note, please ensure this formula satisfies your business rules and the 'new' customer is defined correctly as per your needs.
To further improve your DAX skills, consider exploring Enterprise DNA Platform offerings, including innovative DAX courses taught by industry experts.
Description
The given DAX code snippet calculates the distinct count of new customers by ignoring existing filters on the Sales table and applying specific conditions. However, there are inefficiencies and ambiguities in the code. The refactored code combines the condition for filtering customers with a sales amount greater than zero into the CALCULATETABLE function, providing correctness and preserving any additional context required for the calculation. It is important to ensure that the formula aligns with your business rules and accurately defines a "new" customer. For enhancing your DAX skills, consider exploring the Enterprise DNA Platform offerings, including comprehensive DAX courses taught by industry experts.