trouble with IF, AND, IF formula

%3CLINGO-SUB%20id%3D%22lingo-sub-2917719%22%20slang%3D%22en-US%22%3Etrouble%20with%20IF%2C%20AND%2C%20IF%20formula%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2917719%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20trying%20to%20build%20a%20formula%20to%20do%20the%20following%3A%3C%2FP%3E%3CP%3EIF%20F2%20is%20not%20blank%20%26amp%3B%20G2%20is%20not%20Blank%2C%20calculate%20the%20session%20hours%20(some%20go%20over%20midnight)%3C%2FP%3E%3CP%3EIF%20F2%20and%20G2%20are%20blank%2C%20leave%20the%20cell%20blank%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere%20is%20my%20current%20formula%2C%20that%20is%20returning%20an%20error%3C%2FP%3E%3CP%3EIF((AND(F2%26lt%3B%26gt%3B%22%22%2CG2%26lt%3B%26gt%3B%22%22))%2CIF((F2%26gt%3BG2%2CG2%2B1%2CG2)-F2)%2C%22%20%22)%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EVersion%3A%26nbsp%3B%3C!--%20%20%20%20StartFragment%20%20%20%20%20--%3E%3C%2FP%3E%3CP%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3EMicrosoft%C2%AE%20Excel%C2%AE%20for%20Microsoft%20365%20MSO%20(Version%202110%20Build%2016.0.14527.20234)%2064-bit%3C%2FSPAN%3E%3C%2FP%3E%3CP%20class%3D%22%22%3E%26nbsp%3B%3C%2FP%3E%3CP%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3EThanks%20for%20your%20help!%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3C!--%20%20%20%20EndFragment%20%20%20%20%20--%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2917719%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2918565%22%20slang%3D%22en-US%22%3ERe%3A%20trouble%20with%20IF%2C%20AND%2C%20IF%20formula%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2918565%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1204249%22%20target%3D%22_blank%22%3E%40taraRM%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3ESince%20you%20are%20using%20365%20you%20may%20wish%20to%20return%20the%20results%20as%20a%20dynamic%20array%2C%20in%20which%20case%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3E%3D%20IF((start%26lt%3B%26gt%3B%22%22)*(end%26lt%3B%26gt%3B%22%22)%2C%20MOD(end-start%2C%201)%2C%20%22%22)%0A%0A%3D%20LET(%0A%20%20%20completed%2C%20(start%26lt%3B%26gt%3B%22%22)*(end%26lt%3B%26gt%3B%22%22)%2C%0A%20%20%20elapsed%2C%20MOD(end-start%2C%201)%2C%0A%20%20%20IF(completed%2C%20elapsed%2C%20%22%22))%0A%0A%3D%20MAP(start%2C%20end%2C%0A%20%20%20LAMBDA(s%2Ce%2CIF((s%26lt%3B%26gt%3B%22%22)*(e%26lt%3B%26gt%3B%22%22)%2C%20MOD(e-s%2C1)%2C%22%22)))%0A%0A%3D%20LET(%0A%20%20%20Elapsed%CE%BB%2C%20LAMBDA(s%2Ce%2CIF((s%26lt%3B%26gt%3B%22%22)*(e%26lt%3B%26gt%3B%22%22)%2C%20MOD(e-s%2C1)%2C%22%22))%2C%0A%20%20%20MAP(start%2C%20end%2C%20Elapsed%CE%BB)%0A%20%20%20)%0A%0A%3D%20LET(%0A%20%20%20Elapsed%CE%BB%2C%20LAMBDA(t%2CIF(AND(t%26lt%3B%26gt%3B%22%22)%2C%20MOD(SUM(t*%7B-1%2C1%7D)%2C1)%2C%22%22))%2C%0A%20%20%20BYROW(times%2C%20Elapsed%CE%BB)%0A%20%20%20)%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3Eare%20all%20working%20formulas%2C%20though%20the%20final%20three%20require%20Lambda%20helper%20functions%20so%20are%2C%20as%20yet%2C%20only%20available%20on%20the%20insider%20beta%20version%20of%20Excel.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENote%3A%20Start%20and%20End%20are%20names%20applied%20to%20the%20time%20columns%20whilst%20'times'%20applies%20to%20the%209x2%20array.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Visitor

I am trying to build a formula to do the following:

IF F2 is not blank & G2 is not Blank, calculate the session hours (some go over midnight)

IF F2 and G2 are blank, leave the cell blank

 

Here is my current formula, that is returning an error

IF((AND(F2<>"",G2<>"")),IF((F2>G2,G2+1,G2)-F2)," ")

 

Version: 

Microsoft® Excel® for Microsoft 365 MSO (Version 2110 Build 16.0.14527.20234) 64-bit

 

Thanks for your help!

 

2 Replies

@taraRM 

 

Try using IFS instead of nesting IFs

=IFS(AND(F2="",G2=""),"",G2>F2,G2-F2,F2>G2,G2+1-F2) seems to work.

Let's break it down:

=IFS(

AND(F2="",G2=""),"",

G2>F2,G2-F2,

F2>G2,G2+1-F2

)

the syntax of IFS is condition followed by result, and it stops once it meets a condition. (The pairs of condition and result can continue for many possible conditions.)

In this case, the formula is saying

IF both F2 and G2 are blank, the result is blank

IF G2 is greater than F2, do a simple subtraction of the latter from the former

IF F2 is greater than G2, add 1 to G2 (to take it to the next day) and subtract F2 from the result

 

@taraRM 

Since you are using 365 you may wish to return the results as a dynamic array, in which case

= IF((start<>"")*(end<>""), MOD(end-start, 1), "")

= LET(
   completed, (start<>"")*(end<>""),
   elapsed, MOD(end-start, 1),
   IF(completed, elapsed, ""))

= MAP(start, end,
   LAMBDA(s,e,IF((s<>"")*(e<>""), MOD(e-s,1),"")))

= LET(
   Elapsedλ, LAMBDA(s,e,IF((s<>"")*(e<>""), MOD(e-s,1),"")),
   MAP(start, end, Elapsedλ)
   )

= LET(
   Elapsedλ, LAMBDA(t,IF(AND(t<>""), MOD(SUM(t*{-1,1}),1),"")),
   BYROW(times, Elapsedλ)
   )

are all working formulas, though the final three require Lambda helper functions so are, as yet, only available on the insider beta version of Excel.

 

Note: Start and End are names applied to the time columns whilst 'times' applies to the 9x2 array.