简体   繁体   中英

Spotfire how to calculate end of month

I have a formula in spotfire

Sum([sales])/10000*28

But this 28 is nothing but (EOMONTH(X,-1)-EOMONTH(X,-2))

x is nothing but 31Mar2015

so it is like when current month is march it is like end of month of feb - end of the month of jan and I have to populate this for all the months like when the end of period is April then we calculate no of days between end of month of march - end of month of feb . Can anyone pls help

We can simplify this if all you need is the delta value. Correct me if I'm wrong but taking the end of the month of one month minus the EOM of another is really just the number of days in the first value's month.

Eg Input of March then I'm going to look into February and see that it is 28 days (unless leap year then 29). No need for EOMONTH-EOMONTH calculations since these calendar values are static outside of leap years.

Below is a spotfire expression that should provide the value you need. I also included some modulo logic to handle leap years (if current month is march then check if leap year. If so then result = 29, otherwise 28.)

Case 
when Month(DateTimeNow()) = 1 then 31
when Month(DateTimeNow()) = 2 then 31
when Month(DateTimeNow()) = 3 and Mod(Year(DateTimeNow()),4)=0 
     and (Mod(Year(DateTimeNow()),100)!=0 or Mod(Year(DateTimeNow()),400)=0) then 29
when Month(DateTimeNow()) = 3 then 28
when Month(DateTimeNow()) = 4 then 31
when Month(DateTimeNow()) = 5 then 30
when Month(DateTimeNow()) = 6 then 31
when Month(DateTimeNow()) = 7 then 30
when Month(DateTimeNow()) = 8 then 31
when Month(DateTimeNow()) = 9 then 31
when Month(DateTimeNow()) = 10 then 30
when Month(DateTimeNow()) = 11 then 31
when Month(DateTimeNow()) = 12 then 30
end

You can replace Month(DateTimeNow()) if you're not actually needing the current month and have an input column. Also, you could add your result calculation Sum([sales])/10000*28 into the above or just insert the above as a calculated column which your formula references: Sum([sales])/10000*[calc_col]

Let me know if this works for you and if you have any questions.

For reference:

Leap Year logic without the messy Spotfire code:

if( 0 == year % 4 and (0 != year % 100 or 0 == year % 400) )
{
    # Then year is a leap year.
}

Modified from this perl code .

Days of Month reference

Edit:

As per @Niko if we don't use the leap year logic we can clean up our code a little bit. Really just depends on the necessity of leap year logic for this solution.

Case Month(DateTimeNow())
when 1 then 31
when 2 then 31
when 3 then 28
when 4 then 31
when 5 then 30
when 6 then 31
when 7 then 30
when 8 then 31
when 9 then 31
when 10 then 30
when 11 then 31
when 12 then 30
end

I take the first of the month,add one month and take one day off.

DateAdd('day',-1,date(Year(DateTimeNow()),Month(DateAdd('month',1,DateTimeNow())),1))

DateTimeNow() can be replaced by your date column.

Once you have the last day of month for a given date, you can extract the Day(/above expression here/)

came back to this after running into a similar problem. I made @clesiemo3 's answer a little smaller:

CASE  
        WHEN Month([Date]) IN (1, 3, 5, 7, 8, 10, 12) THEN 31 
        WHEN Month([Date]) IN (4, 6, 9, 11) THEN 30 
        WHEN (Month([Date])=2) AND (Mod(Year([Date]),4)=0)
             AND ((Mod(Year([Date]),100)!=0) or (Mod(Year([Date]),400)=0)) THEN 29
        ELSE 28
END

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM