Check your eligibility for this 50% exam voucher offer and join us for free live learning sessions to get prepared for Exam DP-700.
Get StartedDon'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.
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 |
Solved! Go to Solution.
Hi @win_anthony ,
Here I create a sample to have a test.
Tables:
DimDate =
ADDCOLUMNS (
CALENDARAUTO (),
"month_year", FORMAT ( [Date], "MMM YYYY" ),
"YearMonth",
YEAR ( [Date] ) * 100
+ MONTH ( [Date] )
)
Relationship:
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.
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.
Hi @win_anthony ,
Here I create a sample to have a test.
Tables:
DimDate =
ADDCOLUMNS (
CALENDARAUTO (),
"month_year", FORMAT ( [Date], "MMM YYYY" ),
"YearMonth",
YEAR ( [Date] ) * 100
+ MONTH ( [Date] )
)
Relationship:
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.
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?
If you want to exclude the first purchase in each period then I think you can just use
Repeat Purchases = COUNTROWS('Table') - 1
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.
User | Count |
---|---|
124 | |
79 | |
50 | |
38 | |
38 |
User | Count |
---|---|
196 | |
80 | |
70 | |
51 | |
42 |