the right syntax - minimum value - excel

what is the correct syntax to get the minimum value ? I need the Minimum value between these two :
Duration.Days(Date.EndOfMonth(ay) - [#"Date of Employment (Work)"])
Date.Day(Date.EndOfMonth(ay))
i tried List.Min but it gave error
We cannot convert the value 699 to type List.
i also tried but no such function as "MIN"
MIN(Duration.Days(Date.EndOfMonth(ay) - [#"Date of Employment (Work)"])
,Date.Day(Date.EndOfMonth(ay)))

List.Min will work, if you pass a list as the first argument:
List.Min(
{
Duration.Days(Date.EndOfMonth(ay) - [#"Date of Employment (Work)"]),
Date.Day(Date.EndOfMonth(ay))
}
)
Notice I wrapped your two functions in { }, to make them a list.

Related

Conditional Column: Order of Tests Matters?

Condition to Test Date
I am using Power Query to create a status column that checks the date against a specified date, like so:
However, this gives me the following error:
Expression.Error: We cannot convert the value null to type Logical.
Details:
Value=
Type=[Type]
The column does contain empty cells, which I want to report as "null" in the new column. I then tried the following logic, and it errors out as well:
Then I moved the null test to the top, and it finally works:
Why Does Order Matter?
Why does the third query produce the expected results but not the first one? This seems bizarre to me, so if there is something I am missing please let me know.
M is using lazy evaluation in the if statement. If the first statement is true, then it doesn't even bother evaluating the other conditions.
https://learn.microsoft.com/en-us/powerquery-m/m-spec-introduction
For computer language theorists: the formula language specified in
this document is a mostly pure, higher-order, dynamically typed,
partially lazy functional language.
Easy fix
On a step before your filter, choose "remove nulls" or "replace nulls with values"
using catch
If you want more flexibility, you can use a try + catch pair.
Step FirstTry is meant to; be your filter, then I added two ways to handle errors.
let
Source = Table.FromList(sample, Splitter.SplitByNothing(),
type table[Date = nullable date], null, ExtraValues.Error),
sample = {
#date(2020, 1, 1),
"text", null,
#date(2024, 1, 1)
},
filter = #date(2022, 1, 1),
FirstTry = Table.AddColumn(
Source , "Comparison", each filter > [Date], Logical.Type),
WithFallback = Table.AddColumn(FirstTry, "WithFallback",
each try
filter > [Date]
catch (e) => e[Message], type text),
WithPreservedDatatype = Table.AddColumn(WithFallback, "PreserveColumnType",
each try
filter > [Date]
catch (e) => null meta [ Reason = e[Message] ],
type logical)
in
WithPreservedDatatype
things to note
the query steps are "out of order", which is totally valid. ( above sample was referenced "before" its line )
Errors are propagated so an error on step4 could actually be step2. Just keep going up until you find it.
the schema says column [Date] is type date -- but it's actually type any.
What you need is to call Table.TransformColumnTypes to convert and assert datatypes
= Table.TransformColumnTypes( Source,{{"Date", type date}})
Now row 2 will correctly show an error, because text couldn't convert into a date
Better Understanding of NULLs
I was not understanding how Excel (or any other data tool) handles null values and how logical tests are performed on null values. This response on Reddit really helped clarify this in my mind:
https://www.reddit.com/r/excel/comments/xu37dr/comment/iqtmivn/?utm_source=share&utm_medium=web2x&context=3
In short, logical tests involving nulls do not behave like you would expect.
For a deeper dive into this, read this excellent post:
https://bengribaudo.com/blog/2018/09/13/4617/power-query-m-primer-part9-types-logical-null-binary
My Solution
Given this enlightened understanding of nulls and how they behave in logical tests, I now know that I must either:
Convert the null values to empty strings ("") before the query
Test first for nulls in the query
My choice is to test for nulls first within the query, like so:

If i store index number fetched from db in variable & using in select from list by index, m getting err as expected string, int found-Robot Framework

enter image description here
select from list by index ${locator_var} ${inp_msge_type}
--getting error as expected string, int found
select from list by index ${locator_var} 7
-----not getting any error
${inp_msge_type}----contains 7 from DB query the result is stored in this variable, to avoid hard coding we need to do this
Is there any way to write
Do not add links to screenshots of code, or error messages, and format the code pieces accordingly - use the ` (tick) symbol to surround them.
The rant now behind us, your issue is that the keyword Select From List By Index expects the type of the index argument to be a string.
When you called it
Select From List By Index ${locator_var} 7
, that "7" is actually a string (though it looks like a number), because this is what the framework defaults to on any typed text. And so it works.
When you get the value from the DB, it is of the type that the DB stores it with; and probably the table schema says it is int. So now you pass an int to the keyword - and it fails.
The fix is simple - just cast (convert) the variable to a string type:
${inp_msge_type}= Convert To String ${inp_msge_type}
, and now you can call the keyword as you did before.

Hapi/Joi Validation For Number Fails

I am trying to validate number value which will include integer as well as float values. Following is my implementation for the same.
Joi Schema.
const numcheckschema = Joi.object().keys({
v1:Joi.number().empty("").allow(null).default(99999),
v2:Joi.number().empty("").allow(null).default(99999),
v3:Joi.number().empty("").allow(null).default(99999)
})
Object
objnum={
v1:"15",
v2:"13.",
v3:"15"
}
objValidated = Joi.validate(objnum, numcheckschema);
console.log(objValidated);
When i execute the above mentioned code I get an error
ValidationError: child "v2" fails because ["v2" must be a number]
as per the documentation when we tries to pass any numeric value as a string it converts the values to number but here in this case my value is 13. which is not able to convert into number and throwing an error.
Is there any way by which we can convert this value to 13.0
You can use a regex in order to match numbers with a dot, for instance:
Joi.string().regex(/\d{1,2}[\,\.]{1}/)
And then combine both validations using Joi.alternatives:
Joi.alternatives().try([
Joi.number().empty("").allow(null),
Joi.string().regex(/\d{1,2}[\,\.]{1}/)
])
However, I think you may need to convert the payload to number using Number(string value). You need to check the payload type, if it isn't a Number, you need to convert it.
If you want to know more about the regex used in the example, you can test it in here: https://regexr.com/

AQL: return minimum of two values

i am very new to AQL so this question is very simple i guess.
I would like to return the minimum of two values via aql. But min(valA,valB) returns
[1541] invalid number of arguments for function '_AQL:MIN()'
Unfortunatly i coudn't find any functions like min, max in the documentation, so i dont know what "Invalid number of arguments" means.
Here is a minimal reproducable example:
for art in artikel
return {"Preis" : min(art.preis, art.preisE,1)}
AQL:MIN() is defined for arrays not for arbitrary many parameters.
Try to use:
for art in artikel
return {"Preis" : min([art.preis, art.preisE,1])}

preg_match optional elements in string

I have two strings which contain up to 3 elements:
1) anychar[price]{alphanum} e.g. a1\')[=00.00]{a1234}
2) anychar:anychar{alphanum} e.g. a1\'):a2\'){a1234}
...but the {} element is optional and may not always be there. I wrote the following patterns (respectively):
1) /(.+)\[(.+)\]\{*(\w+)*\}*/ - works as expected
2) /(.+)\:(.+)\{*(\w+)*\}*/ - works fine if the {} element is removed, but not with it.
The result array for 2 is as follows:
(
[0] => a1\'):a2\'){a123}
[1] => a1\')
[2] => a2\'){a123}
)
I've tried a few different permutations of the above but no dice. Any ideas?
First you should remove the * after {, } and (\w+).
'/(.+)\:(.+)\{(\w+)\}/'
Gives
array(4) {
[0]=>
string(18) "a1\'):a2\'){a1234}"
[1]=>
string(5) "a1\')"
[2]=>
string(5) "a2\')"
[3]=>
string(5) "a1234"
}
* means either 0, 1 or several, and PCRE tries to find the quickest route it can, so if you make the whole third part optional (by using * everywhere) then the quickest route is to have everything included in the second group and skip the third, that's why your code didn't work.
Now in order to deal with the fact that the third part is optional, you have to use a positive lookahead: in the second group, you will ask pcre to select it only if it can matches another regex after it. The final regex is this:
'/(.+)\:(.+(?=(?:(?<=[^}])$|\{(\w+)\})))/'
What I changed is that:
inside the second group, i added a positive lookahead in the form (?=regex). As said, this means it has to match. Lookahead are not selective by default, which means that they don't create a entry in your final result/they are not returned to you.
inside that lookahead, I created two cases, which means that in order to match, the .+ from the second group will have to match either case of my lookahead.
The first case is very basic, it means end of string not preceded by a }, this will match the string when the 3rd part is not there
the second case if you selector for the 3rd group, we make it selectable so that it will be returned in the results if present

Resources