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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
Anonymous
Not applicable

Dynamic YYYYMM in Power Query

Dear all,

 

Could you please support below issue:

I have a Power BI getting data from SAP BW. There is select month in parameter (Ex: 202011 for November 2020). I changed this one to a code base on the current date as below:

Currentmonth = Number.ToText(Date.Year(DateTime.Date(DateTime.LocalNow()))) & Number.ToText(Date.Month(DateTime.Date(DateTime.LocalNow())))

However, the result for month from Jan to Sep is only one character. It's not accepted as format of dataset. Could you please advise how can I adjust the code to return the value in 2 character.

 

Current result: 20201, 20202, 20203

Expected result: 202001, 202002, 202003,

 

Thanks much for your support!

1 ACCEPTED SOLUTION
mahoneypat
Microsoft Employee
Microsoft Employee

Please try this change with Text.PadStart

 

= Number.ToText(Date.Year(DateTime.Date(DateTime.LocalNow()))) & Text.PadStart(Number.ToText(Date.Month(DateTime.Date(DateTime.LocalNow()))),2,"0")

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

5 REPLIES 5
mahoneypat
Microsoft Employee
Microsoft Employee

Please try this approach instead

 

= Date.ToText(Date.AddMonths(Date.From(DateTime.LocalNow()), -1), "yyyyMM")

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

Thanks so much!

mahoneypat
Microsoft Employee
Microsoft Employee

Please try this change with Text.PadStart

 

= Number.ToText(Date.Year(DateTime.Date(DateTime.LocalNow()))) & Text.PadStart(Number.ToText(Date.Month(DateTime.Date(DateTime.LocalNow()))),2,"0")

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

Hi Pat,

Thanks for your support.

Could you please advise if I want to create code for last month, how can I adjust this code?

As the previous one, I can minus to 1. But in this new one, it will revert the result is 202100.

let
#"LastMonth"= Number.ToText(Date.Year(DateTime.Date(DateTime.LocalNow()))) & Text.PadStart(Number.ToText(Date.Month(DateTime.Date(DateTime.LocalNow()))-1),2,"0")
in
#"LastMonth"

 

Nguyen_0-1609813759476.png

 

Thanks much!

Hello @Anonymous 

 

you can use this formula for all your data needed (current month, last month, etc)

current month:

Date.ToText(Date.AddMonths(Date.From(DateTime.FixedLocalNow()),-0),"yyyyMM")

Jimmy801_0-1609840793840.png

 

last month

Date.ToText(Date.AddMonths(Date.From(DateTime.FixedLocalNow()),-1),"yyyyMM")

Jimmy801_1-1609840811059.png

etc

 

Copy paste this code to the advanced editor in a new blank query to see how the solution works.

If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

 

 

 

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.