Skip to main content
Solved

Adding previous month name to output filename (python)


clairestoddart
Contributor
Forum|alt.badge.img

I am looking for a way to add the previous months name to the filename of an output Excel e.g. Filename_August.xlsx.

I have been trying to do this with Python (I am a Python beginner) as a Private Parameter using the following:

from datetime import date, timedelta
first_day_of_this_month = date.today().replace(day=1) 
last_day_prev_month = first_day_of_this_month –timedelta(days=1) 
prev_month_name = last_day_prev_month.strftime(“%B”) 
print prev_month_name

This works in python but it fails in FME.

Any ideas?

Best answer by lenaatsafe

Hi @clairestoddart 

you could also wrap @david_r solution into a single expression (to be used in e.g. AttributeCreator or as Destination Microsoft Excel File Excel Writer parameter). The expression would be:

@Value(_filename)_@DateTimeFormat(@DateTimeAdd(@DateTimeNow(),-P1M),%B)

 

View original
Did this help you find an answer to your question?

7 replies

erik_jan
Contributor
Forum|alt.badge.img+17
  • Contributor
  • October 3, 2017

If you could do this using transformers, it would be like this:

TimeStamper (date format ^m) to get the current month.

AttributeValueMapper to map current month to previous month (eg: 01 > December, 02 > January etc).

Then use that attribute to do a Fanout (use the Text Editor on the output filename to add the attribute).


Forum|alt.badge.img
  • October 3, 2017

You were close to the solution, you just need to return the prev_month_date value.

Here's working code in a published parameter.

from datetime import date, timedelta
first_day_of_this_month = date.today().replace(day=1) 
last_day_prev_month = first_day_of_this_month - timedelta(days=1) 
prev_month_name = last_day_prev_month.strftime('%B'return prev_month_name

david_r
Evangelist
  • October 3, 2017

If you want to use Python, I like @larry's solution.

Here's one using transformers (you need FME 2017 or newer for this to work, if not go for the solution outlined by @erik_jan), you can then use _result_datetime as part of the fanout.

0684Q00000ArKepQAF.png

Result:

`_result_datetime' has value `September'

Forum|alt.badge.img
  • Best Answer
  • October 3, 2017

Hi @clairestoddart 

you could also wrap @david_r solution into a single expression (to be used in e.g. AttributeCreator or as Destination Microsoft Excel File Excel Writer parameter). The expression would be:

@Value(_filename)_@DateTimeFormat(@DateTimeAdd(@DateTimeNow(),-P1M),%B)

 


david_r
Evangelist
  • October 3, 2017
lenaatsafe wrote:

Hi @clairestoddart 

you could also wrap @david_r solution into a single expression (to be used in e.g. AttributeCreator or as Destination Microsoft Excel File Excel Writer parameter). The expression would be:

@Value(_filename)_@DateTimeFormat(@DateTimeAdd(@DateTimeNow(),-P1M),%B)

 

Nice!

clairestoddart
Contributor
Forum|alt.badge.img
larry wrote:

You were close to the solution, you just need to return the prev_month_date value.

Here's working code in a published parameter.

from datetime import date, timedelta
first_day_of_this_month = date.today().replace(day=1) 
last_day_prev_month = first_day_of_this_month - timedelta(days=1) 
prev_month_name = last_day_prev_month.strftime('%B'return prev_month_name
@larry Thanks!  Tested this and it works!  Was indeed so close...

 


clairestoddart
Contributor
Forum|alt.badge.img

Thanks all for the answers! Much appreciated :)


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings