Mquery can sometime seem alien to us and we prefer creating Mquery with the drag and drop features within Power BI. However after reading this you will soon realize that Mquery have much more to offer if you learn how to write it. Also my aim here is to make this concept as beginner friendly as possible, so that you can actually go ahead and use these steps in your projects whenever required. So lets jump into it!
What is Mquery?
In a nutshell, Mquery is a programming language used by Power Query engine for data transformation. It has a versatile toolkit that lets you manipulate, clean, and refine your data. With MQuery, you can perform a range of tasks, like filtering, merging different data sources, reformatting data or in this case create data out of thin air.
What will you learn?
We will focus on writing Mquery in formula bar and creating a Date column using Mquery which can be static or dynamic and contains dates ranges of our choice. We will not use any data input for this and will be done completely with just Mquery formula.
Requirement
- Create a DATE table which contains 1 column of dates. Ranging from 1st Dec 2023 to 10th Dec 2023
- Create another DATE table which contains 1 column of dates, starting from 1st Jan 2022. However this column needs to be dynamic, which means at every day this table needs to add an additional row for the current date.
Solution #1 – Static Date Column using Mquery
Lets begin creating our first table which will have a static list of dates ranging from 1/12/2023 till 10/12/2023. So fire up your Power BI Desktop.
- From the main Power BI window, click on Get Data button and then click on Blank Query
![Creating Blank Query in Power Bi](https://static.wixstatic.com/media/e3da6a_ab9d64db7f374070a104468a73b2c1d4~mv2.png/v1/fill/w_1110,h_561,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/e3da6a_ab9d64db7f374070a104468a73b2c1d4~mv2.png)
We are using a blank query because we dont have a external data input, and we will solely create this table using only Mquery.
You will now see the power query editor, and a new query created at the left panel. We will now start writing the Mquery in the formula bar at the middle
![Blank Query in Power Query Editor](https://static.wixstatic.com/media/e3da6a_13bdf31eea9a4c859e239b54fac09b7e~mv2.jpg/v1/fill/w_1110,h_504,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/e3da6a_13bdf31eea9a4c859e239b54fac09b7e~mv2.jpg)
- Now the first thing we need is to have a starting date, which we will use as a reference to calculate remaining entries. In this case our starting date is “1/1/2023”. So to define a date in Mquery, you need to enter the below function in the formula bar:
=#date(2023, 12, 1)
When we define a date in Mquery, the syntax starts with #date
Now you must notice that a date value has been created and the step is automatically named as “Source” in the applied steps section
![Date Mquery in Power Query](https://static.wixstatic.com/media/e3da6a_b48152a924dc4f11a30081bef80eee77~mv2.jpg/v1/fill/w_1110,h_413,al_c,q_85,usm_0.66_1.00_0.01,enc_auto/e3da6a_b48152a924dc4f11a30081bef80eee77~mv2.jpg)
- Now click on the “fx” button in the formula bar section and enter below formula
![Adding a new step in Power Query](https://static.wixstatic.com/media/e3da6a_0b6e20519f5f49549a813df32f2b4529~mv2.png/v1/fill/w_765,h_313,al_c,lg_1,q_85,enc_auto/e3da6a_0b6e20519f5f49549a813df32f2b4529~mv2.png)
- You will see “=Source”, remove that and instead enter below formula
= List.Dates(Source, 10, #duration(1,0,0,0))
Formula Explanation:
Now don’t get worried with above formula, I will explain it you in simple terms. Basically List.Dates create a list of dates with few parameters. i.e. Start Date, Count of Rows & Duration Interval.
- So obviously for Start Date we using Source, which we set earlier i.e. 1/12/2023.
- For Count of Rows, since we need dates till 10/12/2023, we gave it 10 for adding 10 rows
- Lastly the duration which starts with #duration, this parameter helps us determine the duration between the current row & the next row. Since we want each row to be incremented by 1 day, we simply gave it (1,0,0,0). Which mean 1 day, 0 hours, 0 minutes & 0 seconds.
As of now this has just created a list, but the requirement said to create a table. So to create a table, follow below steps:
- Go to List Tools >> Transform
- Click on “To Table”
![Transforming list to a table](https://static.wixstatic.com/media/e3da6a_4c6df22d77244d0bbb0ec526944ef785~mv2.png/v1/fill/w_1029,h_282,al_c,lg_1,q_85,enc_auto/e3da6a_4c6df22d77244d0bbb0ec526944ef785~mv2.png)
- Since we are dealing with date values, you can leave the next prompt as default and press OK.
![transforming list to a table in power query](https://static.wixstatic.com/media/e3da6a_2d159d36d42c4d65a44e41a0ea521201~mv2.png/v1/fill/w_880,h_403,al_c,lg_1,q_85,enc_auto/e3da6a_2d159d36d42c4d65a44e41a0ea521201~mv2.png)
So finally click on “Close & Apply” in the home tab and you have successfully created a new static table with Dates from 1/1/2023 to 10/12/2023.
Great, now lets move on to the next requirement!
Solution #2 – Dynamic Date Column using Mquery
For this, we will again create a blank query i.e. Get Data >> Blank Query and then create the begining date using #date. But remember this time the start date is 1/1/2022. So enter the below function in the formula bar:
= #date(2022,1,1)
Now again click on the “Fx” button besides the formula bar and enter a new steps. And now enter the below formula in the formula bar:
= List.Dates(Source, Number.From(DateTime.LocalNow()) - Number.From(Source), #duration(1,0,0,0))
Formula Explanation:
Similar to before List.Dates is creating a list of dates, and then the first parameter is Source like earlier. However if you remember this requirement was to make this dynamic, so in the row count parameter we need some different magic!
In the middle parameter of count, we are simply first getting today’s date using “DateTime.LocalNow(), and then converting that to a number, because this parameter only accepts integer values. Then we are getting the difference between integer values of Source date and Today’s date.
Finally the #duration parameter is exactly same as earlier and is incremeting each row by 1 day.
![parameters used in List.Date mquery](https://static.wixstatic.com/media/e3da6a_075adade7bda46089c1715710662c3d0~mv2.png/v1/fill/w_714,h_282,al_c,lg_1,q_85,enc_auto/e3da6a_075adade7bda46089c1715710662c3d0~mv2.png)
Finally you should have a list of dates which now needs to be converted to a table to complete our requirement. So similar to earlier, follow below steps:
- Go to List Tools >> Transform
- Click on “To Table”
- Then in the next prompt, keep everything as default and press ok