I have a table and want to create a new column based on some columns in the table using multiple statements.
I want to do something like this:
NewColumn = if( (colA>colB and colC=0)
or (colD >colE and colF = 20)
or colG = "blue",
"True", "False")
How would I code this in DAX?
In DAX you should write something like this:
test =
IF(
OR(
OR(
AND(
[A]>[B];
[C] = 0
);
AND(
[D]>[E];
[F] = 20
)
);
[G] = "Blue"
);
"True";
"False"
)
However, I do believe you'll get the same result by using something like this, though you should double check this code since I don't have your data.
New =
SWITCH(
TRUE();
[A] > [B] && [C] = 0; "True";
[D] > [E] && [F] = 20; "True";
[G] = "Blue"; "True";
"False"
)
This would be the correct syntax. Take care and dont write in upper case.
= if ([ColumnA] > [ColumnB] and [ColumnC] = 0) or
([ColumnD] > [ColumnE] and [ColumnF] = 20) or
[ColumnG] = "blue"
then true
else false
Related
Is there a way of placing three different types of variables into a one column using switch? Or is there a better way of doing it? Or can it be done?
So I have one set of parameters that work in the switch but a few tricky ones that dont work. Here is what I have so far.
Var a = switch ([customer],
"Customer 1001" , "USA",
"Customer 1002", "Asia", "Other ...... and so on
Var b = if [Customer] = "Customer 2002" && [cat] <> "Household" THEN " USA" else "Europe"
Var c = if [Customer] = "Customer 2002" && [order date] > "2021/10/01" then "USA" else "Asia"
You can declare multiple variables within an expression, Measure/Calculated Column/Calculated Table. Just keep in mind they will only be evaluated if refered to, lazy evaluation, and once assigned within it's scope they can't be overwritten.
Example:
My Calculation =
var a = 1
var b = 2
RETURN
a+b
Your problem might be with the incorrect use of the IF.
https://learn.microsoft.com/pt-pt/dax/if-function-dax
Example:
My Calculation =
var a = "Customer 1001"
RETURN
IF(a = "Customer 1001", "USA" , "Other")
I have a table that is something like this:
ID
A
B
1H4
6S8
True
1L7
True
6T8
True
7Y8
6S2
True
True
1H1
True
6S3
True
1H9
True
True
6S0
I want to create a measure that evaluates a table to be able to conditionally (to later make conditional rules for report i.e. place color values in such cells) evaluate the cells for the following 2 conditions:
when there are values in both Column A and Column B
when there are blanks/nulls in both columns
(If both can be done in a single measure this would be ideal)
You can use a measure like this:
Background Color =
var Count_A = COUNTBLANK('Table'[A])
var Count_B = COUNTBLANK('Table'[B])
RETURN
SWITCH(TRUE();
AND(Count_A = 0; Count_B = 0); "Red";
AND(Count_A > 0; Count_B > 0); "Green";
"")
First count the blank values in each of the columns, and then return a different color, depending on both counts. Then use this measure to conditionally format the background color for each of the columns:
to get something like this:
You'll need a custom column with the logic of
Column name =
SWITCH (
TRUE (),
A = 'True'
&& B = 'True', "True",
A = ''
&& B = '', "False",
"Else goes here"
)
You'll have to change the logic if the cells without anything in them are '' or true blanks. SWITCH acts like a multiple IF statement, and Switch with TRUE() evaluates the conditions in the later steps.
You can achieve the desired result by using both custom columns and measures.
Custom Column
Column =
IF (
Table[A] <> BLANK ()
&& Table[B] <> BLANK (),
"Green",
IF ( Table[A] = BLANK () && Table[B] = BLANK (), "Red" )
)
Measure
Measure X =
IF(COUNTBLANK(Table[A]) = 0
&& COUNTBLANK(Table[B]) = 0 , "#00FF00",
IF(COUNTBLANK(Table[A]) <> 0
&& COUNTBLANK(Table[B]) <> 0 , "#FF0000")
)
After creating a measure or custom column go to conditional formatting and select background colour, and you may select either measure or column as per your choice. this will give you the desired result.
Output
I'm trying to build a ema script in trading view but struggling with how to finish up the code. I'm essentially trying to code this, but not sure how to nest this in an if statement:
(tl <= em1 and tc > ema2) and ((tc<ema3) or (tc<em4) or (tc<em5))
How can I put a nested if statement for this? Goal is to use the if statement to tie in the rest of the code
// EMA trap
m1=8
m2=3.1
m3=3.2
m4=3.4
tl=low
tc=close
em1 = ema(tl,m1)
em2 = ema(tc,m1)
em3 = ema(tc,m2)
em4 = ema(tc,m3)
em5 = ema(tc,m4)```
You can't use float periods for ema(), so replaced them with integers.
This plots an arrow on the chart when your condition is true:
//#version=4
study("", "", true)
// EMA trap
m1=8
m2=3
m3=4
m4=5
tl=low
tc=close
em1 = ema(tl,m1)
em2 = ema(tc,m1)
em3 = ema(tc,m2)
em4 = ema(tc,m3)
em5 = ema(tc,m4)
cond = tl <= em1 and tc > em2 and (tc < em3 or tc < em4 or tc < em5)
plotchar(cond, "cond", "▼", location.top)
I have schematic with multiple instances connected to one of the nets.
I need a SKILL function that will print for all instances list of pins connected to this net
Or shorter:
procedure( ABC_findInstsAndPinsByNet(cvId netName)
let( (netId returnTable)
returnTable = makeTable("ABC_findInstsAndPinsByNet" nil)
netId = dbFindNetByName(cvId netName)
foreach( termId netId~>instTerms
returnTable[termId~>inst] = cons(termId~>name returnTable[termId~>inst])
)
returnTable ;;return
)
)
And then, to read it:
rezTbl = ABC_findInstsAndPinsByNet(cv() "net2")
printstruct(rezTbl)
and you will get something like:
printstruct(rezTbl)
Structure of type association table (table:ABC_findInstsAndPinsByNet):
db:0x3f04079b: ("d")
db:0x3f04079c: ("d" "g")
db:0x3f04079a: ("s")
t
or if you want to parse all:
foreach( elem rezTbl
printf("%L => %L\n" elem rezTbl[elem])
)
procedure(CCSgetListOfConnectedInstancePinsForNet(netname)
let((cv netid instid termid revhierarchy subcktPath pinHierName inst_list)
;get cellview
cv = geGetEditCellView()
; get netid
netid = dbFindNetByName(cv netname)
foreach(inst netid~>allInstTerms~>inst
unless(member( inst inst_list )
inst_list = cons(inst inst_list)
);unless
);foreach
;get instid
foreach(instid inst_list
;printf("Instance %L\n" instid~>name )
foreach(term instid~>instTerms
when(car(term~>net~>sigNames) == netname
termid=term
;printf("\tNet %L is connected to terminal %L of Instance %L\n" netname termid~>name instid~>name )
revhierarchy = reverse(mapcar('car geGetHierMemInst()))
subcktPath = CCSgetSubcktPath(revhierarchy)
pinHierName=strcat( subcktPath instid~>name "/" termid~>name)
printf("%s\n" pinHierName)
);when
);foreach
);foreach
t
);let
);procedure
procedure(CCSgetSubcktPath(hierarchy)
let( (path)
path = buildString(reverse(hierarchy)~>name "/")
when(path != ""
path = strcat("/" path "/")
) ; end when
path
) ; end let
) ; end procedure
I solved my problem in an imperative style, but it looks very ugly. How can I make it better (more elegant, more concise, more functional - finally its Scala). Rows with the same values as the previous row, but with a different letter should be skipped, all other values of the rows should be added.
val row1 = new Row(20, "A", true) // add value
val row2 = new Row(30, "A", true) // add value
val row3 = new Row(40, "A", true) // add value
val row4 = new Row(40, "B", true) // same value as the previous element & different letter -> skip row
val row5 = new Row(60, "B", true) // add value
val row6 = new Row(70, "B", true) // add value
val row7 = new Row(70, "B", true) // same value as the previous element, but the same letter -> add value
val rows = List(row1, row2, row3, row4, row5, row6, row7)
var previousLetter = " "
var previousValue = 0.00
var countSkip = 0
for (row <- rows) {
if (row.value == previousValue && row.letter != previousLetter) {
row.relevant = false
countSkip += 1
}
previousLetter = row.letter
previousValue = row.value
}
// get sum
val sumValue = rows.filter(_.relevant == true).map(_.value) reduceLeftOption(_ + _)
val sum = sumValue match {
case Some(d) => d
case None => 0.00
}
assert(sum == 290)
assert(countSkip == 1)
Thanks in advance
Twistleton
(rows.head :: rows).sliding(2).collect{
case List(Row(v1,c1), Row(v2,c2)) if ! (v1 == v2 && c1 != c2) => v2 }.sum
I think the shortest (bulletproof) solution when Row is a case class (dropping the boolean) is
(for ((Row(v1,c1), Row(v2,c2)) <- (rows zip rows.take(1) ::: rows) if (v1 != v2 || c1 == c2)) yield v1).sum
Some of the other solutions don't handle the list-is-empty case, but this is largely because sliding has a bug where it will return a partial list if the list is too short. Clearer to me (and also bulletproof) is:
(rows zip rows.take(1) ::: rows).collect{
case (Row(v1,c1), Row(v2,c2)) if (v1 != v2 || c1 == c2) => v1
}.sum
(which is only two characters longer if you keep it on one line). If you need the number skipped also,
val indicated = (rows zip rows.take(1) ::: rows).collect {
case (Row(v1,c1), Row(v2,c2)) => (v1, v1 != v2 || c1 == c2)
}
val countSkip = indicated.filterNot(_._2).length
val sum = indicated.filter(_._2).map(_._1).sum
Fold it:
scala> rows.foldLeft((row1, 0))((p:(Row,Int), r:Row) => (r, p._2 + (if (p._1.value == r.value && p._1.letter != r.letter) 0 else r.value)))._2
res2: Int = 290
(new Row(0, " ", true) +: rows).sliding(2).map { case List(r1, r2) =>
if (r1.value != r2.value || r1.letter == r2.letter) { r2.value }
else { 0 }
}.sum
Of course you can drop the boolean member of Row if you do not need it for something else
Reduce it:
rows.reduceLeft { (prev, curr) =>
if (prev.value == curr.value && prev.letter != curr.letter) {
curr.relevant = false
countSkip += 1
}
curr
}