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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Find Items frequently purchased together

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:

InvoiceItem
S1A
S1B
S1C
S1D
S1E
S2A
S2B
S2E
S2F
S3B
S4A
S4B
S4C
S4D
S4E
S4F
S4G
S4H
S5C
S5F
S5H

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!

3 ACCEPTED SOLUTIONS
amitchandak
Super User
Super User

@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/

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

View solution in original post

Jihwan_Kim
Super User
Super User

Hi,

I am not sure how your desired output looks like, but please check the below picture and the attached pbix file.

 

Jihwan_Kim_1-1666062399187.png

 

Jihwan_Kim_0-1666062376168.png

 

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.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

View solution in original post

Anonymous
Not applicable

@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:

PairCount
A - B3
A - C2
A - D2
A - E3
A - F2
A - G1
A - H1
B - C2
B - D2
B - E3
B - F2
B - G2
B - H2
C - D2
C - E2
C - F2
C - G1
C - H2
D - E2
D - F1
D - G1
D - H1
E - F2
E - G1
E - H1
F - G1
F - H2
G - H1

That matches the upper or lower diagonal of your matrix. I can convert the format easily enough.
Thanks very much!

 

View solution in original post

5 REPLIES 5
CNENFRNL
Community Champion
Community Champion

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.

CNENFRNL_0-1666101411138.png


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!

Jihwan_Kim
Super User
Super User

Hi,

I am not sure how your desired output looks like, but please check the below picture and the attached pbix file.

 

Jihwan_Kim_1-1666062399187.png

 

Jihwan_Kim_0-1666062376168.png

 

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.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.
Anonymous
Not applicable

@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:

PairCount
A - B3
A - C2
A - D2
A - E3
A - F2
A - G1
A - H1
B - C2
B - D2
B - E3
B - F2
B - G2
B - H2
C - D2
C - E2
C - F2
C - G1
C - H2
D - E2
D - F1
D - G1
D - H1
E - F2
E - G1
E - H1
F - G1
F - H2
G - H1

That matches the upper or lower diagonal of your matrix. I can convert the format easily enough.
Thanks very much!

 

amitchandak
Super User
Super User

@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/

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube
Anonymous
Not applicable

@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.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.