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:   Customer Ye...
  • Greg_Deckler's avatar
    4 years ago

    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