Solved

Adding previous month name to output filename (python)

  • 3 October 2017
  • 7 replies
  • 60 views

Badge

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?

icon

Best answer by lenaatsafe 3 October 2017, 18:26

View original

7 replies

Userlevel 2
Badge +16

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).

Badge

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
Userlevel 4

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'
Badge

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)

 

Userlevel 4

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!
Badge

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...

 

Badge

Thanks all for the answers! Much appreciated :)

Reply