01:10 File download instructions 01:42 Obtaining data from a SQL server 02:58 Database authentication options 03:33 Previewing and selecting database tables 04:05 Selecting only needed fields (i.e., columns) 05:23 Extracting data from a record 05:52 Extracting data from a table 06:28 Creating extended calculations 06:55 Code-level column rennames 07:43 Removing "new" unneeded columns 07:56 Finding and replacing nulls 08:42 Row reduction 09:49 Filtering out the "non-sale" rows 10:04 Setting data types and rounding values 10:41 Loading the results to Excel 10:51 Stylizing the "Sales" column 11:21 Creating the Pivot Table report 12:47 Pivot Table Styling Tips 13:12 Hiding Gridlines and Headings 13:25 Hiding "Expand/Collapse" buttons 13:39 Applying a Currency Style to the values 13:53 Displaying Subtotals at the bottom of their groups 14:14 Adding blank lines between groups 14:27 Customizing Indentations 15:26 Sorting columns 16:05 Sorting Rows (multi-level sorting) 17:06 Zero-fill the empty cells 17:33 Changing the color of zero-filled cells 19:00 Creating custom headings 19:52 Centering the headings 20:24 Hiding the Filter controls 21:38 Standardizing column widths 23:20 Conclusion #PowerQuery#tutorial
0:03 Data Extraction from Multiple Sources
6:22 Data Transformation and Flat Table Creation
10:46 Preparing Data for Pivot Table in Excel
12:16 Building and Designing Pivot Table
19:02 Finalizing Pivot Table with Professional Formatting
23:26 Conclusion and Further Learning
This video provides a comprehensive tutorial on using Power Query to consolidate data from SQL Server into a \"flat file\" and transforming it into a professional Excel report. It covers the entire pipeline from database connection and data cleaning to advanced Pivot Table beautification and layout optimization.
Hello, I'm Brian with BCTI, and I'd like to talk to you in this video about creating a flat file.
Now, when we use something like Power Query to pull data, data comes from a lot of different
places. Data can be in databases, it can be in other Excel spreadsheets, it can be in delimited
text files, PDF files, folders, websites. Did I say that one already? But when the information
is scattered like that, you have to bring it all together under one roof, and so people will often
create what's called a data model. Now, a data model is where we have a lot of different pieces
like tables in a database, but we have them able to communicate with each other by building a
series of what are called relationships, or like connections and pathways between the tables.
Well, if you don't need that kind of sophistication, maybe you need something simple, just a simple
flat table. But I have data in different places, how do I get it all under one roof? And so that's
what I want to show you how to do today, is how to take data from multiple locations and bring it
into a single flat table or a flat file. Be sure to download this solution file from the link in
the video description so you can see the results, but also be able to go into Power Query and look
at all the fully documented M code that's going to drive this. I've also included step-by-step
instructions for this process in a PDF file. In addition to learning how to build the flat file
as a bonus, I'm also going to show you my top 10, probably more like top 15, things I like to do to
pivot tables to beautify them. For this demonstration, we're going to pull data from a SQL server,
and we're going to pull data from three different tables and combine them into a single flat file
table. Now, this data could come from anywhere. Some of this data could be coming from Excel files,
some could be coming from PDFs or websites, like I said earlier. But in this case, just to keep it a
little simple, everything's going to be coming from the same database, but it will be data from three
different tables combined into one. So I've started Excel with a blank workbook. So we're going to go
up to data, and then get data from database, and I'm going to choose a SQL server database.
We need to provide that database with credentials, because most databases don't just let you walk in
and take whatever you want out of them. Now, in this case, I've documented the server name, database
name, and login credentials in the downloadable documentation. But here it is, you'll see on the
screen, there's the information right there. And I'm going to just copy paste that from the supplied
text file, because if I type that in, I'm probably going to screw it up. For the server name, it's
going to be xlgdemos.database.windows.net. And for the database name, it will be AdventureWorks,
because a single SQL server can host multiple databases. I'll hit OK. And now it wants my
credentials. Now, in databases, if you're going to just use your normal login name credentials
from your Windows system, you would choose Windows and just use my current credentials.
You could also supply some alternate credentials, say you want to log in as an elevated user
account. Now, in our case, we're using the credentials from the database itself. And so
the username for this is going to be datamaster. And then the password will be that password
right up there. And I've pasted that in, but we'll assume that I pasted it correctly.
We'll go ahead and hit Connect. So now I'm connected to this database. Now, I could select
a whole series of different tables, but the only table that I really want is this saleslt.product
table. And if we maximize this window, we can peek in and we can see what all that information is.
Now, there are some columns from this table that I want, but there are other columns from other
tables that are linked to this table. So we're going to bring in just this one table, but we're
actually going to be pulling information from two other tables through this one. So I'm going to go
ahead and pick that one table and click Transform Data. Now, this table has 20 columns. I've zoomed
in a bit just to make it a little easier for you to see. The only columns from this that I want
are the name column, the color column, and then if we scroll all the way to the far right,
there's a product category column and an order detail column. Now, if we peek in the product
category column, that is a link to another record. And from this record, I might want something like
the name field, which is like a category. But from the order details, if I click next to the word
table, I may want to get every record from this table to do some sort of aggregation.
But I don't want all of these columns. I just only want certain columns, like maybe the order
quantity and the unit price. Now, because these columns are so far from one another, trying to
select them individually is a little bit tedious. So I'm going to go up to the Choose Columns button
in the upper left. And here I can clear the list and just say I want the name column, I want the
color column, and I also want the product category and the sales order detail. I'll hit OK. And now
I've reduced that down to just four columns. So I'm going to keep these two as is. But from the
category, the product category column, if I peek into that, all I want is the name field that has
that category name. So I'm going to hit the Expand Table button, and I'm only going to pick name. And
I don't want column, I don't want table name prefixing on this. So we'll hit OK. And now I
have my categories. Now that name is kind of clunky, name one, so I'll rename that to category.
And from the sales order detail table, if I peek into one of these, I would like the order quantity
and prices, because I'm going to take those order quantities and multiply them by their prices and
then maybe aggregate them up to a higher level. So those are the only two columns from that table
are those tables that I want. So we'll do an Expand Table. And all I want is order quantity
and unit price. I'll hit OK. Let's zoom out a bit. And so now I have information that was
originally spread across three different tables, all combined into a single table.
Now we're going to perform some additional calculations on these columns that we just
extracted from the tables and answer some other business questions. So in this case, for the
order quantities and the unit prices, I would like to take those two columns and multiply them
against one another. So I'm selecting those two columns and then I'll go up to Add Column,
Standard and then Multiply. And I get a new multiplication column. Now the name multiplication
is kind of a terrible name. I'm not going to go up here and double click it and rename it,
because that's going to create another step in my step list. And I like to keep my step list
as lean as possible. So I would go directly into the M code where it says let's create a column
called multiplication and let's just call it total sales. Because why not just start with the
right name? Why create a column with the wrong name to then turn around and give it the right
name? Just start with the right name. Now if you're really going to be picky, that's not going to
change the execution of this M code in any way by combining those two steps into one. That's just
something I like to do because I like to have as few steps in the list as possible. But it wouldn't
change any code execution. Now that we have the total sales, if we don't need the order quantity
in the unit price, because maybe that's not going to appear in any report, once we've derived what
we need from them, we can just delete those columns. As far as the colors go, if we hit the
drop down list for color, we can see that here are all the colors, but some of these rows have
nulls in them, which means there was no color defined. And if I scroll down, let's see if we can
find any of these. We may not be able to. Oh, there's some right there. So we can see several
of these have null in the color field, but I don't want null as a category in my report. So what I'm
going to do is I'm going to take that color column. I'm going to right click and replace some values.
And we'll replace all of the nulls with no color. And then we'll hit OK. And now if I scroll down,
instead of having nulls, I will have no color. And that will be a more proper category.
Now we're going to reduce the number of rows in this table. There are 695 rows in this table,
but I'm not going to report at this granular of level. So what I want to do is I want to
group this by category, then subgroup it by name, and then subgroup it by color.
Then we'll take whatever's left over and do something like a summation on the total sales
column. So I'm going to select category, hold down control, select name, and then color.
And then I'll go up to home and choose group by. So this opens up the group by dialog box,
and we're grouping by name, then color, then category. And now we're going to create a
summary calculation. Now this summary calculation, I'm going to call total sales. And then for the
summarization, that's going to be a sum function on the total sales column. I'll hit OK. And now I
have 295 rows. So we went from 695 rows to 295 rows. So that's over 50% savings, because this
is the level of granularity that I want in my report. Now anything that's a null in the total
sales column is something I never sold, and I only want a report that shows things that we sold.
So I'm going to filter out the nulls from the total sales column. That gets me down to 142 rows.
I'm going to set the total sales to a currency data type. That way, I limit the fractional
calculation to four decimal places. But when I send this back to Excel, I don't want to see any
four decimal place precision results. So I'm going to round it to two decimal places. So the data
type gets me to four decimal place precision. This transform round round. Now I can get this
down to two. I won't really see anything change here. But when I sent this back to Excel, some
of those calculations may have gone into the thousands and ten thousands place precision.
Now none of them will. Let's now take this and close and load it back out into Excel. So I'll
go up to home, close and load. This is now sent out as a proper table. Now the sales look ugly,
and you might think, well, wait a minute, didn't you make that a currency style? No, I made it a
currency data type. There's a difference. Data types just interpret the data. Styling is for
presentation. Power Query is not about making things look pretty. Power Query is about making
things look accurate. If you want something to look pretty, that's Excel's job. So if I wanted
to, I could take this and I could select that total sales column and give it something like a
currency style and dress it up a little bit. Now let's take this and turn it into a pivot table.
I could have actually gone straight from Power Query into a pivot table and bypassed this whole
table. But I wanted you to see what the output looks like. But let's say that you built this
table and you think, oh, I really didn't need to make this. I could have just gone straight into
the pivot cache and made a pivot table. If you delete that sheet output, that will not delete
the query. That just says, don't use that as output anymore. Just make it what's called a
connection only query. So it's a query that knows what it's supposed to do, and it's waiting to do
it. It's just waiting for you to tell it to do it. So now I can go back and say, right click,
load to, and let's move this out of the way. Now I can say, let's go straight to a pivot table. So
this will take the query output and dump it right into the pivot cache. I'll hit okay. And now we
have an empty pivot table. Let's populate that pivot table by putting category in the rows,
that'll be the parent, and then name below that, that'll be the child. So category is parent,
name is the child. I'm going to put color in the columns, and I'm going to put total sales
in the values. Now if I close my pivot table fields panel, this is a very ugly and unattractive
pivot table. So let's run some beautification on it. Now this part has nothing to do with creating
a flat file. We've already created that flat file. That's what's feeding this pivot table.
But this is all just beautification of the pivot table. These are things I like to do to my pivot
tables just to make them look a little bit better. Now you can do any or some or none of
these things or even more. It's completely up to you. So the first thing I like to do is, since
this is more of a report interface, I'm not going to be writing formulas here or entering data. I
like to go to the view tab and turn off my grid lines and turn off my headings because I don't
need them. I'm also not going to be expanding or collapsing these parent-child relationships.
So all these little plus-minuses on the side are a little weird. So I'm going to go up to the
analyze ribbon and turn off the plus-minus buttons. I want to apply a currency style to these values.
So if you right-click any of those numbers and go to number format, I can then choose currency,
hit okay, and now I've given all those numbers a currency style. I like my subtotals at the
bottom of their respective groups. And right now each subtotal is at the top of the group.
We can see here like this $830 was made up of $766 and a $63 sale. But I like my subtotals
below the transactions. So we can go up to design and then say subtotals below the bottom of the
group. Now when I do this to make it a little easier to read, I also like to inject a blank
line between each group. And so that's where we can come up here to blank rows and insert a blank
line after each item. I like to have a little more indentation between the parent item and the
child item. Now one way you can do that is by going up to the report layout and using an outline form.
But that puts every level in the hierarchy into a separate column. And that's a little bit
too much space for me. So I'm going to go back to the compact layout. But I need something in between.
Well here's something you can do. If you go up to the analyze ribbon and then in the upper left go
to options. These are the pivot table options. Right here it says when in compact form indent
the rows a certain number of characters. Well we're just seeing a single character indentation.
But what if we made it a four character indentation? And I hit okay. Now I get more,
it's easier to identify that this is a child level object because of that extra indentation.
But you're not in a completely different column so you don't seem completely disconnected.
So I like to add a little extra indentation. The next thing I want to do is sort these columns
left to right in descending order. Now I want to do that across the entire set of data. So I'm going
to scroll all the way to the bottom of this table where I see my grand total row. So these are the
totals by color. I'm going to right click on any one of those color calculations and then go to
sort largest to smallest. Now all of the columns are sorted in descending order by the total of
that color. Not by the individual products or categories but by the total of the color.
I want to do the same thing for each of the parent level items. So like gloves,
handlebars, helmets. And I want all those to be sorted in descending order so I know what my best
category is and my worst selling category is. So I'm going to go to a parent level total like
helmets. These are the subtotals by color but this is the grand total for all helmets. So I'm going
to go to something like that and do a right click sort largest to smallest. And now we can see at
the top touring bikes had the most sales followed by road bikes followed by mountain bikes etc.
Now within each of these I would like to have all the child items sorted in descending order. So that
way like road bikes what was my best selling road bike what was my worst selling road bike.
So I'm going to right click on one of those numbers and sort largest to smallest. So now I not only
know in descending order the best category but also in descending order the best actual item.
Another thing I like to do with my pivot tables is zero fill all the gaps in the pivot table so
that way somebody doesn't think that you know some data is missing. Now I'm not actually going to go
in here and type in zeros in all of these because I can just activate it with a feature. So if I go
back up to pivot table options on the layout and format tab for empty cells show currently nothing.
So I'm going to type in a zero and now I zero fill. Now I like the zeros so I get that confirmation
that there was no sale but when you have this many zeros it becomes challenging to find actual data.
So let's use a little bit of conditional formatting to change the color of those zeros.
So I'm going to click on any zero go up to home conditional formatting highlight and I want to
change the color of anything that is equal to a zero. Now I don't want to fill the cell in with
a color like this is doing so I'm going to go to custom color format and all I want to do is change
the font color and I'm going to go to this sort of medium light gray. I'll hit ok hit ok that will
change the color of that one cell but when you use conditional formatting with pivot tables you
then have to hit this little blue button to tell it how to extend that behavior through the rest of
the table. So is it just going to be for these color product combinations and none of the grand
totals or for every number on the table. I want every number on the table so I'm going to choose
this middle option where anything that calculates or contributes to a sum is going to get colored.
So now I get that zero confirmation sale but it's now easy to find real data because of the
the contrast in the color of real data versus just placeholder zeros. Now let's create some better
headings for this pivot table. Currently things like row labels column labels sum of total sales
don't exactly scream professional report. So where it says row labels I'm going to change that
to category and name and for column labels how about color and for sum of sales how about
total sales. Now notice here it won't let me use total sales because total sales is the name of a
field. So here's a workaround cheat if you really have your heart set to call that total sales
call it total sales with a space on the end because total sales space is not the same thing
as total sales and it will allow it. I'll allow it. Now here's a little pivot table feature you've
probably never used before. I would like to have all of my headings centered within their respective
cells. Well I'm not going to do this with justification controls in normal excel because
this is actually a pivot table option. We'll go to pivot table analyze in the upper left we'll go
to options and then right here merge and center cells with labels and if you give that a check
and hit okay it will center all of your headings in their respective columns.
Now the next thing I would want to do is get rid of these filter controls because this report is a
final report. I don't want anybody changing it. I don't want anybody filtering it and this is just
too much of a temptation to want to come in here and filter by product or filter by category or
something else. So I want to take those toys away from them. Now if you go up to the analyze ribbon
and you turn off the field headers feature you will not only turn off those buttons but you will
also lose your headings. So what I've done is I have created a little macro that will turn the
buttons off without losing the headings. Now on my quick access toolbar I have a little button here
that says pivot table filter buttons and when I click that it will turn those buttons off and I
could also turn them back on if I need them. Now I'm not going to go through how to add the code to
build the button and do all of that because I've made a whole video for that. So if you click the
thing in the upper corner if it pops up or just check out the link in the video description and
go watch the video on how to add the feature for turning filter controls on and off in pivot tables.
My next objective is to standardize the column widths for the colors. Now right now the column
widths are all based on the data in those columns and when you do something like a right click
refresh it will auto fit all of these columns based on their contained data. Well if I had like
12 months January through December I don't want May to be a skinny column and September to be a
wide column because of the names in the headers. I want them all to be the same width. So what I'm
going to do I'm going to go back up to view and temporarily turn my headings back on.
I've done a right click refresh and I'm going to assume that the grand total column will be
the widest column and if I click and hold on the divider for that column I can see this is 129
pixels. I'm going to highlight all of these color columns and then take any of those highlighted
dividers and I'm going to set it to 129 pixels. Actually I think I'll go for 140 just to budget
budget in a little expansion. So I'll go to 140. I can get there and now they're all 140 pixels wide.
The problem is if I right click refresh they're all going to get crushed back down again.
So to keep that from happening we'll go to pivot table analyze options in the upper left and then
right here auto fit column widths on update. I'm going to uncheck that. I'll hit ok. Now I'm going
to re-highlight those columns. I'm going to reset them to 140 but see now if I right click refresh
nobody moves. And there you have it how to take data from multiple data sources and combine it
into a single flat table. If you don't need the complexity of a data model and relationships
this could be what you need just to create something simple. But along with that are some
of my top tips for making ugly pivot tables beautiful. Be sure to download these files from
the link in the video description. Also check out the link in the video description for that
other video about hiding the filter buttons in pivot tables. And if you have any thoughts on
other things that you like to do in your pivot tables to beautify them put that down in the
comments and share it with everybody. Thank you for watching and remember at BCTI the learning never
stops.