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

Identify customers who made purchases at a particular store in their last 5 transactions

Hi,

 

I have a list of transaction at user transaction level (daily transaction). I would like to know based on the latest transaction date, I would like to know for a particular store, the users that have purchased there within their last 5 transactions.

 

e.g I would like to know for Store A, who are the customers who purchased there in ther last 5 transactions (atlest once in their last 5 transactions).

 

Sample table below:

Card_numberUser IDstatusVehicle Typestore_idstore_namedateTransaction_timelabelProductVolume (L)Sale Amount
A00001100001ACTIVE2W50000001Store A10/9/202300:00:00CardProduct A2.9950000
A00002100001ACTIVE2W50000001Store A10/10/202300:00:00CashProduct A2.9245000
A00003100002ACTIVE2W50000001Store B10/9/202300:00:00CashProduct C1.6225000
A00004100002ACTIVE4W50000001Store A10/10/202300:00:00CardProduct C23.24380000
A00005100001ACTIVE2W50000001Store A10/8/202300:00:00CashProduct A2.6941372
A00006100001ACTIVE4W50000001Store A10/7/202300:00:00CashProduct B11.16200000
A00007100003ACTIVE2W50000001Store C10/10/202300:00:00CashProduct D3.2550000
A00008100001ACTIVE2W50000001Store A10/6/202300:00:00CardProduct B3.2550000

 

 

1 ACCEPTED SOLUTION
123abc
Community Champion
Community Champion

  1. Import Data:

    • First, import your transaction data into Power BI. You can do this by clicking on "Home" > "Get Data" and then selecting the appropriate data source and connection method.
  2. Data Transformation:

    • Once your data is loaded, you may need to perform some data transformation steps to clean and structure the data. Ensure that the "date" field is recognized as a date data type.
  3. Create Calculated Columns:

    • To identify the last 5 transactions for each user, you can create a calculated column that assigns a transaction rank to each transaction within a user's history, ordered by date. You can use DAX (Data Analysis Expressions) to create this calculated column. For example:

Transaction Rank = RANKX(FILTER(ALL('YourTableName'), [User ID] = EARLIER([User ID])), [date], , ASC)

 

  1. Filter by Store:

    • Create a report or a visualization in Power BI, and add a slicer or filter that allows you to select the specific store you are interested in (e.g., "Store A").
  2. Calculate Last 5 Transactions:

    • Create a measure that calculates whether a user has made a purchase in their last 5 transactions at the selected store. You can use DAX to define this measure. For example:

Purchased in Last 5 at Store A =
VAR LastTransactionRank = MAX('YourTableName'[Transaction Rank])
VAR UserTransactionsAtStoreA = CALCULATE(COUNTROWS(FILTER(ALL('YourTableName'), [store_name] = "Store A")))
RETURN IF(LastTransactionRank >= UserTransactionsAtStoreA - 4, "Yes", "No")

 

  1. Display the Results:

    • Create a table or visual that displays the user ID and the "Purchased in Last 5 at Store A" measure. When you filter for a specific store in your slicer or filter, this table will show you which users have made purchases at that store in their last 5 transactions.

Make sure to replace 'YourTableName' with the actual name of your table in Power BI.

Remember that DAX measures can be customized to fit your specific data model, so you may need to adjust the DAX code to match your data structure and column names.

 

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

View solution in original post

4 REPLIES 4
Jayprekk
Frequent Visitor

Hi All,

I was not able to execute based on the intial solution provided. I would like to filter by store name and to be able to view the last 5 transactions of each customer based on the data fileds below. For each customer, I would like to see the user ID, date, product, store name, volume, sale amount, vehivle type. Appreciate the support. 

 

Card_numberUser IDstatusVehicle Typestore_idstore_namedateTransaction_timelabelProductVolume (L)Sale Amount
A00001100001ACTIVE2W50000001Store A10/9/202300:00:00CardProduct A2.9950000
A00002100001ACTIVE2W50000001Store A10/10/202300:00:00CashProduct A2.9245000
A00003100002ACTIVE2W50000001Store B10/9/202300:00:00CashProduct C1.6225000
A00004100002ACTIVE4W50000001Store A10/10/202300:00:00CardProduct C23.24380000
A00005100001ACTIVE2W50000001Store A10/8/202300:00:00CashProduct A2.6941372
A00006100001ACTIVE4W50000001Store A10/7/202300:00:00CashProduct B11.16200000
A00007100003ACTIVE2W50000001Store C10/10/202300:00:00CashProduct D3.2550000
A00008100001ACTIVE2W50000001Store A10/6/202300:00:00CardProduct B3.2550000
123abc
Community Champion
Community Champion

  1. Import Data:

    • First, import your transaction data into Power BI. You can do this by clicking on "Home" > "Get Data" and then selecting the appropriate data source and connection method.
  2. Data Transformation:

    • Once your data is loaded, you may need to perform some data transformation steps to clean and structure the data. Ensure that the "date" field is recognized as a date data type.
  3. Create Calculated Columns:

    • To identify the last 5 transactions for each user, you can create a calculated column that assigns a transaction rank to each transaction within a user's history, ordered by date. You can use DAX (Data Analysis Expressions) to create this calculated column. For example:

Transaction Rank = RANKX(FILTER(ALL('YourTableName'), [User ID] = EARLIER([User ID])), [date], , ASC)

 

  1. Filter by Store:

    • Create a report or a visualization in Power BI, and add a slicer or filter that allows you to select the specific store you are interested in (e.g., "Store A").
  2. Calculate Last 5 Transactions:

    • Create a measure that calculates whether a user has made a purchase in their last 5 transactions at the selected store. You can use DAX to define this measure. For example:

Purchased in Last 5 at Store A =
VAR LastTransactionRank = MAX('YourTableName'[Transaction Rank])
VAR UserTransactionsAtStoreA = CALCULATE(COUNTROWS(FILTER(ALL('YourTableName'), [store_name] = "Store A")))
RETURN IF(LastTransactionRank >= UserTransactionsAtStoreA - 4, "Yes", "No")

 

  1. Display the Results:

    • Create a table or visual that displays the user ID and the "Purchased in Last 5 at Store A" measure. When you filter for a specific store in your slicer or filter, this table will show you which users have made purchases at that store in their last 5 transactions.

Make sure to replace 'YourTableName' with the actual name of your table in Power BI.

Remember that DAX measures can be customized to fit your specific data model, so you may need to adjust the DAX code to match your data structure and column names.

 

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

HI, 

In regards to the below portion, it seems the code indicates that we are filtering to a specific site and this does not seem to work for me. I have added the calculated column but would like to select a specific site using the slicer to allow me to choose the site that i require. Thankd for the support.

 

Purchased in Last 5 at Store A =
VAR LastTransactionRank = MAX('YourTableName'[Transaction Rank])
VAR UserTransactionsAtStoreA = CALCULATE(COUNTROWS(FILTER(ALL('YourTableName'), [store_name] = "Store A")))
RETURN IF(LastTransactionRank >= UserTransactionsAtStoreA - 4, "Yes", "No")

I was able to use part of the code to get mine working. Thank you very much.

Helpful resources

Announcements
RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel

Power BI Monthly Update - May 2024

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