Tag: DAX

  • How to Build a Date Table in Power BI

    Probably the most essential table you need in your Power BI model is a date or calendar table.
    You join your date table from any other table containing dates, such as your main Fact Sales table.

    The advantage of using a calendar table is that it contains associated fields such as Year, Month, Start of Month, Start of Quarter, and relevant Financial Periods that will be bespoke to the business.

    The date table should be stored in a data flow so everyone can use it. Here we create one using DAX, which could be exported and imported as a table in the Power Query service. You could also create a date table with Power Query directly. Creating one in DAX gives us practice with Time Intelligence functions.


    The steps to create a date table are as follows:
    1. Open Table View and select ‘New Table’
    2. Start by creating the first column which will be our date key. We can use the CALENDAR() function to auto-create a list of dates in the table. The date range should include the minimum date relevant to your date up to any dates in the future. You could use TODAY() as dates up to today, for the end date, but sometimes for example with forecasting, it can be useful to have future data.
    Of course, a big table may slow performance, so just bear that in mind.

    Date = 
    VAR DateStart = DATE(2007, 01, 01)
    VAR DateEnd = DATE(2030, 12, 31)
    RETURN
    CALENDAR(DateStart, DateEnd)

    3. Once you have your first date column setup which will be the key to joining your other tables, you can then start any other useful columns.

    Here are some for starters:

    Year = YEAR('Date'[Date])
    Month = MONTH('Date'[Date])
    Day = DAY('Date'[Date])
    Quarter = QUARTER('Date'[Date])
    StartOfYear = STARTOFYEAR('Date'[Date])
    StartOfQuarter = STARTOFQUARTER('Date'[Date])
    StartOfMonth = STARTOFMONTH('Date'[Date])
    DayOfWeek = WEEKDAY('Date'[Date], 2)


    Be careful with the day of the week number. It’s always best to double-check against another calendar.
    The default is 1 for Sunday, to set the first day of the week, use WEEKDAY(‘Date'[Date], 2).

    Once you have the day number setup, which can be useful in itself, you can then create a column for the day name:

    DayName = 
    
    SWITCH('Date'[DayOfWeek],
    1, "Mon",
    2, "Tue",
    3, "Wed",
    4, "Thu",
    5, "Fri",
    6, "Sat",
    7, "Sun")

    Once we’ve finished creating our data table, we can then join it within the model to the relevant tables, such as the Contoso FactSales table which has a date key. Note, that the Date field in the Date table is unique.


  • How to create Customer Age Bands in Power BI using DAX

    An interesting dimension to report on for demographics is the age of customers. Understanding what type of customers buy what types of products can help inform our customer insight that can be used for targeting marketing offers more effectively.

    In the DimCustomer table of the Contoso Retail Sales DW, we have a field called: Birthdate, but of course, this is unique for all the customers. What we want to do is group these ages into buckets or age bands, so we can create a more informative report. We can then use the grouping in graphs or as filters as in the graph below:


    To create the categorization, I created a new calculated column in the DimCustomer table with the DAX code following the steps outlined below. Note the exact categories you use are up to you or the business. There are no definitive rules on what age bands you should follow, but by studying other reports, you will get the gist of the most common methods.

    The first best practice step is to start creating variables.
    The _dob variable is assigned the [BirthDate] field.
    The _now variable is assigned the date of today, using the TODAY() function.
    The _ageadys variable is then assigned the difference in the number of days between the date of birth field and today’s date, which is essential for the persons’ age in days.
    The _ageyears variable divides the age in days by 365, to get the age in years.
    The section after the RETURN statement then returns the age band category, based on the IF ELSE logic.
    It’s important to put the statements in the correct order, starting from the least upwards, and note that there are some blank Birthdates if have used IF(_ageyears = BLANK(), “None”.
    I only knew there were some blank dates by visually inspecting the BirthDate columns. If I hadn’t used this the blanks would have been picked up as “<18”), so it’s important to sense check as much as possible.

    DAX for Calculated Column (note Age Range is the column name):
    Age Range =
    VAR _dob = DimCustomer[BirthDate]
    VAR _now = TODAY()
    VAR _agedays = DATEDIFF(_dob, _now, DAY)
    VAR _ageyears = _agedays/365

    RETURN
    IF(_ageyears = BLANK(), “None”,
    IF(_ageyears<18, “<18”,
    IF(_ageyears<30, “18 to 29”,
    IF(_ageyears<40, “30 to 39”,
    IF(_ageyears<50, “40 to 49”,
    IF(_ageyears<60, “50 to 59”,
    IF(_ageyears<70, “60 to 69”,
    IF(_ageyears<80, “70 to 79”,
    IF(_ageyears>=80, “80 plus”)))))))))