Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Fowmy

Streamlining Power BI Projects with Tabular Editor Macros: Automated Dates Table Creation

In the realm of Power BI development, where efficiency reigns supreme, certain tasks can become time-consuming. Among these, creating a Dates Table stands out as a fundamental yet repetitive process in any data model. In this blog post, we'll delve into a time-saving solution: using Tabular Editor and its Macros feature to automate the generation of a Dates Table script, ensuring efficiency and consistency across your Power BI projects.

 

Creating and Saving the Dates Table Macro

  • Launch Tabular Editor, whether you're using version 2 or 3, and open a new C# Script Editor.

Fowmy_0-1706128207420.png

 

  • Utilize the following Dates Table script based on DAX. Paste this script on the editor and save it as a Macro under the C# Script menu..

 

 

 

var datestable = 

@"ADDCOLUMNS(
   CALENDAR(""1/1/2023"",""31/12/2023""),
   ""Month No"" , MONTH([Date]),
   ""Month Name"" , FORMAT( [Date] , ""Mmmm"" ),
   ""Year"" , YEAR([Date]),
   ""Month Year No"" , (YEAR([Date]) * 100) + MONTH	([Date]),
   ""Month Year"" , FORMAT( [Date] , ""Mmm yyyy""),
   ""Quarter"" , QUARTER([Date]),
   ""Year Qtr"" , FORMAT( [Date] , ""YYYY \QQ""),
   ""Week Day"" , WEEKDAY([Date],2),
   ""Week"" , FORMAT( [Date] , ""Dddd"" )

)";

Model.AddCalculatedTable("Dates", datestable );​

 

 

 

 

  • Under C# Script Menu Tab, click on “Save as Macro” button to save the script as a Macro which can be used for future projects.

Fowmy_2-1706128207467.png

 

  • Give your macro a name and associate it with the "Model" under the Macro Context section

 

Fowmy_7-1706129021749.png

 

  • Access the Macros Pane under the View tab to see your Dates Macro readily available.

 

Fowmy_6-1706129004357.png

  • The Dates Macro is now available in Tabular editor which can be used for any Tabular model.
    Fowmy_5-1706128996073.png

 

Applying the Dates Table Macro in Power BI:

 

  • Open a Power BI file in need of a Dates Table and launch Tabular Editor through External Tools.

 

Fowmy_4-1706128987949.png

  • In the Macro pane, double-click on the Dates Macro to populate the script. Make any necessary modifications, such as adjusting the date range., for example., I can change the date range to suit my model fact table. I will change the date range to  1/1/2013 to 31/12/2014

 

Fowmy_3-1706128980145.png

 

  • Execute the macro by clicking the Run button and save the changes back to the model.

 

Fowmy_2-1706128971236.png

 

  • Switch back to the Power BI file, and you'll witness the seamless addition of the Dates Table to your model.

    Fowmy_1-1706128960900.png

 

  • Explore the Data view of the newly added table to confirm its integration.

 

Fowmy_0-1706128944333.png

 

 

In conclusion, this method streamlines your Power BI projects by automating the creation of a Dates Table. With just a few clicks, you can consistently implement this essential element, saving valuable time and ensuring uniformity across your data models. By harnessing the power of Tabular Editor Macros, you're not only enhancing efficiency but also fostering a more organized and standardized approach to Power BI development.