sam
sam

Reputation: 1304

Dax function Date is giving error : "Wrong Data type"

I need to create a measure that subtracts 2 months from the date column "Sale_Date". However, when I use the below measure I get error "Wrong Data type". Something is wrong in day argument of date function. Because when I hardcode the day value , I start getting results

Min_Date = 
var val = SELECTEDVALUE(EX4[Sale_Date])
var month = month(SELECTEDVALUE(EX4[Sale_Date]))-2
var Year = Year(SELECTEDVALUE(EX4[Sale_Date]))
var Day = day(SELECTEDVALUE(EX4[Sale_Date]))
return
DATE(Year,month,Day)

You can replicate this scenario using the below calculated table :

EX4 = DATATABLE("Sale_Date",datetime,{{"2022-10-01"},{"2022-10-09"},{"2022-11-12"},{"2022-11-23"}})

Below is the image of visual which is giving error because of this measure:

Below is the image of visual which is giving error because of this measure

Now when I hardcode the day value, I start getting output:

enter image description here

I do not want to solve using calculated column. Any help will be appreciated.

Upvotes: 0

Views: 626

Answers (2)

Peter
Peter

Reputation: 12375

Use this measure instead:

Min_Date = 
VAR thisdate = MAX(EX4[Sale_Date])
RETURN
    DATE(YEAR(thisdate), MONTH(thisdate) - 2, DAY(thisdate))

Works for Jan and Feb too.

Upvotes: 2

Bartek Węgrzyn
Bartek Węgrzyn

Reputation: 57

the error is caused by this line:

var month = month(SELECTEDVALUE(EX4[Sale_Date]))-2

for January and February this value will be -1 and 0. So it can't be passed to the date function enter image description here

Upvotes: 0

Related Questions