Forum Discussion

granthworth's avatar
granthworth
Kudo Kingpin
10 years ago
Solved

DAX issue with Date data type

I am having an issue today with a DAX formula.

 

The scenario is as follows. I have 2 Power BI desktop files which are formatted identically. I have them linked to separate Excel workbooks which are formatted identically. I am inserting a calculated column into both datasets under "Modeling" in Power BI with the following formula:

 

Week_Crctd = if(weeknum('Line Calls'[Date])<=52,(weeknum('Line Calls'[Date])),1)

In one PBI desktop file, I receive the expected result; a whole number between 1 and 52 which varies correctly according to the [Date] value. In the other file, I receive the following error:

 

 

As I mentioned before, both Excel files are formatted with the exact same number formatting in these columns and both PBI desktop files are formatted with the same Data Type, Format, Data Category, and Default Summarization. What gives?

 

Thanks guys and gals

  • Thanks for the input everyone.

     

    However, after going through my source data, I found the following string of typos in the date column. Correcting them also corrected this issue.

     

    381	1/19/16
    382	1/19/16
    383	1/19/16
    384	1/19/16
    385	1/19/16
    386	1/19/16
    387	01/119
    388	01/119
    389	01/119
    390	01/119

    I have adjusted the data validation and formatting restrictions to the user form through which this data is input.

     

    Thanks again for helping with my case of the Mondays.

     

5 Replies

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion
    I'm guessing that in your second sheet that your [Date] column is not tagged as a datetime type, but rather a text type. Could be a regional issue where months and days are flipped.
  • greggyb's avatar
    greggyb
    Resident Rockstar

    That's a real zinger.

     

    It took me a little while to reproduce. I can reliably get that behavior for any date < 1899-12-30. That is the 0-date in the Tabular engine, and my guess is that someone didn't handle negative values appropriately in the function definition. I can bounce up against the upper date boundary without errors.

     

    Do you have data with dates < 1899-12-30?

     

    **Edit:**

    Power Query does not suffer from a similar error, and Date.WeekOfYear() is functional in the full range from 0001-01-01 through 9999-12-31. DAX Can't handle dates as small as 0001-01-01, so you should be good to do this sort of transformation in Power Query. Generally it's a better practice to do transformations before importing to the data model, anyway, to take advantage of better compression.

     

    **Edit2:**

    The error reported in the original post seems to be unique to <1899-12-30 dates. If I input malformed dates or text strings I get

    Cannot convert value '4' of type Text to type Date.
    • granthworth's avatar
      granthworth
      Kudo Kingpin

      Thanks for the input everyone.

       

      However, after going through my source data, I found the following string of typos in the date column. Correcting them also corrected this issue.

       

      381	1/19/16
      382	1/19/16
      383	1/19/16
      384	1/19/16
      385	1/19/16
      386	1/19/16
      387	01/119
      388	01/119
      389	01/119
      390	01/119

      I have adjusted the data validation and formatting restrictions to the user form through which this data is input.

       

      Thanks again for helping with my case of the Mondays.

       

    • Greg_Deckler's avatar
      Greg_Deckler
      Community Champion
      Huh, if it is a pre-1900 date issue, I guess I should have thought of that, I just posted an article on it in the Community Blog! :)
      • greggyb's avatar
        greggyb
        Resident Rockstar

        Greg_Deckler, the 0-date in Tabular is 1899-12-30, not 1900-01-01. This is actually inconsistent with both Excel and SQL Server.

         

        As you can see below, I've created a table with the integer values -1, 0, and 1. I then change the data type to Date, and I get back 1899-12-29, 1899-12-30, and 1899-12-31. The misbehavior starts for anything with a negative offset from the 0-date.

         

        First integersIntegers converted to Date datatype in PBI