Project

Sales Date Analysis Using Power BI

A step-by-step project to implement a detailed date table for sales analysis using Power Query in Power BI.

Empty image or helper icon

Sales Date Analysis Using Power BI

Description

This project involves creating a detailed date table in PowerQuery using M language, derived from a sales data table in Power BI. The date table is designed to encompass dates three months before and after the sales data date range, supporting up to 20 columns of related date information. This tutorial aims to explore data transformations, building functions in M, and working with dates in Power BI. For all beginners, this project gives an insight into useful elements of Power BI and Power Query. As part of the project, the user can expect to learn how to manipulate and transform data, develop specific functions, and create detailed reports.

Introduction to Power BI and Data Transformation

Setting up the Environment

  • Download and install Power BI Desktop. You can download the latest version from the official Microsoft website.
  • After a successful installation, open Power BI Desktop.

Loading the Data into Power BI

Power BI allows you to load various types of data. For this example, let's assume we're loading data from an Excel file.

  • Click on Home > Get Data > Excel.
  • Navigate to your Excel file, select it, and click Open.

In the Navigator dialog box, you can select the sheets you want to import. Let's assume our Excel file contains a sheet named Sales.

  • Tick the checkbox for the Sales sheet, then click on Load.

Understanding Power Query Editor

After loading your data:

  • Click on Home > Edit Queries. This will open up the Power Query Editor.

Power Query Editor is a data transformation tool built into Power BI which uses a functional language called M (Power Query). This language is mainly used for data extraction and data cleaning.

Basic Data Transformations in Power Query Editor

Assuming our Sales table has some missing data and unwanted columns. Let's remove those.

Removing Null Values

  • Select the column you want to clean.
  • Click on Home > Remove Rows > Remove Empty.

Removing Unwanted Columns

  • Select the column you want to remove.
  • Click Right Click > Remove.

Creating a Date Table

For sales analysis, a date table is typically beneficial. Here's how to create a date table with common attributes such as Year, Quarter, Month, and Day.

  • Click Home > New Source > Blank Query.
  • Within the formula bar, type in the following 'M' code:
= List.Dates(Start= #date(2010, 1, 1), Count= 3652, Step= #duration(1,0,0,0))
  • Now, we convert this list to a table. Click on To Table.
  • Next, we expand the column by clicking on the expand icon in the column header.

Now, let's extract common attributes from the date column:

  • Select the Date column.
  • Click Add Column > Date > Year.
  • Repeat the process for Quarter, Month, and Day.

Renaming the Columns (Optional)

To rename columns, right-click on the column and choose Rename.

Loading the Date Table into the Model

  • Click Home > Close & Apply.

Conclusion

Congratulations! You have now learned how to set up Power BI, perform basic data transformations using Power Query Editor, and create a detailed date table for sales analysis. With this knowledge, you should be able to implement practical solutions to real-life scenarios using Power BI.

Project Implementation: Understanding Sales Data Structure

Let's delve right into understanding the structure of your sales data. We'll first import the data into Power Query before exploring its features. Assume the data is saved in Excel (i.e., "Sales_Data.xlsx").

1. Importing Sales Data

let Source = Excel.Workbook(File.Contents("C:\Path\to\your\Sales_Data.xlsx"), null, true), Sales_Data_Sheet = Source{[Item="SalesData",Kind="Sheet"]}[Data], #"Promoted Headers" = Table.PromoteHeaders(Sales_Data_Sheet, [PromoteAllScalars=true]), #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Order Date", type date}, {"Order ID", Int64.Type}, {"Product", type text}, ... in #"Changed Type"

This piece of M code reads an Excel file from your local computer, and promotes the first row as headers.

2. Understanding Sales Data Structure

With the sales data successfully imported, it's important to understand its structure. This typically involves identifying the variables (columns) present and the type of data they hold.

In the 'Transform' tab, you can have an overview of your data and also modify the data type of each column if needed.

3. Normalizing Data

Ensure that the data is in a 'table' or 'list' format where each row is an entity (e.g., a sale) and every column is an attribute of that entity (like sales ID, product name, etc.).

let Source = Excel.Workbook(File.Contents("C:\Path\to\your\Sales_Data.xlsx"), null, true), Sales_Data_Sheet = Source{[Item="SalesData",Kind="Sheet"]}[Data], #"Promoted Headers" = Table.PromoteHeaders(Sales_Data_Sheet, [PromoteAllScalars=true]), #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Promoted Headers", {"SalesData"}, "Attribute", "Value") in #"Unpivoted Columns"

In this block, we unpivot the table using the default 'Unpivot Columns' option in Power Query so the data is structured correctly.

4. Treating Missing Values

Missing data can lead to serious problems while analyzing data. For this, you may like to delete those rows, fill with a specific value, or use data imputation methods. Here is how to remove rows with null values.

let Source = Excel.Workbook(File.Contents("C:\Path\to\your\Sales_Data.xlsx"), null, true), Sales_Data_Sheet = Source{[Item="SalesData",Kind="Sheet"]}[Data], #"Promoted Headers" = Table.PromoteHeaders(Sales_Data_Sheet, [PromoteAllScalars=true]), #"Removed Nulls" = Table.SelectRows(#"Promoted Headers", each not List.Contains(Record.FieldValues(_), null)) in #"Removed Nulls"

In the above code, we've removed any rows that contain at least one null value using the 'Remove Rows' => 'Remove Blank Rows' function.

At this point, you have successfully imported and cleaned your sales data. Next steps would involve closer examination of data fields based on requirement and performing operations as needed. Remember, Power Query M language is strong and can handle a variety of data transformation tasks. Keep exploring!

Detailed Date Table for Sales Analysis Using Power Query in Power BI

Now that we've understood how to transform data and know the structure of the sales data to our advantage, let's move on to using Power Query and the M Language in Power BI to generate a detailed date dimension table to aid in our sales analysis.

Step 1: Loading Sales Data into PowerBI

Before creating a date table, let's load the sales data into PowerBI and examine its structure. Ensure that the sales date is in the correct format.

let
    Source = Excel.Workbook(File.Contents("C:\YourFilePath\SalesData.xlsx"), null, true),
    SalesData_Sheet = Source{[Item="SalesData",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(SalesData_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Date", type date}})
in
    #"Changed Type"

Note, in the path of your file make sure to use \\ or \ instead of /. You will see something like "C:\\YourFilePath\\SalesData.xlsx".

Step 2: Creating Date Dimension Table

Let's generate a date table layer for every unique date in our sales data.

let
    Source = Excel.Workbook(File.Contents("C:\YourFilePath\SalesData.xlsx"), null, true),
    SalesData_Sheet = Source{[Item="SalesData",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(SalesData_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Date", type date}}),
    DateList = List.Distinct(#"Changed Type"[Date]),
    #"Converted to Table" = Table.FromList(DateList, Splitter.SplitByNothing())
in
    #"Converted to Table"

This script is similar to one you would use to load data, but it includes two additional steps. List.Distinct(#"Changed Type"[Date]) produces a distinct list of all dates in the sales data, and Table.FromList(DateList, Splitter.SplitByNothing()) transforms this list into a table, which you may then load in PowerBI.

Step 3: Expanding Date Dimension

Next, expand upon this basic date dimension table to add more columns that assist in data analysis. Elements such as 'Year', 'Quarter', 'Month', etc. can be helpful.

let
    Source = Excel.Workbook(File.Contents("C:\YourFilePath\SalesData.xlsx"), null, true),
    SalesData_Sheet = Source{[Item="SalesData",Kind="Sheet"]}[Data],
    #"Promoted Headers" = Table.PromoteHeaders(SalesData_Sheet, [PromoteAllScalars=true]),
    #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Date", type date}}),
    DateList = List.Distinct(#"Changed Type"[Date]),
    #"Converted to Table" = Table.FromList(DateList, Splitter.SplitByNothing()),
    #"Renamed Columns" = Table.RenameColumns(#"Converted to Table",{{"Column1", "Date"}}),
    #"Inserted Year" = Table.AddColumn(#"Renamed Columns", "Year", each Date.Year([Date]), type number),
    #"Inserted Quarter" = Table.AddColumn(#"Inserted Year", "Quarter", each Date.QuarterOfYear([Date]), type number),
    #"Inserted Month" = Table.AddColumn(#"Inserted Quarter", "Month", each Date.Month([Date]), type number),
    #"Inserted Day of Week" = Table.AddColumn(#"Inserted Month", "DayOfWeek", each Date.DayOfWeek([Date]), type number),
    #"Inserted Week of Year" = Table.AddColumn(#"Inserted Day of Week", "WeekOfYear", each Date.WeekOfYear([Date]), type number)
in
    #"Inserted Week of Year"

The 'Table.AddColumn()' function allows you to add new columns to your table with syntax:

Table.AddColumn(table as table, newColumnName as text, columnGenerator as function, optional columnType as nullable type) as table.

The 'each' keyword helps iterate over rows and make transformations. 'Date.WeekOfYear([Date])', for instance, works by extracting the week number from dates.

Step 4: Loading Date Table in Power BI

The final step is loading your newly created date table in Power BI. With this date dimension easily accessible, it can be combined with the sales data in multiple ways to provide detailed analytics.

This creates a dimension table filled with dates that we can use for analysing the sales data. It's important to note that Microsoft M is a case-sensitive language.

Creating Date Tables in Power Query

Step 1: Create a Blank Query

  • Begin by creating a new blank query. To do this, go to Home -> New Source -> Blank Query.

Step 2: Generate Dates

  • After the blank query has been created, we will insert a formula to generate a list of dates.

= List.Dates(#date(2010, 1, 1), Duration.Days(DateTime.Date(DateTime.LocalNow())-#date(2010, 1, 1)), #duration(1, 0, 0, 0))

  • This formula will generate a list of dates starting from January 1, 2010, to today's date. You can adjust the start date as per your own requirements.

Step 3: Convert List to Table

  • Now, we have to convert this list of dates into a table format. For doing this, go to List Tools -> Transform Menu -> To Table.

Step 4: Define Data Type

  • After the table has been generated, we will define the data type for our "Column1". Do this by going to Home -> Transform -> Date.

Step 5: Adding Additional Date Fields

  • Now, we will generate additional columns for our date fields. Such as Year, Month, Day, Quarter etc. To perform this, right-click on your Column1 -> Transform -> select any fields per your requirement.

For example, if you choose Year, Month and Quarter, you would get three additional columns.

Step 6: Naming Columns

We will now rename our columns for ease of identification.

  1. Rename "Column1" to "Date"
  2. Rename "Year.Column1" to "Year"
  3. Rename "Month.Column1" to "Month"
  4. Rename "Quarter.Column1" to "Quarter"

Step 7: Reorder Columns

If you like to rearrange your columns, you can simply select any column and drag it any position you like.

Step 8: Load to Data Model

  • Our date table is now ready. To load this data into your data model, click on Home -> Close and Load.

Step 9: Set as Date Table

This is an optional step. If you like, you can mark your Table as a Date Table.

  • Right-click on your Table -> Mark as Date Table. Then choose your "Date" column as Date column.

So, that's it! You have just created a date table using Power Query in Power BI.

Manipulating Date Ranges and Utilizing Power M Functions in Power BI

In this section, the date range of the sales data will be manipulated and Power M functions will be utilized for a more detailed analysis. Here's the step-by-step process:

1. Load the Sales Data into Power Query

let
  Source = Excel.Workbook(File.Contents("C:\YourFilePath\SalesData.xlsx"), null, true),
  SalesData_Sheet = Source{[Item="SalesData",Kind="Sheet"]}[Data],
  #"Promoted Headers" = Table.PromoteHeaders(SalesData_Sheet, [PromoteAllScalars=true]),
  #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Order Date", type date}})
in
  #"Changed Type"

2. Expand the Date Range

If your sales data does not include every day within the range (e.g., weekends or holidays), you may want to manually expand this range using Power Query. It can be done with the use of Lists.

let
  Source = Excel.Workbook(File.Contents("C:\YourFilePath\SalesData.xlsx"), null, true),
  SalesData_Sheet = Source{[Item="SalesData",Kind="Sheet"]}[Data],
  #"Promoted Headers" = Table.PromoteHeaders(SalesData_Sheet, [PromoteAllScalars=true]),
  #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Order Date", type date}}),
  MinDate = List.Min(#"Changed Type"[Order Date]),
  MaxDate = List.Max(#"Changed Type"[Order Date]),
  DateList = {Number.From(MinDate)..Number.From(MaxDate)},
  #"Converted to Table" = Table.FromList(DateList, Splitter.SplitByNothing()),
  #"Renamed Columns" = Table.RenameColumns(#"Converted to Table",{{"Column1", "Date"}}),
  #"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"Date", type date}})
in
  #"Changed Type1"

3. Join The Expanded Date Range with the Sales Data

After expanding the date range, the expanded date range table needs to be joined with sales data based on the 'Order Date' field.

let
  Source = Excel.Workbook(File.Contents("C:\YourFilePath\SalesData.xlsx"), null, true),
  SalesData_Sheet = Source{[Item="SalesData",Kind="Sheet"]}[Data],
  #"Promoted Headers" = Table.PromoteHeaders(SalesData_Sheet, [PromoteAllScalars=true]),
  #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Order Date", type date}}),
  MinDate = List.Min(#"Changed Type"[Order Date]),
  MaxDate = List.Max(#"Changed Type"[Order Date]),
  DateList = {Number.From(MinDate)..Number.From(MaxDate)},
  #"Converted to Table" = Table.FromList(DateList, Splitter.SplitByNothing()),
  #"Renamed Columns" = Table.RenameColumns(#"Converted to Table",{{"Column1", "Date"}}),
  #"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"Date", type date}}),
  #"Merged Queries" = Table.NestedJoin(#"Changed Type",{"Order Date"},#"Changed Type1",{"Date"},"NewColumn",JoinKind.LeftOuter),
  #"Expanded NewColumn" = Table.ExpandTableColumn(#"Merged Queries", "NewColumn", {"Sales"})
in
  #"Expanded NewColumn"

4. Use Power M Function to Create New Metrics

Power M functions can be used to create new metrics for the dataset.

let
  Source = Excel.Workbook(File.Contents("C:\YourFilePath\SalesData.xlsx"), null, true),
  SalesData_Sheet = Source{[Item="SalesData",Kind="Sheet"]}[Data],
  #"Promoted Headers" = Table.PromoteHeaders(SalesData_Sheet, [PromoteAllScalars=true]),
  #"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Order Date", type date}}),
  MinDate = List.Min(#"Changed Type"[Order Date]),
  MaxDate = List.Max(#"Changed Type"[Order Date]),
  DateList = {Number.From(MinDate)..Number.From(MaxDate)},
  #"Converted to Table" = Table.FromList(DateList, Splitter.SplitByNothing()),
  #"Renamed Columns" = Table.RenameColumns(#"Converted to Table",{{"Column1", "Date"}}),
  #"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"Date", type date}}),
  #"Merged Queries" = Table.NestedJoin(#"Changed Type",{"Order Date"},#"Changed Type1",{"Date"},"NewColumn",JoinKind.LeftOuter),
  #"Expanded NewColumn" = Table.ExpandTableColumn(#"Merged Queries", "NewColumn", {"Sales"}),
  #"Added Custom" = Table.AddColumn(#"Expanded NewColumn", "Cumulative Sales", each List.Sum(Table.SelectRows(#"Expanded NewColumn", each [Date] <= _[Date])[Sales])),
  #"Changed Type2" = Table.TransformColumnTypes(#"Added Custom",{{"Cumulative Sales", type number}})
in
  #"Changed Type2"

This script adds a new column 'Cumulative Sales'. The function List.Sum() calculates the sum conditionally, where the condition is applied via the Table.SelectRows function.

This illustrates manipulating date ranges and utilizing Power M functions in Power BI.

Splitting Dates into Components (Day, Month, Year)

Given that a Date table has been created in the previous steps and understanding the basic structure of M Language, I will focus on demonstrating how to add new columns which will correspond to Year, Quarter, Month, Week, and Day.

Adding Year Column

Use Table.AddColumn function to add a new Year column. The second parameter in this function is the new column name, and the third parameter is an expression that defines the column's values. Here we use Date.Year:

DateTable = Table.AddColumn(DateTable, "Year", each Date.Year([Date]), Int64.Type)

Adding Quarter Column

To add the Quarter column, use the Date.QuarterOfYear function:

DateTable = Table.AddColumn(DateTable, "QuarterOfYear", each Date.QuarterOfYear([Date]), Int64.Type)

Adding Month Column

To add a Month column (number) to the table, use the Date.Month function:

DateTable = Table.AddColumn(DateTable, "MonthOfYear", each Date.Month([Date]), Int64.Type)

Adding Month Name Column

To have a column with the Name of the Month, you can use the Date.ToText function and specify the format of the date, to only show the Month:

DateTable = Table.AddColumn(DateTable, "MonthName", each Date.ToText([Date], "MMMM"), Text.Type)

Adding Week Column

To assign a Week number in the year for each date, use Date.WeekOfYear function:

DateTable = Table.AddColumn(DateTable, "WeekOfYear", each Date.WeekOfYear([Date]), Int64.Type)

Adding Day Column

Finally, add the Day column which can be achieved using Date.Day function:

DateTable = Table.AddColumn(DateTable, "DayOfMonth", each Date.Day([Date]), Int64.Type)

Insert each of these steps one at a time into the Advanced Editor. By building up your transformations step-by-step, you are less likely to make mistakes and it's easier to fix mistakes, since Power Query will create more checkpoints that you can revert back to.

Don't forget the syntax of the "each" keyword. The following are equivalent:

  • each Date.Month([Date])
  • (row) => Date.Month(row[Date])

The "each" keyword is just a shorthand.

After applying all these transformations, your Date table is completely ready for your further data analysis. You can now connect your Sales table to the Date table through a relationship and start your deep dive into time series analysis!

Finalizing and Loading the Date Table into Power BI

In this section, we will finalize our date table that we have created with Power Query in the previous sections and load it into Power BI. In the previous steps, we've already explored the sales data structure, created and manipulated date ranges using M functions and added some date-related columns into our table.

Finalizing Date Table

Lastly, make sure that all columns in the date table are appropriately formatted. The dates should be in date format, and the numerical columns should be in numerical format in Power Query.

To ensure this:

  1. Click on the column header in Power Query to select a column.
  2. Find the "Transform" tab in Power Query editor. In the transform tab, select the correct data type for the column.

Repeat the above steps for each of the columns in the date table.

Loading the Date Table in Power BI

Once you're happy with the structure of your date table, you can load it into Power BI. Follow these steps:

  1. Click "Close & Apply" on the Home Tab in your Power Query editor.

This will load your date table into Power BI. Once the data loading process completes, your date table should now be visible in the "Fields" pane of the Power BI desktop.

Joining Date Table with Sales Data

Now that our date table is loaded into Power BI, we need to join it with sales data to perform meaningful analysis.

To do this:

  1. Go to Power BI Desktop Data View.
  2. Click on the "Relationships" view in the left pane. Here, you should be able to see all your tables represented as blocks.
  3. If the relationship between the Date Table and Sales Data isn't automatically formed, form it manually. Click on the date column in Sales and drag it to connect it to the date table's date column.

Now our date table is fully incorporated into Power BI, and it can be used to create interactive visuals and perform in-depth time series analysis.

Using Date Table in Power BI

Your date table is now ready to use in Power BI.

For instance, to use the date table to analyse sales by year, follow these steps:

  1. Go to Report View in Power BI.
  2. Click on any particular visual. For example, if you want to create a bar chart, click on the bar chart icon.
  3. Drag the "Sales" field to Values area in the Visualizations pane.
  4. Drag the "Year" field from your Date Table to Axis area in the Visualizations pane.

In conclusion, now that the date table is loaded into Power BI and joined with our base sales data, we can perform all sorts of time-based analysis, thanks to all the additional columns like Year, Month, Quarter, etc. that we've added earlier in Power Query.

Power BI Reporting and Further Steps

Now that we have prepared our detailed date table, we can utilize it to create meaningful visualizations and insights in Power BI. Let's dive right into creating a report that will fetch distinct features of our sales data.

Section 1: Creating a Sales Summary Report

Once you've loaded the date table into Power BI, the next step is to generate reports using this data. In this section, we'll create a sales summary report using measures.

The first thing we create are the measures to indicate total sales, accurate to date. Following these steps:

  1. Go to the 'Modeling' tab and select 'New Measure'.
  2. Enter the formula for the measure. Let's say we want to create a measure for Total Sales, the formula would be:
Total Sales = SUM('TableName'[SalesAmount])

Here, replace 'TableName' with your actual table name.

  1. Hit enter. The measure will be created.

In the same way, you can add additional measures according to your requirements.

To display these measures, we can utilize various visuals in Power BI. For example, let's use a 'Card' visual to display the Total Sales.

  1. From the 'Visualizations' pane, select 'Card'.
  2. In the 'Values' area of the visual, add our 'Total Sales' measure.

Repeat the steps for the remaining measures. Arrange them in the report as required.

Section 2: Creating a Sales By Month Report

Now, let's create a report that shows the Total Sales by Month. This will allow us to see the distribution of sales throughout different months.

  1. From the 'Visualizations' pane, select the 'Column chart' visual.
  2. Drag and drop the 'MonthYear' field from your date table into the 'Axis' field well.
  3. Drag and drop 'Total Sales' measure into the 'Values' field well.
  4. Sort the visual by the 'MonthYear' field.

This will create a column chart of total sales by month.

Section 3: Creating a Sales By Year and Month Report

We can create a matrix that will show us sales by year and month.

  1. From the 'Visualizations' pane, select 'Matrix'.
  2. Drag and drop the 'Year' and 'MonthName' fields from your date table into the 'Rows' field well.
  3. Drag and drop 'Total Sales' measure into the 'Values' field well.
  4. Now, customize the matrix as required. You can move the 'MonthName' to the Columns field well to get a more structured view.

We have now created a Sales Summary report, a Total Sales By Month report, and a Sales By Year and Month matrix. Each visual provides a different perspective of your data, giving you a comprehensive analysis of sales.

Further Steps

This practical implementation ends here with the formulation of reports. However, there are limitless opportunities with Power BI. You can enhance the interactivity of your reports with slicers, introduce more complex measures with DAX, create more detailed tables with Power Query and M language, or even use AI insights provided by Power BI to predict future sales. All of these steps would require an understanding of the respective tools and language, so consider advancing your skills in these areas for further exploration of the platform. Each of these tasks expands your report's potential and allows for deeper and richer insights into your data.