Month end day from parameter

I'm trying to get the last day of the month generated from a parameter to update a column, e.g.

monthend(#{month})

where the {month} parameter is 2023-07-01

I keep getting the "Date literal has incorrect format. Must be #yyyy-MM-dd."

what am I doing wrong?

I've also tried pulling the date in from a column and can't get it to work.

I basically want to generate a date for the last day of the month as e.g.:

20230731

derived from a parameter which is ideally the month, i.e. 202307, or the first day of the month 2023-07-01 for eg.

You don't need the hash here, just use

monthend({month})

If you need to format it additionally, use the format() function:

format(monthend({month}), "yyyy-MM-dd")

I'm still getting an error - I'm using the "Modify column(s)" Action - see below

It works if I specify the date, but if I specify a parameter or column, I get the error - can't figure it out!

Quick update!

If I use:

format(monthend(#2023-07-01), "yyyyMMdd")

I get the expected output of 20230731

If I use:

format(monthend(2023-07-01), "yyyyMMdd")

I get 19050731, so I suspect I might need the #?

However, if I try to change the 2023-07-01 for a parameter or column value I get the error in the screenshot above.

I tried to get around this using the "Rule" action, but I also received the same error.

:crazy_face:

monthend_error_001.morph (3.5 KB)

tried to see if it might be something in my project so created a new one to test and get the same error.

That's because the dashes between the year and the month and the day are understood by EasyMorph as the minus operator:

2023 - 7 - 1 = 2015 // This corresponds to July 7, 1905 because it's 2015 days since Jan 1st, 1900

So you effectively calculate

format(monthend(2015), "yyyyMMdd")

which returns the last day in July 1905 which is July 31, 1905

format(monthend(2015), "yyyyMMdd") // returns 19050731
1 Like

The monthend(number_date) function expects a number (date) as the argument. But in your example, you're giving it a text date.

monthend("2023-07-01") // returns error

Text dates are not real dates for EasyMorph, because in EasyMorph, like in Excel, dates are numbers. See this tutorial article: EasyMorph | Type system and expressions

A date literal (e.g. #2023-07-01) is also effectively a number (in this case 45108). A date literal is just a more convenient way to specify a date, because it is immediately clear that #2023-07-01 is July 1st, 2023. While it's not obvious that number 45108 is also July 1st, 2023 (because there are 45108 days between July 1st, 2023 and Jan 1st, 1900). The pound sign can't be used for anything else except specifying date constants explicitly. It's not a function or an operator. It's a syntax for date literals (constants).

Therefore, in order to use monthend() you need to convert text "2023-07-01" into a number date. You can do this with the date() function. So the expression below works:

monthend(date("2023-07-01", "yyyy-MM-dd")) // returns 45138

Finally, if you want to convert the number date 45138 into a text date, the use the format() function, which is the opposite of the date() function:

format(monthend(date("2023-07-01", "yyyy-MM-dd")), "yyyy-MM-dd") // returns text value "2023-07-31"

Here is your project updated:
monthend_error_001.morph (3.6 KB)

To avoid unnecessary conversions, perform all operations with dates as numbers, and only convert them into text dates when no other operations should be done.

EasyMorph helps see numbers as dates by offering a date format for numbers in a column:

This format only affects how numbers are displayed in the datagrid. They do not modify the numeric values themselves.

So to work conveniently with dates:

  1. Convert text dates into number dates
  2. Set column formatting to a date format to see dates instead of numbers in columns with dates
1 Like

Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaah!

Thank you for the detailed response - all makes sense now.