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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

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
Employee
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
Employee
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
Employee
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
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

Check out the May 2024 Power BI update to learn about new features.

Top Solution Authors