Calculating Sales Amount by Product Category in DAX and Power Query
20K views
Oct 30, 2023
In this video, we'll delve into how to calculate the total sales amount by product category by writing DAX formula and using the Power Query General User Interface
View Video Transcript
0:00
In this video, I'm going to show you how to use the general user interface of Power Query
0:06
of Excel to calculate the total sales by product category. We're going to switch over to the Power BI data model and write the DAX formula to achieve the
0:18
same result. So let's get started. Now, these are all our tables loaded into the Power Query editor
0:26
This is going to be the fact table and of course we have a couple of dimension tables
0:32
Now we want to work with the fact table and the dim product table
0:36
We want to be able to access the pro category Now to do that we're going to create what's called a snowflake margin
0:43
So I can right click at this space here and choose new query
0:47
And then click on combine and choose merge queries And then we're going to see the merge dialog box
0:53
Now we need to select the tables and the matching columns to create a merged table, in other
0:59
words, to create the snowflake margin. Now I'm going to select the fact table, and then I'm going to choose the dim product table
1:08
because we want to focus on total sales by product category. Now we can see the product category in the dim product table
1:16
Now we can see the product key is associated with the product key in the dim product table
1:23
So now for the join kind, we're going to choose the inner with only the matching rows returned
1:29
And then we're going to see the selection matches 49,999 of 49,999 rows from the first table and so on
1:40
So go ahead and click OK. Now we can see we have the matched query as new
1:46
I going to rename this I going to call it sales amount by product category so I going to select everything control C to copy control enter or click enter to commit alright so I going to scroll to
2:02
the right and of course you can see this dim product table now want to access the
2:07
product category column in this dim product so I'm going to click on this
2:11
expandable button and uncheck use original column name as prefix and I'm
2:17
to uncheck all the columns now i won't actually pull across the product category because i want
2:21
to do total sales by product category so i'm going to click on okay so you can see the product
2:28
category now included into the fact table of the matched table which is fine then let's go ahead
2:34
and use the general user interface now to do that i'm going to come to the transform tab under the
2:40
table group i'm going to choose group by and of course you can see the group by dialog box now
2:46
because the product category column is selected i can see that's already populated for me as the
2:52
column to go by and then i can just give name for the new column i'm going to control v instead
2:57
sales amount but product category now for the operation i want to perform the sum i'm going to
3:03
choose the sum and then for the column that i want to aggregate on i'm going to choose the sales
3:08
amount okay and then i'm going to click okay amazing so we can see the output so you can see
3:16
the total sales amount by product and that's quite amazing. I'm going to fix this in the currency data type and then I can come to the home tab
3:27
close and load to. Now we don't want to dump all the tables, we just want to create a connection
3:32
So choose only create connection and go ahead and click on OK
3:36
And I'm going to scroll down. We have the sales amount by product categories we can right click and choose load to and then I can load to a table in the existing sheet and go ahead and click okay all right amazing so let apply currency formatting
3:56
to make it more visible let's type in the total here i'm going to come to the home tab click on
4:01
the styles sales styles click on the total and i'm going to do the alt equal and that's perform the
4:09
other sum and then i can type in the total here so we can see the total sales amount for the product
4:16
category is 75 million 357 thousand nine pounds now let's go to the power bi data model and achieve
4:25
the same thing again we have all the same tables loaded the fact table and the dimension table
4:32
now we just need to write the dax measure so i'm going to right click and choose new measure
4:37
we calculate the sales amount by product category now we're going to use the amazing calculate
4:44
dax function which is the attribute of performing dax or writing dax formula within the power of
4:50
the data model now just like you know the calculate evaluate an expression in a context that is
4:56
modified by filters okay you can use the sum aggregate function and then i can call the
5:01
the sales amount column that is present in the fact table. So sales amount, right
5:09
And then I'm gonna close that up. Now, comma. So for the filter
5:13
so I'm gonna actually use the filter function. I wanna filter the fact table, comma
5:20
Now for the filter expression, what's the criteria? Now we'll filter the column where we have the product key
5:27
present in the fact table and the product key also present or that exists in the
5:34
the product table So I going to call the product key in the fact table here and then I gonna use the aim and use the values DAX formula and I want a point I
5:45
want to check whether that the simple key exists in the DIMM product column so
5:50
DIMM product so I'm gonna check that and close the brackets and of course we can
5:54
go ahead and commit this DAX formula so let's just click enter so you can see
6:01
our dax measure is created and we can apply currency to format nicely and let's use that
6:09
in our report now i'm going to look for the dean product that is the product category so i'm going
6:16
to grab the product category drop it in the report canvas so we can see the list of the product
6:22
category and then i can go ahead and grab the measure we just created sales amount by pro
6:28
category i'm going to drop it and of course this actually creates a chart the bar chart
6:34
i want to actually switch over to either a table or a matrix let's just use a matrix that's fine
6:41
so we can see this return 75 million 357 thousand nine or pounds or dollars we can change the
6:49
currency and when you check this total with what we achieve in excel you can see we have the same
6:55
out with 75 million 357 thousand nine pounds so let's come to the power bi and check again
7:02
75 million 357 thousand nine dollars per pounds so this is basically how we can use the power bi
7:11
to write a dax formula to calculate the total sales by product category and how we can use
7:18
excel general user interface to calculate the sales amount by product category so i hope you
7:25
enjoyed this video if you do comment like and share with your friends thank you and bye for now cheers
#Advertising & Marketing
#Business Services
#Calculators & Reference Tools
#Consulting
#Public Relations
#Sales
#Web Stats & Analytics