All,
Well, I think we’re off to a very good start here on the inaugural Enterprise DNA Problem of the Week - over 500 views on this topic in the past week, and a dozen very different solutions submitted. A huge thanks to all who participated, whether you posted your solution or not. I really hope you found this to challenging, instructive and entertaining.
When I formulated this first problem, I had no idea that there would be as many creative ways to solve it as you all found. Here’s how I went about it…
In the YouTube solution video that I did for this problem, I discussed in some detail the general approach I take in solving these types of complex problems, focusing on the following three techniques that you can apply to almost any DAX problem:
- “Rubberducking” (and introducing William the Debugging Duck) - verbally articulating your general strategy before diving into coding
- Solving in Parts - structuring your DAX so that you can test key components of your solution as you develop them to make sure they are working as expected
- Searching for Unknown Functions - how to locate DAX functions, when you know generally what you want the function to do but don’t know what function specifically performs that operation
OK, onto my specific solution. In formulating this solution, I focused on keeping it broadly applicable to any set of holiday data, and thus I broke the dates into three categories:
a) Specific Dates (e.g., January 1, December 25 of the selected year) - these were the simplest to deal with - just required harvesting the proper year, month and day values and assembling them using the DATE() function
b) Relative Dates on Specific Days (e.g., fourth Thursday in November) - my approach for these was to start with the MIN date from my Dates Table that matched the selected year, month and day, in this case starting with the first Thursday in November of the selected year, and then based on whether the occurrence was first, second, third or fourth adding 0, 7, 14 or 21 respectively to get to the proper week.
c) Relative Dates on Nonspecific Days (e.g., last Monday in May) -the approach above won’t work for these type of dates, since depending on the year there may be four or five Mondays in May. For this category, I just used the same approach above in b) used to calculate the MIN date, but instead calculated the MAX date.
In 1) above, I harvested the necessary year, month and day values
In 2) above, I broke the Holiday Day field into its two component parts, occurrence (e.g., Fourth) and Day (e.g., Thursday) using the SEARCH function to identify the position of the space between the words, and then the LEFT, MID and LEN text functions to work to the left and right of the space to capture each word individually into separate variables.
In 3) above, I calculated the MIN and MAX dates corresponding to the selected year, month and day.
In 4) above, I used a SWITCH( TRUE () ) construct to determine which category of dates (specific, relative on specific days, relative on nonspecific days) the given holiday information fell into and depending on the result of that evaluation applied the appropriate strategy to that date type as discussed above.
Once I got to that point, the Celebrated On measure was relatively easy to implement:
For the given Holiday Date, it just does a LOOKUPVALUE of the day on which that date falls, and then applies another SWITCH( ) statement to subtract a day if the day is a Saturday, add a day if the day is a Sunday, and return a blank otherwise.
Even if you took a different (and better…) approach, this hopefully provides a different and useful perspective on general strategies you can use to break down these types of problems.
While each of the solutions you posted had interesting elements definitely worthy of further discussion, there were specifically a couple that I did want to highlight here. @Rajesh’s first solution was truly a thing of beauty – 13 lines of DAX to solve the entire problem, and making perfect use of a technique I first saw from @AntrikshSharma that reduced the parsing of the text field from multiple variables in multiple lines to a single line using an ingenious application of the PATHITEM function.
While @Rajesh had the shortest solution, I also wanted to highlight the longest which was @quantumudit’s. What was really creative about this one was its flexibility in handling information that could be formatted/specified in a number of different ways. If you have a real-world application of this problem that deals with holiday data from multiple countries, I definitely recommend looking at his solution.
At this point, I wanted to open the discussion up and invite anyone to discuss their solution (at this point no summary hiding or spoiler alert structures needed), the thought process you employed to get to the answer, specific technical innovations you want to highlight, or any other element of this problem you want to talk about. In addition, I absolutely welcome feedback about this initiative in general and thoughts you have after going through the first problem.
Again thanks to all for participating - I look forward to the discussion and subsequent Problems of the Week!
P.S. Drawing results to be posted shortly. Just want to make sure my list of entrants was complete…