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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
win_anthony
Resolver III
Resolver III

Duplicate Transaction Count Excluding First Instance

The goal is to count the number of transactions a customer has purchased the same product excluding their very first purchase of the product. Below you will find sample data and expected output. I cannot figure out how to exclude the customer's very first purchase of a product. Any advice on how to achieve this is greatly appreciated. 

 

Sample Data 

trans_id cust_id prod_id prod_name date
1 100 A Pizza Dec 1, 2022
2 100 B Sushi Dec 1, 2022
3 100 A Pizza Dec 3, 2022
4 200 B Sushi Dec 4, 2022
5 100 A Pizza Dec 4, 2022
6 200 B Sushi Dec 6, 2022
7 100 A Pizza Jan 1, 2023
8 200 A Pizza Jan 1, 2023
9 100 B Sushi Jan 1, 2023
10 100 A Pizza Jan 2, 2023

Expected Output *Notice cust_id = 100, Pizza was purchased 3 times in December 2022 but expected count = 2 (bc excluding the first transaction) and January 2023 = 2*

month_year cust_id prod_id prod_name expected_count
Dec 2022 100 A Pizza 2
Dec 2022 200 B Sushi 1
Jan 2023 100 A Pizza 2
Jan 2023 100 B Sushi 1
1 ACCEPTED SOLUTION
v-rzhou-msft
Community Support
Community Support

Hi @win_anthony ,

 

Here I create a sample to have a test.

Tables:

RicoZhou_0-1672906389394.png

DimDate =
ADDCOLUMNS (
    CALENDARAUTO (),
    "month_year", FORMAT ( [Date], "MMM YYYY" ),
    "YearMonth",
        YEAR ( [Date] ) * 100
            + MONTH ( [Date] )
)

Relationship:

RicoZhou_1-1672906436108.png

Measure:

Repeat Purchases =
VAR _LASTDATE =
    CALCULATE (
        MIN ( 'Table'[date] ),
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[cust_id], 'Table'[prod_id], 'Table'[prod_name] ),
            'Table'[date] < MAX ( 'Table'[date] )
        )
    )
VAR _COUNTROW =
    COUNTROWS ( 'Table' )
RETURN
    IF (
        _LASTDATE
            IN VALUES ( DimDate[Date] )
                || _LASTDATE = BLANK (),
        IF ( _COUNTROW - 1 <= 0, BLANK (), _COUNTROW - 1 ),
        _COUNTROW
    )

Result is as below.

RicoZhou_2-1672906458038.png

 

Best Regards,
Rico Zhou

 

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

3 REPLIES 3
v-rzhou-msft
Community Support
Community Support

Hi @win_anthony ,

 

Here I create a sample to have a test.

Tables:

RicoZhou_0-1672906389394.png

DimDate =
ADDCOLUMNS (
    CALENDARAUTO (),
    "month_year", FORMAT ( [Date], "MMM YYYY" ),
    "YearMonth",
        YEAR ( [Date] ) * 100
            + MONTH ( [Date] )
)

Relationship:

RicoZhou_1-1672906436108.png

Measure:

Repeat Purchases =
VAR _LASTDATE =
    CALCULATE (
        MIN ( 'Table'[date] ),
        FILTER (
            ALLEXCEPT ( 'Table', 'Table'[cust_id], 'Table'[prod_id], 'Table'[prod_name] ),
            'Table'[date] < MAX ( 'Table'[date] )
        )
    )
VAR _COUNTROW =
    COUNTROWS ( 'Table' )
RETURN
    IF (
        _LASTDATE
            IN VALUES ( DimDate[Date] )
                || _LASTDATE = BLANK (),
        IF ( _COUNTROW - 1 <= 0, BLANK (), _COUNTROW - 1 ),
        _COUNTROW
    )

Result is as below.

RicoZhou_2-1672906458038.png

 

Best Regards,
Rico Zhou

 

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

 

@v-rzhou-msft thank you so much for your support. This worked but when put into a table, the totals are not adding correctly. Ex: The expected total for measure "Repeated Purchases" should = 6 (this is what we want). Once we turn on the visual total, it now = 9 (this is what we are getting). Any advice on how to have the expected total reflected?

johnt75
Super User
Super User

If you want to exclude the first purchase in each period then I think you can just use

Repeat Purchases = COUNTROWS('Table') - 1

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!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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