Forum Discussion

SG_17's avatar
SG_17
Frequent Visitor
6 years ago
Solved

Filtering rows in one table by values from another table

Hi, relatively new user trying to complicate my own life with difficult tasks. lol.  I need some help with filtering out rows from a data table (Table A) based on matching IDs from a lookup table (Table B). I would prefer to do this with a measure rather than through a creating a new table to minimize data redundancy. I would like to have both with and without the filter to be shown in different columns on the report. I think this can be better explained via visualization in my example below:

Table A

IDCostRevenue
1124
1235
1337
14511

 

Table B (discontinuation)

ID
11
12

 

Matrix on report:

 Before DiscAfter Disc
Total Cost138
Total Revenue2718

 

Any recommendations?  Thank you!!

  • I figured it out: 

    CALCULATE(sum(Table A[Total Cost]),EXCEPT(VALUES(Table A[Product_ID]),VALUES(Table B[Product_ID])))

5 Replies

  • edhans's avatar
    edhans
    Community Champion

    I would just bring in one table. Merge Table B to Table A and create a Discontinued column. Then you can have very simple measures that just total revenue and costs with this:

     

    Total Revenue = SUM('Table A'[Revenues])
    
    Total Cost= SUM('Table A'[Cost])

     

    See my PIBX attached. Note that in the Matrix settings I had to tick the Show the Values on the Rows setting. That is not the default. You can see in the Power Query (Transform Data) part where I merged the tables to just create a True/False field for the Discontinued. You can change that formulat to show "Discontinued" and "Not Discontinued" vs the true and false values there.

     

    • SG_17's avatar
      SG_17
      Frequent Visitor

      Thank you for your help. I'm not sure if that can work though.  Table A has multiple instances of the ID that are added daily (the table is updated weekly), while Table B has one instance of each ID and changes (not necessarily all IDs) quarterly. Timing would also play a factor in how the discontinuation is applied. To provide a little more clarity, I used the term discontinuation, but really we offer different plans that provide for the ability to opt-out of specific products based on a given plan for reduced costs. This opt-out product list (plan) is generated quarterly.  The goal of this analysis is to see the change in costs and revenues when a client wants to opt-out to determine overall pricing.  The way that we calculate this is that we apply this opt-out plan to a clients previous product order history (Table A is total product orders) to tell them the savings from opting out.  Accordingly, I would prefer to just update the data tables (Table A weekly and Table B quarterly), rather than having to re-create merged tables.   

      Here's a little more clarity via example:

       

      Table A

      Order_dateProduct_IDCostRevenue

      1/1/2020

      12358
      1/2/2020111715
      1/3/20202311020
      1/3/202022235
      1/4/202012358

       

      Table B

      YearQuarterProduct_ID
      2020Q1123
      2020Q1222

       

      Output

       Total costTotal revenue
      Opt-in3056
      Opt-out1735
      • SG_17's avatar
        SG_17
        Frequent Visitor

        I figured it out: 

        CALCULATE(sum(Table A[Total Cost]),EXCEPT(VALUES(Table A[Product_ID]),VALUES(Table B[Product_ID])))
  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi SG_17 

    edhanss way is good. And you may try my way.

    I build a table as below.

    And build two measures to achieve your goal.

    Total cost = IF(MAX('Table'[Statue])="Before",SUM(A[Cost]),CALCULATE(SUM(A[Cost]),FILTER(ALL(A),NOT A[ID] IN VALUES(B[ID]))))
    Total Revenue = IF(MAX('Table'[Statue])="Before",SUM(A[Revenue]),CALCULATE(SUM(A[Revenue]),FILTER(ALL(A),NOT A[ID] IN VALUES(B[ID]))))

    Use measures to build a matrix, and select show on rows in Values.

    Result:

    You can download the pbix file from this link: Filtering rows in one table by values from another table

     

    Best Regards,

    Rico Zhou

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.