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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
BachDinh
Helper I
Helper I

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.



Follow on LinkedIn
@ 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!:
Power BI Cookbook Third Edition (Color)

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?



Follow on LinkedIn
@ 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!:
Power BI Cookbook Third Edition (Color)

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.



Follow on LinkedIn
@ 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!:
Power BI Cookbook Third Edition (Color)

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
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.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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