SWITCH TRUE WITH OR in power bi dax

1.3k views Asked by At

I have 15 columns in a power bi table. I would like to create new measure that will return the column names if the percentage value of any columns is less than 60%.

Example table:

ID total col2 col3 col4 col5
a100 50 35 10 5 6
a101 36 25 5 12 18

I created a new measure for each column that shows the percentage

%col2 = SUM(col2)/SUM(total)*100
%col3 = SUM(col3)/SUM(total)*100
%col4 = SUM(col4)/SUM(total)*100
%col5 = SUM(col5)/SUM(total)*100

By the new measure above, I will get col2 >60%

What I would like is to create a visual, maybe by kpi or a table that will return only the columns that have less than 60%.

I tried the following:

col_to_improv = SWITCH(TRUE(), OR(table[col2] < 60, "columnname", table[col3] < 60, "col_name2", table[col4] < 60, "col_name3"), table[col5] < 60, "col_name4],"")

I would like to show only the column names that has less than 60%, otherwise, do not show.

From the above example table, In the kpi (or other visual), I am expecting only col3,col4, and col5, because they have less than 60%.

2

There are 2 answers

1
Darkitechtor On

I would advice you to transpose your table (if it's able) because it's easier to work with values than column names.

enter image description here Don't forget to add to your table one row with metrics description (Column1 in my example)

If you do so you with get desired result with pretty easy DAX measure:

col_to_improv =
SWITCH(
    TRUE,
    SELECTEDVALUE('Table'[Column2]) < 60,
    SELECTEDVALUE('Table'[Column1])
)

enter image description here

13
Mik On

Column names - [Table Name],[Column Name] comes from COLUMNSTATISTICS(), so, don't change them, the rest you can name according to your model

VAR myTableStat=
        Filter(
            COLUMNSTATISTICS()
            ,[Table Name]="MyTable"
        )
        
VAR withSUM=
       ADDCOLUMNS(
            myTableStat
            ,"colSum",
                      SWITCH(
                               [Column Name]
                               ,"col2",SUM(MyTable[col2])
                               ,"col3",SUM(MyTable[col3])
                               ,"col4",SUM(MyTable[col4])
                               ,"col5",SUM(MyTable[col5])
                       )

                         
        )
VAR totalSum= SUM(MyTable[total])
VAR  withAveLessThen60 =
        FILTER(
            withSUM
            ,AND(
                DIVIDE(
                        [colSum]
                        ,totalSum
                )<0.6
                ,NOT ISBLANK([colSum])
            )
        )
            
VAR result = 
    CONCATENATEX(
                withAveLessThen60
                ,IF(
                    NOT ISBLANK([colSum])
                    ,[Column Name]                  
                )
                ,"; "
                
    )
        
RETURN 
      result

enter image description here