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

Shape the future of the Fabric Community! Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions. Take survey.

Reply
InsightSeeker
Helper III
Helper III

DAX Formula Help: Returning Values Based on Multiple Conditions Across Two Tables

Hello everyone,

 

I need help writing a DAX formula in Power BI that applies specific conditional logic based on fields in two tables: *Table 1* and *Table 2*. My goal is to return different values based on combinations of values in `settle_code`, `supplier_name`, `Lcard`, and `Value_Code`.

 

My preference would be creating a calculated column in Table 1.

 

Here are the specific conditions I’m looking to implement:

1. **Return "card" when:**
- `Table 1[settle_code]` is "CREDIT" **and** `Table 2[supplier_name]` is "SPA(AA)Sale" **and** `Table 2[Lcard]` is "Card"
- `Table 1[settle_code]` is "CASH" **and** `Table 2[supplier_name]` is "SPA(AA)Sale" **and** `Table 2[Lcard]` is "Card"
- `Table 1[settle_code]` is "CARD" **and** `Table 2[supplier_name]` is "SPA(AA)Sale" **and** `Table 2[Lcard]` is "Card"

 

2. **Return "SPA(AA) Sale" when:**
- `Table 1[settle_code]` is "CREDIT" **and** `Table 2[supplier_name]` is "SPA(AA)Sale" **and** `Table 2[Lcard]` is empty (`""`)
- `Table 1[settle_code]` is "CASH" **and** `Table 2[supplier_name]` is "SPA(AA)Sale" **and** `Table 2[Lcard]` is empty
- `Table 1[settle_code]` is "CARD" **and** `Table 2[supplier_name]` is "SPA(AA)Sale" **and** `Table 2[Lcard]` is empty

 

3. **Return "UCCF" when:**
- `Table 1[settle_code]` is "UCCF" **and** `Table 1[Value_Code]` is one of ("AX", "CA", "TP", "VI") **and** `Table 2[supplier_name]` is "SPA(AA)Sale" **and** `Table 2[Lcard]` is empty

 

4. **Return "error" when:**
- `Table 1[settle_code]` is "UCCF" **and** `Table 1[Value_Code]` is not one of ("AX", "CA", "TP", "VI") 

 

5. **Else return "Other" for all other cases.**

 

I'm not sure how to best structure this in DAX. Any advice on creating this formula would be much appreciated. Thank you!

 

TABLE 1

settle_codesettle_nameValue_Codeunique_key
CARDCardAXPOS10158086
CARDCardCAPOS10158088
CARDCardTPPOS10158090
CARDCardVIPOS10158092
UCCFUCCFVI10158094
UCCFUCCFCA10158096
UCCFUCCFAX10158098
UCCFUCCFTP10158100
CREDITCredit000000010010158102
CREDITCredit000000010010158104
CREDITCredit000000010010158106
CASHCash000000010010158108
CASHCash000000010010158110
CASHCash000000010010158112

 

 

TABLE 2

 

unique_keysupplier_nameLcard 
10158086SPA(AA)Sale  
10158088SPA(AA)Sale  
10158090SPA(AA)Sale  
10158092SPA(AA)SaleCard 
10158094SPA(AA)Sale  
10158096ABC Corp  
10158098SPA(AA)SaleCard 
10158100SPA(AA)SaleCard 
10158102SPA(AA)SaleCard 
10158104ABC Corp  
10158106ABC Corp  
10158108LPA Corp  
10158110LPA Corp  
10158112KLW Corp  
1 ACCEPTED SOLUTION
DataNinja777
Super User
Super User

Hi @InsightSeeker ,

 

You can use the dax formula like below using lookupvalue to write a calculated column in Table 1 to produce your required output. 

Result = 
VAR SupplierName = LOOKUPVALUE('Table 2'[supplier_name], 'Table 2'[unique_key], 'Table 1'[unique_key])
VAR LcardValue = LOOKUPVALUE('Table 2'[Lcard], 'Table 2'[unique_key], 'Table 1'[unique_key])

RETURN 
    SWITCH(
        TRUE(),
        
        // Condition 1: Return "card"
        (
            'Table 1'[settle_code] IN {"CREDIT", "CASH", "CARD"} &&
            SupplierName = "SPA(AA)Sale" &&
            LcardValue = "Card"
        ), "card",
        
        // Condition 2: Return "SPA(AA) Sale"
        (
            'Table 1'[settle_code] IN {"CREDIT", "CASH", "CARD"} &&
            SupplierName = "SPA(AA)Sale" &&
            (LcardValue = "" || ISBLANK(LcardValue))
        ), "SPA(AA) Sale",
        
        // Condition 3: Return "UCCF"
        (
            'Table 1'[settle_code] = "UCCF" &&
            'Table 1'[Value_Code] IN {"AX", "CA", "TP", "VI"} &&
            SupplierName = "SPA(AA)Sale" &&
            (LcardValue = "" || ISBLANK(LcardValue))
        ), "UCCF",
        
        // Condition 4: Return "error"
        (
            'Table 1'[settle_code] = "UCCF" &&
            NOT('Table 1'[Value_Code] IN {"AX", "CA", "TP", "VI"})
        ), "error",
        
        // Default case: Return "Other"
        "Other"
    )

 

I have attached an example pbix file. 

 

Best regards,

 

View solution in original post

3 REPLIES 3
DataNinja777
Super User
Super User

Hi @InsightSeeker ,

 

You can use the dax formula like below using lookupvalue to write a calculated column in Table 1 to produce your required output. 

Result = 
VAR SupplierName = LOOKUPVALUE('Table 2'[supplier_name], 'Table 2'[unique_key], 'Table 1'[unique_key])
VAR LcardValue = LOOKUPVALUE('Table 2'[Lcard], 'Table 2'[unique_key], 'Table 1'[unique_key])

RETURN 
    SWITCH(
        TRUE(),
        
        // Condition 1: Return "card"
        (
            'Table 1'[settle_code] IN {"CREDIT", "CASH", "CARD"} &&
            SupplierName = "SPA(AA)Sale" &&
            LcardValue = "Card"
        ), "card",
        
        // Condition 2: Return "SPA(AA) Sale"
        (
            'Table 1'[settle_code] IN {"CREDIT", "CASH", "CARD"} &&
            SupplierName = "SPA(AA)Sale" &&
            (LcardValue = "" || ISBLANK(LcardValue))
        ), "SPA(AA) Sale",
        
        // Condition 3: Return "UCCF"
        (
            'Table 1'[settle_code] = "UCCF" &&
            'Table 1'[Value_Code] IN {"AX", "CA", "TP", "VI"} &&
            SupplierName = "SPA(AA)Sale" &&
            (LcardValue = "" || ISBLANK(LcardValue))
        ), "UCCF",
        
        // Condition 4: Return "error"
        (
            'Table 1'[settle_code] = "UCCF" &&
            NOT('Table 1'[Value_Code] IN {"AX", "CA", "TP", "VI"})
        ), "error",
        
        // Default case: Return "Other"
        "Other"
    )

 

I have attached an example pbix file. 

 

Best regards,

 

Bibiano_Geraldo
Memorable Member
Memorable Member

Hi @InsightSeeker ,

Please in Table 1 create calculated column and past the following DAX code:

Result = 
SWITCH(
    TRUE(),
    // Condition 1: Return "card"
    
        Table1[settle_code] IN {"CREDIT", "CASH", "CARD"} &&
        LOOKUPVALUE(Table2[supplier_name], Table2[unique_key], Table1[unique_key]) = "SPA(AA)Sale" &&
        LOOKUPVALUE(Table2[Lcard], Table2[unique_key], Table1[unique_key]) = "Card"
    , "card",
    
    // Condition 2: Return "SPA(AA) Sale"

        Table1[settle_code] IN {"CREDIT", "CASH", "CARD"} &&
        LOOKUPVALUE(Table2[supplier_name], Table2[unique_key], Table1[unique_key]) = "SPA(AA)Sale" &&
        ISBLANK(LOOKUPVALUE(Table2[Lcard], Table2[unique_key], Table1[unique_key]))
    , "SPA(AA) Sale",
    
    // Condition 3: Return "UCCF"

        Table1[settle_code] = "UCCF" &&
        Table1[Value_Code] IN {"AX", "CA", "TP", "VI"} &&
        LOOKUPVALUE(Table2[supplier_name], Table2[unique_key], Table1[unique_key]) = "SPA(AA)Sale" &&
        ISBLANK(LOOKUPVALUE(Table2[Lcard], Table2[unique_key], Table1[unique_key]))
    , "UCCF",
    
    // Condition 4: Return "error"
    AND(
        Table1[settle_code] = "UCCF",
        NOT Table1[Value_Code] IN {"AX", "CA", "TP", "VI"}
    ), "error",
    
    // Default: Return "Other"
    "Other"
)

 

Your table1 should now look like this:

Bibiano_Geraldo_0-1731150988653.png

 

To learn more about SWITCH function, please follow the link: SWITCH 

 

If this help you, please give a kudo and mark as solution

I hope this helps! 
If you found this answer helpful:
✔️ Mark it as the solution to help others find it faster.
 Give it a like to show your appreciation!

Thank you for contributing to our amazing Power BI community! 
shafiz_p
Resident Rockstar
Resident Rockstar

Hi @InsightSeeker  Try this below code (Using Lookupvalue for unrelated tables):

NewColumn = 
VAR SettleCode = 'Table1'[settle_code]
VAR SupplierName = LOOKUPVALUE('Table2'[supplier_name], 'Table2'[unique_key], 'Table1'[unique_key])
VAR Lcard = LOOKUPVALUE('Table2'[Lcard], 'Table2'[unique_key], 'Table1'[unique_key])
VAR ValueCode = 'Table1'[Value_Code]

RETURN
SWITCH(
    TRUE(),
    (SettleCode IN {"CREDIT", "CASH", "CARD"} && SupplierName = "SPA(AA)Sale" && Lcard = "Card"), "card",
    (SettleCode IN {"CREDIT", "CASH", "CARD"} && SupplierName = "SPA(AA)Sale" && Lcard = ""), "SPA(AA) Sale",
    (SettleCode = "UCCF" && ValueCode IN {"AX", "CA", "TP", "VI"} && SupplierName = "SPA(AA)Sale" && Lcard = ""), "UCCF",
    (SettleCode = "UCCF" && NOT(ValueCode IN {"AX", "CA", "TP", "VI"})), "error",
    "Other"
)

 

Output:

shafiz_p_1-1731150701317.png

 

 

Hope this helps!!

If this solved your problem, please accept it as a solution and a kudos!!

 

Best Regards,
Shahariar Hafiz

 

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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