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

Sumx and datesbetween two date columns

Hello community 

 

I'm having trouble creating a column that sums up the turnover for each customer created within the first 20 days of his first purchase. 

 

I have added a picture and a google sheets example of the data ( the yellow column is what I'm trying to create).
My dax so far looks like this:

 

20DaysPurchases = SUMX(
  FILTER(Table[customer id]=EARLIER(Table[customer id]) &&
   DATESBETWEEN(Table[Purchase Date];(Table[Customer Added Date]-20);Table[Customer Added Date]))
     ;Table[Turnover])
 
It returns the error "A date column containing duplicate dates was specified in the call to function 'DATESBETWEEN'. This is not supported."

 

 

AlexanderBP_0-1597929889587.png

AlexanderBP_1-1597929889662.png

 


https://docs.google.com/spreadsheets/d/1EQ5-fy2Q66skkIX4aP6RJnltIdeb5OUVYX_SaHg_swg/edit?usp=sharing

Thank you in advance

2 ACCEPTED SOLUTIONS
richbenmintz
Solution Sage
Solution Sage

Hi @AlexanderBP,

 

Please try this column formula, it results in the following table based on your sample data. 

 

20DaysPurchases = 
    var custId = [Customer id]
    var firstPurchaseDate  = CALCULATE(MIN([Customer Added Date]), filter(ALL('Table'), 'Table'[Customer id] = custId))+20
return 
    CALCULATE(SUM([Turnover]), FILTER(ALL('Table'), 'Table'[Purchase Date] <= firstPurchaseDate && 'Table'[Customer id] = custId))

 

richbenmintz_1-1597933183229.png

 

Hope this Helps,
Richard
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!

 



I hope this helps,
Richard

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

Proud to be a Super User!


View solution in original post

Icey
Community Support
Community Support

Hi @AlexanderBP ,

 

The below column works in your scenario, too. The calculation logic is similar to that of @richbenmintz.

And if you create it as a measure, it also works.

 

20DaysPurchases = 
VAR FirstPurchase =
    CALCULATE (
        MIN ( 'Table'[Purchase Date] ),
        ALLEXCEPT ( 'Table', 'Table'[Customer id] )
    )
RETURN
    CALCULATE (
        SUM ( 'Table'[Turnover] ),
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[Customer id] ),
            'Table'[Purchase Date] <= FirstPurchase + 20
        )
    )

 

purchase.png

 

You can check more details from here.

 

 

Best Regards,

Icey

 

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

4 REPLIES 4
Icey
Community Support
Community Support

Hi @AlexanderBP ,

 

The below column works in your scenario, too. The calculation logic is similar to that of @richbenmintz.

And if you create it as a measure, it also works.

 

20DaysPurchases = 
VAR FirstPurchase =
    CALCULATE (
        MIN ( 'Table'[Purchase Date] ),
        ALLEXCEPT ( 'Table', 'Table'[Customer id] )
    )
RETURN
    CALCULATE (
        SUM ( 'Table'[Turnover] ),
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[Customer id] ),
            'Table'[Purchase Date] <= FirstPurchase + 20
        )
    )

 

purchase.png

 

You can check more details from here.

 

 

Best Regards,

Icey

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

richbenmintz
Solution Sage
Solution Sage

Hi @AlexanderBP,

 

Please try this column formula, it results in the following table based on your sample data. 

 

20DaysPurchases = 
    var custId = [Customer id]
    var firstPurchaseDate  = CALCULATE(MIN([Customer Added Date]), filter(ALL('Table'), 'Table'[Customer id] = custId))+20
return 
    CALCULATE(SUM([Turnover]), FILTER(ALL('Table'), 'Table'[Purchase Date] <= firstPurchaseDate && 'Table'[Customer id] = custId))

 

richbenmintz_1-1597933183229.png

 

Hope this Helps,
Richard
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!

 



I hope this helps,
Richard

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

Proud to be a Super User!


Greg_Deckler
Super User
Super User

@AlexanderBP - I'm not sure I understand what you are trying to do with your DATESBETWEEN statement. Can you explain what you are trying to do in your formula?

 

You may find this helpful - https://community.powerbi.com/t5/Community-Blog/To-bleep-With-Time-Intelligence/ba-p/1260000

Also, see if my Time Intelligence the Hard Way provides a different way of accomplishing what you are going for.

https://community.powerbi.com/t5/Quick-Measures-Gallery/Time-Intelligence-quot-The-Hard-Way-quot-TIT...


@ 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...
amitchandak
Super User
Super User

Helpful resources

Announcements
PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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