DAX Calculate Measure with IF Statement - if-statement

I am trying to count the number of codes {"1V", "2V", "3V"} contained in the [ToStatus] and [FromStatus] column of the' Master Report' table. However, I need only count the code once if it appears in both columns.
I am relatively new to DAX and the below is my attempt at coding same. I would be obliged if any of the gurus out there could be of assistance :)
Psuedo code:
If any of the values {"1V", "2V", "3V"} is in column [ToStatus] and [FromStatus]
Just count [ToStatus] else count [FromStatus] and return the total
Attempt in DAX:
=IF(('Master Report'[ToStatus] IN {"1V", "2V", "3V"} && 'Master Report'[FromStatus] IN {"1V", "2V", "3V"}),
CALCULATE([_PatientCount],
FILTER('Master Report',
'Master Report'[ToStatus] IN {"1V", "2V", "3V"}),
CALCULATE([_PatientCount],
FILTER('Master Report',
'Master Report'[FromStatus] IN {"1V", "2V", "3V"} ||
'Master Report'[ToStatus] IN {"1V", "2V", "3V"})
)
Sample Data:
ID Code FromStatus ToStatus
145320150978 3V 2V
145320150978 3V 2V
145320210617 RH 3V
145320210617 2V ZO
145320210628 3C 2V
145320210628 2V 5L
145320211246 3V 5S
Update: Also I forgot to mention that the purpose of the _PatientCode measure is to count DISTINCT patient codes!
Update: I have now resolved this by doing it in MySQL instead of DAX and then just counting the 1 values in DAX.
Thanks in advance,
Conor.

Some brackets were missing, others were superfluous:
MyMeasure =
IF (
'Master Report'[ToStatus] IN { "1V", "2V", "3V" }
&& 'Master Report'[FromStatus] IN { "1V", "2V", "3V" },
CALCULATE (
[_PatientCount],
FILTER (
'Master Report',
'Master Report'[ToStatus] IN { "1V", "2V", "3V" } )
),
CALCULATE (
[_PatientCount],
FILTER (
'Master Report',
'Master Report'[FromStatus] IN { "1V", "2V", "3V" }
|| 'Master Report'[ToStatus] IN { "1V", "2V", "3V" }
)
)
)

Resolved from MySQL side with code below:
(CASE
WHEN ToStatus LIKE '%V' OR FromStatus LIKE '%V' THEN 1
ELSE 0
END) as V_Count

Related

Count of active rows between two dates, sliced by department

I'm trying to get count of active employees by date that can be filtered by department.
The following is what my Employees data more or less looks like. Additionally, there's a dim_Department table connected to the main Employees table and a TERMINATED(Y/N) column. I found a similar case here (without the departments and terminated(y/n) though):
I used the following DAX expression to get a table of total number of employees (organization-wide), but I'm not sure how to filter this metric using the slicer showing departments.
CountOfActive =
var _selectedDate = MAX('Calendar'[Date])
return
CALCULATE(COUNTROWS('employee'), filter(ALL(employee), employee[Hire Date] <= VALUE(_selectedDate) && (employee[Termination Date] >= VALUE(_selectedDate) || ISBLANK(employee[Termination Date]))))
CountOfTerminated =
var _selectedDate = MAX('Calendar'[Date])
return
CALCULATE(COUNTROWS('employee'), filter(ALL(employee), employee[Hire Date] <= VALUE(_selectedDate) && (employee[Termination Date] < VALUE(_selectedDate) )))
How do I get my department slicer to filter the results of this column? Or should I use a different expression, or make a column/table for each department?
we have answered that before i guess...
Unique Count
Unique Count =
VAR _max =
MAX ( 'Calendar Table'[Date] )
VAR _min =
MIN ( 'Calendar Table'[Date] )
RETURN
CALCULATE (
DISTINCTCOUNT ( 'Table'[ID ] ),
'Table'[Date ] <= _max
&& 'Table'[Date ] >= _min
)
Unique Count (Active)
Unique Count (Active) =
VAR _max =
MAX ( 'Calendar Table'[Date] )
VAR _min =
MIN ( 'Calendar Table'[Date] )
RETURN
CALCULATE (
DISTINCTCOUNT ( 'Table'[ID ] ),
'Table'[Date ] <= _max
&& 'Table'[Date ] >= _min
&& 'Table'[Status] = "Active"
)
Unique Count (Terminated)
Unique Count (Terminated) =
VAR _max =
MAX ( 'Calendar Table'[Date] )
VAR _min =
MIN ( 'Calendar Table'[Date] )
RETURN
CALCULATE (
DISTINCTCOUNT ( 'Table'[ID ] ),
'Table'[Date ] <= _max
&& 'Table'[Date ] >= _min
&& 'Table'[Status] = "Terminated"
)
also you can insert your data into that sample file and see if it works...

Using lookups to match multiple values in columns in Power BI/DAX

I have a table of university unit data. The units have historically been delivered in two different lengths (under the same unit code): sessions and terms. It looks something like this:
UnitCode
Year
OfferedIn
UNIT001
2021
Session1
UNIT001
2021
Session2
UNIT002
2021
Session1
UNIT002
2021
Term4
UNIT003
2020
Session1
UNIT003
2020
Session3
UNIT003
2021
Term2
I need to slice various graphs by units that were offered in both styles, regardless of which actual year, session, or term. So the extra column would be as on the right below:
UnitCode
Year
OfferedIn
OfferedInBoth
UNIT001
2021
Session1
UNIT001
2021
Session2
UNIT002
2021
Session1
TRUE
UNIT002
2021
Term4
TRUE
UNIT003
2020
Session1
TRUE
UNIT003
2020
Session3
TRUE
UNIT003
2021
Term2
TRUE
So I want to look at a value in the UnitCode column and see if it has both different kinds of values in the OfferedIn column (I can do 'contains' expressions for the types) in any row where the unit code is the same. How do I generate the OfferedInBoth column? (Or do I need to create a new table? I'd prefer to keep it as dynamic as possible, as the dataset will add new unit codes over time.)
You can create a measure as below-
true_false =
var current_row_unitcode = min('your_table_name'[UnitCode])
var count_sess =
COUNTROWS(
FILTER(
ALL('your_table_name'),
'your_table_name'[UnitCode] = current_row_unitcode
&& LEFT('your_table_name'[OfferedIn],4) = "Sess"
)
)
var count_term =
COUNTROWS(
FILTER(
ALL('your_table_name'),
'your_table_name'[UnitCode] = current_row_unitcode
&& LEFT('your_table_name'[OfferedIn],4) = "Term"
)
)
return if(
count_sess >=1 && count_term >= 1, "True",""
)
Output-
You could create a calculated column using the DAX code below. Replace "Table1" references with the name of your table and add any session/term values applicable to the SWITCH statements.
OfferedInBoth =
VAR unitCode = Table1[UnitCode]
VAR unitCodeTable = FILTER(Table1, Table1[UnitCode] = unitCode)
VAR containsSession =
SWITCH(
TRUE,
CONTAINS(unitCodeTable, Table1[OfferedIn], "Session1"), TRUE(),
CONTAINS(unitCodeTable, Table1[OfferedIn], "Session2"), TRUE(),
CONTAINS(unitCodeTable, Table1[OfferedIn], "Session3"), TRUE(),
CONTAINS(unitCodeTable, Table1[OfferedIn], "Session4"), TRUE(),
FALSE
)
VAR containsTerm =
SWITCH(
TRUE,
CONTAINS(unitCodeTable, Table1[OfferedIn], "Term1"), TRUE(),
CONTAINS(unitCodeTable, Table1[OfferedIn], "Term2"), TRUE(),
CONTAINS(unitCodeTable, Table1[OfferedIn], "Term3"), TRUE(),
CONTAINS(unitCodeTable, Table1[OfferedIn], "Term4"), TRUE(),
FALSE
)
RETURN containsSession && containsTerm

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/

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/

Rolling 20 workday revenue

Currently visualizing sales for the past 30days, but looking to switch it in to the past 20 workdays instead, I've got workday column up and running in datetable, so ideally id want to use a filter of workday=1 and grab the 20 newest rows?
Sales 30d =
CALCULATE([Sales],
FILTER(
ALL(d_dates[date]),
d_dates[date]
>TODAY()-30))
This is what im using to show revenue for past 30 days, what'll i need to change?
You can try with this below measure-
slaes_last_20_days =
VAR today = TODAY()
VAR selected_date_min =
MINX(
TOPN(
20,
FILTER(
ALL(d_dates),
d_dates[date].[Date] <= today
&& workday = 1
),
d_dates[date].[Date],
DESC
),
d_dates[date].[Date]
)
RETURN
CALCULATE(
[Sales],
FILTER(
ALL(d_dates),
d_dates[date].[Date] >= selected_date_min
&& workday = 1
)
)
VAR Last20Workdays =
selectcolumns(
TOPN(
20,
FILTER(
d_dates,
d_dates[date] < TODAY()
&& d_dates[workday] = 1
),
d_dates[date],
DESC
),
"WorkDay",
d_dates[date]
)
This worked.