Kusto - Conditional formating in Kusto - azure-data-explorer

I have a Kusto Query where the result is in the table format. But when I apply the conditional format it does not highlight the cell with the correct color as per the rule I created.
for example--I have a column with duraion and set the rule as below
Green if > 0
Blue if > 1
Yellow if > 5
Red if > 20
But I see some of the cell that has value 2.5 is highlighted in Red . where the color should be blue. When I delete the rule for Red, the cell changes to Yellow color. Is there a solution for this or right way to apply rule.Thanks. This is projecting a wrong alert. Thanks for any inputs.
Screenshot of column with formating
some of the values are in red which should be in blue as per the rules (refer screenshot below)
You can see the rules below
datatable (AvgDuration: string, AvgDurationWin: string, AvgDurationLinux: string, MinDuration: string, MaxDuration: string) [
"0.0666 s","0.0732 s","0.0525 s","0.015 s","0.684 s",
"0.0663 s","0.0712 s","0.0535 s","0.015 s","0.851 s",
"0.0649 s","0.0700 s","0.0521 s","0.014 s","0.674 s",
"25.050 s","17.614 s","18.428 s","13.133 s","56.284 s",
"0.0982 s","0.1074 s","0.0805 s","0.021 s","1.078 s",
"0.0982 s","0.1046 s","0.0814 s","0.021 s","1.041 s",
"0.0982 s","0.1058 s","0.0813 s","0.021 s","1.106 s",
"0.0987 s","0.1089 s","0.0814 s","0.022 s","1.039 s",
"0.0992 s","0.1074 s","0.0817 s","0.022 s","1.032 s"
]

This is because your values are string. If you want to calculate it by seconds and have conditional formatting you can do the following:
Use the round() function to limit the number of digits after the dot
Indicate the unit in the column name
For example:
| summarize AvgDurationInSeconds = avgif round((Duration, Tests == "Success"), 2)

Related

Place parentheses around characters separated by comma using regex in r

I'd like to add parentheses around grouped text separated by a comma using stringr. So if there is text that is separated by one or more commas, then I'd like parentheses around the text. There will always be a "=" before this type of string begins and there will either be a space or nothing (vector ends) after the string. Is there a generalized way to do this? Here's a sample problem:
Sample:
a <- data.frame(Rule = c("A=0 & B=Grp1,Grp2", "A=0 & B=Grp1,Grp3,Grp4 & C=1"))
a
Rule
1 A=0 & B=Grp1,Grp2
2 A=0 & B=Grp1,Grp3,Grp4 & C=1
Desired Output:
Rule
1 A=0 & B=(Grp1,Grp2)
2 A=0 & B=(Grp1,Grp3,Grp4) & C=1
Here is another potential solution. I have altered the example input to show that it works with multiple "Grp's" per line:
library(stringr)
a <- data.frame(Rule = c("A=0 & B=Grp1,Grp2",
"A=0 & B=Grp1,Grp3,Grp4 & C=1 & D=Grp5,Grp6"))
str_replace_all(a$Rule, "=([^, &]+,[^ $]+)", "=(\\1)")
#> [1] "A=0 & B=(Grp1,Grp2)"
#> [2] "A=0 & B=(Grp1,Grp3,Grp4) & C=1 & D=(Grp5,Grp6)"
Created on 2022-11-23 by the reprex package (v2.0.1)
Explanation:
regex = "=([^, &]+,[^ $]+)", "=(\\1)"
=( starting with an equals sign, capture a group
[^, &]+, with one or more characters that aren't ",", " ", and "&" followed by a comma
[^ $]+) followed by one or more characters that aren't " " or the end of the line ("$")
=(\\1) then replace the equals sign and add parentheses around the captured group (e.g. the Grp1,Grp2)
This should work:
Find: (([A-Za-z\d]+,)+[A-Za-z\d]+)
Replace: ($1)
Explanation:
[A-Za-z\d] is any alphanumeric character.
The inner group looks for 1 or more copies of groups of alphanum characters separated by commas. (e.g. Abcd1,Abcd2,)
The outer group then looks for the closing alphanumeric group, which doesn't have a comma after it. (e.g. Abcd3)
These are concatenated then the whole group is captured.
Last thing to do is the replacement, which is pretty self explanatory.

Naming the rows in a matrix in R

I am trying to name the rows in a matrix, but it adds the prefixes 'X.', 'X..' etc. in front of these names. Also, the row names don't come out properly. For example, the first-row name is supposed to be 'e subscript (t+1)' but it shows something else. The even-numbered row names should be vacant, but they are given names. Could you help please?
Please see the dataset here.
Below is the code I used:
rownames(table2a)=paste(c("$e_t+1$"," ","$r_t+1$", " ","$\\Delta y_{n,t+1}$"," ",
"$s_{n,t+1}$"," ", "$d_t+1-p_t+1$"," ", "$rb_t+1$"," "))
Included libraries: matrix, dplyr, tidyverse, xtable.
Below is the data from dput(table2a):
structure(c(-0.011918875562309, 0.0493186644094629, 0.00943711646402318,
0.0084043692395113, 0.0140061617086464, 0.00795790133389348,
-0.00372516684283399, 0.00631517247007723, 0.00514156266584497,
0.0039339752041611, 0.0148362913561212, 0.00793003246354337,
-0.0807656037164587, 0.0599852917766847, 0.991792361285981, 0.0102220639400435,
-0.00608828061911691, 0.00967903407684488, 0.010343002117867,
0.00768101625973846, 0.0578541455030235, 0.00478481429473926,
-0.00902328873121743, 0.00964513773477125, -0.799680080407018,
0.340494864072598, 0.0519648273240202, 0.0580235615884655, 0.0850517813830584,
0.0549411579861702, -0.0665428760388874, 0.0435997977143392,
-0.032698572959578, 0.027160069487786, 0.114163705951583, 0.0547487519805466,
0.352025366916776, 0.197746547959218, 0.0476825327079758, 0.0336978915546042,
0.0464511908714403, 0.0319077480426568, 0.904849333951824, 0.0253211146465119,
0.132904050913606, 0.0157735418364402, 0.0653710645280691, 0.0317960059066269,
0.939695537568421, 0.612311426298072, -0.0578948128653228, 0.104343687684969,
-0.0744692071400603, 0.0988006057025484, 0.121089017775182, 0.0784054537723728,
0.0345069733304992, 0.048841914052704, -0.090885199308955, 0.0984546022582597,
-0.280821673428002, 0.248826811381596, -0.0288068135696716, 0.0424024540117092,
-0.0239685609446809, 0.0401498953370305, 0.00219488911775388,
0.0318618569231297, 0.066433933135983, 0.0198480335553826, 0.871940074366622,
0.0400092888905855), .Dim = c(12L, 6L), .Dimnames = list(c("$e_t+1$",
" ", "$r_t+1$", " ", "$\\Delta y_{n,t+1}$", " ", "$s_{n,t+1}$",
" ", "$d_t+1-p_t+1$", " ", "$rb_t+1$", " "), c("ex_stock_ret_100.l1",
"real_int_100.l1", "Chg_1month.l1", "spreads.l1", "log_dp.l1",
"rb_rate_100.l1")))
My desired output (row & column names) is as shown in this picture
enter image description here
In case you want to remove that prefix, you can do the following:
rownames(table2a) <- substring(rownames(table2a), 2)
you can remove the first X and all following dots (no matter how many there are) with the gsub command:
rownames(table2a) <- gsub("^X\\.*","",rownames(table2a))
^ = beginning of the string;
X = your actual X;
\\. = a dot;
* = 0 or more of the before mentioned (in this case \\.); so in total ^X\\. means: if you find X as the first letter and all possible dots following directly behind it.
gsub replaces this find with "", meaning nothing, leaving only whatever comes after
EDIT:
to also get rid of every 2nd rowname, add a little something extra:
rownames(table2a) <- gsub("^X\\.*[1-9]*","",rownames(table2a))
which gets rid of any number directly behind the dots. This should leave those rows empty.

Remove quotes if "=" (equal) sign exists in the middle of the string. REGEX

In this string the character “=” differentiates attributes for a product, and commas distinguish variables within an attribute. However, we found that sometimes extra quotes have been added when there are no variables to put together.
The complete string is :
Uso="Protector para patas de silla,mesas,escaleras,muebles","Topes,4-Tipo=Topes,regatones",2-Familia=Ferretería y Plomería,regatones,7-Contenido="12 unidades,4-Origen=China,4-Material=Goma,2-Modelo=Goma transparente,9-Incluye=12 unidades,3-Color=Transparente"
This is right:
Uso="Protector para patas de silla,mesas,escaleras,muebles"
This is wrong:
"Topes,4-Tipo=Topes,regatones",2-Familia=Ferretería y Plomería,regatones,7-Contenido="12 unidades,4-Origen=China,4-Material=Goma,2-Modelo=Goma transparente,9-Incluye=12 unidades,3-Color=Transparente"
Categoría="Topes,4-Tipo=Topes,regatones",2-Familia=Ferretería y Plomería,regatones,7-Contenido="12 unidades,4-Origen=China,4-Material=Goma,2-Modelo=Goma transparente,9-Incluye=12 unidades,3-Color=Transparente"
I´ve tried "|w+=" but selects all quotes. I don´t want to select text between quotes, the goal is select and remove these quotes.
We want to remove those quotes that contains an equal in between. The quotes that are ok and need to stay are those used to separate commas within the string, differentiating the variables from the string.
The regex needs to detect an = contained into and opening and closing quotes, but considering text in between. And once this is detected remove those quotes, which no need to be there.
Thanks!
I understand the quoted substring should be preceded with =. Then, you need
gsub('="([^"=]*=[^"]*)"', '=\\1', x)
See the R demo online:
x <- '10-Uso="Protector para patas de silla,mesas,escaleras,muebles",6-Características=Regaton interior 1 1/4 plástico blanco 4 unidades,1-Marca=Nagel,Tipo=Topes,5-Medidas=3 cm,3-Categoría=Topes y regatones,7-Contenido=4 unidades,4-Tipo=Regatones,2-Familia=Ferretería y Plomería,9-Incluye=4 regatones plásticos,regatones,4-Origen="Argentina,4-Material=Plástico,2-Modelo=Regatón interior 1 1/4,3-Color=Blanco"'
cat(gsub('="([^"=]*=[^"]*)"', '=\\1', x))
## => 10-Uso="Protector para patas de silla,mesas,escaleras,muebles",6-Características=Regaton interior 1 1/4 plástico blanco 4 unidades,1-Marca=Nagel,Tipo=Topes,5-Medidas=3 cm,3-Categoría=Topes y regatones,7-Contenido=4 unidades,4-Tipo=Regatones,2-Familia=Ferretería y Plomería,9-Incluye=4 regatones plásticos,regatones,4-Origen=Argentina,4-Material=Plástico,2-Modelo=Regatón interior 1 1/4,3-Color=Blanco
So, the quote after muebles is kept and quote after blanco is removed.
How does this work?
=" - matches =" substring
([^"=]*=[^"]*) - matches and captures into Group 1:
[^"=]* - zero or more chars other than " and =
= - a = sign
[^"]* - any 0+ chars other than "
" - matches ".
The replacement pattern is a = and the value stored in Group 1 memory buffer (\1, a replacement backreference).
See the regex demo.

How to combine two stacked bar charts onto the same axis

I have been tasked to make a figure with two stacked bar charts on the same axis.
While this is generally easy enough, I have run into trouble since the two bar charts do not have a set of common values (like year for instance) that they share in common.
The dataset I am using can be found below:
clear
input str42 outcomes perct str42 highcred hperct
"Certificate & Diploma Only" 8.33 "Certificate & Diploma" 8.33
"Associate Only" 2.93 "Associate" 14.29
"Bachelor Only" 11.36 "Bachelor" 6.93
"Certificate + AA" 2.2 "" .
"Associate + Bachelor" 4.33 "" .
end
My code is the following:
*B. Create a seperate variable for each value of outcomes
levelsof outcomes, local(out)
tokenize "1 2 3 4 5"
foreach level of local out {
gen outcome`1' = .
replace outcome`1' = perct if outcomes=="`level'"
mac shift
}
*C. Create a seperate variable for each value of highgred
levelsof highcred, local(high)
tokenize "1 2 3"
foreach level of local high {
gen highcred`1' = .
replace highcred`1' = hperct if highcred=="`level'"
mac shift
}
//2: Create Bar graphs
*A. Bar 1
graph bar outcome1-outcome5, stack saving(bar1)
*B. Bar 2
graph bar highcred1-highcred3, stack saving(bar2)
*C. Combine graphs
graph combine bar1.gph bar2.gph, ycommon
The ycommon option works as intended. However, your solution for stacking the bars in separate graphs and then combining them is problematic in the sense that the two graphs share the same colors, which makes it impossible to distinguish the different categories. An additional challenge is how these categories can be incorporated in a single legend.
Below you can find a solution that addresses both of these problems:
levelsof outcomes, local(out)
levelsof highcred, local(high)
local highcopy "`high'"
local c1: word count `out'
local c2: word count `high'
local colors1 ebblue pink brown
local colors2 `colors1'
forvalues i = 1 / `= `c1' + `c2'' {
generate outcome`i' = .
gettoken outc out : out
if `i' <= `c1' replace outcome`i' = perct if outcomes == "`outc'"
if `i' > `c1' {
gettoken color colors1 : colors1
local bars1 `bars1' bar(`i', color(`color'))
}
if `i' <= `c2' {
generate highcred`i' = .
gettoken highcc highcopy : highcopy
replace highcred`i' = hperct if highcred == "`highcc'"
gettoken color colors2 : colors2
local bars2 `bars2' bar(`i', color(`color'))
}
if `i' <= `c1' local legend `legend' label(`i' "`outc'")
else {
gettoken highc high : high
local legend `legend' label(`i' "`highc'")
}
}
order outcome* high*
graph bar outcome1-outcome8, stack ///
ylabel(, nogrid) ///
graphregion(color(white)) ///
`bars1' ///
name(bar1, replace) ///
legend(`legend')
graph bar highcred1-highcred3, stack ///
ylabel(, nogrid) ///
yscale(off) ///
graphregion(color(white)) ///
`bars2' ///
name(bar2, replace)
grc1leg bar1 bar2, ycommon graphregion(color(white)) legendfrom(bar1)
Adding the option blabel(bar, position(base)) in each graph bar command will produce:
Note that the community-contributed command grc1leg is used to create the combined graph.
I don't really understand the data. I guess that the order of values is worth preserving, although I don't think your code does that. I suggest that you will be much, much better off with a different data structure, horizontal bars and no stacking. graph bar (asis) is a better idea to avoid nonsense about means in the legend if you have a legend, but you don't need a legend at all.
For this, you would need to install labmask from the Stata Journal (search labmask to get a link).
You should be able to use better text than outcomes highcred.
clear
input str42 outcomes perct str42 highcred hperct
"Certificate & Diploma Only" 8.33 "Certificate & Diploma" 8.33
"Associate Only" 2.93 "Associate" 14.29
"Bachelor Only" 11.36 "Bachelor" 6.93
"Certificate + AA" 2.2 "" .
"Associate + Bachelor" 4.33 "" .
end
rename (outcomes-hperct) (x1 p1 x2 p2)
gen id = _n
reshape long x p , i(id) j(which)
sort which id
replace id = _n
drop in 9/10
labmask id, values(x)
label def which 1 "outcomes" 2 "highcred"
label val which which
graph hbar (asis) p, over(id) over(which) nofill scheme(s1color) ytitle(percent) ///
bar(1, bfcolor(none)) blabel(total, pos(base) format(%3.2f)) yla(none) ysc(alt)

capital letters in firt letter

In python, I want a program that turn the first leter on a word capital letter.
For exemple:
turn "a red apple is sweeter than a green apple" in "A Red Apple is Sweeter Than A Green Apple"
How can I do?
I've tried this:
d = input('insert a quote')
def mydic(d):
dic = {}
for i in d:
palavras = dic.keys()
if i in palavras:
dic[i] += 1
else :
dic[i] = 1
return dic
You could use the title() method.
For example:
sentence = str(input("Insert a quote: ")).title()
print(sentence)
Input: a red apple is sweeter than a green apple
Output: A Red Apple Is Sweeter Than A Green Apple
What you want to do is this:
split the input string into words ie. string.split(' ') splits a given string by spaces, returns a list.
for each word, capitalize the first letter and concatenate onto a bigger string ie. word[:1].upper() + word[1:] this will uppercase the first letter
Add all the words back into a list and return it.

Resources