If statement with division - Power Query - if-statement

Trying to enter an if statement into the query so that if the UOM says Btl and the Min Quantity is 0 that it just returns the Unit Price. If the Min quantity is not 0 but the UOM equals BTL then divide Unit Price/Min Quantity. If the UOM does not equal BTL, I want to divide Unit Price by the Case Pack.
I'm not sure what I'm missing here.

Put it on separate lines and you can tell what you are missing
You need if .. then ... else parts
So your last row is missing the then and the else
if [UOM]="BTL" and [Min Qty]=0 then [Unit Price] else
if [UOM]="BTL" and [Min Qty]<>0 then [Unit Price]/[Min Qty] else
if [UOM]<>"BTL" then [Unit Price]/[Case Pack] else
null

Related

Tableau - IF Statement with aggregation

I need to do a aggregation under IF statement in a calculated field
If a city is the same as selected by the user (parameter PAR_SELECT_CITY);
In case the condition 1 is true, then SUM(Number of records) - [PAR_SELECT_QTY]
[PAR_SELECT_QTY] is a parameter that user choose to deduct from the total quantity
In case the condition 1 is false, then SUM(Number of records)
IF [City] = [PAR_SELECT_CITY] THEN
SUM([Number of Records])-[PAR_SELECT_QTY]
ELSE
SUM([Number of Records])
END
However, IF Statament does not accept to mix aggregation and not aggregation
How do I solve this issue?
As your error suggests, the issue is the mix of aggregate and "row level" data. In Tableau, you ideally want your row level data to be contained within an aggregate function.
i.e. sum(if true then 1 end) instead of if true then sum(1) end
For your example, you could try
SUM([Number of Records])
-
AVG(IF [City] = [PAR_SELECT_CITY] THEN [PAR_SELECT_QTY] ELSE 0 END)
Your PAR_SELECT_CITY also needs to return an aggregate number. If PARA_SELECT_CITY = 5 (for example) and your dataset contains 100 rows, the AVG(PARA_SELECT_CITY) will also be 5, whereas SUM(PARA_SELECT_CITY) would return 500. Therefore the AVG should work as an aggregate function that returns the desired value.

Creating a calculated boolean field based on thresholds

I'm analysing some data using Power BI, and I have a field in the data called deflection. Normally the value in the field is 0, but a small number of results will have other values ranging from -360 to +360.
Within the dataset, any value > 2 or < -2 might be significant.
I would like to create a new boolean value to indicate if the deflection value is significant or not.
Any value between -2 and + 2 is NOT significant - bSigDeflection = False
Any other value - bSigDeflection = true
How do I approach this?
I have started by using an abs(deflection) value in the sql query, just to keep everything positive. I could write a sql CASE statement that would probably work but I feel that there must be a simpler way to achieve this in Power BI that I am missing.
Any suggestions, clever people?
Thanks
You may use TransformColumns to replace the value with the bool. Or you can use the same code with Table.AddColumn if you want to keep the original value.
let
Source = ...,
coerceDefelectBool = (input) as logical =>
if input < 2 or input > 2 then true else false,
Final = Table.TransformColumns(
Source,
{"Deflection", coerceDefelectBool},
null, MissingField.Ignore
)
in
Final
If you wanted a measure, not a DAX calculated column, you can try something like this.
[ bSigDeflection ] = SWITCH (
TRUE,
SELECTEDVALUE( [Table].[Deflection] ) > 2, TRUE(),
SELECTEDVALUE( [Table].[Deflection] ) < 2, TRUE(),
FALSE()
)

i need to replace NaN with blank space where the operation fails in dax power bi

The table is like the image:
I need to keep the NaN as empty but when I use IFERROR() and put blank() in the ValueifError it just deletes the entire row which I dont want. Is there any way to replace NaN with a blank space
I used the dax below:
oscar wins = SUM(Films[OscarWins])/SUM(Films[OscarNominations])
Try using the DIVIDE function instead of '/' operator.
Ex: Test = DIVIDE(Col1,Col2)
You can handle the case when denominator is 0 as below. This will simply check, if the denominator is 0, it will return BLANK(). Other case it will return the result from normal calculation.
oscar wins =
IF(
SUM(Films[OscarNominations]) = 0,
BLANK(),
SUM(Films[OscarWins])/SUM(Films[OscarNominations])
)
It is likely your SUM(Films[OscarNominations]) is returning 0, resulting in a divide by zero.
For your IFERROR fix, right click on a field value in your visual and select "Show items with no data".
Alternatively, on error, return 0. It really depends on how you want your audience to interpret the data. In reality, it is Not A Number (NaN)...

IF Statement with multiple conditions and an overriding value

Thanks in advance for helping with this.
I have an issue where this I need the following formula to calculate this- i would advise seeing the picture in the link to fully understand the question (as I'm probably not going to explain myself!);
Calculating Risk Score
Condition 1: If Blood Result is less <=9 or Score <=6 or Tumour Size = <5mm show as text value Low
Condition 2 : If Blood Result 10-20 or Score = 7 or Tumour Size 5mm-9mm show as text value Medium
Condition 3: If Blood Result >=20 or Score = 8 or Tumour Size >=10mm show as text value High
The issue I am having is that any person can have a value from any of the condition, but I need to display the overriding value.
Example:
Blood Result = 5 (condition 1)
Score = 7 (condition 2)
Tumour Size = 10mm (condition 3)
SHOW VALUE: HIGH.
The problemn i'm having is that when I'm doing IF statements, as condition 1 of blood result is True, its always displaying Low without looking at the other values which could overwrite it.
I have only tried nested IF statements with AND OR in them, but no luck.
See examples of values
you can do one thing for that type condition
store your message in one variable, for example, var = msg
if condition 1 true assign msg = "Low"
if condition 2 true assign msg = "Medium"
if condition 3 true assign msg = "High"
after checking all conditions print your message.
I hope this will help you

How to write a complex calculated field in Data Studio with regex?

I have been trying to make this regular expression REGEX filter work in Google Data Studio. It is supposed to do the following
Check the field "src_id" and COUNT all the values containing "widget".
Check the field "Page" and COUNT all the values starting with a "/" and ending with "/start".
Check the field "real_title" and NOT COUNT any value containing "-".
I have tried using the code below but it's not providing the correct result:
COUNT(CASE WHEN REGEXP_MATCH(src_id, "^widget" ) THEN 1
WHEN REGEXP_MATCH(Page, ".*(/start)$") then 1
WHEN REGEXP_MATCH(real_title, "^[^-]") then 1
ELSE 0 END)
I expect the result to "52" but it's giving me "582. I need help to spot what I'm doing wrong.
The problem is your count() - it is counting all the entries including zeroes.
either use sum() or just use the case statement and sum where you want it .
Examples
TestField1
COUNT(CASE WHEN REGEXP_MATCH(Page Title , "^How.*" ) THEN 1
ELSE 0 END)
This returns 58 - the number of page titles on my site.
TestField2
Sum(CASE WHEN REGEXP_MATCH(Page Title, "^How.*" ) THEN 1
ELSE 0 END)
This returns 7 - the number of titles on the site that start with "How"
You really don't need the sum() function in most cases because you can sum the field in the places you need it.