Using CASE or IF Statement in Power BI - if-statement

I want to get a percentage of White people separate while the rest of the races will be People of color in a project am currently working in. Please I need help how to write the DAX either using CASE or IF statement.
I used SWITCH and it gave error
Race_Ethnicity =
SWITCH(TRUE(),
'Contact'[race_and_ethnicity] = "White or European", "White or European",
'Contact'[race_and_ethnicity] = "Black or African", "Black or African",
'Contact'[race_and_ethnicity] = "Hispanic or Latinx", "Hispanic or Latinx",
'Contact'[race_and_ethnicity] = "Prefer not to answer", "Prefer not to answer",
'Contact'[race_and_ethnicity] = "South Asian", "South Asian",
'Contact'[race_and_ethnicity] = "East Asian", "East Asian",
'Contact'[race_and_ethnicity] = "Middle Eastern", "Middle Eastern",
'Contact'[race_and_ethnicity] = "Southeast Asian", "Southeast Asian",
'Contact'[race_and_ethnicity] = "Pacific Islander", "Pacific Islander",
'Contact'[race_and_ethnicity] = "Indigenous","Indigenous",
'Contact'[race_and_ethnicity] = "Other, please specify:", "Other, please specify:",
'Contact'[race_and_ethnicity] = ".*(;).*", "Mixed Race or Ethnicity",
)
I am expecting to have the percentage of White or European and the percentage of People of color

When doing something akin to SUMIF in Power BI, we need to invoke CALCULATE. This function allows you to set filters that should be followed for parts of the calculation wrapped in CALCULATE.
Here are two measures. One for percentage of whites/europeans:
WOE % =
VAR _count =
CALCULATE (
COUNTROWS ( 'Contact' ) ,
'Contact'[race_and_ethnicity] = "White or European"
)
RETURN
DIVIDE (
_count ,
COUNTROWS ( 'Contact' )
)
And one for the opposite:
POC % =
VAR _count =
CALCULATE (
COUNTROWS ( 'Contact' ) ,
'Contact'[race_and_ethnicity] <> "White or European"
)
RETURN
DIVIDE (
_count ,
COUNTROWS ( 'Contact' )
)

Related

DAX summarize with measures for This YR Last YR

I am using DAX SUMMARIZE() as shown in this YT video. It works great to summarize measurement for a Dimension table with relationship to Fact table. But when I try to introduce a filtered measure for "This Year" and "Last Year" columns, the calculation ignores the summarized columns and calculates the entire year.
How can I write the query to add "This Year" and "Last Year" columns that respect the summarized values.
DAX query (#1) >
Disposal Attributes (calc field) =
VAR VolumeTotal = SUM('Piped Trucked Volume'[volume_bbls])
VAR vol_CurrentYear = CALCULATE(VolumeTotal, FILTER('Calendar', 'Calendar'[Year] = YEAR(TODAY())) )
VAR vol_PriorYear = CALCULATE(VolumeTotal, FILTER('Calendar', 'Calendar'[Year] = YEAR(TODAY())-1) )
var ListTotal =
SUMMARIZE (
'Disposal',
[unified_disposalId],
[Disposal (unified_name)],
[Disposal (groups)],
"Volume", FORMAT(VolumeTotal, "#,#"),
"Vol_ThisYr", FORMAT(vol_CurrentYear, "#,#"),
"Vol_PriorYr", FORMAT(vol_PriorYear, "#,#")
)
RETURN
FILTER(
ListTotal, VolumeTotal > 0
)
DAX query (#2) >
Disposal Attributes (calc field) =
VAR VolumeTotal = SUM('Piped Trucked Volume'[volume_bbls])
VAR vol_CurrentYear = CALCULATE(VolumeTotal, FILTER('Calendar', 'Calendar'[Year] = YEAR(TODAY())) )
VAR vol_PriorYear = CALCULATE(VolumeTotal, FILTER('Calendar', 'Calendar'[Year] = YEAR(TODAY())-1) )
var ListTotal =
SUMMARIZE (
'Disposal',
[unified_disposalId],
[Disposal (unified_name)],
[Disposal (groups)],
"Volume", FORMAT(VolumeTotal, "#,#")
)
RETURN
ADDCOLUMNS(
FILTER(
ListTotal, VolumeTotal > 0
),
"Vol_ThisYr", FORMAT(vol_CurrentYear, "#,#"),
"Vol_PriorYr", FORMAT(vol_PriorYear, "#,#")
)
Relationships >
Result >
with "VAR vol_CurrentYear" you always get the same value. To put calculations into the row context use CALCULATE() in the ADDCOLUMNS(). You can create measures instead of variables and use them then in ADDCOLUMNS it will have the same effect.
Disposal Attributes (calc field) =
VAR VolumeTotal = SUM('Piped Trucked Volume'[volume_bbls])
--VAR vol_CurrentYear = CALCULATE(VolumeTotal, FILTER('Calendar', 'Calendar'[Year] = YEAR(TODAY())) )
--VAR vol_PriorYear = CALCULATE(VolumeTotal, FILTER('Calendar', 'Calendar'[Year] = YEAR(TODAY())-1) )
var ListTotal =
SUMMARIZE (
'Disposal',
[unified_disposalId],
[Disposal (unified_name)],
[Disposal (groups)],
"Volume", FORMAT(VolumeTotal, "#,#")
)
RETURN
ADDCOLUMNS(
FILTER(
ListTotal, VolumeTotal > 0
),
"Vol_ThisYr", FORMAT(CALCULATE(VolumeTotal, FILTER('Calendar', 'Calendar'[Year] = YEAR(TODAY())) ), "#,#"),
"Vol_PriorYr", FORMAT(CALCULATE(VolumeTotal, FILTER('Calendar', 'Calendar'[Year] = YEAR(TODAY())-1) ), "#,#")
)
The answer from Mik got me down the correct path.
To put calculations into the row context use CALCULATE() in the ADDCOLUMNS(). You can create measures instead of variables and use them then in ADDCOLUMNS it will have the same effect.
I noticed that the "variables" weren't working at all. Best as I can tell in PBI Desktop the DEFINE syntax is invalid, so I couldn't define measures, I had to add the measure definition within the query.
Disposal Attributes (calc field) =
// VAR VolumeTotal = SUM( 'Piped Trucked Volume'[volume_bbls] )
// VAR vol_CurrentYear = CALCULATE(VolumeTotal, FILTER('Calendar', 'Calendar'[Year] = YEAR(TODAY())) )
// VAR vol_PriorYear = CALCULATE(VolumeTotal, FILTER('Calendar', 'Calendar'[Year] = YEAR(TODAY())-1) )
var ListTotal =
SUMMARIZE (
'Disposal',
[unified_disposalId],
[Disposal (unified_name)],
[Disposal (groups)],
"Volume", SUM( 'Piped Trucked Volume'[volume_bbls] )
)
RETURN
ADDCOLUMNS(
FILTER(
ListTotal, [Volume] > 0
),
"Vol_ThisYr", FORMAT(CALCULATE(SUM( 'Piped Trucked Volume'[volume_bbls] ), FILTER('Calendar', 'Calendar'[Year] = YEAR(TODAY())) ), "#,#"),
"Vol_PriorYr", FORMAT(CALCULATE(SUM( 'Piped Trucked Volume'[volume_bbls] ), FILTER('Calendar', 'Calendar'[Year] = YEAR(TODAY())-1) ), "#,#"),
"Volume (,)", FORMAT( [Volume], "#,#")
)
Result >

Power BI Dax - Trying to break circular dependency after one attempt

I am trying to figure out ancestor bloodline in data I have. I feel like there is something I am missing to make this work. This data wouldn't change so not sure if I am missing something I can write in the power query editor when loading / refreshing the data.
While technically it is circular in fields it never going to return the same row it is currently in and the first generation is hard number making a starting point. I only want to reference the mother and father to calculate the child's bloodline. The first generation is a basic IF() statement. Below is as far as I can get before hitting the circular dependency error. I have tried a few things to break it thinking its going to loop.
Logic is:
Each blood is 100% for 1st generations based on their birthplace then it is ((mother blood + father blood) / 2) for each generation after that. I found I can use PATHITEM() to isolate the type of blood but errors with a circular dependency. (This is where I can't figure out how to reference the mother / father to do the calculation.) If I take this part out I get the image below working for 1st generation and correct mother / father for second generation.
Asisa Blood =
VAR current_id = 'Sheet1'[ID]
VAR current_gen = 'Sheet1'[Generation]
VAR current_blood = 'Sheet1'[Birthplace]
VAR current_mother_blood =
PATHITEM(
CALCULATE(
DISTINCT('Sheet1'[Mother's Blood Mix]),
FILTER(
ALLNOBLANKROW('Sheet1'[ID]),
'Sheet1'[ID] = current_id
),
REMOVEFILTERS('Sheet1')
),1,INTEGER)
VAR current_father_blood =
PATHITEM(
CALCULATE(
DISTINCT('Sheet1'[Father's Blood Mix]),
FILTER(
ALLNOBLANKROW('Sheet1'[ID]),
'Sheet1'[ID] = current_id
),
REMOVEFILTERS('Sheet1')
),1,INTEGER)
VAR gen1_value = 100
RETURN
IF(AND(LOWER(current_gen) = "1",LOWER(current_blood) = "asisa"),
gen1_value,
((current_mother_blood + current_father_blood)/2)
)
Blood Mix concatenates the four blood types into one field for easy look up in next step.
Blood mix =
VAR current__id = 'Sheet1'[ID]
VAR current_blood_a = 'Sheet1'[Asisa Blood]
VAR current_blood_b = 'Sheet1'[Africa Blood]
VAR current_blood_c = 'Sheet1'[Europe Blood]
VAR current_blood_d = 'Sheet1'[North America Blood]
RETURN
current_blood_a & "|" & current_blood_b & "|" & current_blood_c & "|" & current_blood_d
Mother and Father are lookups on blood mix with mother or father ids
Mother's Blood Mix =
VAR current_id = 'Sheet1'[ID]
VAR current_gen = 'Sheet1'[Generation]
VAR gen_value = 'Sheet1'[Blood mix]
VAR current_parent_id =
IF(LOWER(current_gen) = "1",current_id,'Sheet1'[Mother ID])
VAR result =
CALCULATE(
DISTINCT('Sheet1'[Blood mix]),
FILTER(
ALLNOBLANKROW('Sheet1'[ID]),
'Sheet1'[ID] = current_parent_id
),
REMOVEFILTERS('Sheet1')
)
RETURN
result
You can try to do this way (rather high resource consuming):
NEW COLUMN:
heritage_Father = PATH(Blood[ID],(Blood[FatherID]))
heritage_Mother = PATH(Blood[ID],(Blood[MotherID]))
Mancestor = LOOKUPVALUE(Blood[heritage_Mother],Blood[ID],Blood[FatherID]) &"|"& Blood[heritage_Mother]
Fancestor = LOOKUPVALUE(Blood[heritage_Father],Blood[ID],Blood[MotherID])&"|"&Blood[heritage_Father]
NEW MEASURE:
ASIA_check =
VAR Table0 =
SELECTCOLUMNS(
ADDCOLUMNS (
GENERATE (
ROW ( "Text", "0"&SELECTEDVALUE(Blood[Fancestor]) ),
VAR TokenCount =
PATHLENGTH ( [Text] )+1
RETURN
GENERATESERIES ( 1, TokenCount )
),
"Word", PATHITEM ( [Text], [Value] )
),
"Word",VALUE([Word]))
VAR Table1 =
SELECTCOLUMNS(
ADDCOLUMNS (
GENERATE (
ROW ( "Text", "0"&SELECTEDVALUE(Blood[Mancestor]) ),
VAR TokenCount =
PATHLENGTH ( [Text] )+1
RETURN
GENERATESERIES ( 1, TokenCount )
),
"Word", PATHITEM ( [Text], [Value] )
),
"Word",VALUE([Word]))
RETURN
DIVIDe(
if(SELECTEDVALUE(Blood[Generation]) <> 1,
calculate(sum(Blood[AsiaBlood]), FILTER(ALL(Blood), Blood[ID] in Table0 || Blood[ID] in Table1)),0
),
(POWER(2, SELECTEDVALUE(Blood[Generation])-1)))
AFRICA_check =
VAR Table0 =
SELECTCOLUMNS(
ADDCOLUMNS (
GENERATE (
ROW ( "Text", "0"&SELECTEDVALUE(Blood[Fancestor]) ),
VAR TokenCount =
PATHLENGTH ( [Text] )+1
RETURN
GENERATESERIES ( 1, TokenCount )
),
"Word", PATHITEM ( [Text], [Value] )
),
"Word",VALUE([Word]))
VAR Table1 =
SELECTCOLUMNS(
ADDCOLUMNS (
GENERATE (
ROW ( "Text", "0"&SELECTEDVALUE(Blood[Mancestor]) ),
VAR TokenCount =
PATHLENGTH ( [Text] )+1
RETURN
GENERATESERIES ( 1, TokenCount )
),
"Word", PATHITEM ( [Text], [Value] )
),
"Word",VALUE([Word]))
RETURN
DIVIDe(
if(SELECTEDVALUE(Blood[Generation]) <> 1,
calculate(sum(Blood[AfricaBlood]), FILTER(ALL(Blood), Blood[ID] in Table0 || Blood[ID] in Table1)),0
),
(POWER(2, SELECTEDVALUE(Blood[Generation])-1)))

PowerBI Conditional Column with multiple different conditions

I am looking to created a new column in my dataset based on some other fields. I want to use this then as a filter for my visuals.
I have created a switch formula for 95% of my customers fall into. I have a few exceptions and I am wondering how I could cater for them.
Eg:
VAR _1 = IF ('Table'[Customer ID] = "Customer 1011" && 'Table'[ITEM Cat] <> "House", "USA" , "EUROPE')
VAR _2 = IF('Table'[Customer ID] = "Customer 1013" && 'Table'[OrderDate] < "2021/07/01", "Europe", "USA")
This is the switch code so far I have:
PostalCode = SWITCH([Customer ID] ,
"Customer 1001" , "USA",
"Customer 1002" ,"EU",
"Customer 1003" , "ASIA",
Any feedback would be greatly appreciated :)
Use switch this way:
YourColumn = SWITCH(
TRUE(),
'Table'[Customer ID] = "Customer 1011" && 'Table'[ITEM Cat] <> "House", "USA",
'Table'[Customer ID] = "Customer 1013" && 'Table'[OrderDate] < "2021/07/01", "Europe",
"ELSEHERE"
)
https://dax.guide/switch/

How to check using DAX if multiple columns for group of records contain specific text?

I have a table with columns Date, MainID, Par1, Par2 and Par3 like below
If for the date and MainID values any of Par1, Par2 and Par3 values contain "UNMATCH" then it should indicate "UNMATCH" overall. I am looking for output like in "Overall" column.
I have tried Group By, Contains and other options but couldn't crack this issue.
Any idea where can I start with?
If you are comfortable to do this in M query, you can follow these below instructions-
Go to Power query editor and then open the Advanced Editor for your table. Now add this below code to your existing code and you will have a new column in your table with your expected output-
let
//#"your_last_step_name",
//---- Add this below code with adjusting the last step name
#"Added Custom" = Table.AddColumn(#"your_last_step_name", "Custom", each if [Par1] = "UNMATCH" then [Par1] else if [Par2] = "UNMATCH" then [Par2] else [Par3]),
_grouped = Table.Group(#"Added Custom", {"Date", "MainID"}, {{"max", each List.Max([Custom]), type text}}),
#"Merged Queries" = Table.NestedJoin(#"Added Custom", {"Date", "MainID"}, _grouped, {"Date", "MainID"}, "_grouped", JoinKind.LeftOuter),
#"Expanded _grouped" = Table.ExpandTableColumn(#"Merged Queries", "_grouped", {"max"}, {"Overall"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded _grouped",{"Custom"})
in
#"Removed Columns"
here is the final output-
Here is my funky suggestion:
Overall =
VAR _table =
CALCULATETABLE (
UNION (
VALUES ( 'Table'[Par1] ) ,
VALUES ( 'Table'[Par2] ) ,
VALUES ( 'Table'[Par3] )
),
ALLEXCEPT ( 'Table' , 'Table'[Date] , 'Table'[Main ID] )
)
RETURN
IF (
CONTAINS (
_table ,
'Table'[Par1] ,
"UNMATCH"
),
"UNMATCH" ,
"MATCH"
)
Edit: And a slightly more boring (but perhaps more intuitive) suggestion:
Another Overall =
VAR _table =
CALCULATETABLE (
'Table' ,
ALLEXCEPT ( 'Table' , 'Table'[Date] , 'Table'[Main ID] )
)
RETURN
SWITCH (
TRUE ,
CONTAINS( _table , 'Table'[Par1] , "UNMATCH" ) , "UNMATCH" ,
CONTAINS( _table , 'Table'[Par2] , "UNMATCH" ) , "UNMATCH" ,
CONTAINS( _table , 'Table'[Par3] , "UNMATCH" ) , "UNMATCH" ,
"MATCH"
)
You don't need to use "Groupby" or more complicated function, "If else statement" with "Distinct count" will do, the only difference is that you need to store each count in a variable, then compared the value:
Overall =
var par1 = CALCULATE(DISTINCTCOUNT(Sheet1[Par1]),
FILTER(ALL(Sheet1),Sheet1[MainID]=EARLIER(Sheet1[MainID])),
FILTER(Sheet1,Sheet1[Date]=EARLIER(Sheet1[Date])),Sheet1[Par1]="UNMATCH")
var par2 = CALCULATE(DISTINCTCOUNT(Sheet1[Par2]),
FILTER(ALL(Sheet1),Sheet1[MainID]=EARLIER(Sheet1[MainID])),
FILTER(Sheet1,Sheet1[Date]=EARLIER(Sheet1[Date])),Sheet1[Par2]="UNMATCH")
var par3 = CALCULATE(DISTINCTCOUNT(Sheet1[Par3]),
FILTER(ALL(Sheet1),Sheet1[MainID]=EARLIER(Sheet1[MainID])),
FILTER(Sheet1,Sheet1[Date]=EARLIER(Sheet1[Date])),Sheet1[Par3]="UNMATCH")
return
IF(par1 + par2 + par3 >=1, "UNMATCH", "MATCH")

In Power BI, is it possible to hold a column in a Measure variable?

We have a slicer with the value "Local" and "USD".
Depending on the selection we want to use a different column of data for calculations.
This works.
Billings Sum =
IF(
SELECTEDVALUE(CurrencyPickerTable[Currency]) = "Local",
SUM('BillingsTable'[Billings (local)]),
SUM('BillingsTable'[Billings (USD)])
)
However, it's going to get more complicated because we want to also add a slicer for "Fiscal Year" and "Calendar Year" year.
If both possible selections are in the RETURN section there will be a bunch of repeated code.
Is it possible to put a column into a variable and use it later in the calculation?
This is my failing attempt.
Billings Sum =
var selectedCurrencyColumn =
IF(
SELECTEDVALUE(CurrencyPickerTable[Currency]) = "Local",
SELECTCOLUMNS(BillingsTable, "local", [Billings (local)]),
SELECTCOLUMNS(BillingsTable, "USD", [Billings (USD)])
)
RETURN
SUM(selectedCurrencyColumn)
How can I get a column into the currencyColumn variable?
What you are trying to do is not possible afaik, but you might obtain a nice formula using the SWITCH TRUE method and some variables to check the selection
[Billings Sum] =
VAR CurrencySelection =
SELECTEDVALUE ( CurrencyPickerTable[Currency] )
VAR CalendarSelection =
SELECTEDVALUE ( CalendarPickerTable[Calendar] )
RETURN
SWITCH (
TRUE (),
CurrencySelection = "Local"
&& CalendarSelection = "Calendar Year", [Billings (local)],
CurrencySelection = "Local"
&& CalendarSelection = "Fiscal Year", [Billings (local) FC],
CurrencySelection = "USD"
&& CalendarSelection = "Calendar Year", [Billings (USD)],
CurrencySelection = "USD"
&& CalendarSelection = "Fiscal Year", [Billings (USD) FC],
BLANK ()
)
This assumes that you have a measure to cover each possible combination of the selections matrix (Currency and Calendar in this case), but if that's not the case you can also create some variables as measures or write the formula inside the switch.
Reference:
https://www.sqlbi.com/articles/optimizing-if-and-switch-expressions-using-variables/