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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
cmncp
Helper III
Helper III

Filter Invoices where at least one line item contains a particular product

I have an invoice table with columns including InvoiceNo and ProductID. I need to be able to filter so that it returns all Invoices that contain a particular Product. When this filter is applied, it needs to only display the invoices with that product, but it should show all products on that invoice.

For example, I have the following table:

 

InvoiceNo   ProductID
111         ProdA
111         ProdB
111         ProdC
222         ProdA
222         ProdB

I only want to display invoices that have ProdC. When filtered, it needs to show

InvoiceNo   ProductID
111         ProdA
111         ProdB
111         ProdC

I have tried the following DAX Measure:

CALCULATE(
    COUNTROWS(Invoice),
    SUMMARIZE(
        FILTER(
            SUMMARIZE(
                 Invoice
                ,Invoice[InvoiceNo]
                ,Invoice[ProductID]
            )
            ,Invoice[ProductID] = "ProductC"
        )
    ,Invoice[InvoiceNo]
    )
)

But when I apply the filter (where measure > 0), it only returns the following

InvoiceNo   ProductID
111         ProdC

If I was doing this in SQL, I would use the following query:

select 
     [InvoiceNo]
    ,[ProductID]
from
    [Invoice] 
where
    [InvoiceNo] in (SELECT [InvoiceNo] FROM [Invoice] where [ProductID] = 'ProdC')
1 ACCEPTED SOLUTION

@cmncp 

 

It works with me

I am attaching a sample file with your data and MEASURE


Regards
Zubair

Please try my custom visuals

View solution in original post

5 REPLIES 5
Zubair_Muhammad
Community Champion
Community Champion

@cmncp 

 

Try this MEASURE and set it as MEASURE =1 in VISUAL FILTER

 

Measure =
IF (
    CONTAINS (
        CALCULATETABLE (
            VALUES ( Invoice[ProductID] ),
            ALLEXCEPT ( Invoice, Invoice[InvoiceNo] )
        ),
        Invoice[ProductID], "ProdC"
    ),
    1,
    0
)

Regards
Zubair

Please try my custom visuals

Hello @Zubair_Muhammad ,

Your solution here seems pretty elegant. I have a similar use case where I have confirmations with different lines and if any line's description CONTAINS "CBOT" then I want to include all lines for that confirmation in my dataset. 

 

So here's a data table example:

Confirm NumLine Item Descr
01Expense A
01Expense B
01CBOT Labor 1
01CBOT Travel 1
02Labor for Job
02Expense for Job
03Expense 123
03CBOT Labor Job
03Expense 456

 

The expected result:

Confirm #   CBOT?

01                   1

02                   0

03                   1

 

Here is the expression I am using:

CBOT Confirmations =
IF(
CONTAINS(
CALCULATETABLE(
VALUES('srvc_cnfrmn'[ln_itm_desc]),
ALLEXCEPT('srvc_cnfrmn','srvc_cnfrmn'[cnfrmn_num])
),
'srvc_cnfrmn'[ln_itm_desc],"CBOT"
),
1,
0
)
 
However, I am receiving an error: The expression specified in the query is not a valid table expression. Do you know why? Is it because I am trying to search for a string within a larger string? Do you have any ideas how to resolve? Thanks for your help.
Shelley
Continued Contributor
Continued Contributor

@Zubair_Muhammad I figured out what I was doing wrong. When I saw CALCULATETABLE, I thought I had to add a table, but this is a measure. Once I did it as a measure, it worked. Thank you!

 

Thanks @Zubair_Muhammad 

 

This is close, but if I include Product in my table, it will list every product in the product table, not just those on the invoice.  Eg.  I am expecting to see this (NewMeasure is the measure you defined):

InvoiceNo   ProductID    NewMeasure
111            ProdA         1
111            ProdB         1
111            ProdC         1

But instead I am seeing this:

InvoiceNo   ProductID    NewMeasure
111            ProdA         1
111            ProdB         1
111            ProdC         1
111            ProdD         1
111            ProdE         1
111            ProdF         1
111            ProdG         1
111            ProdH         1

Even though the invoice only has ProdA, B and C on it.

@cmncp 

 

It works with me

I am attaching a sample file with your data and MEASURE


Regards
Zubair

Please try my custom visuals

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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