Creating a DAX Calculated Column for Today, Yesterday and Next Working Day with Variables
Below was a question from the Power BI Community where the output was to have a calculated column in the Date Table, which would have Today for Todays date, Yesterday for Yesterdays Date, and Next Working Day (Being the following Monday)
The challenge was that for the “Next Working Day” it should only be for the next Monday and not all Mondays.
If you did not know you can use DAX Variables not only in DAX measures, but also in DAX Calculated columns.
I would like to mention that I had always like to step out my calculations when working in long or complex DAX measures or calculated columns. This video from Guy In a Cube with Patrick and Marco gives a great introduction on how to Debug DAX, in which they demonstrate how to use variables in DAX measures to debug the measures.
So below is my DAX calculated column with the explanation afterwards.
T/Y/Tom = VAR TodaysDate = TODAY () VAR YesterdayDate = TODAY () - 1 VAR NextMondaysDate = SWITCH ( TRUE (), 'Date'[Date] - WEEKDAY ( 'Date'[Date], 2 ) + 1 >= TodaysDate, 'Date'[Date] - WEEKDAY ( 'Date'[Date], 2 ) + 1, TODAY () - 9 ) VAR NextWorkingDay = SWITCH ( TRUE (), 'Date'[Date] = TodaysDate, "Today", 'Date'[Date] = YesterdayDate, "Yesterday", 'Date'[Date] = NextMondaysDate, "Next Working Day", "Not Applicable" ) RETURN NextWorkingDay
Code above formatted with the DAX Formatter
- Line 1 – This is where I am giving my Calculated Column a name called T/Y/Tom meaning Today/Yesterday/Tomorrow
-
Lines 2 & 3 – This is where I am creating a variable TodaysDate which today’s date using the DAX function TODAY()
- With today’s date being 02 July 2018
- Lines 3 & 4 – This is where I am creating a variable called YesterdayDate getting yesterday’s date, going back one day from today’s date.
-
Lines 6-8 is where I am creating a variable called NextMondaysDate.
- I am then using the DAX SWITCH function which I find personally is a lot easier to use than an IF function.
- My experience has also taught me that often an initial requirement is for two possible conditions, but once it is looked at more closely there are more than two conditions. And using the combination of the SWITCH and TRUE function makes it a lot easier to add in as many conditions are required.
-
Lines 9 & 10 – This line is quite interesting, because what I wanted to do was to find what Next Mondays date would be.
- And if I had to put this single calculated column into my dataset I would get the following as shown below.
- I actually did this as part of my debugging process to ensure that I was getting the right date.
-
As you can see above what this does is it gives me the date for each Monday in each Week.
- For this current week the date for Monday is 07/02/2018
- Whilst for next week the date for Monday is 07/09/2018
-
Lines 11 & 12 is where I am comparing to see if it is greater than equal to todays date.
- If it is then use the Mondays Date.
- In this example the output would appear to be the same as the DAX in lines 9 & 10.
- The difference here is if you had to use it outside of the SWITCH context it would then result in every Monday having the “Next Working Day” which is not the required result.
- Lines 13 & 14 – This is where I am putting in the ELSE condition which is set to take Today’s date and go back 99 999 days. Which should not be seen in the dataset.
-
Lines 15 – 22 – This is where once again I am using the SWITCH and TRUE DAX functions, because I have got multiple conditions to evaluate.
-
The first condition is where I am looking at my date table and where my calculated DAX column matches my variable for today.
-
If you had to not imagine it, but see what it would look like, it would look like the following:
- ‘Date'[Date] = “07/02/2018”, “Today”,
-
-
The second condition is where I am looking at my date table and where my calculated DAX column matches my variable for yesterday.
-
If you had to not imagine it, but see what it would look like, it would look like the following:
- ‘Date'[Date] = “07/01/2018”, “Yesterday”,
-
-
The third condition is where I am looking at my date table and where my calculated DAX column matches my variable for next Monday.
-
If you had to not imagine it, but see what it would look like, it would look like the following:
- ‘Date'[Date] = “07/09/2018”, “Next Working Day”,
-
-
- Lines 23 & 24 – This is where I close off my variables and use my final variable called NextWorkingDay
So now when I look at my date table I see the following below which what is I wanted to get.
As you can see above there is a previous Monday’s date, and it says Not Applicable, so I am getting the results I expect.
I hope you found this blog post useful on how to use DAX variables in DAX calculated columns, as well as how to use it for debugging your DAX code.
As always if you have got any questions or suggestions please leave them in the comments section below.
Thanks Gilbert, great article!
Glad you enjoyed it. Thanks for the feedback