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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
jesspiorier
Frequent Visitor

DAX to flag Customer as Yes, No, or Both based on purchase date from Sales Table

I'm struggling with a dax equation to identify different types of customers, specifically a Yes, No, or Both scenario for holiday shoppers.

 

I have a transaction table and a customer table (Sales and Cust tables both have an ID#). 

 

I've flagged Transactions as a Yes or No, based on Purchase Month. (Holiday Shoppers=10, 11, 12, Non-Holiday= 1, 2, 3, 4, 5, 6, 7, 8, 9).  How do I identify customers who are exclusively Yes or No, or those who are both?  

 

I found a post that looked at "One-Time", "First Order" and "Returned", but the DAX was based on Min/Max purchase dates and I can't figure out how to customize it for my needs.

 

Sample:

Transaction #Customer IDPurchase MonthHoliday Purchase
123A6N
124B7N
125C8N
126D9N
127E10Y
128A10Y
129A11Y
130B12Y
131C1N
132D2N
133D3N

 

Desired Reesults:

Customer IDHoliday Shopper
ABoth
BBoth
CNo
DNo
EYes

 

Thanks in advance.  -Jess

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

Hi @jesspiorier ,

 

Solution using measure and table visual is great, but in case you want a calculated table, you can use the following formula.

 

Table 2 = 
ADDCOLUMNS (
    DISTINCT ( 'Table'[Customer ID] ),
    "Holiday Shopper",
    VAR t =
        FILTER ( 'Table', 'Table'[Customer ID] = EARLIER ( [Customer ID] ) )
    VAR holiday =
        COUNTROWS ( FILTER ( t, [Holiday Purchase] = "Y" ) ) + 0
    VAR noholiday =
        COUNTROWS ( FILTER ( t, [Holiday Purchase] = "N" ) ) + 0
    RETURN
        SWITCH (
            TRUE (),
            holiday > 0
                && noholiday > 0, "Both",
            holiday > 0
                && noholiday <= 0, "Yes",
            "No"
        )
)

10.PNG

 

Or you can add a Calculated column in your customer table:

 

Holiday Shopper =
VAR id = [Customer ID]
VAR t =
    FILTER ( 'Table', 'Table'[Customer ID] = id )
VAR holiday =
    COUNTROWS ( FILTER ( t, [Holiday Purchase] = "Y" ) ) + 0
VAR noholiday =
    COUNTROWS ( FILTER ( t, [Holiday Purchase] = "N" ) ) + 0
RETURN
    SWITCH (
        TRUE (),
        holiday > 0
            && noholiday > 0, "Both",
        holiday > 0
            && noholiday <= 0, "Yes",
        "No"
    )

 

And here is my measure:

 

Holiday Shopper = 
VAR holiday =
    COUNTROWS ( FILTER ( 'Table', 'Table'[Holiday Purchase] = "Y" ) ) + 0
VAR noholiday =
    COUNTROWS ( FILTER ( 'Table', 'Table'[Holiday Purchase] = "N" ) ) + 0
RETURN
    SWITCH (
        TRUE (),
        holiday > 0
            && noholiday > 0, "Both",
        holiday > 0
            && noholiday <= 0, "Yes",
        "No"
    )

9.PNG

 


BTW, pbix as attached.

 

Best regards,

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

8 REPLIES 8
v-lid-msft
Community Support
Community Support

Hi @jesspiorier ,

 

Solution using measure and table visual is great, but in case you want a calculated table, you can use the following formula.

 

Table 2 = 
ADDCOLUMNS (
    DISTINCT ( 'Table'[Customer ID] ),
    "Holiday Shopper",
    VAR t =
        FILTER ( 'Table', 'Table'[Customer ID] = EARLIER ( [Customer ID] ) )
    VAR holiday =
        COUNTROWS ( FILTER ( t, [Holiday Purchase] = "Y" ) ) + 0
    VAR noholiday =
        COUNTROWS ( FILTER ( t, [Holiday Purchase] = "N" ) ) + 0
    RETURN
        SWITCH (
            TRUE (),
            holiday > 0
                && noholiday > 0, "Both",
            holiday > 0
                && noholiday <= 0, "Yes",
            "No"
        )
)

10.PNG

 

Or you can add a Calculated column in your customer table:

 

Holiday Shopper =
VAR id = [Customer ID]
VAR t =
    FILTER ( 'Table', 'Table'[Customer ID] = id )
VAR holiday =
    COUNTROWS ( FILTER ( t, [Holiday Purchase] = "Y" ) ) + 0
VAR noholiday =
    COUNTROWS ( FILTER ( t, [Holiday Purchase] = "N" ) ) + 0
RETURN
    SWITCH (
        TRUE (),
        holiday > 0
            && noholiday > 0, "Both",
        holiday > 0
            && noholiday <= 0, "Yes",
        "No"
    )

 

And here is my measure:

 

Holiday Shopper = 
VAR holiday =
    COUNTROWS ( FILTER ( 'Table', 'Table'[Holiday Purchase] = "Y" ) ) + 0
VAR noholiday =
    COUNTROWS ( FILTER ( 'Table', 'Table'[Holiday Purchase] = "N" ) ) + 0
RETURN
    SWITCH (
        TRUE (),
        holiday > 0
            && noholiday > 0, "Both",
        holiday > 0
            && noholiday <= 0, "Yes",
        "No"
    )

9.PNG

 


BTW, pbix as attached.

 

Best regards,

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thanks again @v-lid-msft . This worked perfectly.  

However, now I'm trying to do it for clients who fall into four different categories, or a combination of them.  I tried modifying your original "if then else" formula, but I'm not getting the expected results.  Perhaps you can help?

It's essentially the same issue.  One customer who has had at least one interaction (potentially many). Those interactions can fall into four categories (not the original post's two).  It is NOT time-related, nor do I want results at the customer ID level.  I want a Distinct Count of IDs based on Category Distribution.

 

It is a single flat table. Client ID is unique. Each interaction (row) has the Category flag.

I can get a Distinct Count of IDs per Category easily.  I want to know how many IDs fit into one Category only, or several.  Expected result:

 

Category A Only:  5

Category B Only: 2

Category C Only: 1

Category D Only: 4

AB -   3

AC  - 2

AD - 1

BC -2

BD - 0

CD - 5

ABC: 0

ABD: 1
BCD: 3

CDA: 1
ABCD: 10

Does that make sense?  Is it possible with DAX?  

Hi @jesspiorier ,

 

Could you please share the sample data based on the expected table you have shared? 

 

Please don't have any Confidential Information or Real data in your reply.

 


Best regards,

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Thank you @v-lid-msft . The calculated column (your second suggestion) gave me the expected results.  Thanks for the help!  

 

Note: I did need to change the names of the variables you suggested in your example.  I was getting a syntax error on "id" and "t" for some reason.  So I called them "Customer Count" and "Truth". Worked just fine.  Thanks again.

 

Holiday Shopper =
VAR CustomerCount = 'Sales'[Customer ID]
VAR truth =
    FILTER ( 'Sales', 'Sales'[Customer ID] = CustomerCount )
VAR holiday =
    COUNTROWS ( FILTER ( truth, [Holiday Purchase] = "Y" ) ) + 0
VAR noholiday =
    COUNTROWS ( FILTER ( truth, [Holiday Purchase] = "N" ) ) + 0
RETURN
    SWITCH (
        TRUE (),
        holiday > 0
            && noholiday > 0, "Both",
        holiday > 0
            && noholiday <= 0, "Yes",
        "No"
    )

 

 

Based on some additional equations I have used, the results are as expect.

holiday shopper.PNG

ChrisMendoza
Resident Rockstar
Resident Rockstar

@jesspiorier -

Does the following accomplish your task?

Measure 2 =
VAR _holidayYes =
    CALCULATE (
        COUNTROWS ( TableName ),
        TableName[Holiday Purchase] = "Y"
    )
VAR _holidayNo =
    CALCULATE (
        COUNTROWS ( TableName ),
        TableName[Holiday Purchase] = "N"
    )
RETURN
    SWITCH (
        TRUE (),
        _holidayNo >= 1
            && _holidayYes >= 1, "Both",
        _holidayNo >= 1, "No",
        "Yes"
    )





Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

Proud to be a Super User!



Hi @ChrisMendoza . Thanks for the prompt response.  Unfortunately, the measure did not work as expected.  Even though I see Y and N flags in my dataset (and I know that there are multiple one-time or repeat shoppers), the only value returned was "both".  I used the calculated column suggested below and it worked.  Thank you though.

Hi @jesspiorier ,

 


Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos are nice too.
Nathaniel

 

Shopper Timing = 

var Holiday = CALCULATE(COUNTA(Customer[Holiday Purchase]),FILTER(ALLEXCEPT(Customer,Customer[Customer ID]), Customer[Holiday Purchase] = "Y" ))
var Othermonth =  CALCULATE(COUNTA(Customer[Holiday Purchase]),FILTER(ALLEXCEPT(Customer,Customer[Customer ID]), Customer[Holiday Purchase] = "N" ))

var Whichmonths = If(Holiday>0 && Othermonth>0,"Both", If(Holiday>0 && Othermonth=0,"Yes",  IF(Holiday=0 && Othermonth>0,"No","N/A")))



return Whichmonths




Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




holiday.PNG

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.