Code Generator | DAX

Measure Formula for New Customers

This code provides a DAX measure formula for calculating the number of new customers in a given month and year based on their first purchase within that period.


Empty image or helper icon

This Query related with thread ""

Prompt

Help me create a measure formula for new customers using the CALCULATETABLE function, but please don’t include any calculated columns. I want one formula that will be used against a month and year context

Answer

Creating a measure formula for new customers involves calculating the number of distinct customers in a certain month and year who made their first purchase within the same period. The below DAX measure is a great way of achieving this without the use of calculated columns:

DAX Code

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

Code Explained

  • The CALCULATETABLE function creates a derived table of unique customer who made their first purchase in the period context and reducing it by the condition that SalesAmount should be greater than zero
  • The inner FILTER function removes the existing context on the Date column and takes dates less or equal to the maximum date
  • The outer FILTER function on ALL Sales gets sales records for each Customer in derived table
  • DISTINCTCOUNT counts the number of unique customers

Usage

This measure, NewCustomers, can be used in a report that provides monthly sales data, filtering on the month and year.

Unit testing

It's imperative that you validate the measure by testing it in different scenarios. Change the context by applying filters on different months/years of data. It should provide the number of new customers against the applied month/year context.

Further Learning You may take advantage of online resources and training courses on Enterprise DNA Platform to understand more about the DAX functions used and also the underlying principles of calculated tables, calculated columns, and context in Power BI.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This code demonstrates how to create a DAX measure, called "NewCustomers", that calculates the number of distinct customers who made their first purchase within a specific month and year. The formula utilizes various DAX functions such as CALCULATE, DISTINCTCOUNT, FILTER, ALL, and VALUES to create a derived table of unique customers and apply necessary filters based on the period and sales amount. The measure can be used in a report to provide insights on monthly new customer acquisition. Proper unit testing is recommended to verify the measure's accuracy in different scenarios. Additional learning resources are suggested to further understand the DAX functions and concepts used.