Combining IF formulas - if-statement

I have two IF formulas that I would like to combine - please see attached excel doc.
If C2 = "Blue" =IF(E2="","",IF(((((((B2*(C2-2))*1.02)/(E2-1))/1.02)+(-B2))+(B2))/(B2)<0.65,"NO BET",((C2-1)/(E2-F2)B2)))
If C2 = "Green" =IF(E3="","",IF(((((((B3(C3-2))*1.02)/(E3-1))/1.02)+(-B3))+(B3))/(B3)<0.65,"NO BET",(C3/(E3-F3)*B3)))
The formulas are the same up until after "NO BET". I would like this to be one formula only so that I can change value C2 and it calculates correctly.
Many thanks

I don't have the rep required to comment for clarifications so I've got a couple of possible options depending on what you need. I've also imposed a bit of spacing in my answer so its a bit more readable.
If C2 = "Blue"
=IF(E2="","",IF(((((((B2*(C2-2))*1.02)/(E2-1))/1.02)+(-B2))+(B2))/(B2)<0.65,"NO
BET",((C2-1)/(E2-F2)B2)))
If C2 = "Green"
=IF(E3="","",IF(((((((B3(C3-2))*1.02)/(E3-1))/1.02)+(-B3))+(B3))/(B3)<0.65,"NO
BET",(C3/(E3-F3)*B3)))
In the question you say that the formulas are the same up to NO BET so, assuming that:
you want both forumlas to work on both rows 2 and 3 and
the C column is filled in Green/Blue for all the different rows
this is how it can work for row 2:
=IF(E2="",
"",
IF(
((((((B2*(C2-2))*1.02)/(E2-1))/1.02)+(-B2))+(B2))/(B2)<0.65,
"NO BET",
IF(C2 = "Blue",
((C2-1)/(E2-F2)B2),
IF(C2 = "Green",
(C2/(E2-F2)*B2)
)
)
)
)
If Blue/Green is only ever in C2 and the rest of the C column is irrelevent
=IF(E2="",
"",
IF(
((((((B2*(C2-2))*1.02)/(E2-1))/1.02)+(-B2))+(B2))/(B2)<0.65,
"NO BET",
IF($C$2 = "Blue",
((C2-1)/(E2-F2)B2),
IF($C$2 = "Green",
(C2/(E2-F2)*B2)
)
)
)
)

Related

How to create a measure that can mark two columns conditionally?

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

How do I create calculated column in power bi?

I am new to Power BI and was wondering if someone can help me.
I have a table
A B Status
---------------------------
Asset1 B1 Compliant
Asset1 B2 N/A
Asset2 B1 Non- Compliant
Asset2 B2 Compliant
Asset3 B1 Compliant
Asset3 B2 Compliant
I have to find the Asset which are 100% compliant(Count of Asset where status of all the rows of Column B is Compliant and not non Compliant and we need to ignore N/A.
Out put I want
A 100%Compliant
Asset1 Y
Asset2 N
Asset3 Y
Thanks
You can create an extra column in your table:
100% Cpmpliant =
var asset = Compliant[A]
var allRows = CALCULATE(COUNTROWS(Compliant); FILTER(Compliant; Compliant[A] = asset && Compliant[Status] <> "N/A"))
var compliantRows = CALCULATE(COUNTROWS(Compliant); FILTER(Compliant; Compliant[A] = asset && Compliant[Status] = "Compliant"))
return if (allRows = compliantRows; "Y";"N")
This calculates the rows of the asset without the N/A and compares it against teh rowcount of the compliant. If this is equal, they are all compliant-
Next you can create a visual with two columns ass below:
Edited:
You can create a measure:
IsCompliant =
VAR NumNonCompliant = CALCULATE(COUNTAX('Table', [B]), 'Table'[Status] = "Non - Compliant")
RETURN IF(NumNonCompliant = 0, "Y", "N")
The variable NumConCompliant calculates the number of rows with status = "Non - Compliant". Then it is compared with 0 to find any non-compliance.
Then the measure returns the state of "A" in the given context. This gives you the flexibility of assigning compliance measure to a combination of assets. For example if you filter data on "A = Asset1", the result would be "Y", and (A = Asset1 || A = Asset2) results in a "N". (Since the combination of Asset1 & Asset2 are non-compliant).
Refer to this doc about measures and contexts.

DAX: How do I write an IF statement to return a calculation for multiple (specific) values selected?

This is driving me nuts. Let's say we want to use a slicer which has two distinct values to choose from a dimension. There is A and B.
Let us also say that my Fact table is connected to this dimension, however it has the same dimension with more options.
My slicer now has A, B and (Blank). No biggie.
Let's now say I want to list out all of the possible calculation outcomes by selecting the slicer in a DAX formula, but in my visual I need all those outcomes to be listed in an IF() branched formula:
I can list out A:
IF(MAX(SlicerDim[Column]) = "A", CALCULATE([Calculation], SlicerDim[Column] = "A")
I can list out B:
IF(MAX(SlicerDim[Column]) = "A", CALCULATE([Calculation], SlicerDim[Column] = "A")
I can list out the (Blank) calculation too:
CALCULATE([Calculation], SlicerDim[Column] = Blank())
And I've managed to get a calculation out of it even when all of the slicer elements are on or off, using:
NOT(ISFILTERED(SlicerDim[Column])), CALCULATE([Calculation], SlicerDim[Column] = "A" || SlicerDim[Column] = "B")
Notice I need this IF() branch to actually return a calculation using A & B values, so now I have returns for when A or B or (Blank) or All or None are selected; BUT NOT when multiple values of A & B are selected!
How do I write out this IF() branch for it to return the same thing, but when both A & B are selected? Since there are only two real options in the slicer - I managed to use MIN() and MAX() get it to work by using their names or Index numbers.
IF((MIN(SlicerDim[Column]) = "A" && MAX(SlicerDim[Column]) = "B") || NOT(ISFILTERED(Paslauga[Paslauga])), CALCULATE([Calculation], SlicerDim[Column] = "A" || SlicerDim[Column] = "B")
BUT - I want a more understandable/robust/reusable formula, so that I could list out many selectable values from the slicer and have it return a calculation for specifically selected slicer values.
Please, help.
I've been searching high and low and there seems to not be an easy way to fix this albeit scraping the IF route and just using a damn slicer for this type of dilemma.
TL;DR:
How do I write an IF() branch calculation using DAX to get an outcome when All/None or non-blank or Specific slicer values are selected?
My best effort:
I am looking to improve the first IF() branch to not have to use MIN/MAX, because I would like to be able to reuse this type of formula if there were more than two real options in the slicer:
IF_branch =
IF((MIN(SlicerDim[Column]) = "A" && MAX(SlicerDim[Column]) = "B" || NOT(ISFILTERED(SlicerDim[Column])), CALCULATE([Calculation], SlicerDim[Column] = "A" || SlicerDim[Column] = "B"),
IF(MAX(SlicerDim[Column]) = "A", CALCULATE([Calculation], SlicerDim[Column] = "A"),
IF(MAX(SlicerDim[Column]) = "B", CALCULATE([Calculation], SlicerDim[Column] = "B"),
CALCULATE([Calculation], SlicerDim[Column] = BLANK()))))
Think what you are looking for is CONTAINS and VALUES
VALUES will give you the distinct current selection in scope.
CONTAINS lets you check if a table contains any row with a set of values.
[]
Formulas:
selected Scenarios = CONCATENATEX(VALUES(DimScenario[ScenarioName]);[ScenarioName];";")
Contains Forecast and Budget? =
IF(
CONTAINS(VALUES(DimScenario[ScenarioName]);[ScenarioName];"Forecast") &&
CONTAINS(VALUES(DimScenario[ScenarioName]);[ScenarioName];"Budget")
;"Yes"
;"No"
)

How to Return Text with IF Function in an Array

In Google Sheets, I'm trying to query a column and look for a state abbreviation, and if that abbreviation is a match, then "East" if not then "West"
Wanting to return text values in my column based on state abbreviation. We have territory manager split into two domains--East and West. So, trying to easily sort my data by East/West.
Here's what I have:
=IF(M:M={"AL", "CA", "DE","FL","GA","IA","KY","ME","MD","MA","MN","MS","NH","NJ","NY","ND","RI","SD","TN","VT","VA","WV","WI"},"East","West")
But, when I fill down, it just fills down East, and does not seem to actually query M:M
Thoughts?
Not the cleanest code, but this should work:
=ARRAYFORMULA(IF(LEN(A:A), IF((A:A = "foo")+(A:A = "bar") = 1, "WEST", "EAST"), ))
To use IF with an OR in an ARRAYFORMULA, you evaluate the column with 1s and 0s. The A:A = "foo" will evaluate to 1 if foo is in the cell. So if one of your OR criteria is in the cell, the total value in the IF will be 1.
You have a lot of criteria so writing each of them in will take a while ...
E.g. IF( (A:A = "AL") + (A:A = "CA") ... (A:A = "WI") = 1, "East", "West")
Use ISERROR/MATCH():
=IF(ISERROR(MATCH(M:M,{"AL", "CA", "DE","FL","GA","IA","KY","ME","MD","MA","MN","MS","NH","NJ","NY","ND","RI","SD","TN","VT","VA","WV","WI"},0)),"West","East")

Declaring variables in Power Query M Functions

I am trying to create a reusable function in Power Query that will be used by several tables.
This works well (written in blank query):
let
is_emergency = (color as text) =>
if color = "red" or color = "orange" then
"emergency"
else
"not emergency"
in
is_emergency
I can call it using a custom column like this =emergency([color_column]).
However - my color column contains a lot of extra spaces so I somehow need to call Text.Trim() on the color-parameter. How to write this?
What I thought would work was to just write this:
let
is_emergency = (color as text) =>
color = Text.Trim(color, " "),
if color = "red" or color = "orange" then
"emergency"
else
"not emergency"
in
is_emergency
but this gives me the error Token Literal Expected.
How to write it proper? I am aware I can use the Power Query GUI to create simple functions like this, but my real case is more advanced and I would like to understand the M syntax.
I managed to solve it myself after some research.
let
is_emergency = (color as text) =>
// you need to put the variable declaration inside a new let - in
let
trimmed_color = Text.Trim(color, " "),
// you also need to define the return value and return it in the new let - in
return_value = if trimmed_color = "red" or trimmed_color = "orange" then
"emergency"
else
"not emergency"
in
return_value
in
is_emergency