Forum Discussion

JustinT's avatar
JustinT
New Member
8 years ago
Solved

Creating a Date

Hi, I have a table which has the year and month as text in two separate columns. I need to create a single column which acts as a date so that I can use this as an axis on a bar chart,   I tried th...
  • TomMartens's avatar
    8 years ago

    Hey,

     

    I would do the following, by creating three new calculated columns.
    The first column just concatenates the existing Year and Month columns (this will be used as axis), this concatenation can be done by this DAX statement:

     

    Year-Month = 'Table1'[Year] & " " & 'Table1'[Month]

     

    Than I create a real date column using this DAX statement:

     

    Date = DATE('Table1'[Year],
        SWITch('Table1'[Month]
        ,"January", 1
        ,"February",2
        ,"March",3
        ,"April",4
        ,"May",5
        ,"June",6
        ,"July",7
        ,"August",8
        ,"September",9
        ,"October",10
        ,"November",11
        ,"December",12
        ),1)

     

    Be aware that a date always needs a Day, for this reason I used 1 as the 1st of the month, the last parameter of the DAY(year,month,day) formula.

     

    The 3 column is a running index that uniquely identiefies a the combination of year and month, i will use this column to order the "Year-Month" column. To create this index I use this DAX statement:

     

    RunningMonthIndex = 
    (YEAR('Table1'[Date])-YEAR(MIN('Table1'[Date])))*12+MONTH('Table1'[Date])

     

    My final table would look like this:

     

    Now in the Data view you can mark the Year-Month column, in the modeling menu choose "Sort by column" from the "Sort" ribbon and select RunningMonthIndex.

     

    You can hide both columns "Date" and "RunningMonthIndex" in the report view, to minimize complexity for the users of the report.

     

    Hopefully this is what you are looking for.

     

    Regards

    Tom