0binary Name1
0binary Name1

Reputation: 11

AND function in Power BI

I'm trying to create a new mesure in order to create a gantt chart, but the dax field keeps spitting that there's an error. Where in fact i literally copied the same code in the tutorial i'm following.

And the new mesure is as follows:

CF Gantt = 
var startdate =
CALCULATE(
    min(Contracts_range[Start Date ],
    REMOVEFILTERS(TabelaCalculada)
), var EndDate =
CALCULATE(
    min(Contracts_range[End Date]),
    REMOVEFILTERS(TabelaCalculada)

)
var ProjectPeriod =
    min(TabelaCalculada[Date]) >= startdate
    && min(TabelaCalculada[Date]) <=  startdate

var result =
if(
    TabelaCalculada,
    1
)
return

You see PBI is saying the following: "the syntax for '&&' is incorrect", i even searched the documentation for this particular AND method but i don't see how it's wrong.

Upvotes: 0

Views: 83

Answers (2)

0binary Name1
0binary Name1

Reputation: 11

I've found a third syntax alternative that worked. So my goal as i explained was to create a Gantt Chart using the matrix innate visual and for that one way to do it is to create a measure that returns a 1 if i have a date that's either on or after the starting date and on or before the ending date.

BetweenDates = 

 VAR beginning = min ( Contracts_range[Start Date])

 VAR finish = max ( Contracts_range[End Date] )

 VAR duration = max ( Contracts_range[Duration] )

 VAR colorVar = 0
RETURN

SWITCH (

TRUE(),

AND ( MAX ( 'Calendar'[Date] ) >= beginning ,  MIN ( 'Calendar'[Date] ) <= finish ) && duration >0,1

)

Upvotes: 0

Dani U
Dani U

Reputation: 434

This DAX is invalid in multiple ways which I point out on the assumption that you are trying to learn what valid DAX is.

  • Comma after the definition of var startdate.
  • Project Period is a bunch of filters with no actual function.
  • Project Period is trying to filter on an aggregate, which is not allowed.
  • return statement is missing the return argument
  • var result is incomprehensible.

This will return a 1 for each row where the date falls between minimum Start Date and maximum End Date, per your latest comment.

CF Gantt = 
    var startdate =
        CALCULATE(
            min(Contracts_range[Start Date]),
            REMOVEFILTERS(TabelaCalculada)
        )
    var EndDate =
        CALCULATE(
            max(Contracts_range[End Date]),
            REMOVEFILTERS(TabelaCalculada)    
        )
    var result = if(
        selectedvalue(TabelaCalculada[Date] >= startdate 
        && TabelaCalculada[Date] <= EndDate,1)
    )

return result

Upvotes: 2

Related Questions