ALar
ALar

Reputation: 13

How do I put a condition inside a filter in power BI (DAX)?

I am just getting started with Power BI and am struggling to create a measure. I am trying to count the sum of my column "Repartition", but with some filters.

Here is my current measure:

Nb_Actifs = CALCULATE (SUM(PAIE[Repartition]), FILTER(PAIE, PAIE[REMUNERABLE] = "Oui"), IF(PAIE[PROFIL]="CC", FILTER(PAIE, PAIE[Taux_Contractuel]<> 0)))

The filters that I put before my IF statement are the ones that I want to apply to all rows. The ones after the statement are the filters that I only want to apply to the rows where the condition PAIE[PROFIL]="CC" is met.

However, I get this error message: "A function 'FILTER' has been used in a True/False expression that is used as a table filter expression. This is not allowed."

From what I understand I need to replace the IF function by a filter, but since there are several filters already I get confused as to how to do that in Power BI.

Could someone help me?

Upvotes: 1

Views: 12156

Answers (2)

Matt Kocak
Matt Kocak

Reputation: 808

I think a SUMX function would be more appropriate here. You would also want to put all logic into a single FILTER. Try

Nb_Actifs = 
    SUMX(
        FILTER(
            PAIE, 
            PAIE[REMUNERABLE] = "Oui" && 
                (
                    (PAIE[PROFIL] = "CC" && PAIE[Taux_Contractuel] <> 0) || 
                    (PAIE[PROFIL] <> "CC")
                )
        ), 
        PAIE[Repartition]
    )

Regardless of whether you use SUMX or not, putting all of your logic into a single FILTER with corresponding logic would solve your conditional filter problem.

Upvotes: 0

Olly
Olly

Reputation: 7891

You can add some logic within your filter to catch all the conditions you need to return:

Nb_Actifs = 
    CALCULATE ( 
        SUM ( PAIE[Repartition] ), 
        FILTER ( 
            PAIE, 
            PAIE[REMUNERABLE] = "Oui" && (( PAIE[PROFIL] = "CC" && PAIE[Taux_Contractuel] <> 0 ) || PAIE[PROFIL] <> "CC" )
        )
    )

Upvotes: 1

Related Questions