March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now
I have a sales invoice table that lists items purchased. What I want to do is create a table of item pairs to determine the items that are most often purchased together. Here's an example table:
Invoice | Item |
S1 | A |
S1 | B |
S1 | C |
S1 | D |
S1 | E |
S2 | A |
S2 | B |
S2 | E |
S2 | F |
S3 | B |
S4 | A |
S4 | B |
S4 | C |
S4 | D |
S4 | E |
S4 | F |
S4 | G |
S4 | H |
S5 | C |
S5 | F |
S5 | H |
In this case, the item pair A,B occurs 3 times, B,E occurs 2 times, etc.
I'm not sure how to do this without looping.
Any help is very much appreciated!
Solved! Go to Solution.
@Anonymous , to me it seem like a market basket problem
https://finance-bi.com/power-bi-basket-analysis/
https://businessintelligist.com/2017/03/27/market-basket-analysis-using-power-bi/
https://www.daxpatterns.com/usecases/market-basket-analysis/
Hi,
I am not sure how your desired output looks like, but please check the below picture and the attached pbix file.
Invoice count measure: =
VAR _invoicelistitemA =
SUMMARIZE (
CALCULATETABLE (
Data,
Data[Item] = SELECTEDVALUE ( Item_A[Item] ),
ALL ( Item_B[Item] )
),
Data[Invoice]
)
VAR _invoicelistitemB =
SUMMARIZE (
CALCULATETABLE (
Data,
Data[Item] = SELECTEDVALUE ( Item_B[Item] ),
ALL ( Item_A[Item] )
),
Data[Invoice]
)
RETURN
IF (
SELECTEDVALUE ( Item_A[Item] ) = SELECTEDVALUE ( Item_B[Item] ),
BLANK (),
COUNTROWS ( INTERSECT ( _invoicelistitemA, _invoicelistitemB ) )
)
If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.
@Jihwan_Kim
The output I want is a table of item pairs and frequency of occurrence. So based on the sample data, the output would look like this:
Pair | Count |
A - B | 3 |
A - C | 2 |
A - D | 2 |
A - E | 3 |
A - F | 2 |
A - G | 1 |
A - H | 1 |
B - C | 2 |
B - D | 2 |
B - E | 3 |
B - F | 2 |
B - G | 2 |
B - H | 2 |
C - D | 2 |
C - E | 2 |
C - F | 2 |
C - G | 1 |
C - H | 2 |
D - E | 2 |
D - F | 1 |
D - G | 1 |
D - H | 1 |
E - F | 2 |
E - G | 1 |
E - H | 1 |
F - G | 1 |
F - H | 2 |
G - H | 1 |
That matches the upper or lower diagonal of your matrix. I can convert the format easily enough.
Thanks very much!
An interesting brain teaser. Try to wrap your head around the following solution, then essence of DAX, such as Calculate(), relationship, context transition are no more secrets to you.
Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension! |
DAX is simple, but NOT EASY! |
Hi,
I am not sure how your desired output looks like, but please check the below picture and the attached pbix file.
Invoice count measure: =
VAR _invoicelistitemA =
SUMMARIZE (
CALCULATETABLE (
Data,
Data[Item] = SELECTEDVALUE ( Item_A[Item] ),
ALL ( Item_B[Item] )
),
Data[Invoice]
)
VAR _invoicelistitemB =
SUMMARIZE (
CALCULATETABLE (
Data,
Data[Item] = SELECTEDVALUE ( Item_B[Item] ),
ALL ( Item_A[Item] )
),
Data[Invoice]
)
RETURN
IF (
SELECTEDVALUE ( Item_A[Item] ) = SELECTEDVALUE ( Item_B[Item] ),
BLANK (),
COUNTROWS ( INTERSECT ( _invoicelistitemA, _invoicelistitemB ) )
)
If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.
@Jihwan_Kim
The output I want is a table of item pairs and frequency of occurrence. So based on the sample data, the output would look like this:
Pair | Count |
A - B | 3 |
A - C | 2 |
A - D | 2 |
A - E | 3 |
A - F | 2 |
A - G | 1 |
A - H | 1 |
B - C | 2 |
B - D | 2 |
B - E | 3 |
B - F | 2 |
B - G | 2 |
B - H | 2 |
C - D | 2 |
C - E | 2 |
C - F | 2 |
C - G | 1 |
C - H | 2 |
D - E | 2 |
D - F | 1 |
D - G | 1 |
D - H | 1 |
E - F | 2 |
E - G | 1 |
E - H | 1 |
F - G | 1 |
F - H | 2 |
G - H | 1 |
That matches the upper or lower diagonal of your matrix. I can convert the format easily enough.
Thanks very much!
@Anonymous , to me it seem like a market basket problem
https://finance-bi.com/power-bi-basket-analysis/
https://businessintelligist.com/2017/03/27/market-basket-analysis-using-power-bi/
https://www.daxpatterns.com/usecases/market-basket-analysis/
@amitchandak It is most definitely a market basket problem, although I forgot the common name.
After submitting this question, I was pointed to a similar post:
https://community.powerbi.com/t5/DAX-Commands-and-Tips/Common-Items/m-p/2002442
It provides me the output I'm looking for.
I will review the links above and compare results.
Thank you very much for your reply.
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.
Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.
User | Count |
---|---|
25 | |
16 | |
15 | |
7 | |
7 |
User | Count |
---|---|
37 | |
31 | |
16 | |
16 | |
12 |