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

Reply
Jaxidian
Frequent Visitor

Need a leading zero on a Month with DirectQuery

So I have a table that looks like this:

ID int

[Value] decimal(29,9)
DateForSearching datetime
YearNumber int

MonthNumber int

DayNumber int

HourNumber int

 

I want to do a bunch of visualizations on this data and I need the ability to group them by year-month, year-month-day, and year-month-day-hour. Ideally, those would be represented as (respectively): "2016-12", "2016-12-25", and "2016-12-25 23:00". I can mostly get this done with some new calculated columns (Year-Month = MyTable[YearNumber] & "-" & MyTable[MonthNumber]) except for the leading zero scenarios for something like "2017-01" ends up being "2017-1" (I need the leading zeroes for month, day, and hour).

 

How can I get these leading zeroes from either my integer values or from the datetime value? I just can't find anything that works with DirectQuery. 😞

1 ACCEPTED SOLUTION

Hi @Jaxidian,

First, you should click File -> Options and then Settings -> Options -> DirectQuery, then selecting the option "Allow unrestricted measures in DirectQuery mode" shown in following screenshot. When that option is selected, you can create calculated column and measures.

Capture1.PNG

As I tested, If function can be used in DirectQuery Model. I reproduce your scenario(connect to SQL Server database) and get the expected result. Create a column using th following formula, please see the result in screenshot below.

Year-month = IF('HumanResources vEmployeeDepartment'[Month]<=10,CONCATENATE('HumanResources vEmployeeDepartment'[Year],CONCATENATE("-0",'HumanResources vEmployeeDepartment'[Month])),CONCATENATE('HumanResources vEmployeeDepartment'[Year],CONCATENATE("-",'HumanResources vEmployeeDepartment'[Month])))


Capture2.PNG



Please ckeck if you invoke creating calculated columns and measures as the solution above. If you have any question, please let me know.

Best Regards,
Angelia

View solution in original post

12 REPLIES 12
JimKingPowerBI
Advocate II
Advocate II

I believe that the best solution is the following one that was provided by ebalcaen on 05/31/18.

 

This is much easier, and will work in all situations:

 

We add a zero to the month formula by using the concatenation operator (&). Then we use the Right formula to pick only the first two digits from the right.

 

Year-Month with Trailing Zero on Month:

Year-Month = Year([DateColumn]) & "-" & Right("0" & Month([DateColumn]),2) 

 

ebalcaen
New Member

This is much easier, and will work in all situations:

 

We add a zero to the month formula by using the concatenation operator (&). Then we use the Right formula to pick only the first two digits from the right.

 

Year-Month with Trailing Zero on Month:

Year-Month = Year([DateColumn]) & "-" & Right("0" & Month([DateColumn]),2) 

 

Thank you @ebalcaen , just what i needed!
Any suggestion on how I can skip it if the the date is not yet set, or should I just filter them out in the visualisation? My result is displayed as "-0" now.

BooDaa_0-1684228996311.png

 



Best regards,
Fredrik

Looks like I can't login to my old account 😞  But this should work with nulls and as an alternative to the solution below with the IF statement.
 

 

 

Year-Month = Year([DateColumn]) & switch(Month([DateColumn]),1,"-01",2,"-02",3,"-03",4,"-04",5,"-05",6,"-06",7,"-07",8,"-08",9,"-09",10,"-10",11,"-11",12,"-12")

 

 

Oh yes! IF solved my problem.

 

dateSolvedYearMonth = 
    IF( ISBLANK(AllRequests[dateSolvedLocalSE]), BLANK(), 
    Year(AllRequests[dateSolvedLocalSE]) & "-" & Right("0" & Month(AllRequests[dateSolvedLocalSE]),2)
   )  

 

Thank you again!

Best regards,
Fredrik

This is the one that worked for me.

 

Year-Month = Year([DateColumn]) & "-" & Right("0" & Month([DateColumn]),2) 

 

parry2k
Super User
Super User

 You should able to do it by using if condition

 

Year-Month = 'Calendar'[Year] & "-" & if('Calendar'[Month Number] < 10, "0", BLANK()) & 'Calendar'[Month Number] 


Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

It tells me I cannot use an IF in a DirectQuery report.

I just used the formula on direct query.

 

What is your data source for direct query? Are you using Live COnnection?



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

It's direct-to-Azure SQL Database (no caching/syncing).

I thought "DirectQuery" was synonymous with a "live connection". If that's untrue then my terminology is mixed up.

Hi @Jaxidian,

First, you should click File -> Options and then Settings -> Options -> DirectQuery, then selecting the option "Allow unrestricted measures in DirectQuery mode" shown in following screenshot. When that option is selected, you can create calculated column and measures.

Capture1.PNG

As I tested, If function can be used in DirectQuery Model. I reproduce your scenario(connect to SQL Server database) and get the expected result. Create a column using th following formula, please see the result in screenshot below.

Year-month = IF('HumanResources vEmployeeDepartment'[Month]<=10,CONCATENATE('HumanResources vEmployeeDepartment'[Year],CONCATENATE("-0",'HumanResources vEmployeeDepartment'[Month])),CONCATENATE('HumanResources vEmployeeDepartment'[Year],CONCATENATE("-",'HumanResources vEmployeeDepartment'[Month])))


Capture2.PNG



Please ckeck if you invoke creating calculated columns and measures as the solution above. If you have any question, please let me know.

Best Regards,
Angelia

That solved the problem! And I just deployed the pbix as a Power BI Embedded report and there were no issues with that kind of deployment, either. Thanks for the help!!

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.