Search code examples
excelpowerquerynested-if

Using more than 2 IF conditions to identify different data types in a mixed column - Power Query


I am trying to Add a new Column that uses multiple If conditions to check if a particular column contains a specific text, numbers formatted as text or textual values.

The below If conditon works only for the first 2 criteria but adding a 3rd checking criteria does not yield the desired NULL results for numeric values that are formatted as text.

===============================
ColumnToCheck       | NewColumn
===============================
R0                  |  Questions
Is this my life?    |  Is this my life?
0.5445              |  null
0.23                |  null
0.23335             |  null
===============================

The formula is:

= Table.AddColumn(PreviousStep, "NewColumn", 
    each    
if 
       Value.Is([ColumnToCheck],Text.Type) and not Text.Contains([ColumnToCheck],"R0") then 
     [ColumnToCheck] 
else if 
  Text.Contains([ColumnToCheck],"R0", Comparer.OrdinalIgnoreCase) then 
     "Questions" 
else if 
Value.Is(Number.FromText([ColumnToCheck]),type number) then 
     null 
else 
     null)

Solution

  • Maybe you need something like that

     if
        try Value.Is(Number.FromText([ColumnToCheck]), type number) otherwise Value.Is([ColumnToCheck],type number)  
    then 
        null 
    else 
        if 
           Value.Is([ColumnToCheck],Text.Type) and not Text.Contains([ColumnToCheck],"R0") 
        then 
            [ColumnToCheck] 
        else 
            if 
                Value.Is([ColumnToCheck],Text.Type) and  Text.Contains([ColumnToCheck],"R0", Comparer.OrdinalIgnoreCase) 
            then 
                "Questions" 
            else 
                null
    

    Result in PQ looks like

    enter image description here