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
anonymous_98
Resolver I
Resolver I

ERROR - "the expression contains columns from multiple tables, but only columns from a single table.

ERROR - "the expression contains columns from multiple tables, but only columns from a single table can be used in a True/False expression that is used as a table filter expression."

Here is the code sample for which I am getting the above error. 
can anyone please help why am getting this error?
DAX Code Error.PNG

 

Top Purchaser Sales = 
    VAR _CurrentTopPurchaserCountry = 
        MAX(Customers[Country])

    VAR _CurrentProductID = 
        MAX('Online Sales'[Product Key])

    VAR _Sales = 
        CALCULATE(
            [Sales Amount],
                'Online Sales'[Product Key] = _CurrentProductID &&
                Customers[Country] = _CurrentTopPurchaserCountry
        )

    RETURN
        _Sales

 

 

1 ACCEPTED SOLUTION
anonymous_98
Resolver I
Resolver I

Hey,

Here is the fix for your error
Before Change:

 

Top Purchaser Sales = 
    VAR _CurrentTopPurchaserCountry = 
        MAX(Customers[Country])

    VAR _CurrentProductID = 
        MAX('Online Sales'[Product Key])

    VAR _Sales = 
        CALCULATE(
            [Sales Amount],
                'Online Sales'[Product Key] = _CurrentProductID &&
                Customers[Country] = _CurrentTopPurchaserCountry
        )

    RETURN
        _Sales

 


After Change:

 

Top Purchaser Sales = 
    VAR _CurrentTopPurchaserCountry = 
        MAX(Customers[Country])

    VAR _CurrentProductID = 
        MAX('Online Sales'[Product Key])

    VAR _Sales = 
        CALCULATE(
            [Sales Amount],
            FILTER(
                'Online Sales',
                'Online Sales'[Product Key] = _CurrentProductID &&
                RELATED(Customers[Country]) = _CurrentTopPurchaserCountry
            )
        )

    RETURN
        _Sales

 

 

So why did I get this error in the first place?

  1. Under the Filter argument, you are using columns from 2 different table
  2. Both the columns are from two different table so you can not directly use them to filter [Sales Amount] measure
  3. Country column is from Customer Table and Product Key is from Online Sales Table


So what should we do when we need to filter based on columns from 2 different tables?

  1. Use FILTER inside an CALCULATE function
  2. Use RELATED inside the FILTER to filter using the column of another related table.

View solution in original post

4 REPLIES 4
Pragati11
Super User
Super User

Hi @anonymous_98 ,

 

Can you try the following DAX:

Top Purchaser Sales = 
    VAR _CurrentTopPurchaserCountry = 
        MAX(Customers[Country])

    VAR _CurrentProductID = 
        MAX('Online Sales'[Product Key])

    VAR _Sales = 
        CALCULATE(
            [Sales Amount],
                'Online Sales'[Product Key] = _CurrentProductID &&
                RELATED(Customers[Country]) = _CurrentTopPurchaserCountry
        )

    RETURN
        _Sales

I am assuming there is an active relationship between both tables used in the your DAX expression.

 

Thanks,

Pragati

Best Regards,

Pragati Jain


MVP logo


LinkedIn | Twitter | Blog YouTube 

Did I answer your question? Mark my post as a solution! This will help others on the forum!

Appreciate your Kudos!!

Proud to be a Super User!!

Hey @Pragati11 ,


Thanks for your help.
I was able to fix the issue. 
Tried your code but it is not working the reason is the RELATED function needs to be used inside a FILTER function for it to work.

DimaMD
Solution Sage
Solution Sage

HI @anonymous_98 
As far as I understand you want to know the top customer, 
You need to choose "MAX" from 1 sales table
You choose the maximum country and the maximum unrelated product
If you want to know the top buyer you need to use the TOPN or RANX function


__________________________________________

Thank you for your like and decision

__________________________________________

Greetings from Ukraine

To help me grow PayPal: embirddima@gmail.com
anonymous_98
Resolver I
Resolver I

Hey,

Here is the fix for your error
Before Change:

 

Top Purchaser Sales = 
    VAR _CurrentTopPurchaserCountry = 
        MAX(Customers[Country])

    VAR _CurrentProductID = 
        MAX('Online Sales'[Product Key])

    VAR _Sales = 
        CALCULATE(
            [Sales Amount],
                'Online Sales'[Product Key] = _CurrentProductID &&
                Customers[Country] = _CurrentTopPurchaserCountry
        )

    RETURN
        _Sales

 


After Change:

 

Top Purchaser Sales = 
    VAR _CurrentTopPurchaserCountry = 
        MAX(Customers[Country])

    VAR _CurrentProductID = 
        MAX('Online Sales'[Product Key])

    VAR _Sales = 
        CALCULATE(
            [Sales Amount],
            FILTER(
                'Online Sales',
                'Online Sales'[Product Key] = _CurrentProductID &&
                RELATED(Customers[Country]) = _CurrentTopPurchaserCountry
            )
        )

    RETURN
        _Sales

 

 

So why did I get this error in the first place?

  1. Under the Filter argument, you are using columns from 2 different table
  2. Both the columns are from two different table so you can not directly use them to filter [Sales Amount] measure
  3. Country column is from Customer Table and Product Key is from Online Sales Table


So what should we do when we need to filter based on columns from 2 different tables?

  1. Use FILTER inside an CALCULATE function
  2. Use RELATED inside the FILTER to filter using the column of another related table.

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.