How do I cast a number to a set - ssas

I think this is a bit of a beginner MDX question. I'm trying to get the following as a calculated member:
Max(Min([Measures].[My Data] / 0.2, 1), 0)
I get #VALUE! as the result, I presume because Min/Max work on sets, and [Measures].[My Data] is in the same scope, so it is a scalar. Basically I want My Data / .2, as long as it is between 1 or zero. If greater than 1, I want 1, if less than 0 I want 0.
Is there any way to cast [Measures].[My Data] to a set? Such that set functions will work on it?

This calculated measure can help or you have to use Min/Max functions?
with
member [Measures].[Value] as ([Measures].[My Data] / 0.2)
member [Measures].[Test] as
IIF([Measures].[Value] < 1,
IIF([Measures].[Value] < 0, 0,[Measures].[Value])
, 1)
[Measure].[Test] is the output you need.
HTH

Related

SQL Server - Strange round behaviour when using variable vs just value

I'm facing this strange issue in SQL Server when dividing a float that it's stored in variable vs when just use the value.
The case is the following:
DECLARE #FLOAT FLOAT = 6.80
print ROUND(#FLOAT / 2, 2, 0);
print ROUND(#FLOAT / 2, 2, 1);
It prints:
3.4
3.39
The second ROUND that truncates the value is giving an wrong value. It should be 3.4 and not 3.39.
But when I use the value without the variable it works properly:
print ROUND(6.80 / 2, 2, 0);
print ROUND(6.80 / 2, 2, 1);
It prints:
3.400000
3.400000
Can anybody help me understanding this issue?
Thanks in advance.
This behaviour is expected. In the first expression you are using a float, and in the latter literal decimal. These two data types are very different. The former is a Base 2 value, and the latter Base 10.
6.80 cannot be stored precisely using Base 2, and so it's actual value is closer to the value 6.79999999999999982236431605997495353221893310546875. As you can see, this is ever so slightly less than 6.8 and so when you divide that number by 2, you get a number ever so slightly less than 3.40,probably 3.399999999999999911182158029987476766109466552734375.
As a result ROUND is rounding the number correctly, as the above value rounded to 2 digits is 3.39 not 3.40.
For the literal, 6.80 is represented exactly, and so is 6.80 / 2 (3.40) so when you round that number again to 2 decimal places, you still get 3.40

How to turn a number to zero if the result of a calculation is negative inside a squareroot in BigQuery?

I have a query that can result in a negative squareroot, leading to an error. I'm using BigQuery.
Is there a way to turn the result in to a 0 if inside de POWER() function is less than 0?
Here's my query:
UPDATE `table` SET
SW_FERTL = CASE
WHEN POR_CLEAN < 0.04 THEN 1
WHEN (1/((rho_matrix-RHOB)/(rho_matrix-rho_mud_filt)))*POWER(((RW/ILD)+(POWER((0.3*VSHALE/2), 2)-(0.3*VSHALE/2))), 0.5) > 1 THEN 1
ELSE (1/((rho_matrix-RHOB)/(rho_matrix-rho_mud_filt)))*POWER(((RW/ILD)+(POWER((0.3*VSHALE/2), 2)-(0.3*VSHALE/2))), 0.5)
END
WHERE DEPTH_M IS NOT NULL
The negative squareroot can happen here:
POWER(((RW/ILD)+(POWER((0.3*VSHALE/2), 2)-(0.3*VSHALE/2))), 0.5)
So, if the result of
((RW/ILD)+(POWER((0.3*VSHALE/2), 2)-(0.3*VSHALE/2)))
is less than zero, I would like to set it to zero instead.
Is it possible to do this?
Thanks in advance!
You can use GREATEST():
GREATEST(0, ((RW/ILD)+(POWER((0.3*VSHALE/2), 2)-(0.3*VSHALE/2))))
In many (if not most) practical cases you want to distinguish zeros coming from power(-4, .5) vs. power(0, .5)
So, you can consider below approach
SAFE.POWER(((RW/ILD)+(POWER((0.3*VSHALE/2), 2)-(0.3*VSHALE/2))), 0.5)
above returns NULL in case of negative argument

Meaning of last values in test sampling

data.test_1$pred <- ifelse(data.test_1$score<=cut_off, 0, 1)
I am trying to get the predictions from my test sampling. Since I am running this code I would like to know the meaning of the last values in the code "[...], 0, 1)" stands for.
data.test_1$pred <- ifelse(data.test_1$score<=cut_off, 0, 1)
If data.test_1$score is less than or equal to the cut_off value then return 0 else return 1

Optimizing specific numbers to reach value

I'm trying to make a program, that when given specific values (let's say 1, 4 and 10), will try to get how much of each value is needed to reach a certain amount, say 19.
It will always try to use as many high values as possible, so in this case, the result should be 10*1, 4*2, 1*1.
I tried thinking about it, but couldn't end up with an algorithm that could work...
Any help or hints would be welcome!
Here is a python solution that tries all the choices until one is found. If you pass the values it can use in descending order, the first found will be the one that uses the most high values as possible:
def solve(left, idx, nums, used):
if (left == 0):
return True
for i in range(idx, len(nums)):
j = int(left / nums[idx])
while (j > 0):
used.append((nums[idx], j))
if solve(left - j * nums[idx], idx + 1, nums, used):
return True
used.pop()
j -= 1
return False
solution = []
solve(19, 0, [10, 4, 1], solution)
print(solution) # will print [(10, 1), (4, 2), (1, 1)]
If anyone needs a simple algorithm, one way I found was:
sort the values, in descending order
keep track on how many values are kept
for each value, do:
if the sum is equal to the target, stop
if it isn't the first value, remove one of the previous values
while the total sum of values is smaller than the objective:
add the current value once
Have a nice day!
(As juviant mentionned, this won't work if the skips larger numbers, and only uses smaller ones! I'll try to improve it and post a new version when I get it to work)

How to get value in percentage (%) or pixel (px) after doing some maths? | Lesscss

I've a little and simple question regarding Lesscss. How I can get calculated value in percentage or pixel in Lesscss. Like, I have this : ((1 / 1) * 1) = 1. I know the answer is 1, but I want this "1" to be in percentage or pixel like this: "1%" or "1px".
I just need to insert or put percentage (%) or pixel (px) sign in the calculated value.
I'd appreciate the help.
use:
unit(#yourvalue,px)
or
unit(#yourvalue,~"%")
read more here
Additional note:
by default the first occurring unit in the calculation will be assigned to the result. For example unit((1 / 2 * 3),px); and (1px / 2% * 3rem) will both return 1.5px.
I got the solution. What's required is to simply multiply (1%) or (1px) with the calculated value.
For Percentage: ((1 / 1) * 1) * 1% = 1%
For Pixel: ((1 / 1) * 1) * 1px = 1px
Good Luck! (Y).