Skip to Content

Power BI: How to Integrate Dynamic Date Parameters into Your SQL Queries

Enhance Flexibility in Power BI with Dynamic Date Parameters and SQL Integration
November 15, 2024 by
Power BI: How to Integrate Dynamic Date Parameters into Your SQL Queries
ML Solutions, Marie-Lou Mailloux-Desrochers
| No comments yet

Introduction

In the previous article, we explored how automating SQL analyses with stored procedures can simplify and accelerate the process of obtaining actionable insights. With a well-designed stored procedure, we were able to centralize calculation logic and automate data extraction directly from SQL Server. This not only reduced processing time but also improved data consistency by avoiding repeated manual adjustments.

In this article, we’ll go further by integrating dynamic parameters in Power Query to enhance the flexibility of our Power BI report. Using the [dbo].[GetSalesByRegion] stored procedure we created earlier, we’ll show how to set up date parameters to easily adjust the time period of the data, while retaining the power and efficiency of SQL automation.


1. Running the Stored Procedure in SSMS

The first step is to execute the [dbo].[GetSalesByRegion] stored procedure in SQL Server Management Studio (SSMS) to retrieve sales data by region. This procedure takes two date parameters (@StartDate and @EndDate), allowing you to filter sales by the desired period.

For this example, we’ll use the year 2012, which is one of the years available in the AdventureWorks2022 database. Here’s how to execute the procedure for this period:





In this example, the procedure returns sales by region for the entire year of 2012. You can adjust the values of the @StartDate and @EndDate parameters to explore different periods.

Instead of simply copying the query results, we’ll copy the query itself and use it as a data source in Power BI. This approach will allow us to have a direct connection to SQL data and make the process even more dynamic and flexible.

Now let’s move on to setting up this source in Power BI.


2. Importing the Query into Power Query

Once the stored procedure is validated in SSMS, we will now copy the query and use it directly in Power BI as an SQL data source.

2.1 Copy the Query in SSMS: In SSMS, select this part of the query, then copy it

  


2.2 Create a New SQL Source in Power BI:
  • In Power BI Desktop, go to Home > Get Data > SQL Server

  • In the Server Name field, enter your SQL server name.
  • In Advanced options, paste this part of the copied SQL query into the SQL statement field. 


2.3 Connecting and Loading Data

Click OK to establish the connection. Power BI will execute this query directly in SQL Server and load the result into your data model.

We have now integrated the stored procedure as a data source in Power BI, which allows us to leverage the full flexibility of the SQL query without manually handling the results.


3. Creating Date Parameters in Power Query

To make our SQL query even more flexible, we’ll create two parameters in Power Query: StartDate and EndDate. These parameters will make it easy to filter the data by period without directly modifying the SQL query.

3.1 Open Power Query Editor:

Once the SQL connection is established in Power BI, click Transform Data to open the Power Query editor.

Create StartDate and EndDate Parameters:

  • In the Power Query editor, go to Home > Manage Parameters > New Parameter


  • Name the first parameter StartDate, set its Type as Date, and choose a Current Value  (for example, 2012-01-01). 

  • Repeat the process for the EndDate parameter, setting a Current Value of 2012-12-31


3.2 Modify the Query in the Advanced Editor:

Once the parameters are created, go to Power Query's Advanced Editor to access the SQL query. 


  • Modify the query to replace static dates with the dynamic StartDate and EndDate parameters. Here’s an example syntax:


let

    StartDateText = Text.From(StartDate),

    EndDateText = Text.From(EndDate),

    SqlQuery = "EXEC [dbo].[GetSalesByRegion] @StartDate = '" & StartDateText & "', @EndDate = '" & EndDateText & "'",

    Source = Sql.Database("<NOMDUSERVERSQL>", "AdventureWorks2022", [Query=SqlQuery])

in

    Source


This approach integrates Power Query parameters into the SQL query, making the sales period adjustable directly in Power BI.


3.3 Apply Changes:

Click Done in the Advanced Editor, then apply the changes to see the filtered data in Power BI based on the selected dates.


4. Applications and Benefits of Dynamic Parameters in Power BI

Integrating dynamic parameters like StartDate and EndDate in Power BI offers many benefits in terms of flexibility and data management. Here are a few practical applications and the benefits they provide:

  • Increased Flexibility for Users: With dynamic parameters, users can easily select different periods in Power BI without needing to modify the SQL query directly. This simplifies the experience and allows data exploration from different time perspectives.
  • Up-to-Date Reports and Analyses: By simply adjusting the dates in Power Query, you can update the data on a regular and targeted basis for monthly, quarterly, or annual reports. No need to return to SQL Server Management Studio for each modification!
  • Optimized Period Management: This approach is especially useful for financial, sales, or other time-sensitive analyses. End users can switch between different periods with a few clicks, making Power BI reports much more interactive.
  • Simplified Report Maintenance: By integrating date parameters directly in Power Query, reports can evolve without requiring a complete restructuring. If a new period is added, simply update the dates in Power BI, ensuring great flexibility.

Combining Power Query parameters with SQL stored procedures provides a robust, efficient, and easy-to-maintain data model that adapts to the company’s real-time needs.


Conclusion

In this article, we demonstrated how to use dynamic parameters in Power Query to enhance a Power BI report. By integrating a SQL stored procedure directly into Power BI and adding date parameters, we created a flexible and interactive data model that allows precise analysis of sales by region. This approach simplifies report maintenance and enhances the user experience by offering full control over the analysis period.

In our next article, we’ll stay within the Power BI realm with an equally practical topic: creating basic measures to analyze sales. We’ll cover how to easily calculate key metrics like total sales, growth percentage, and average sales for clear and impactful reports.

Sign in to leave a comment