DAX IF(OR( and LOOKUPValue in one - powerbi

Very new to using PowerBI & DAX but wanting to see if it is the better option for some of my reporting.
I'm trying to replicate a formula that I have in Excel within PowerBI but just cant seem to do it!
The formula is an IF statement with OR/AND (nested) and then a vlookup at the end. I shortened part of the IF(OR using a named range and Count - see below
IF(AND(Client_Name="Client 1",Scheme_Name="Scheme 1"),"Team 1",IF(AND(Client_Name="Client 2",Scheme_Name="Scheme 2"),"Team 2",IF(AND(Client_Name="Dummy",Handling_Team_Name="Team 3"),"Team 3",IF(COUNTIF(C_Team_Helper,Client_Name),VLOOKUP(Scheme_Name,'Team Helper 123'!$A:$C,3,FALSE),""))))
with 'C_Team_Helper' being the named range which is just a column found on another sheet that contains a list of clients.
struggling to mimic this using DAX! I have the two sheets and my current failed attempt is the following
ClientTrigger =
SWITCH(
TRUE(),
Sheet1[Client Name] = "Client 1" && Sheet1[Scheme Name] = "Scheme 1" ,"Team 1",
Sheet1[Client Name] = "Cleint 2" && Sheet1[Scheme Name] = "Scheme 2" ,"Team 2",
Sheet1[Client Name] = "Dummy" && Sheet1[Scheme Name] = "Scheme 3", "Team 3",
Sheet1[Client Name] = CONTAINS('Team Helper - Setup','Team Helper - Setup'[Team Helper],Sheet1[Client Name]),
CALCULATE (
FIRSTNONBLANK ( 'Team Helper - Setup'[Team], 1 ),
FILTER ( ALL ( 'Team Helper - Setup'), 'Team Helper - Setup'[Scheme Name] = Sheet1[Scheme Name])
))
Really appreciate any help, I've tried my usual google to get answer which has got me closer but no luck :(

So after some playing around I believe I have figured it out -
ClientTrigger =
VAR Client = Sheet1[Client Name]
VAR Scheme = Sheet1[Scheme Name]
VAR HandlingTeam = Sheet1[Handling_Team_Name]
VAR GetTeam = CALCULATE (
FIRSTNONBLANK ( 'Team Helper - DAX'[Team], 1 ),
FILTER ( ALL ( 'Team Helper - DAX'), 'Team Helper - DAX'[Scheme] = Sheet1[Scheme Name])
)
VAR Matching = CALCULATE(COUNTROWS('Team Helper - DAX'), FILTER('Team Helper - DAX', 'Team Helper - DAX'[Team Helper]=Sheet1[Client Name])) > 0
RETURN
SWITCH(
TRUE(),
Client = "Client 1" && Scheme = "Scheme 1" ,"Team 1",
Client = "Client 2" && Scheme = "Scheme 2" ,"Team 2",
Client = "Dummy" && HandlingTeam = "Team 3", "Team 3",
Matching = TRUE(), GetTeam
)
This is getting the same result as I was using the Excel formula and using the variables helped tidy up the overall look of the DAX expression.
Any clever folk want to glance their eyes over this and check there isn't some glaring errors? If not I think this is works for what I need :)

Related

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/

Max of date, based on different column

i'd like to identify the most recent date of one column, grouped by other column.
i want to identify my bigger "Valuation date" based on "Policy Effective Date", and if is possible assign 1 if is the most recent date, and 0 if not, i couldn't group by dates ,
this is my formula now:
_z_ValDate =
CALCULATE(
MAX(vwLossRunData[ValuationDate]),
ALLEXCEPT(vwLossRunData,vwLossRunData[EffectiveDate])
)
Thanks !!
What you can do is create a column:
_z_ValDate =
var curDate = vwLossRunData[Policy Effective Date]
return CALCULATE(MAX(vwLossRunData[Valuation date]), FILTER(vwLossRunData, curDate = vwLossRunData[Policy Effective Date]))
It takes the max of the [Valuation Date] from the filtered table by [Policy Effective Date]
You can try this below Measure-
_z_ValDate =
VAR current_row_pe_date = MIN(vwLossRunData[Policy Effective Date])
VAR max_v_date =
CALCULATE(
MAX(vwLossRunData[ValuationDate]),
FILTER(
ALLSELECTED(vwLossRunData),
vwLossRunData[Policy Effective Date] = current_row_pe_date
)
)
RETURN IF(current_row_pe_date = max_v_date, 1 0)

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/

Power BI DAX - Need help in displaying Measure with Values and Text

I'm trying to display a measure value with text in Card Visual.  For instance, I'd like to display all IDs with HasWrongTitle = True by Department (Slicer).  Based on the below data, I'd like to show "2 of 4" without slicer selection and "1 of 4" with slicer set to IT or Finance.
The below measure works partially but displays the same value for total as well.
MeasureWrongTitle =
IF(
ISBLANK(
CALCULATE(
DISTINCTCOUNT(Table[ID]),
FILTER(
Table,
Table[HasWrongTitle]="True"
)
)
),
"0 of " &
COUNTROWS(
SUMMARIZE(
Table,
Table[ID]
)
),
CALCULATE(
DISTINCTCOUNT(Table[ID]),
FILTER(
Table,
Table[HasWrongTitle]="True"
)
) & " of " & COUNTROWS(SUMMARIZE(Table,Table[ID]
)
)
Table:
ID Name Department HasDirectReport HasWrongTitle
100 John Doe1 Admin True False
101 John Doe2 IT False True
102 John Doe3 HR True False
103 John Doe4 Finance True True
You can try this below measure with your logic but in simplified way-
MeasureWrongTitle =
var has_wrong_title_count =
CALCULATE(
DISTINCTCOUNT('Table'[ID]),
FILTER(
'Table',
'Table'[HasWrongTitle] = TRUE()
//-- User ""True" if your column is string type
)
)
var total_id = DISTINCTCOUNT('Table'[ID])
RETURN IF(has_wrong_title_count = BLANK(),0,has_wrong_title_count) & " of " & total_id
Here is the output using your data-

Custom matrix header

I need to create a custom header like the picture below:
I check this link Custom aggregate column in power bi matrix
But I don't undestand how to do the same to my case?
Edit
I try to create calculated table but I didn't get the data for dim5 and dim6, how can I modify it?
Edit
Dim_prduit
My problem is how to dispaly Nombre product , and then like hierarchy dim5 then dim6 in the header?
It's ugly but you can write a header table like this and then define a switching measure based on the appropriate indices:
Header =
ADDCOLUMNS (
UNION (
DATATABLE (
"Top", STRING,
"Index1", INTEGER,
"Middle", STRING,
"Index2", INTEGER,
"Bottom", STRING,
"Index3", INTEGER,
{
{ "Nombre product", 1, "", 0, "", 0 },
{ "Affaires nouvelles", 2, "Total", 8, "", 0 },
{ "Affaires nouvelles", 2, "%Total", 9, "", 0 }
}
),
SELECTCOLUMNS (
SUMMARIZECOLUMNS ( Dim_Prod[dim5], Dim_Prod[dim6] ),
"Top", "Affaires nouvelles",
"Index1", 2,
"Middle", Dim_Prod[dim5],
"Index2", RANK.EQ ( Dim_Prod[dim5], Dim_Prod[dim5], ASC ),
"Bottom", Dim_Prod[dim6],
"Index3", RANK.EQ ( Dim_Prod[dim6], Dim_Prod[dim6] )
)
),
"Index0", 100 * [Index1] + 10 * [Index2] + [Index3]
)
Output:
Sample measure:
SampleMeasure =
VAR Top = SELECTEDVALUE ( Header[Top] )
VAR Middle = SELECTEDVALUE ( Header[Middle] )
VAR BottomIndex = SELECTEDVALUE ( Header[Index3] )
RETURN
SWITCH (
TRUE (),
Top = "Nombre product", [NombreProductMeasure],
Top = "Affaires nouvelles" && BottomIndex <> 0, [DimensionMeasure],
Middle = "Total", [TotalMeasure],
Middle = "%Total", [%TotalMeasure]
)
This is pretty hacky though. Power BI may not be the best tool here.
Power BI is not a pixel-perfect data visualization tool, therefore, it is not possible to create customer headers using Built-in visualizations.
Therefore you have pretty much two options:
Build your own custom visualization, using Javascript, Python or R
Use a pixel-perfect tool like SSRS