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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It 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
PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors