PowerBI Conditional Column with multiple different conditions - powerbi

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/

Related

Trying to get list of Venues with respect to Suppliers

Hi guys so i am trying to build a report where i have to show new customers this month compared to last month. I am able to calculate new customers fine but i have to show them in a Matrix in Power Bi. This is the Code i am using
New Customers This Month =
VAR ThisMonth =
SELECTEDVALUE( DailyReport[DateCreated].[MonthNo])
VAR ThisYEAR =
SELECTEDVALUE(DailyReport[DateCreated].[Year])
VAR SelectedSupplier =
SELECTEDVALUE(DailyReport[SupplierName])
VAR LastMonth = ThisMonth - 1
VAR CustomersThisMonth =
DISTINCT(
SELECTCOLUMNS(
FILTER( ALL( DailyReport ), DailyReport[DateCreated].[MonthNo] = ThisMonth && DailyReport[DateCreated].[Year] = ThisYEAR && DailyReport[SupplierName] = SelectedSupplier),
"C1", DailyReport[VenueName]
)
)
VAR CustomersLastMonth =
DISTINCT(
SELECTCOLUMNS(
FILTER( ALL( DailyReport ), DailyReport[DateCreated].[MonthNo] = LastMonth && DailyReport[DateCreated].[Year] = ThisYEAR && DailyReport[SupplierName] = SelectedSupplier),
"C1", DailyReport[VenueName]
)
)
VAR T1 =
EXCEPT(CustomersThisMonth, CustomersLastMonth )
RETURN
CONCATENATEX( T1, [C1], ", " )
I am getting result like this, two columns one for supplier and one for new venues this month
getting result like this
But expected result should be like this, you know like a list
expected result
i think it may be causing due to how i am concatening it in the last line in code
and please let me know if you need any more information

DAX IF(OR( and LOOKUPValue in one

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 :)

filter the new table by range of data, e.g. 1997-2020 PowerBI

Error:The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value.
I intend to select Data by datecolumns from 1997 to 2020. But no success
New Periode =
VAR DateStart =
DATE ( "1997", "1", "1" )
VAR DateEnd =
DATE ( "2021", "11", "10" )
RETURN
CALCULATETABLE (
'Date_data',
FILTER ( 'Date_data', 'Date_data'[Date] <= DateEnd && 'Date_data'[Date] >= DateStart )
)
Can you please try this below code-
Periode =
VAR DateStart = DATE ( "1997", "1", "1" )
VAR DateEnd = DATE ( "2021", "11", "10" )
RETURN
CALCULATETABLE (
'Date_data',
'Date_data'[Date] <= DateEnd
&& 'Date_data'[Date] >= DateStart
)

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-