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

The Power BI DataViz World Championships are on! With four chances to enter, you could win a spot in the LIVE Grand Finale in Las Vegas. Show off your skills.

Reply
Anonymous
Not applicable

Time slots in months between rows in different categories

Hello all,

 

I need help in finding a solution to calculate the time slots in months between 2 products purchased by the same customer. I have lots of customers, but only 3 categories of products: apples, pears and bananas and some missing values in PurchasedDate column.

 

CustomerIDProductPurchasedDate
1apples27/09/2017
1pears04/09/2019
2apples26/09/2018
2pears01/10/2020
2bananas26/11/2020
3apples 
3pears25/05/2020

 

I have to calculate a measure that shows:

Customer 1 has bought pears vs apples: >12 months

Customer 2 has bought bananas vs pears: 0-3 months

Customer 3 has bought pears vs apples: Not defined

 

The time slots I'm interested in are:

0-3 m

3-6 m

6-9 m

9-12

>12

Not defined

 

Any idea is very helpful for me and much appreciated.

Thanks in advance!

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

Hi @Anonymous 

 

Please use below measure:

Measure =
VAR a =
    CALCULATE (
        MAX ( 'Table (2)'[PurchasedDate] ),
        FILTER (
            ALL ( 'Table (2)' ),
            [CustomerID] = MAX ( 'Table (2)'[CustomerID] )
                && [Product] <> MAX ( 'Table (2)'[Product] )
                && [PurchasedDate] <= MAX ( 'Table (2)'[PurchasedDate] )
        )
    )
VAR b =
    DATEDIFF ( a, MAX ( 'Table (2)'[PurchasedDate] ), MONTH )
RETURN
    IF (
        ISBLANK ( a ),
        "NotDefined",
        IF (
            b <= 3,
            "0-3 m",
            IF (
                b <= 6,
                "3-6 m",
                IF ( b <= 9, "6-9 m", IF ( b <= 12, "9-12 m", IF ( b > 12, ">12" ) ) )
            )
        )
    )

001.PNG

Pbix attached.

Community Support Team _ Dina Ye
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

2 REPLIES 2
v-diye-msft
Community Support
Community Support

Hi @Anonymous 

 

Please use below measure:

Measure =
VAR a =
    CALCULATE (
        MAX ( 'Table (2)'[PurchasedDate] ),
        FILTER (
            ALL ( 'Table (2)' ),
            [CustomerID] = MAX ( 'Table (2)'[CustomerID] )
                && [Product] <> MAX ( 'Table (2)'[Product] )
                && [PurchasedDate] <= MAX ( 'Table (2)'[PurchasedDate] )
        )
    )
VAR b =
    DATEDIFF ( a, MAX ( 'Table (2)'[PurchasedDate] ), MONTH )
RETURN
    IF (
        ISBLANK ( a ),
        "NotDefined",
        IF (
            b <= 3,
            "0-3 m",
            IF (
                b <= 6,
                "3-6 m",
                IF ( b <= 9, "6-9 m", IF ( b <= 12, "9-12 m", IF ( b > 12, ">12" ) ) )
            )
        )
    )

001.PNG

Pbix attached.

Community Support Team _ Dina Ye
If this post helps, then please consider Accept it as the solution to help the other members find it more
quickly.
amitchandak
Super User
Super User

@Anonymous , Create a new column like

new column = 
var _month = datediff([PurchasedDate], maxx(filter(Table,[CustomerID]=earlier([CustomerID]) && [PurchasedDate] < earlier([PurchasedDate])),[PurchasedDate]), month)

return 
switch( true(),
_month <=3 , "   0-3 m",
_month <=6 , "  3-6 m",
_month <=9 , "  6-9 m",
_month <=12 , "  9-12 m",
_month >12 , ">12",
"Not defined"
)
Full Power BI Video 20 Hours YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

Check out the February 2025 Power BI update to learn about new features.

Feb2025 NL Carousel

Fabric Community Update - February 2025

Find out what's new and trending in the Fabric community.