SOLVED

Power Query Multiple IF Conditions in Custom Column

Copper Contributor

Good morning, 

 

I have a formula calculated in Excel that I am now looking to calculate in Power Query. I know I can only use column reference while writing IF statements in PQ. Can you please let me know what the Custom Column would be based on the below Excel calculation?

 

=IF(M5=N5,M5,IF(AND(M5="BLANK1",N5="BLANK2"),"Investigate",IF(AND(M5<>"BLANK1",N5="BLANK2"),M5,IF(AND(M5<>"BLANK1",N5<>"BLANK2",M5<>N5),"300-Corporate",IF(AND(M5="BLANK1",N5<>"BLANK2"),N5,"ERROR")))))

 

Thank you in advance!

8 Replies

@Alicia_Bucci In PQ the manner of writing IF formulae is quite different from Excel's IF function. It would look something like this:

 

= try

if something then this else that

otherwise "error message"

 

where the bolded words are essential key words in the M-language. The non bolded words can be replaced by expressions, for example [Column1] >= 10, or just [Column2]

 

and you can nest these if / then / else statements. How exactly is difficult to say without seeing your file.

@Riny_van_Eekelen Thank you for the reply. 

 

I am struggling a bit with the difference from Excel IF function to PQ. Attached is a sample file I am working on. I was originally trying to work with 3 columns to create an IF formula but could not get it to work the way I needed it to, so I am now going to work with 2 columns, which is the original Excel formula I posted. 

@Alicia_Bucci Thanks, but can't work on this file. It links to a file on your local system.

best response confirmed by Alicia_Bucci (Copper Contributor)
Solution

@Alicia_Bucci 

I'd simplify initial formula first, perhaps

=IF( M5=N5,
     M5,
     IF( M5="BLANK1",
         IF( N5="BLANK2","Investigate", N5),
         IF( N5="BLANK2",
             M5,
             IF( M5 <> N5,
                 "300-Corporate", 
                 "ERROR" ) ) ) )

with that

=if [M] = [N]
 then [M]
 else
    if [M] = "BLANK1"
    then
        if [N] = "BLANK2" then "Investigate" else [N]
    else
        if [N] ="BLANK2"
        then [M]
        else
            if [M] <> [N]
            then "300-Corporate" 
            else  "ERROR"

 

@Sergei Baklan Thank you for your help! It is greatly appreciated. 

 

If I wanted to add "O5" to this formula, is that doable or will that create an issue with the formula? 

@Alicia_Bucci 

I only tried to illustrate what @Riny_van_Eekelen suggested and didn't test the formula. As for O5 - question is bit abstract, not clear in which part of formula you'd like to add it and what formula shall do with it. 

Perhaps you may illustrate the logic if not with draft formula, when by diagram like

image.png

(again didn't test)

@Sergei Baklan 

 

Here is the Excel IF statement, which gets more complex when adding AST3. My issue that I am running into is when I need to add AST4, AST5, and possibly AST6. Is there a better way to calculate this formula with adding the additional AST from 1-6? Would this be easier to formulate in Power BI?

 

IF(AND(A2="BLANK1",B2="BLANK2",C2="BLANK3"),"Investigate",IF(AND(A2=B2,A2=C2),A2,IF(AND(A2<>"BLANK1",B2="BLANK2",C2="BLANK3"),A2,IF(AND(A2=C2,B2="BLANK2"),A2,IF(AND(A2<>C2,A2<>"BLANK1",C2<>"BLANK3",B2="BLANK2"),"House",IF(AND(A2<>"BLANK1",B2<>"BLANK2",A2<>B2,C2="BLANK3"),"House",IF(AND(A2="BLANK1",B2<>"BLANK2",C2="BLANK3"),B2,IF(AND(A2="BLANK1",B2<>"BLANK2",B2<>C2),"House",IF(AND(A2="BLANK1",B2="BLANK2",C2<>"BLANK3"),C2,IF(AND(A2<>"BLANK1",B2<>"BLANK2",C2<>"BLANK3",A2<>B2,B2<>C2),"House",IF(AND(A2="BLANK1",B2=C2),B2,"ERROR")))))))))))

 

Here is the illustration of how this is laid out in Excel. I included a copy of the file as well. 

 

AST1AST2AST3Expected ResultLine Item FormulaComp Territory
BLANK1BLANK2BLANK3InvestigateInvestigateInvestigate
T1T1T1T1T1T1
T1BLANK2BLANK3T1T1T1
T1BLANK2T1T1T1T1
T1BLANK2T2HouseHouseHouse
T1T2BLANK3HouseHouseHouse
BLANK1T2BLANK3T2T2T2
BLANK1T2T1HouseHouseHouse
BLANK1BLANK2T2T2T2T2
T1T2T3HouseHouseHouse
BLANK1T2T2T2T2T2

@Alicia_Bucci 

Convert logic (if I understood it correctly) to Power Query that's with some coding.

First way with minimum one

- query the table and add Index, nothing more

image.png

- reference this one, remove all columns but Index and all AST.. Select Index and Unpivot Other columns

image.png

- add another column replacing all values where Text.StartsWith "BLANK" replace on null and remove Value column

image.png

- Group By column Index without aggregation, manually add selection only of Custom column in result and keeping distinct values

image.png

- add another column removing rows with null from above, remove Data column

image.png

- add column with number of rows in each table

image.png

- add one more columns with text in first row of each table and remove column with tables

image.png

- add column with conditional result 

image.png

and remove other but Index and Result columns

 

Now merge first table with above one on Index into new query, expand Result.

If you add more columns the only you need is to change columns selected at the beginning of second query.

 

Another variant is do everything with lists, more coding, perhaps bit more flexible and less steps. Not sure that's better, Power Query is optimized for tables, not lists. And here is sample how to automate columns selection assuming some logic in their names.

let
    Source = Excel.CurrentWorkbook(){[Name="data"]}[Content],
    #"Promoted Headers" = Table.PromoteHeaders(
        Source, [PromoteAllScalars=true]),
    prevStep = #"Promoted Headers",

    GetValues = Table.AddColumn(
        prevStep,
        "values",
        each
            Record.ToList(
            Record.SelectFields(
                _,
                List.Select(
                    Table.ColumnNames( prevStep ),
                    each Text.StartsWith(_, "AST")
                )
            )
    )),
    CleanLists = Table.AddColumn(
        GetValues,
        "notBlanks",
        each List.Distinct(
                List.RemoveNulls(
                    List.Transform(
                        [values],
                        each
                            if Text.StartsWith( _, "BLANK")
                            then null
                            else _ )
                )
        ) ),
    RemoveValues = Table.RemoveColumns(CleanLists,{"values"}),
    ApplyCondition = Table.AddColumn(
        RemoveValues,
        "Result",
        each
            if List.IsEmpty( [notBlanks] )
            then "Investigate"
            else
                if List.Count( [notBlanks] ) = 1
                then List.Single( [notBlanks] )
                else "House"
    ),
    RemoveLists = Table.RemoveColumns(
        ApplyCondition,
        {"notBlanks"}
    )
in
    RemoveLists

Perhaps other variant exist, but in any case for flexible number of columns on which make the selection that will be bit more complex than nested if ... then ... else

1 best response

Accepted Solutions
best response confirmed by Alicia_Bucci (Copper Contributor)
Solution

@Alicia_Bucci 

I'd simplify initial formula first, perhaps

=IF( M5=N5,
     M5,
     IF( M5="BLANK1",
         IF( N5="BLANK2","Investigate", N5),
         IF( N5="BLANK2",
             M5,
             IF( M5 <> N5,
                 "300-Corporate", 
                 "ERROR" ) ) ) )

with that

=if [M] = [N]
 then [M]
 else
    if [M] = "BLANK1"
    then
        if [N] = "BLANK2" then "Investigate" else [N]
    else
        if [N] ="BLANK2"
        then [M]
        else
            if [M] <> [N]
            then "300-Corporate" 
            else  "ERROR"

 

View solution in original post