This is my DAX Query solution to Maven Analytics Data Drill #5 – Movie Metrics.
The challenge is to create new columns in the USERS table based on data given in the USER ACTIVITY table. The new columns to be added are :
- Date from the first movie they finished
- Name of the first movie they finished
- Date from the last movie they finished
- Name of the last movie they finished
- Movies started
- Movies finished
Note: You might notice, I didn’t follow the instructions to the T . I did not create columns in the USERS table. Instead, I decided to solve this using a series of measures. This approach was natural for me, as I new the final query would produce the final table. Also, I completely missed the point where it should have been solved in the USERS table !
First we create the Count Movies metric which as the name implies, gives as a count of the movies. This may help us with troubleshooting DAX code.
DEFINE
MEASURE 'Box'[Count_movies] =
CALCULATE(
COUNTROWS( VALUES(
'activity'[movie_name]
)) Then we find the first movie the user has finished by getting the minimum date using the MIN() function, followed by the CALCULATE() function. The CALCULATE() function will force a context transition for each user. We then filter the activity table to only query finished movies.
MEASURE 'Box'[First_Date] =
CALCULATE(
MIN('activity'[date]),
'activity'[finished] = 1
)For First Name , we need to filter the table, by the minimum date and the finished flag. We access the min_date by referring to the first_date measure and store it in a variable. Thereafter we use it to filter the table and get the movie name by using the MAX() function.
MEASURE 'Box'[First_Name] =
VAR min_date = [First_Date]
RETURN
CALCULATE(
MAX('activity'[movie_name]),
'activity'[date] = min_date,
'activity'[finished] = 1
)For Last Date and Last Name, the logic is similar to First Date and First Name but we change the MIN() to a MAX() function.
MEASURE 'Box'[Last_Date] =
CALCULATE(
MAX('activity'[date]),
'activity'[finished] = 1
)
MEASURE 'Box'[Last_Name] =
VAR max_date = [Last_Date]
RETURN
CALCULATE(
MAX('activity'[movie_name]),
'activity'[date] = max_date,
'activity'[finished] = 1
)For the final two measures, Movies Started and Movies Finished, we use the COUNTROWS() function to count the rows in the table while filtering it by 0 or 1 respectively, while wrapping it around a calculate function.
MEASURE 'Box'[Movies_Started] =
CALCULATE(
COUNTROWS('activity'),
'activity'[finished] = 0
)
MEASURE 'Box'[Movies_Finished] =
CALCULATE(
COUNTROWS('activity'),
'activity'[finished] = 1
)Finally, after creating the measures, we can create the table and pass the measures as columns in the USER ACTIVITY table. We filter the table using the FILTER() function for the “Fight Club” movie in the last name column, to get users who watched the movie last. To get the final answer, we count the rows in the table using COUNTROWS().
(Note: If we were to pass them in the USERS table we would most likely have to use the RELATEDTABLE() function to refer to the columns in the other table.)
EVALUATE
VAR T =
SUMMARIZECOLUMNS(
'activity'[user_id],
"Count_movies", [Count_movies],
"First_Date", [First_Date],
"First_Name", [First_Name],
"Last_Date", [Last_Date],
"Last_Name", [Last_Name],
"Movies_Started", [Movies_Started],
"Movies_Finished", [Movies_Finished]
)
VAR Y = FILTER(T, [Last_Name] = "Fight Club")
RETURN
// T
// V
{ COUNTROWS( Y ) }
ORDER BY
[Count_movies] DESCSummarised Table

Final Table

Final Count

Submitting the answer

That’s all for today ! 🤗👋🏾
Video Tutorial




















