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
kamiscfr
Regular Visitor

Filter A Second Column by Filter Context

Hi all,

 

I have a table with a Company ID, Date and Sales. If the customer is a group partner company, a further Partner Company ID column is filled with the Company ID of the internal customer.

 

I’d like to sum the sales but only for sales other than internal sales between the companies within the filter context. That is the sales with no (empty) Partner Company IDs or with Partner Company IDs other than the Company ID’s within the filter context.

 

I tried some formulas with “Values”, “Filter” and “Earlier” but didn’t make it.

 

Thanks a lot for helping.

 

example.jpg

6 REPLIES 6
Eric_Zhang
Microsoft Employee
Microsoft Employee

@kamiscfr

You can try a measure as below. See more details in the attached pbix file.

external SumTotal =
CALCULATE (
    SUM ( yourTable[Sales] ),
    FILTER (
        yourTable,
        NOT (
            CONTAINS (
                CALCULATETABLE ( VALUES ( yourTable[Company ID] ), ALLSELECTED ( yourTable ) ),
                yourTable[Company ID], LASTNONBLANK ( yourTable[Partner Company ID], "" )
            )
        )
    )
)

Capture.PNGCapture.PNG

Anonymous
Not applicable

Hello Eric,

I have similar problems with the DAX formula - the sum seems to be right, but the values per line are wrong.
"IsExternal" does not work properly for me too.
 
 
IsExternal = IF(CONTAINS(CALCULATETABLE(VALUES(Sales[CompanyID]);ALLSELECTED(Sales));
Sales[CompanyID];LASTNONBLANK(Sales[PartnerCompanyID];""));0;1)
TotalSales = CALCULATE (
   SUM ( Sales[Sales] );
    FILTER (
        Sales;
        NOT (
            CONTAINS (
                CALCULATETABLE ( VALUES ( Sales[CompanyID] ); ALLSELECTED ( Sales ) );
               Sales[CompanyID]; LASTNONBLANK ( Sales[PartnerCompanyID]; "" )
            )
        )
    )
)

 

 

DAX_1.PNG

 

 

 

The main problem is that the processing over 10+ million records takes forever and breaks off.

 

PBIX File

 

Thanks a lot.

Best regards

Anonymous
Not applicable

Dear Eric,

 

thanks so much for helping.

 

I tried to understand the formula (for hours Smiley Happy) but still I have some problems understanding:

 

  CALUCATE SUM - understand

  FILTER - understand

  NOT CONTAINS - understand

  CALCULATETABLE - Only the companies within the external filter context?

  LASTNONBLANK - Don't understand

 

The sum seems to be ok, but I can not use it in a table, as the sceenshots shows.

 

Thanks again

Frank

 

 InkedScreenshot_LI.jpg

Dear Eric,

 

I think I found a solution when learning more about Dax by reading "The Definitive Guide to Dax". Here I learned about the funktion USERELATIONSSHIP.

 

I created a new table "Companies" for the definition of the companies. Then I linked that table to the fact table twice. First with the Company ID and second with the Partner Company ID. The I use the first connection to sum the sales and the second connection with the USERELATIONSHIP function to sum the partner sales. Then I calculate sum of sales minus partner sales = correct sales.

 

It seems to work correctly (and faster). And I understand it Smiley LOL

 

I'll test it with my real case. If I still have problems, I'll write again.

 

Best

Frank

 

2017-09-08 14_09_12-externalSales V2 - Power BI Desktop.png

...no.

 

PartnerSales subtracts All parter sales but only partner sales within the filter context are allowed to be subtracted. I need both conditions simultaneously. Smiley Frustrated

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.