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
EuniceFleurs
Frequent Visitor

Consecutive Months on last 12 Months

Hi! I need help with DAX formula. I have some rules that I need to cover:

 

  • 20% discount: if the customer made a purchase in the last 12 months and made a purchase 5 months in a row
  • 5% discount: if the customer made a purchase in the last 12 months and made a purchase 3 differents months (regarless of whether they are consecutive or not)

hope you can help me, I couldn't find the way to validate consecutive months with a purchase.

regards.

6 REPLIES 6
EuniceFleurs
Frequent Visitor

This is an example. Date columns, customer name  and amount. I need to create "discount" column following the rules. I marked each client in a different color. I added a column where I show how purchases should be read, from the first purchase to the third (5% discount) and from the first to the fifth consecutive purchase (20% discount). Hope this is helpful and hope you can help me to solve this.

 

Regards. 

 

YearMonthCustomerAmountDiscount  
20181Fred5000% 1st purchase
20182     
20183     
20184     
20185     
20186Jorch3990% 1st purchase
20187     
20188Fred4000% 2nd purchase
20189Fred8005% 3rd purchase
201810Fred9005% 3rd purchase
201811Fred5005% 3rd purchase
201812Fred30020% 5 in a row
20191Jorch5000% 2nd purchase
20192     
20193Jorch4005% 3rd purchase
20194     
20195     
20196Jorch5005% 3rd purchase
20197     
20198     
20199Jorch6005% 3rd purchase

@EuniceFleurs To test 5 months in a row in DAX is really a head-scratching problem, isn't it? @Greg_Deckler

Fortunately, Excel, our old good pal comes in rescue. A solution with Excel fomulas is attached. Just for fun! 😎

streak.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

@CNENFRNL I guess I have to say, not really?? It seems rather straight-forward. It's the __5Distinct variable. If you have exactly 5 months of data then you're just looking for 5 distinct values for the month column.

 

@EuniceFleurs I attached a PBIX with a DAX solution to the issue at hand. Attached below sig, you want Table (9). Let me know if it works for you, essentially these three columns:

Date = DATE([Year],[Month],1)

Purchase # in Last 12 Months = 
    VAR __Date = [Date]
    VAR __Dates = { [Date] }
    VAR __MinDate = DATE(YEAR(__Date)-1,MONTH(__Date),DAY(__Date))
    VAR __5Date = EOMONTH(__Date,-6)+1
    VAR __Count = COUNTROWS(FILTER('Table (9)',[Date]>=__MinDate && [Date]<=__Date && [Customer] = EARLIER([Customer])))
    VAR __5Count = COUNTROWS(FILTER('Table (9)',[Date]>=__5Date && [Date]<=__Date && [Customer] = EARLIER([Customer])))
    VAR __5Distinct = COUNTROWS(DISTINCT(SELECTCOLUMNS(FILTER('Table (9)',[Date]>=__5Date && [Date]<=__Date && [Customer] = EARLIER([Customer])),"__Month",[Month])))
RETURN
    SWITCH(TRUE(),
        [Customer] = "",BLANK(),
        __5Count >= 5 && __5Distinct=5,"5 in a row",
        __Count>=3,"3rd purchase",
        __Count=2,"2nd purchase",
        "1st purchase"
    )

Discount = 
    SWITCH(TRUE(),
        [Purchase # in Last 12 Months] = "3rd purchase",.05,
        [Purchase # in Last 12 Months] = "5 in a row",.2,
        [Customer]="",BLANK(),
        0
    )

 

@ me if problems


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Don't know how to attach an excel file, but here is a google sheet link with the same dataset

 

Example Data 

Anonymous
Not applicable

DAX can't be written in vacuum. You have to describe your model. And then give us some good examples of what it really is you want. Otherwise, it's not possible to help.
Greg_Deckler
Super User
Super User

@EuniceFleurs Sorry, having trouble following, can you post sample data as text and expected output?


Not really enough information to go on, please first check if your issue is a common issue listed here: https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882

Also, please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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.

Top Solution Authors