cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
BachDinh
Frequent Visitor

IF chain spot check -> Chain not working properly

Hi,

 

I have a following chain of IFs for a calculated column, that doesn't seem to work properly and I cannot figure out why, as logically each steps should be fine. 

 

 

 

Product Category= 
IF(
    --ABC Bks
   AND([Related Kiosk_Concept] IN {"ABC", "Multibrand"}, CONTAINSSTRING(Products[Product],"Bks"))
   , "ABC Bks"
     --ABC Non-Bks
   , IF(
       [Related Kiosk_Concept] IN {"ABC", "Multibrand"} && Products[Type] IN {"Made abc","Made ABC"} && NOT(CONTAINSSTRING(Products[Product],"Bks"))
       ,"ABC Non-Bks"
    --Wok
       ,IF(
            [Related Kiosk_Concept] = "MainABCLight" && Products[Type] = "Made ABC"
            ,"Wok"
    --Main Product 
            ,IF([Related Kiosk_Concept] = "Main" && Products[Type] = "Made" && Products[Leaf Cat] IN 
            {"1","B2","3","4", "5","6","7","8", "9"}
            ,"Main Product"
            ,""
            )
       )
   )
)

 

 

 

The output of this seems to be either 'Wok' or Blank. 

One of the outputs is as follows: 

Kiosk_ConceptRelated Kiosk_ConceptProductTypeProduct Category
ABCABCXYZ BksMade ABCWok

 

It looks like it should have been caught in the first couple IF -> The [Related Kiosk_Concept] is ABC, and the Product contains "Bks", so the output should be 'ABC bks', but it skips that and somehow gets 'Wok'. 

 

This might be a simple silly mistake, but I cannot figure it out, so I would appreciate help. 

6 REPLIES 6
Greg_Deckler
Super User
Super User

@BachDinh These kinds of things are easier to debug if you switch to using SWITCH(TRUE(),...) instead of nested IF statements.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler 

 

I have switched it to 

 

Product Category = 
SWITCH(
    TRUE()
    ,AND([Related Kiosk_Concept] IN {"ABC", "Multibrand"}, CONTAINSSTRING(Products[Product],"Bks")), 
        "ABC Bks"
    ,[Related Kiosk_Concept] IN {"ABC", "Multibrand"} && Products[Type] IN {"Made abc","Made ABC"} && NOT(CONTAINSSTRING(Products[Product],"Bks")),
        "ABC Non-Bks"
    ,[Related Kiosk_Concept] = "MainABCLight" && Products[Type] = "Made ABC",
        "Wok"
    ,[Related Kiosk_Concept] = "Main" && Products[Type] = "Made" && Products[Leaf Cat] IN {"list"}
    ,""
)

 

The issue is still present, with the example output in my original post being the same. 

 

EDIT: From looking at the results, it almost seems like it is only looking whether the Type is 'Made ABC' and assigning 'Wok' or blank based on the result of that check.

@BachDinh Wait, I recognize this, did you convert your relationship between Kiosks table and that other table to Both direction instead of single?


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler 

Curiously enough, I've tried the below both as a calculated column and as a measure: 

SWITCH(
    TRUE()
    ,[Related Kiosk_Concept] = "ABC", "ABC"
    ,[Related Kiosk_Concept] = "Multibrand","Multibrand"
    ,[Related Kiosk_Concept] = "MainABCLight" ,"Wok"
    ,[Related Kiosk_Concept] = "Main","Main"
    ,""
)

For the calculated column, it only outputs WOK. No blanks. 

For the measure, it works fine. 

Is there a way perhaps to do the entire nexted IFs / SWITCH(TRUE() ) in a measure instead of the calculated column?

@BachDinh I don't see why not.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Mastering Power BI 2nd Edition

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler I didn't, since my workaround works and provides a valid output linking product to [kiosk_concept]. 

I did change it to see whether it would help, but it broke multiple visuals with an invalid handle error, so I changed it back. Considering that the measure inside the calculated column returns the correct 'Kiosk_concept), I am not sure whether changing relationship direction woudd matter much

Helpful resources

Announcements
Carousel_PBI_Wave1

2023 Release Wave 1 Plans

Power BI release plans for 2023 release wave 1 describes all new features releasing from April 2023 through September 2023.

Power BI Summit Carousel 2

Global Power BI Training

Make sure you register today for the Power BI Summit 2023. Don't miss all of the great sessions and speakers!

BizApps LATAM 2023

Business Application LATAM Summit 2023

Join the biggest FREE Business Applications Event in LATAM this February.

Power Platform Bootcamp

Global Power Platform Bootcamp

In this bootcamp we will deep-dive into Microsoft’s Power Platform stack with hands-on sessions and labs, delivered to you by experts and community leaders.