Forum Discussion

mterry's avatar
mterry
Helper V
8 years ago
Solved

Problems with dates in visuals

I have merged a few tables and have data from the beginning of 2015 through YTD 2018. The dates are formatted as dates, and when I sort them I can see that they start from the correct date (1/1/2015)...
  • Anonymous's avatar
    Anonymous
    8 years ago

    You should use a Calendar table. That normalizes the date data. The Date pulled into the Visuals is from the Calendar Table. 

     

    Connect the Calendar table to the primary Data Tables. The Data tables should not be connected to each other (most mlikely will not connect). the Calendar table is the common link in the case of using dates. 

     

    That will not clean up bad date data.

     

    Here is the code for a Calendar Table I use (change the name in the front to your chosen name but don't use a DAX command name:

     

    Dates_INV = GENERATE (

        CALENDAR( DATE( YEAR( TODAY() ) - 3, MONTH( TODAY() ), DAY( TODAY()) ), TODAY()),

        VAR startOfWeek = 2 // Where 1 is Sunday so this returns Monday as Week Start   

        VAR currentDay = [Date]

        VAR days = DAY( currentDay )

        VAR months = MONTH ( currentDay )

        VAR years = YEAR ( currentDay )

        VAR nowYear = YEAR( TODAY() )

        VAR nowMonth = MONTH( TODAY() )

        VAR dayIndex = DATEDIFF( currentDay, TODAY(), DAY) * -1

        VAR todayNum = WEEKDAY( TODAY() )

        VAR weekIndex = INT( ROUNDDOWN( ( dayIndex + -1 * IF( todayNum + startOfWeek <= 6, todayNum + startOfWeek, todayNum + startOfWeek - 7 )) / 7, 0 ) )

      RETURN ROW (

        "day", days,

        "month", months,

    "MonthNameShort", FORMAT ( months, "mmm" ),

    "MonthNameLong", FORMAT ( months, "mmmm" ),

        "year", years,

        "day index", dayIndex,

        "week index", weekIndex,

        "month index", INT( (years - nowYear ) * 12 + months - nowMonth ),

        "year index", INT( years - nowYear )

    )

    ))