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
gbarr12345
Helper V
Helper V

Trying to get the days between orders for a particular customer

Hi everyone,

 

I wrote a DAX measure to try and get the days between orders for a particular customer - in the sample the name is Angela Mcintosh.

 

When I input the measure it's still showing all the customers when I just want it to show Angela Mcintosh.

 

Is there something that I could add or remove to fix this?

 

The code and screenshot is below thanks!

 

DaysBetweenOrders =
VAR OrderNum = max(Orders[Order ID])
VAR PreviousOrderID = CALCULATE(MAX(Orders[Order ID] ), FILTER(ALLSELECTED(Orders), Orders[Order ID] < OrderNum &&
Orders[Customer Name] = "Carrie Mcintosh")
)
VAR CurrentDate = VALUE( SELECTEDVALUE(Orders[Order Date]))
VAR PriorDate = VALUE( CALCULATE( SELECTEDVALUE( Orders[Order Date] ), FILTER( ALL(Orders), Orders[Order ID] = PreviousOrderID)))

RETURN
IF(OrderNum = CALCULATE( MIN(Orders[Order ID] ), ALLSELECTED(Orders)),
    0,
    CurrentDate - PriorDate)

 

 

gbarr12345_0-1714357634762.png

 

4 REPLIES 4
gbarr12345
Helper V
Helper V

Also I got this error - 

gbarr12345_1-1714422236123.png

 

MFelix
Super User
Super User

Hi @gbarr12345 ,

 

You need to filter out the values for that specific customer something similar to this should work:

DaysBetweenOrders =
VAR OrderNum =
    MAX ( Orders[Order ID] )
VAR PreviousOrderID =
    CALCULATE (
        MAX ( Orders[Order ID] ),
        FILTER (
            ALLSELECTED ( Orders ),
            Orders[Order ID] < OrderNum
                && Orders[Customer Name] = "Carrie Mcintosh"
        )
    )
VAR CurrentDate =
    VALUE ( SELECTEDVALUE ( Orders[Order Date] ) )
VAR PriorDate =
    VALUE (
        CALCULATE (
            SELECTEDVALUE ( Orders[Order Date] ),
            FILTER ( ALL ( Orders ), Orders[Order ID] = PreviousOrderID )
        )
    )
RETURN
    IF (
        Orders[Customer Name] = "Angela Mcintosh"
            && OrderNum = CALCULATE ( MIN ( Orders[Order ID] ), ALLSELECTED ( Orders ) ),
        0,
        CurrentDate - PriorDate
    )

 

Be aware that you refer Angela but on the first part of your calculation you refer Carrie


Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português



Thanks for the response.

 

The IF statement part doesn't recognise Orders[Customer Name] = "Angela Mcintosh" part. It's not bringing up Orders[Customer Name] for me. Is there something I'm missing?

 

gbarr12345_0-1714421859856.png

 

 

Hi @gbarr12345 ,

 

You are correct we need to specify the value of the customer try with this measure:

DaysBetweenOrders =
VAR OrderNum =
    MAX ( Orders[Order ID] )
VAR PreviousOrderID =
    CALCULATE (
        MAX ( Orders[Order ID] ),
        FILTER (
            ALLSELECTED ( Orders ),
            Orders[Order ID] < OrderNum
                && Orders[Customer Name] = "Carrie Mcintosh"
        )
    )
VAR CurrentDate =
    VALUE ( SELECTEDVALUE ( Orders[Order Date] ) )
VAR PriorDate =
    VALUE (
        CALCULATE (
            SELECTEDVALUE ( Orders[Order Date] ),
            FILTER ( ALL ( Orders ), Orders[Order ID] = PreviousOrderID )
        )
    )
RETURN
    IF (
        SELECTEDVALUE(Orders[Customer Name]) = "Angela Mcintosh"
            && OrderNum = CALCULATE ( MIN ( Orders[Order ID] ), ALLSELECTED ( Orders ) ),
        0,
        CurrentDate - PriorDate
    )

Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português



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.