Forum Discussion

OAkanbi's avatar
OAkanbi
Frequent Visitor
4 years ago
Solved

New and Return Customer States - Power Query

Hi All, 

 

I have a bit of a problem that I have been stuck with for a week or two and I was wondering whether you guys could help out. 

 

I would like to transform this list:

 

CustomerYear
A2015
A2016
A2017
A2018
A2019
A2020
B2015
B2019
B2020
C2015
D2015
D2019
D2020
E2019

 

To look like the following matrix

 

Customer201520162017201820192020
ANew CustomerActiveActiveActiveActiveActive
BNew CustomerActiveLapsedLapsedActiveActive
CNew CustomerActiveLapsedLapsedNon-CustomerNon-Customer
DNew CustomerActiveLapsedLapsedReturneeActive
E    New CustomerActive

 

The rules are as follows:

 

New Customer - first purchase

Active - 0-2 years after last purchase

Lapsed - 2-4 years after last purchase

Non-Customer - 4 years after last purchase

Returnee - A return customer

 

I've tried looking at the New and Returning dax pattern but I haven't been able to work in the states that we require. Which is why I started to take a step back and look at power query. Any advice?

 

Ola

  • OAkanbi I don't know the Power Query way to do it, but this DAX table works:

    Table2 = 
        VAR __Customers = DISTINCT('Table'[Customer])
        VAR __Years = GENERATESERIES(MIN('Table'[Year]),MAX('Table'[Year]),1)
        VAR __Table = GENERATE(__Customers,__Years)
        VAR __Table1 = 
            ADDCOLUMNS(
                SELECTCOLUMNS(__Table,"__Customer",[Customer],"__Year",[Value]), 
                "Status",
                    VAR __MinYear = MAXX(FILTER('Table','Table'[Customer] = [__Customer] && [Year] <= [__Year]),[Year])
                    VAR __MinYear1 = MAXX(FILTER('Table','Table'[Customer] = [__Customer] && [Year] < [__Year]),[Year])
                    VAR __Diff = [__Year] - __MinYear
                    VAR __Diff1 = [__Year] - __MinYear1
                RETURN
                    SWITCH(TRUE(),
                        __Diff <= 2 && ISBLANK(__MinYear1), "New Customer",
                        __Diff1 > 2 && __Diff = 0,"Returnee",
                        __Diff < 2, "Active",
                        __Diff >=2 && __Diff <4,"Lapsed",
                        __Diff >= 4, "Non-Customer"
                    )
            )
    RETURN
        __Table1

4 Replies

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    OAkanbi I don't know the Power Query way to do it, but this DAX table works:

    Table2 = 
        VAR __Customers = DISTINCT('Table'[Customer])
        VAR __Years = GENERATESERIES(MIN('Table'[Year]),MAX('Table'[Year]),1)
        VAR __Table = GENERATE(__Customers,__Years)
        VAR __Table1 = 
            ADDCOLUMNS(
                SELECTCOLUMNS(__Table,"__Customer",[Customer],"__Year",[Value]), 
                "Status",
                    VAR __MinYear = MAXX(FILTER('Table','Table'[Customer] = [__Customer] && [Year] <= [__Year]),[Year])
                    VAR __MinYear1 = MAXX(FILTER('Table','Table'[Customer] = [__Customer] && [Year] < [__Year]),[Year])
                    VAR __Diff = [__Year] - __MinYear
                    VAR __Diff1 = [__Year] - __MinYear1
                RETURN
                    SWITCH(TRUE(),
                        __Diff <= 2 && ISBLANK(__MinYear1), "New Customer",
                        __Diff1 > 2 && __Diff = 0,"Returnee",
                        __Diff < 2, "Active",
                        __Diff >=2 && __Diff <4,"Lapsed",
                        __Diff >= 4, "Non-Customer"
                    )
            )
    RETURN
        __Table1
    • OAkanbi's avatar
      OAkanbi
      Frequent Visitor

      Thanks very much. Does exactly what I was looking for. Brilliant work.