How to convert an Excel formula to Calc

Discuss the spreadsheet application
Post Reply
debbys
Posts: 1
Joined: Wed Oct 16, 2019 4:34 pm

How to convert an Excel formula to Calc

Post by debbys »

Hi

I have tried to figure it out myself and failed miserably.

I have the following formula from excel imported on a file converted to open office:

=DATEDIF(C5,TODAY(),"Y")

I literally want to know age in years, not years and months. I have popped = and selected YEARS from functions which gives me (Start_date; End_date: Type) and have tried entering =YEARS(26/4/13; now; 1) and =YEARS(26-Apr-13; now; 1) but neither work I am guessing I am guessing I am missing something simple but cannot figure what :-(

Please enlighten me

Thank you
Last edited by MrProgrammer on Wed Oct 16, 2019 11:25 pm, edited 1 time in total.
Reason: Changed title; was: How to write an Excel formula
Open Office 4.1.6 on Windows 10 home
User avatar
RoryOF
Moderator
Posts: 35203
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: how to write an excel formula

Post by RoryOF »

Try =(Today()-Cell_holding_StartDate)/365.25

There are more accurate methods, but that might be sufficient for you
Apache OpenOffice 4.1.16 on Xubuntu 24.04.4 LTS
User avatar
Villeroy
Volunteer
Posts: 31363
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: how to write an excel formula

Post by Villeroy »

In any spreadsheet 26/4/13 means 26 divided by 4 divided by 13.
A clean way to specify a day number: DATE(2013;4;13) returns the day number of the date that is specified by year;month;day. Notice that OpenOffice always uses semicolon as argument separator.
TODAY() returns today's day number.
=YEARS(DATE(2013;4;13);TODAY();0) returns the number of full years between the 2 day numbers as in age calculations.
Instead of DATE(2013;4;13) you may use the ISO string of a date enclosed in double-quotes: "2013-4-14"
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
John_Ha
Volunteer
Posts: 9604
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: how to write an excel formula

Post by John_Ha »

If you want to do it properly you need

1. An Excel Manual so you know exactly what the Excel formula does.
2. A Calc Guide (manual) so you can replicate exactly what the formula does with the functions available in Calc.

A list of Excel functions can be found at Excel functions (alphabetical)

You can download the Calc Guide from here. Functions are listed in Appendix B.
LO 6.4.4.2, Windows 10 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
User avatar
RoryOF
Moderator
Posts: 35203
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: how to write an excel formula

Post by RoryOF »

In my example Cell_holding_StartDate was formatted as Date, DD/MM/YY.
Apache OpenOffice 4.1.16 on Xubuntu 24.04.4 LTS
User avatar
Zizi64
Volunteer
Posts: 11505
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: how to write an excel formula

Post by Zizi64 »

The LibreOffice Calc has DATEDIF() function:
DATEDIF_LO.png
Tibor Kovacs, Hungary; LO7.5.8/25.8.5.2 /Win7-10-11 x64Prof.
PortableApps: LO3.3.0-25.8.5.2;AOO4.1.14
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
John_Ha
Volunteer
Posts: 9604
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: how to write an excel formula

Post by John_Ha »

The Excel function calculates the years between a start date and an end date.

The closest Calc function is YEARS which purports to do the same. However I could not get it to work using the definition as in the manual with dates like "25-12-2016".

However the example given in the YEARS function in the Calc wiki - found by using Help - does work. See Documentation/How Tos/Calc: YEARS function

Code: Select all

=YEARS("2009-04-03"; "2011-11-17"; 0)
gives the value 2. Note the spaces, quotes, the date format used and the " 0 " which selects years where
If mode is 0, YEARS returns the number of whole years between startdate and enddate, day/month to day/month.

If mode is 1, YEARS identifies the year that startdate and enddate each lie in, and returns the difference between those years. In other words it returns YEAR(enddate) - YEAR(startdate)
You will appreciate the necessity for understanding exactly what the Excel and Calc functions do.
Clipboard01.png
Clipboard01.gif
years.ods
(7.76 KiB) Downloaded 85 times
Showing that a problem has been solved helps others searching so, if your problem is now solved, please view your first post in this thread and click the Edit button (top right in the post) and add [Solved] in front of the subject.
LO 6.4.4.2, Windows 10 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
User avatar
Villeroy
Volunteer
Posts: 31363
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: how to write an excel formula

Post by Villeroy »

Since the topic is about "Excel formulas", YEARS is not Excel compatible.
A compatible formula would be:

Code: Select all

YEAR(B1)-YEAR(A1)-(MONTH(B1)<MONTH(A1))-AND(MONTH(B1)=MONTH(A1);DAY(B1)<DAY(A1))
which calculates the difference between year numbers and subtracts one year if the month is lower or if the month is the same but day number is lower.
or this one

Code: Select all

YEAR(B1)-YEAR(A1)-(DATE(YEAR(B1);MONTH(A1);DAY(A1))<B1);
which calculates the difference between year numbers and subtracts 1 if the A1 date in the year of B1 is smaller than B1.

The Calc component in https://libreoffice.org/ has a DATEDIF function like Excel and with English locale settings it can deal with commas as list separators.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
John_Ha
Volunteer
Posts: 9604
Joined: Fri Sep 18, 2009 5:51 pm
Location: UK

Re: how to write an excel formula

Post by John_Ha »

Villeroy wrote:Since the topic is about "Excel formulas", YEARS is not Excel compatible.
I read the post as "A file created with Excel has been saved as a .ods file. A Calc equivalent to an Excel function is required. The one I tried using YEARS does not work" and gave a Calc equivalent by getting YEARS to work.

Of course, if the .ods file is to be edited by both Excel and by Calc then, as you say, an Excel and Calc compatible formula such as yours will be required.
LO 6.4.4.2, Windows 10 Home 64 bit

See the Writer Guide, the Writer FAQ, the Writer Tutorials and Writer for students.

Remember: Always save your Writer files as .odt files. - see here for the many reasons why.
User avatar
Villeroy
Volunteer
Posts: 31363
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: how to write an excel formula

Post by Villeroy »

Or if you save it as xls and re-open with Calc. People do this kind of mistakes.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Post Reply