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

Be 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

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/

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

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.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

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.


Visit my LinkedIn page by clicking here.


Schedule a meeting with me to discuss further by clicking here.

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/

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here
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
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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.