Excel vs Power BI vs SQL vs Python | Grouping 37,000 Marathon Finish Times

Versatility
93 followers
⏱️ Timestamps:
00:00 - Intro to Data Drill #12: “Making the Cut”
01:03 - Meet the experts
01:28 - Excel solution (Enrique)
05:45 - Power BI solution (Aaron)
11:33 - SQL solution (John)
15:46 - Python solution (Chris B)
19:58 - Results & winner selection
#Excel #PowerBI #SQL #Python
0:00 Introduction and Problem Statement
1:27 Excel Data Analysis and Binning
4:54 Power BI Data Processing and Visualization
8:16 SQL Implementation and Time Banding
13:56 Python Analysis and Binning Workflow
19:11 Final Results and Winner Announcement
Four data professionals compete to analyze marathon finish times for 37,000 runners using Excel, Power BI, SQL, and Python. The objective is to categorize runners into eight time bands and calculate the percentage of the total field for each. Python emerged as the fastest method, winning the \"Data Drill\" challenge.
We just put four data pros head to head
to see how they'd solve the same task
using Excel, Power BI, SQL, and Python.
Four experts, four tools, one winner.
Welcome back to another Data Drill Showdown.
All right, welcome to Data Drill number 12, Making the Cut.
For this one, we're looking at the distribution
of finish times for 37,000 marathon runners.
Our objective is to calculate the percent of racers
who finished in each of these eight time bands,
from sub three to six plus hours.
The only catch, these bands are lower inclusive,
meaning that a runner who finishes
in exactly three hours and 30 minutes
should fall in the three and a half to four hour bucket.
Now, if you wanna play along, pause the video here
and grab the data set for free
at mavenanalytics.io slash data-drills.
It's a great way to sharpen your skills
and practice solving the same types of problems
that data professionals encounter on the job.
All right, time to introduce our showdown competitors.
Kicking us off is our defending champ, Enrique Ruiz,
gunning for back-to-back wins with Excel.
Next, we've got Aaron Perry,
tackling this drill with Power BI.
In the number three slot,
give it up for John Pollard, the SQL baller.
And last but not least, we've got Chris Brule,
back in the game with Python.
So that's our lineup for today.
Let's start the showdown.
To solve this data drill using Excel,
we're going to start by having to categorize
each of these finish times
into the 30-minute band where they belong.
Now, we can't just round down.
We can't use the round down function
to round down to the nearest 30-minute mark
because the only argument that this takes
outside of the number is the number of digits.
What we actually need to do is round
based on it being a multiple of 30 minutes.
And for that, we need to use the floor math function
since that's going to round to the nearest multiple.
So we want to round this number, which is the finish time.
And the significance or the multiple
that we want to use is every 30 minutes.
Now, in Excel, time is just fractions of days
and a day is a single unit, right?
A value of one.
So to get 30 minutes from a day,
we need to do one day divided by 24 hours,
divided by two for half an hour.
Close that out, press Enter.
Again, we get this in fractions of a day to the time.
All we need to do is copy over the formatting.
So this is rounded down to two.
And then as soon as we get into the 2.30 mark,
2.30, just want to double check that if I had two exactly,
it's going to stay in this band, which is what we want.
If I were to make this 159, now it's in the previous one.
So that's working as expected.
Now we can start to aggregate these, right?
So I'm going to insert a pivot table to it in this worksheet
and let's drag the band into the rows of our pivot table.
Because it's time Excel is going to try
to auto group into hours, we don't want that.
So I'm going to undo.
We want the bins or bands that we created for these.
I can just drag the band into the values
going to be the count.
Now if I want the percent of the field,
I can just show values as percent column total.
This is technically accurate,
but we actually wanted to create further grouping here.
So we want anything beneath three hours,
we want this to be a sub three hour marathon time.
And anything six above is going to be six plus.
So all we need to do is just select these, right click,
group them, can remove the original band
and just keep this next group level.
So this is going to be sub three hour
and everything after this is going to be into six hour plus.
And then for this, this is going to be three, two, three,
30, three, 30, four, four, two, four, 30, four, 30, two,
five, this is probably going to cost me the data drill.
And then five, 30, two, six,
but we wanted to make sure we got these correct.
Oops, and there we go.
So these are the finish bands
and this is the percent of the field.
Now the validation question is what percentage
of the runners finished in the sub three hour mark
it's going to be 2.59.
And we want digits only around it down to be two.
Press submit.
And that is how you solve this data drill using Excel.
All right, another strong showing for Enrique
knocking out that solution in about three and a half minutes.
I don't know about you guys,
but I'd expected to see a bunch of conditional logic
with if statements or switch functions to create those bins.
So it was really cool to see how he used
the floor math function as a much more efficient way
to solve that part of the drill.
And like we've seen in the past,
pivot tables just continue to be such a powerful tool
for quick aggregation and those value calculations
and custom groups were a nice touch as well.
So that's a pretty strong bid from Enrique.
Next up, let's see if Aaron can top
that solution with Power BI.
All right, so let's solve this data drill with Power BI
and we're going to use a mix of Power Query
and DAX to accomplish it.
So first up, let's create a new conditional column.
This is going to help us create our bins
for the finish bands.
So let's call this finish bands
and this is going to be based on the final time.
Operator is less than.
Here, we're going to start with three hour mark
and this will be sub three.
All right, we need seven total pieces here.
So we've got one, two, three, four,
five, six, and seven.
And then the last one is going to be
the else clause with six plus.
So let's come back through and quickly update all of these.
We got 330, sub 330.
That is a fast marathon time.
Center equal to four, sub four.
Final less than or equal to.
You could also write all of this out using M code
if you were more comfortable that way than using the UI,
but this is pretty quick here.
All right, less than five, sub five.
We got two more.
So 530, sub 530.
And then the final one is going to be sub six hours.
All right, and this will be sub six.
Awesome, and then we've got our else clause here.
We'll click okay.
We've got our calculated column.
We're going to update this to a text data type
and we'll come and close and apply.
So Power BI is going to add in this calculated column.
So wrap up in a second here
and we'll be able to visualize these bins
and then we'll create a measure on top of it.
All right, we've got our finisher bands.
We'll add this into the canvas.
All right, so we've got all of our times here.
We'll zoom in on that tiny bit.
All right, the next thing that we need to do
is we want to create a calculated column,
or excuse me, a new measure.
So we'll call this percent of total field.
And we're going to start off with a couple of variables.
The first variable that we want to define is for our runners.
So we're going to use count rows
from the marathon data table.
This is going to count all of those records
as individual runners.
The next thing we want to do
is create a variable for all runners.
And here we're going to use calculate
and then count rows from the marathon data table.
And then for the filter context,
we want to filter everything from the marathon data table.
So it might be a little counterintuitive there,
but this is a way to basically return everything.
So we can have our part divided by our whole,
those runners divided by all runners,
or the divide function.
We're also being asked to round this down.
So let's go with round down.
Then we can throw in divide.
Numerator part is the runners,
the whole is all runners.
We want to round it to two.
We'll update that.
We'll set this to a percentage for our formatting
and click out of that and into the visual.
We can drag in the percent of field
and we're being asked for the percent
that finished sub three hours.
So we'd see that at 2%.
Come over, enter our answer is two.
And that's how you solve this data drill using Power BI.
All right, solid solution from Aaron there,
showcasing a combination of Power Query and DAX,
both within Power BI.
Pretty straightforward approach,
using a conditional column in Power Query
to define those bins.
Basically just a bunch of nested if statements
along with a DAX measure to calculate
and round down the percent of finishers.
Now we could have potentially used visual calculations
or the prebuilt value calculations in the matrix visual
instead of writing the DAX from scratch,
but there are pros and cons
to each of those approaches as well.
By the way, if you're interested in diving deeper
into Excel, Power BI or other popular data and AI tools,
check out Maven Analytics and get started for free.
Got self-paced courses and learning paths,
free data sets and projects
you can host in your own portfolio
and a brand new series of live expert led AI workshops
every single week.
Hit the link in the description to learn more.
All right, let's kick things over to John
and see how he solves this one with SQL.
Here's how you're gonna solve this one in SQL.
So you can see I've already copied these buckets
over to my SQL Workbench editor.
Anytime you see segmentation created like this,
you should be thinking case statement.
And what we're gonna do is we're gonna say
when final is less than three,
then we're gonna put it into this first bucket.
And basically we're just gonna follow that same pattern
to create all the buckets.
So I'm just gonna copy down a whole bunch
of when then pairs.
I think we need how many?
Perfect.
And I'm gonna actually just bring each of those up.
I should have done that in the reverse order
to be a little smoother, but this'll work just fine.
And I'll show you, it's just a quick edit
to create each of these buckets.
And then we'll basically just count everything
in these buckets.
And then finally we'll turn that count
into a percentage of the total runners.
So not by bucket, but we'll just put this as an else.
Should be easy enough.
So then we'll end this case statement
and we'll say as time bucket and we'll do count.
Obviously I still need to update the logic
in each of these, but we'll call that runners.
And so yeah, so that's up to 330.
This one's up to four.
This one's up to five.
This one's up to six.
This one's up to 530.
This one's up to 430.
And I think we should be good to go.
We'll just quickly just confirm that logic's working.
Should be.
All right.
So yeah, we've got our time buckets.
We've got our runners.
And then we'll put in one more column here.
That's just gonna be a sum of a count star.
And we're just gonna call this total.
So you can see what that does here.
That gives you all of the runners here.
That's a window function.
For anybody who was in Alice's workshop recently,
she did a great section on window functions
in her advanced SQL workshop.
And basically the last thing we're gonna do
is just instead of just doing the total,
we're gonna add that count
and then I'll call it percent total.
And this should work unless I've done anything wrong here.
So now we've got percentage of total.
It's not formatted as percentages,
but it should work for our calculations.
So I think that's what we need.
The validation question is what percentage of the field
finished in the sub three minute band.
So that's three minute band.
So it's about two and a half percent.
Digits only rounded down.
So we'll say 2%.
And let's see if that's right.
Awesome.
So that's how you do it in SQL.
All right, that was John coming in neck and neck
with Enrique solving this one in three minutes and 30 seconds
but what do you guys think?
Do we penalize John for copying those bucket labels
ahead of time when Enrique typed them by hand?
We may have to consult the judging committee on this one.
Now, as far as the query is concerned,
pretty straightforward approach here
using case one statements to create the bins
and a simple window function to calculate the percent total.
It's clean, it's readable,
and it's a nice pattern to use
for this type of distribution exercise.
Now, last but not least,
let's welcome back Chris Brule
and see if he has what it takes
to pull out the win for Python.
All right, so let's go ahead and start
by reading in our marathon data CSV.
And if we take a look at our data,
the most important column for this problem is final.
That represents how long it took someone
to finish the marathon.
And what we need to do first is convert this timestamp,
so two hours and eight minutes,
into an integer that represents the total number of minutes.
So we want this to look like 128 point something
to represent the value in minutes.
So what we're going to do
is first create a column called marathon df final minutes.
And what we need to do first
is use pandas.toTimeDelta function,
which gives us the amount of nanoseconds
in a given timestamp.
So we'll pass in marathon df dot final.
Then we're going to extract the total seconds
within that time delta.
And then finally, we'll divide by 60
to get the total number of minutes.
So if you take a look at marathon df now,
you can see that this final minute has 128.85
representing this finish time here.
And now comes the tedious part.
We need to create some band edges
to get the bucket specified in the problem.
So we're going to start with zero minutes,
then move up to 180 to get our zero to three hour bucket.
Then it's just adding 30 every time for every 30 minutes.
And then finally, at the very end
to get an edge for our last bucket,
which can go to infinity,
we'll do float and pass an infinity
just to make sure we capture anything over six hours.
We'll then do band labels, which is especially tedious.
And we're going to go ahead
and call this first label sub three.
We'll then have three to 330.
We'll have 330 to four.
We'll then have four to 430.
And so on, I won't narrate this whole thing.
And finally, six plus.
And so that's the tedious part.
Now we just need to create a column
that cuts our final minutes and places them in these bands.
So we'll create a column called band.
And there's a very handy pandas function
called pandas.cut.
And what we do is specify a column.
So marathondf.finalminutes.
We specify our bins, which is our band edges,
the label for those bins.
So labels equals band labels.
And then finally, we're just going to specify
right equals false to make sure we're using
the left side of our bin as the cutoff there.
And so if we go ahead and take a look
at our data frame now, marathon,
you can see we now have this band,
sub three, and six plus for the longest runners.
So now all we need to do is use the value counts function
to get the percentage and our final answer.
So marathondf.band.valuecounts.
And to get the percentage,
we'll specify normalize equals true.
And if we take a look at our sub three category,
we had about 2.6% of our runners finish under three hours.
And so because we're going to round down,
we'll go ahead and plug in two.
And that's how you make the cut using Python.
Ladies and gentlemen,
we might just have our closest race in data drill history.
That was Chris Brule showing absolutely no mercy
on this drill, clocking in at three minutes and 13 seconds.
Now, one of the things I love most about these drills
is comparing solutions across tools
because there are almost always multiple ways
to attack the same problem.
The Excel, Power BI, and SQL solutions
all use different techniques,
but followed a very similar pattern.
But Chris took a very different approach here
with Python.
In this case, he converted those timestamps into minutes
using the Panda's time delta function,
created placeholders for each bin
by defining labels and cutoff values,
cut the data to populate those bins,
and then normalized it to calculate those percentages.
Very cool to see such a different approach
to solving the same task.
All right, so with that, let's summarize our results.
Enrique kicked us off strong with Excel,
clocking in at an official time
of three minutes and 36 seconds.
Aaron came in about a minute behind with Power BI
at four minutes and 41 seconds.
John bounced back with an impressive SQL solution
coming in at three minutes and 30 seconds.
And last but not least, Chris dropped the hammer with Python
with a time of three minutes and 13 seconds.
So it was a tight race, especially at the top,
but looks like Python is our clear winner.
Congrats, Chris, you're officially the Datadrill champion.
So that's a wrap for Datadrill number 12, making the cut.
If you learned something new
or if you have your own solution to share,
please drop a comment and let us know how you solved it.
Now, if you wanna build expert level data and AI skills
and learn directly from the experts in this video,
head to Maven Analytics,
create your own personalized learning plan
and get started for free.
As always, don't forget to like, subscribe,
ring that bell so you don't miss out,
and we'll see you in the next one.
