Forum Discussion

Adamkowalsky92's avatar
Adamkowalsky92
Frequent Visitor
1 year ago
Solved

DAX Measure - Co-occurrences

 

Hi, I'm having trouble coming up with a measurement.

Simple table, 2 columns, Invoice No. and Product. The task is to obtain the result of co-occurrences on one invoice.
I want to have a slicer with product selection and a table where I add a product column to the rows and this measure to the value. Selecting product 1 in the slicer will show me the remaining products and the number of co-occurrences on one of the invoices.
for example: let's assume that product 1 appeared on 100 invoices, it will show me that product 2 appeared 50 times out of these 100 invoices, product 3 20 times etc.


Example:

Invoice no.Product
1231
1232
1233
2341
2343
3451
3454
3455
4561
4562
4563
4564


Results, after choose Product 1 from slicer

ProductOccurreces
22
33
42
51




  • You'd need to create a separate table for use in the slicer. You can use

    Product for slicer = DISTINCT( 'Table'[Product] )

    Do not connect that to the main table, just use it in the slicer.

    You can then create a measure like

    [Num co-occurences] = IF(
    SELECTEDVALUE('Product for slicer'[Product]) <> SELECTEDVALUE('Table'[Product]),
    VAR InvoicesForChosenProduct = CALCULATETABLE(
    	VALUES('Table'[Invoice no.]),
    	TREATAS(
    		VALUES('Product for slicer'[Product]),
    		'Table'[Product]
    	)
    )
    VAR InvoicesForCurrentProduct = VALUES('Table'[Invoice no.])
    VAR Result = COUNTROWS(INTERSECT(
    	InvoicesForChosenProduct,
    	InvoicesForCurrentProduct
    ))
    RETURN
    	Result
    )

    and use the [Product] column from the main table in your visual.

3 Replies

  • You'd need to create a separate table for use in the slicer. You can use

    Product for slicer = DISTINCT( 'Table'[Product] )

    Do not connect that to the main table, just use it in the slicer.

    You can then create a measure like

    [Num co-occurences] = IF(
    SELECTEDVALUE('Product for slicer'[Product]) <> SELECTEDVALUE('Table'[Product]),
    VAR InvoicesForChosenProduct = CALCULATETABLE(
    	VALUES('Table'[Invoice no.]),
    	TREATAS(
    		VALUES('Product for slicer'[Product]),
    		'Table'[Product]
    	)
    )
    VAR InvoicesForCurrentProduct = VALUES('Table'[Invoice no.])
    VAR Result = COUNTROWS(INTERSECT(
    	InvoicesForChosenProduct,
    	InvoicesForCurrentProduct
    ))
    RETURN
    	Result
    )

    and use the [Product] column from the main table in your visual.

    • Adamkowalsky92's avatar
      Adamkowalsky92
      Frequent Visitor

      Great, great, great! Exactly what i want! Thank you so much.

  • It's solved already when I saw it; but just for fun only, I provide a more sophisticated solution applicable to a more generic model. What's more an intricacy of it is that when more products selected in the slicer (ie, product 1&2), only those invoices (invoice 123 & 456) containing all selected products will be further investigated for co-existing products (product 3&4).