if then if .. else if Tableau to Power BI - powerbi

I have a tableau report and trying to move things into Power BI. I have a scenario where there is a lot of "IF THEN IF" situations as shown in example
IF [SYSTEM] == "ABC"
THEN [Result Flag]
ELSEIF [SYSTEM] == "XYZ" THEN
IF [Option] == 'HIGH'
AND [Activity] == 'Add' THEN
IF [Add Indicator] == "Yes" THEN "GA"
ELSEIF [Name] == "Add 1"
OR [Name] == "Add 2"
THEN "Not Eligible"
ELSE [Result Flag]
END
ELSEIF [Option] == 'Low' THEN
"2019"
ELSE "Not Eligible"
END
ELSE
In Power BI, we have IF and SWITCH functions but they support "IF THEN ELSE" situation and in my scenario, I have lot of "IF THEN IF" situation. I know Power BI DAX IF and SWITCH Function can handle it but when there are lot of values in the condition(Name field in above example) it is little cumbersome. Is there any better way to handle it?
Appreciate all your help!

IF THEN IF... are nested IF statements and ELSEIF can be handled with SWITCH ( TRUE (), ... ).
There are a ton of possible variations on exactly how to implement this. Here's one as an example:
SWITCH (
[SYSTEM],
"ABC", [Result Flag],
"XYZ",
SWITCH (
TRUE (),
[Option] = "High" && [Activity] = "Add",
SWITCH (
TRUE (),
[Add Indicator] = "Yes", "GA",
[Name] IN { "Add 1", "Add 2" }, "Not Eligible",
[Result Flag]
),
[Option] = "Low", "2019",
"Not Eligible"
)
)

Related

Nested IF functions in DAX power bi

So I have this logic from crystal reports formula builder that I'd like to follow into powerBI's DAX and create a new column with the result:
if {tableA.Col1} = "PAY" and {tableB.Col#} in ["LEG", "HAND"] and {tableA.Col2} = "Y" then "N" else "Y"
I cant seem to create any logical statement to refer to another table within DAX (up to my knowledge) or actually nest multiple ifs like above, here is what I have so far..
Error = IF(tableA[col1] = "PAY" && IF(tableB[col#] = "LEG"&&"HAND" && IF(tableA[col2] = "Y","N","Y"),"Y"),"Y")
Error Column =
IF (
tableA[col1] = "PAY"
&& RELATED ( tableB[col#] )
IN { "HANDS", "LEGS" }
&& tableA[col2] = "Y",
"N",
"Y"
)

MDX (Tableau) to DAX (Power BI) calculations

I have been using Tableau for quite some time but am getting some new experience with Power BI and the use of DAX, and am wondering how I would go about recreating this formula to DAX.
IF [Threshold] <0 and [Threshold] <[Value] then 'Below'
elseif [Threshold] <0 and [Threshold] >[Value] then 'Above'
ELSEIF [Threshold] < [Value] THEN 'Above'
ELSEIF [Threshold] > [Value] THEN "Below"
ELSEIF [Threshold] = [Value] THEN "At"
ELSE "N/A"
END
Looking forward to whoever can assist, thank you!
Assuming you already have a measure called Threshold and a measure called Value:
Indicator = SWITCH(
TRUE(),
[Threshold] <0 && [Threshold] <[Value], "Below",
[Threshold] <0 && [Threshold] >[Value], "Above",
[Threshold] < [Value], "Above",
[Threshold] > [Value], "Below",
[Threshold] = [Value], "At",
"N/A"
)

Power BI Totals evaluation

I have the below table with many values that gives me a total PerecntageChange too.
Date CheckType State Previous Daily PercentChange KPI light
6/13/21 AM AR 29489 33023 11.98% Yellow
6/13/21 AM KY 105496 104648 -.80% Green
I need to create seperate KPI light metrics for totals. How can I do this?
Measure
Percentage change =
VAR __BASELINE_VALUE = SUM('AutoDialerDaily'[PREV_CNT])
VAR __VALUE_TO_COMPARE = SUM('AutoDialerDaily'[CURR_CNT])
RETURN
IF(
NOT ISBLANK(__VALUE_TO_COMPARE),
DIVIDE(__VALUE_TO_COMPARE - __BASELINE_VALUE, __BASELINE_VALUE)
)
Column Color Indicator that drives the KPI light (Conditional Formatting)
COLOR_IND_FORMAT =
//IF(HASONEVALUE(AutoDialerDaily[State]),
SWITCH(
TRUE(),
AutoDialerDaily[Check type] = "STATE AM" && AutoDialerDaily[PRCNT_DIFF] >=16, "RED",
AutoDialerDaily[Check type] = "STATE AM" && AutoDialerDaily[PRCNT_DIFF] >=11 && AutoDialerDaily[PRCNT_DIFF] <=15 , "YELLOW",
AutoDialerDaily[Check type] = "STATE AM" && AutoDialerDaily[PRCNT_DIFF] <=10, "GREEN",
"BLACK")
I'm not sure what needs to happen here.
I want a separate set of "TOTAL Metrics" and to control the color. So, for example if I wanted to say if the Total is 2% that should display as a "BLUE" indicator
You can create a measure as below:
COLOR_IND_FORMAT =
SWITCH(
TRUE(),
VALUES('Table'[CheckType]) = "AM" && [Percentage change] >= 0.10, 1,
VALUES('Table'[CheckType]) = "AM" && [Percentage change] < 0.10, 2)
After this, select the "Conditional Formatting" option from the Format Pane. Select the "Percentage Change" column in the dropdown > Go to Icons > Advanced Controls
In the Advanced Controls box, you can format based on the measure you created. Please refer to the image below:
Final Output:
Hope this solves your problem.

Write a DAX IF expression that in the instance of a Blank Value has a separate result?

I am trying to evaluate on a column against another column in a different table that can only have (3)
outcomes (The output result desired) :
A Match = True
Mismatch = False
Blank = Data missing
So far I have: (Column is on different table)
IF(LOOKUP(Column, Column, Table[Column1])=Table[Column1], "True", "False")
This works but I don't know how to have it also evaluate for blank value for a separate output result??
Use LOOKUPVALUE(), there is no LOOKUP in DAX,
and then another IF. For my understanding you want to look for an empty searchValue
IF (
ISBLANK ( Table1[Column1] ),
IF (
LOOKUPVALUE ( Table2[Column], Table2[Column], Table1[Column1] ) = Table1[Column1],
"True",
"False"
),
"Data missing"
)
If you want to check is blank LOOKUPVALUE-Result then
VAR result =
LOOKUPVALUE ( Table2[Column], Table2[Column], Table1[Column1] )
RETURN
IF (
ISBLANK ( result ),
"Data Missing",
IF ( result = Tabelle1[Column1], "true", "false" )
)

Converting Case statement, Filter from t-SQL query to DAX

I have a problem converting below t-sql query into DAX.
Overview - There are two sample tables - Table1 and Table2 with below schema
Table1 (ID varchar(20),Name varchar(30))
Table2 (CapID varchar(20),CAPName varchar(30), CapID_Final varchar(20))
Please note : There exists one to many relationship between above tables : [ID] in Table2 with [CapID] in Table1
I am trying to derive CapID_Final column in table2 based on conditions as per my t-SQL query in below which works perfectly fine -
SELECT CASE
WHEN [CapID] like 'CA%' and [CAPName]='x12345-Sample'
and [CapID] not in(select [ID] from Table1 where Name='x12345-Sample')
THEN 'Undefined_Cap_1'
WHEN [CapID] like 'CA%' and [CAPName]='z12345-Sample'
and [CapID] not in(select [ID] from Table1 where Name='z12345-Sample')
THEN 'Undefined_Cap_2'
WHEN [CapID] like 'CA%' and [CAPName]='a123-Sample'
and [CapID] not in(select [ID] from Table1 where Name='a123-Sample')
THEN 'Undefined'
ELSE [CapID]
END AS [CapID_Final] from Table2
However, I want the same derivation for CapID_Final column in Power BI in a calculated column using DAX.
So far, I have tried below code - but it returns "Undefined" for even matched conditions -
CapID_Final =
IF(LEFT(Table2[CapID],2)="CA" && Table2[CAPName]="z12345-Sample" &&
NOT
(COUNTROWS (
FILTER (
Table1,CONTAINS(Table1,Table1[ID],Table2[CapID])
)
) > 0),"Undefined_Cap_1","Undefined"
)
I am not familiar with DAX, however I tried and couldn't figure it out.
Could you please let me know how to convert my sql query to equivalent DAX in Power BI?
A SWITCH is basically the equivalent of a CASE clause here:
CapID_Final =
SWITCH (
TRUE (),
LEFT ( Table2[CapID], 2 ) = "CA"
&& Table2[CAPName] = "x12345-Sample"
&& NOT (
Table2[CapID]
IN CALCULATETABLE ( VALUES ( Table1[ID] ), Table1[Name] = "x12345-Sample" )
), "Undefined_Cap_1",
LEFT ( Table2[CapID], 2 ) = "CA"
&& Table2[CAPName] = "z12345-Sample"
&& NOT (
Table2[CapID]
IN CALCULATETABLE ( VALUES ( Table1[ID] ), Table1[Name] = "z12345-Sample" )
), "Undefined_Cap_2",
LEFT ( Table2[CapID], 2 ) = "CA"
&& Table2[CAPName] = "a12345-Sample"
&& NOT (
Table2[CapID]
IN CALCULATETABLE ( VALUES ( Table1[ID] ), Table1[Name] = "a12345-Sample" )
), "Undefined",
Table1[CapID]
)
You might even be able to refactor it a bit to be more code efficient. Assuming I didn't make any logic mistakes:
CapID_Final =
VAR IDs =
CALCULATETABLE ( VALUES ( Table1[ID] ), Table1[Name] = Table2[CAPName] )
RETURN
IF (
LEFT ( Table2[CapID], 2 ) = "CA"
&& NOT ( Table2[CapID] IN IDs ),
SWITCH (
Table2[CAPName],
"x12345-Sample", "Undefined_Cap_1",
"z12345-Sample", "Undefined_Cap_2",
"a12345-Sample", "Undefined"
),
Table1[CapID]
)
As a best-practice never use calculated column. In fact, if extensively used they slow down your model refresh and heavily increase your model weight (because they are not compressed). Instead, calculate it in your back-end database or using M Query.
Having said this, the solution to your question is very simple using a SWITCH function:
SWITCH ( <Expression>, <Value>, <Result> [, <Value>, <Result> [, … ] ] [, <Else>] )
In your case would be as follow:
CapIDFinal:=
SWITCH(TRUE(),
AND(CONDITION_1_1, CONDITION_1_2), "Value if condition 1 is true",
AND(CONDITION_2_1, CONDITION_2_2), "Value if condition 2 is true",
"Value if none of above conditions is true
)