How to write a nested IF statement that has multiple outputs?

老子叫甜甜 提交于 2021-01-29 09:40:21

问题


The context:

Column 1   Column 2   Column 3    Column 4  (IF statement result column)
a,b                   2.99        $2.99     1/2 mismatch     
a          a,b        3.49        $2.89     Column 1/2 mismatch,3/4 mismatch  
b          a          1.99        $2.99     Column 1/2 mismatch,3/4 mismatch
a,b        a,b        3.49        $3.49

so only in stance of and exact match (I have many columns that have comparable values that I need to follow this pattern). Is there a way to do this?


回答1:


I don't think you need nested IF statements, just CONCATENATEX:

Result =
VAR ColList = { [Column1], [Column2], <...more columns if needed..> }
RETURN
    CONCATENATEX ( FILTER ( ColList, LEN ( [Value] ) > 0 ), [Value], "," )

Edit: The basic approach above can still be used but you'll want to modify what is in your ColList.

Result =
VAR Pair12  = IF ( [Column 1] <> [Column 2], "1/2 mismatch" )
VAR Pair34  = IF ( [Column 3] <> [Column 4], "3/4 mismatch" )
VAR ColList = FILTER ( { Pair12, Pair34 }, LEN ( [Value] ) > 0 )
RETURN
    "Column " & CONCATENATEX ( ColList, [Value], "," )

Or refactored a bit to remove variables:

Result =
"Column "
    & CONCATENATEX (
        FILTER (
            {
                IF ( [Column 1] <> [Column 2], "1/2 mismatch" ),
                IF ( [Column 3] <> [Column 4], "3/4 mismatch" )
            },
            LEN ( [Value] ) > 0
        ),
        [Value],
        ","
    )


来源:https://stackoverflow.com/questions/61960428/how-to-write-a-nested-if-statement-that-has-multiple-outputs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!