[Solved] Formula to subtract A1-A2, with conditions

Discuss the spreadsheet application
Locked
User avatar
donnje
Posts: 74
Joined: Sun Apr 20, 2025 4:06 pm

[Solved] Formula to subtract A1-A2, with conditions

Post by donnje »

Hi everyone,
Can anyone tell me the formula to subtract A1-A2 if A1>A2 and add if B1<B2?
Thanks

Image
Last edited by donnje on Sat Feb 07, 2026 8:19 pm, edited 2 times in total.
LibreOffice 24.8.6.2
Alex1
Volunteer
Posts: 852
Joined: Fri Feb 26, 2010 1:00 pm
Location: Netherlands, EU

Re: SUM IF

Post by Alex1 »

You also need to tell what these formulas should give in case the conditions aren't satisfied.
AOO 4.1.16 & LO 25.8.3 on Windows 10
morchat
Posts: 58
Joined: Wed Dec 26, 2012 6:13 pm
Location: Poland

Re: Formula to subtract A1-A2, with conditions

Post by morchat »

I understand it like this: =A1-A2*(A1<A2)+A2*(B1<B2)
AOO 4.1.16, LibreOffice 25.8
Windows 11 64 bits
User avatar
donnje
Posts: 74
Joined: Sun Apr 20, 2025 4:06 pm

Re: Formula to subtract A1-A2, with conditions

Post by donnje »

thanks I solved with this: =SE(A1>A2; A2-A1; A2+A1)
but now I would like that if A1 is negative then A2-A1 otherwise A1+A2, how is this possible?
LibreOffice 24.8.6.2
User avatar
karolus
Volunteer
Posts: 1243
Joined: Sat Jul 02, 2011 9:47 am

Re: Formula to subtract A1-A2, with conditions

Post by karolus »

donnje wrote: Wed Feb 04, 2026 6:18 pm
but now I would like that if A1 is negative then A2-A1 otherwise A1+A2, how is this possible?

Code: Select all

=A2+ABS(A1)
Libreoffice 25.2… on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (trixie) (on RaspberryPI5)
User avatar
donnje
Posts: 74
Joined: Sun Apr 20, 2025 4:06 pm

Re: Formula to subtract A1-A2, with conditions

Post by donnje »

sorry I attach the test file, I would like that in column C it is calculated from the difference between the two times and that in case the result is negative as in row C4 it is indicated as -03:00
Attachments
PROVA.ods
(12.83 KiB) Downloaded 44 times
LibreOffice 24.8.6.2
morchat
Posts: 58
Joined: Wed Dec 26, 2012 6:13 pm
Location: Poland

Re: Formula to subtract A1-A2, with conditions

Post by morchat »

Remember that time is represented by the fractional part of a number (from 0.0 to 0.99999(9)), e.g., 0.34375 is 8:15, so time cannot be a negative number. To get a negative result, as you want, the result is presented as text, so such a result cannot be used for arithmetic.
Attachments
PROVA_1.ods
(13.58 KiB) Downloaded 44 times
AOO 4.1.16, LibreOffice 25.8
Windows 11 64 bits
User avatar
MrProgrammer
Moderator
Posts: 5430
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: Formula to subtract A1-A2, with conditions

Post by MrProgrammer »

morchat wrote: Fri Feb 06, 2026 9:49 pm so time cannot be a negative number
Read about duration in section 4. Times in cells in Ten concepts that every Calc user should know.
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.7.8, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
Alex1
Volunteer
Posts: 852
Joined: Fri Feb 26, 2010 1:00 pm
Location: Netherlands, EU

Re: Formula to subtract A1-A2, with conditions

Post by Alex1 »

For duration instead of clock time just format column C as [HH]:MM instead of HH:MM or whatever is used in Italian.
Attachments
PROVA_2.ods
(13.63 KiB) Downloaded 46 times
AOO 4.1.16 & LO 25.8.3 on Windows 10
User avatar
donnje
Posts: 74
Joined: Sun Apr 20, 2025 4:06 pm

Re: Formula to subtract A1-A2, with conditions

Post by donnje »

I was using the same formula but why does it show 21:21:00 in my C4 file and not -3?
LibreOffice 24.8.6.2
Alex1
Volunteer
Posts: 852
Joined: Fri Feb 26, 2010 1:00 pm
Location: Netherlands, EU

Re: Formula to subtract A1-A2, with conditions

Post by Alex1 »

The formatting determines how the result is shown. Please follow the link provided by MrProgrammer.
AOO 4.1.16 & LO 25.8.3 on Windows 10
Locked