Nested if functions in arrayformula - if-statement

I have this formula (1) in Google sheets:
=IF($A2="","",IF(AND(($G2 <= EOMONTH(TODAY(),-5)),($G2 <> ""),OR($H2 > EOMONTH(TODAY(),-5),$H2 = ""),OR($E2 > EOMONTH(TODAY(),-5),$E2 = "")),1,0))
which I want to convert to an arrayformula. I tried this formula (2) but I get a "#N/A no match" error:
=ARRAYFORMULA (
IFs (
ROW(A:A) = 1, "Contract Published, " & TEXT(EDATE(TODAY(),-5),"mmm-YY"),
IF(AND((G:G <= EOMONTH(TODAY(),-5)),(G:G <> ""),OR(H:H > EOMONTH(TODAY(),-5),H:H = ""),OR(E:E > EOMONTH(TODAY(),-5),E:E = "")),1,0),)
)
I also tried this formula (3), but get a "Formula parse error"
=ARRAYFORMULA (
IF(ROW(A:A) = 1, "Contract Published, " & TEXT(EDATE(TODAY(),-5),"mmm-YY")),
IF((G:G <= EOMONTH(TODAY(),-5),G:G <> ""),
IF((H:H > EOMONTH(TODAY(),-5),H:H = ""),
IF((E:E > EOMONTH(TODAY(),-5),E:E = "")),1,0)))
Does anyone have an idea what Im doing wrong, or how I can convert formula (1) into an Arrayformula?

try in row 2:
=ARRAYFORMULA(IF(A2:A="",,
IF(((G2:G <= EOMONTH(TODAY(), -5))*
(G2:G <> "")*
((H2:H > EOMONTH(TODAY(), -5)) + (H2:H = ""))*
((E2:E > EOMONTH(TODAY(), -5)) + (E2:E = ""))), 1, 0)))

Related

Is there a method to pass the formatting selection made in the PowerBI menu to a Deneb visual?

See title, trying to pass a formatting selection (for example, percentage) to a Deneb visual instead of defining it in the vega / vega-lite JSON, and wanted to know if such a thing was possible.
Right now, we are just manually changing the formatting in the JSON
No, but Kerry Kolosko has a clever solution for this in her KPI charts that are in the sample file for the Deneb visual. In addition to passing in number fields, she also does formatting in DAX and passes in text to use with text marks.
DAX
Format Variance =
var a = calculate(DIVIDE(MAX('KPIsDATA'[Actual]),'KPITarget'[KPITarget Value])-1,'KPIsDATA'[Date]=MAX('KPIsDATA'[Date]))
var b = FORMAT(ABS(a),"#%")
Var c = IF(a>0,"▲ " , "▼ ")
Var d = IF(a>0," above target" , " below target")
Return
c & b & d
You could do an "Auto" format like this in DAX:
Format Actual =
VAR _number = SUM(KPIsDATA[Actual])
VAR _digits = 1
RETURN IF( ABS(_number) >= 1000000000, FORMAT(_number, "0,,," & IF(_digits > 0, ".", "") & REPT("0",_digits) & "B"),
IF( ABS(_number) >= 1000000, FORMAT(_number, "0,," & IF(_digits > 0, ".", "") & REPT("0",_digits) & "M"),
IF( ABS(_number) >= 1000, FORMAT(_number, "0," & IF(_digits > 0, ".", "") & REPT("0",_digits) & "K"),
FORMAT(_number, "0" & IF(_digits > 0, ".", "") & REPT("0",_digits) )
)
)
)

How to extract all the value from a returned Map to a List?

So i have this map final Map<DateTime, List<CleanCalendarEvent>>? events;. I want to take some item from this Map based on the DateTime, so i do:
final recentSunday = DateTime(_selectedDate.year, _selectedDate.month,_selectedDate.day - _selectedDate.weekday % 7);
final nextSunday = DateTime(recentSunday.year, recentSunday.month, recentSunday.day + 7);
selectedCloseEvents = (widget.events?.firstWhereOrNull((date) =>
date.key.compareTo(recentSunday) > 0 &&
date.key.compareTo(nextSunday) < 0) ??
[])
this should return all event in a week in Map<DateTime, List<CleanCalendarEvent>> format. I need it as List<CleanCalendarEvent>.
I have tried:
_selectedCloseEvents = (widget.events?.firstWhereOrNull((date) =>
date.key.compareTo(recentSunday) > 0 &&
date.key.compareTo(nextSunday) < 0) ??
[])
.value as List<CleanCalendarEvent>?;
But it didn't worked. How to select only value from this Map?
According to the documentation this should give you a list of CleanCalenderEvent:
_selectedCloseEvents = selectedCloseEvents.values.toList()

Format row with color in DataTable with JavaScript

how do I modify the code in this thread here to apply the color format to the entire row, not just a particular cell?
Particularly, this one:
changeCellColor <- function(row, col){
c(
"function(row, data, num, index){",
sprintf(" if(index == %d){", row-1),
sprintf(" $('td:eq(' + %d + ')', row)", col),
" .css({'background-color': 'orange'});",
" }",
"}"
)
}
datatable(dat,
options = list(
dom = "t",
rowCallback = JS(changeCellColor(1, 2))
)
)
Thanks,
Here you go:
changeCellColor <- function(row){
c(
"function(row, data, num, index){",
sprintf(" if(index == %d){", row-1),
"for(n = 0; n <= 4; n++) {",
" $('td:eq(' + n + ')', row)",
" .css({'background-color': 'orange'});",
"}",
" }",
"}"
)
}
datatable(dat,
options = list(
dom = "t",
rowCallback = JS(changeCellColor(1))
)
)

Getting all timeDuration from a day which are not in a list of timeDuration in Scala

I have a list of timestamp tuples of the form List((startTime,endTime)), which are basically denoting periods of time throughout the day.
For example:
(("2016-03-28 14:00:00","2016-03-28 15:00:00"),
("2016-03-28 17:00:00","2016-03-28 21:00:00"),
("2016-03-28 01:00:00","2016-03-28 13:00:00"))
I want to get a list of the durations which are not included in this list.
So the output should be:
(("2016-03-28 00:00:00","2016-03-28 01:00:00"),
("2016-03-28 13:00:00","2016-03-28 14:00:00"),
("2016-03-28 15:00:00","2016-03-28 17:00:00"),
("2016-03-28 17:00:00","2016-03-28 24:00:00"))
Can anyone suggest a good and efficient way of doing that in Scala?
The naive solution that I've tried so far is as follows:
import java.sql.Timestamp
import scala.collection.mutable.ListBuffer
def comparator(first: (Timestamp,Timestamp), second: (Timestamp, Timestamp)) = first._2.getTime <= second._1.getTime
val data = List((Timestamp.valueOf("2016-03-28 00:00:00"),Timestamp.valueOf("2016-03-28 10:00:00")),
(Timestamp.valueOf("2016-03-28 12:00:00"),Timestamp.valueOf("2016-03-28 15:00:00")),
(Timestamp.valueOf("2016-03-28 23:00:00"),Timestamp.valueOf("2016-03-28 23:59:59")),
(Timestamp.valueOf("2016-03-28 16:00:00"),Timestamp.valueOf("2016-03-28 21:00:00"))
).sortWith(comparator)
var emptySlots = new ListBuffer[(Timestamp,Timestamp)]()
var currTime = Timestamp.valueOf("2016-03-28 00:00:00")
var index = 0
var cond = 0
while(cond == 0){
if (currTime.compareTo(Timestamp.valueOf("2016-03-28 23:59:59")) < 0 && index >= data.size){
emptySlots += ((currTime,Timestamp.valueOf("2016-03-28 23:59:59") ))
cond = 1
}
else if(index >= data.size)
{
cond = 1
}
else if(currTime.compareTo(data(index)._1) < 0) {
emptySlots += ((currTime, data(index)._1))
currTime = data(index)._2
index += 1
}
else if(currTime.compareTo(data(index)._1) >= 0 && currTime.compareTo(data(index)._2) < 0 ) {
currTime = data(index)._2
index += 1
}
else if(currTime.compareTo(data(index)._1) > 0 && currTime.compareTo(data(index)._2) > 0 ) {
index += 1
}
}
emptySlots.toList

a really basic SML issue I just can't seem to figure out (small code)

Just a basic Casaer Cipher. I've tested all of the sub functions, just encryptChar() does not particularly work. I get an infinite loop. It's supposed to be recursive. Here's the all code:
fun replace (str : string, index : int, newChar : char) : string = String.substring(str,0,index) ^ String.str(newChar) ^ String.substring(str,index+1,(size str) - index - 1;
fun encryptChar (msgStr : string, shiftAmnt : int, index : int) : string =
let val asciiCode = 0
in
if (not (String.sub(msgStr, index) = #" ")) then
(
asciiCode = ord( String.sub(msgStr, index) ) + shiftAmnt;
if (asciiCode < ord(#"A")) then asciiCode = asciiCode + 26
else if (asciiCode > ord(#"Z")) then asciiCode = asciiCode - 26
else asciiCode = asciiCode;
msgStr = replace(msgStr, index, chr(asciiCode))
)
else asciiCode = asciiCode;
index = index + 1;
if (index < (size msgStr - 1)) then encryptChar(msgStr, shiftAmnt, index)
else msgStr
end
;
fun encrypt(msgStr : string, shiftAmnt : int) : string = encryptChar (String.map Char.toUpper msgStr, shiftAmnt mod 26, 0);
The problem here is that you're misusing =. Outside of a variable definition, = is simply a boolean function which checks its arguments for equality. So if you do for example asciiCode = ord( String.sub(msgStr, index) ) + shiftAmnt;, it will simply return false (because asciiCode is not equal to ord( String.sub(msgStr, index) ) + shiftAmnt) and then throw that result away (because you have additional expressions after the ;). It will not reassign asciiCode.
Variables in SML are immutable. If you want to emulate mutable variables you can use refs and the := operator. However I would not recommend that approach as it is generally not good functional style and not necessary in this case. The preferable approach would be to rewrite the code in a way that each variable is only assigned once.
This is very basic indeed, and it's surprising that you ran into it in such a complicated situation.
Did you port this from some other language?
You need to forget everything you know about programming using assignments.
let val x = y in something
means more or less "within 'something', replace the identifier 'x' with the value of 'y'".
There is no way for you to change the value of x.
Do the substitution (this is not the actual evaluation order or anything, but it should give you an idea of what's going on):
encryptChar("THIS", amount, 0)
=>
let val asciiCode = 0
in
if (not (String.sub("THIS", 0) = #" ")) then
(
asciiCode = ord( String.sub("THIS", 0) ) + amount;
if (asciiCode < ord(#"A")) then asciiCode = asciiCode + 26
else if (asciiCode > ord(#"Z")) then asciiCode = asciiCode - 26
else asciiCode = asciiCode;
"THIS" = replace("THIS", 0, chr(asciiCode))
)
else asciiCode = asciiCode;
0 = 0 + 1;
if (0 < (size "THIS" - 1)) then encryptChar("THIS", amount, 0)
else str
end ;
=>
if (not (String.sub("THIS", 0) = #" ")) then
(
0 = ord( String.sub("THIS", 0) ) + amount;
if (0 < ord(#"A")) then 0 = 0 + 26
else if (0 > ord(#"Z")) then 0 = 0 - 26
else 0 = 0;
"THIS" = replace("THIS", 0, chr(0))
)
else 0 = 0;
0 = 0 + 1;
if (0 < (size "THIS" - 1)) then encryptChar("THIS", amount, 0)
else str
=>
if (not (String.sub("THIS", 0) = #" ")) then
(
0 = ord( String.sub("THIS", 0) ) + amount;
if true then false
else if false then false
else true;
false
)
else true;
false;
if (0 < (size "THIS" - 1)) then encryptChar("THIS", amount, 0)
else "this"
->
if (not false) then
(
false;
false;
false
)
else true;
false;
if true then encryptChar("THIS", amount, 0)
else "THIS"
=>
(
false;
false;
false
)
false;
encryptChar("THIS", amount, 0)
=>
encryptChar("THIS", amount, 0)
Which is where your infinite loop came from.
You would do well to get hold of an introductory text about ML programming.