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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! 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
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.