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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
TimHoare
Frequent Visitor

Measure to calculate new and previous values

I have some data that is of a format not disimilar to this (it is an oversimplification but what's important is that there is a list of records where 'CustomerName' - in this case - can appear multiple times):

CustomerNameItemAmountDate
AlexGloves1001/01/2020
SarahShoes2002/01/2020
SteveShirt1503/01/2020
SteveShirt1504/01/2020
AliceHat1205/01/2020
ChrisTie605/01/2020
ChrisShoes2006/01/2020
TomShirt1505/01/2020
TomShoes2006/01/2020
TomSocks507/01/2020
CatherineHat1206/01/2020
JohnTie608/01/2020
TomSocks508/01/2020
PeteJacket4009/01/2020
TomSuit8009/01/2020
CatherineShirt1510/01/2020
DaveShirt1510/01/2020
TomTie610/01/2020
TomTrousers2511/01/2020
ChrisSocks513/01/2020

 

An analysis I might like to do is: How many customers made at least one purchase on each day? I can do this by creating a measure 

 

TotalCustomers = DISTINCTCOUNT(transactions[CustomerName])

 

 and then plotting on a line chart by the date field.

 

I also want to know which customers made a purchase on a previous date and which ones are making their first. A way I have found to do this is to add a column at the database level that calculates the first date that a customer made a purchase: 

 

CustomerNameItemAmountDateFirstPurchase
AlexGloves1001/01/202001/01/2020
SarahShoes2002/01/202002/01/2020
SteveShirt1503/01/202003/01/2020
SteveShirt1504/01/202003/01/2020
AliceHat1205/01/202005/01/2020
ChrisTie605/01/202005/01/2020
ChrisShoes2006/01/202005/01/2020
TomShirt1505/01/202005/01/2020
TomShoes2006/01/202005/01/2020
TomSocks507/01/202005/01/2020
CatherineHat1206/01/202006/01/2020
JohnTie608/01/202008/01/2020
TomSocks508/01/202005/01/2020
PeteJacket4009/01/202009/01/2020
TomSuit8009/01/202005/01/2020
CatherineShirt1510/01/202006/01/2020
DaveShirt1510/01/202010/01/2020
TomTie610/01/202005/01/2020
TomTrousers2511/01/202005/01/2020
ChrisSocks513/01/202005/01/2020

 

This way I can create another couple of measures:

 

NewCustomers =
CALCULATE (
    DISTINCTCOUNT ( transactions[CustomerName] ),
    FILTER ( transactions, transactions[Date] = transactions[FirstPurchase] )
)

PreviousCustomers = [TotalCustomers] - [NewCustomers]

 

 

and visualise this using something like a stacked area chart. 

 

My question is: This doesn't seem like an ideal way to do this, hardcoding these values doesn't seem right and maybe won't be as flexible if I want to breakdown by other dimensions - is there a way to achieve the same result using only measures (i.e. calculate first purchase date and then new and previous customers using DAX), or is this the best solution?

 

Thanks!

1 ACCEPTED SOLUTION
v-kelly-msft
Community Support
Community Support

Hi @TimHoare ,

 

You need to create the following measures:

The first dayM =
CALCULATE (
    MIN ( 'transactions'[Date] ),
    ALLEXCEPT ( transactions, 'transactions'[CustomerName] )
)
Numbers of customers =
VAR a =
    SUMMARIZE ( 'transactions', 'transactions'[CustomerName], 'transactions'[Date] )
RETURN
    CALCULATE (
        COUNTROWS ( a ),
        ALLEXCEPT ( 'transactions', 'transactions'[Date] )
    )
Customer category =
VAR diff2 =
    SELECTEDVALUE ( 'transactions'[Date] ) - [The first dayM]
RETURN
    IF ( diff2 = 0, "the first time", "have purchased on a previous date" )

Finally you will see :

1111.png

 

Also you can create calculated columns as below:

 

Firstday = CALCULATE(MIN('transactions'[Date]),ALLEXCEPT(transactions,'transactions'[CustomerName]))
Column = 
var diff='transactions'[Date]-'transactions'[Firstday]
Return
IF(diff=0,"the first time","have purchased on a previous date")

And you will see:

1112.png

For the related .pbix file,pls click here.

 

Best Regards,

Kelly

 

View solution in original post

1 REPLY 1
v-kelly-msft
Community Support
Community Support

Hi @TimHoare ,

 

You need to create the following measures:

The first dayM =
CALCULATE (
    MIN ( 'transactions'[Date] ),
    ALLEXCEPT ( transactions, 'transactions'[CustomerName] )
)
Numbers of customers =
VAR a =
    SUMMARIZE ( 'transactions', 'transactions'[CustomerName], 'transactions'[Date] )
RETURN
    CALCULATE (
        COUNTROWS ( a ),
        ALLEXCEPT ( 'transactions', 'transactions'[Date] )
    )
Customer category =
VAR diff2 =
    SELECTEDVALUE ( 'transactions'[Date] ) - [The first dayM]
RETURN
    IF ( diff2 = 0, "the first time", "have purchased on a previous date" )

Finally you will see :

1111.png

 

Also you can create calculated columns as below:

 

Firstday = CALCULATE(MIN('transactions'[Date]),ALLEXCEPT(transactions,'transactions'[CustomerName]))
Column = 
var diff='transactions'[Date]-'transactions'[Firstday]
Return
IF(diff=0,"the first time","have purchased on a previous date")

And you will see:

1112.png

For the related .pbix file,pls click here.

 

Best Regards,

Kelly

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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