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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
brunovetupcombr
New Member

DAX - Calculate the quantity of cross-sales in a physical table.

The goal of the formula I am trying to create is to generate a table with two columns: one containing the product names from the dProdutosAux table, and another containing the values of cross-selling for those products with the selected "CONSULTA" product from the fVendas table.

To achieve this objective, the formula I am attempting involves:

1. Selecting the product name from the dProdutosAux table in the current context using SELECTEDVALUE(dProdutosAux[nomeProduto]).

2. Intersecting two calculated tables:
- The first calculated table contains the sales codes where the product is "CONSULTA," filtered to exclude blank rows (NOT(ISBLANK(fVendas[codigoCliente]))).
- The second calculated table contains the sales codes where the product is the selected product from the dProdutosAux table, filtered to exclude blank rows and taking into account the relationship between the tables.

3. Counting the number of rows in the intersection table to obtain the quantity of cross-selling.

4. Creating a calculated table using the ADDCOLUMNS function, which adds a "qtdCruzada" column to the dProdutosAux table and associates the calculated value of cross-selling with the correct product.

The measure should return a table with the products from the dProdutosAux table and the quantity of cross-selling between each product and the "CONSULTA" product. However, it is not correctly recognizing the value of the selected product.

 

'''

produtoY =
VAR vNomeProdutoSelecionado = "CONSULTA"
VAR vNomeProdutoAux = SELECTEDVALUE(dProdutosAux[nomeProduto])

VAR vIntersecao =  
    INTERSECT(
        CALCULATETABLE(      
            VALUES(fVendas[codigoVenda]),  
            NOT(ISBLANK(fVendas[codigoCliente])),
            FILTER(fVendas, fVendas[nomeProduto] = vNomeProdutoSelecionado)
        ),
        CALCULATETABLE(  
            VALUES(fVendas[codigoVenda]),
            NOT(ISBLANK(fVendas[codigoCliente])),
            FILTER(fVendas, fVendas[nomeProduto] = vNomeProdutoAux),  
            USERELATIONSHIP(dProdutosAux[nomeProduto], fVendas[nomeProduto])
        )
    )

VAR vResultado =  
    COUNTROWS(vIntersecao)  

VAR vtabela0 =
    ADDCOLUMNS(
        CALCULATETABLE(
            VALUES(dProdutosAux[nomeProduto]),
            USERELATIONSHIP(fVendas[nomeProduto], dProdutosAux[nomeProduto])
        ),
        "qtdCruzada", vResultado
    )

RETURN  
    vtabela0
'''
 
Result:
brunovetupcombr_0-1692623823498.png


Expected Result:

brunovetupcombr_1-1692968528544.png

 

Sample Data:
dProduto:
nomeProduto
EXAME HEMOGRAMA

DIÁRIA

MANUTENÇÃO
ULTRASSOM
CONSULTA
EXAME CHEM
RAIO X
APLICAÇÃO
VACINA
 
dProdutoAux:
nomeProduto
EXAME HEMOGRAMA

DIÁRIA

MANUTENÇÃO
ULTRASSOM
CONSULTA
EXAME CHEM
RAIO X
APLICAÇÃO
VACINA
 
fVendas:
codigoVendacodigoClientenomeProduto
00000001000001CONSULTA
00000002000002EXAME HEMOGRAMA
00000002000002

DIÁRIA

00000003000003MANUTENÇÃO
00000003000003ULTRASSOM
00000003000003CONSULTA
00000004000005EXAME CHEM
00000004000005RAIO X
00000004000005APLICAÇÃO
00000005000001CONSULTA
00000005

000001

DIÁRIA
00000006000006VACINA
00000006000006ULTRASSOM
00000006

000006

CONSULTA
00000007000008DIÁRIA
00000007000008CONSULTA
00000007000008VACINA
00000007000008EXAME HEMOGRAMA
00000008000009EXAME HEMOGRAMA
00000008000009CONSULTA
00000008000009RAIO X
00000009000010MANUTENÇÃO
00000009000010EXAME HEMOGRAMA
00000009000010CONSULTA
 
Relationships:
brunovetupcombr_3-1692969548002.png
brunovetupcombr_4-1692969578622.png
brunovetupcombr_6-1692969743035.png

 

1 REPLY 1
lbendlin
Super User
Super User

Please provide sample data (with sensitive information removed) that covers your issue or question completely, in a usable format (not as a screenshot). Leave out anything not related to the issue.
https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...
Please show the expected outcome based on the sample data you provided.

https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.