Hi @RapplK , something like this could do:
When working with Dates, or rather manipulating Dates, it’s best to work with the Dates nodes rather than manually moving days.
The logic I used to get the last day of previous month is to simply take current date minus the current day. So if today is 2022-03-14, the current day is 14, and if you do 2022-03-14 - 14 days, you get the last day of the previous month.
Elaborating the workflow:
First, I create a variable where I can add the template for the file name. This will also allow you to change the template as you will without having to figure out where to make the changes in the “code”. You just need to change the template and the filename will adjust.
The template is defined as a variable in the Variable creator as follows:
calulation_dd.MM.yyyy.xlsx
where:
dd: double digits day
MM: double digits month
yyyy: 4-digits year
You can move them around, or change “calculation_” for something else, the file name will adjust.
I use the Create Date&Time Range to get the current date - it’s the usual practice to get the current date, and I get this as current date:
After manipulation, I get this as last day of last month:
After applying this value to the template and converting to a variable, I get this:
At this point, you have to now convert this String variable to a Path variable, which you will then be able to use in your Excel Writer. To do this, we use the String to Path (Variable):
So, I want to convert the String variable “filename”, in my demo I’m generating a path that’s relative to the workflow data folder (so it will point to the workflow’s data folder), and it will add suffix “_location” to the new Path variable. You may choose other locations if you want.
We check the variable, and we see this:
You can see the variable “filename_location” was created and it’s of type Path.
Now in your Excel Writer, you can use this new variable. Click on the Variable button:
You will get this pop-up. Check the box in the “Use Variable” section, and choose the variable from the dropdown:
Click OK, and you should see this message at the bottom of the Excel Writer:
And this is it.
As you can see, the data was saved as an Excel with the expected file name:
If you want to test other dates to make sure that the workflow works, you can just do so by playing around in the Create Date&Time Range:
This is what we would get as current date if it ran on 2022-02-14:
And you can see the values of the variables for last day of previous month and file name for the test date:
And similarly if you are running in January, it knows how to get Dec 31 of the previous year.
Current date:
Generated variables:
Here’s the workflow: Name Excel file with last day of last month.knwf (35.7 KB)