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

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

Related

How to Create a Slicer that uses dates and that only contains YTD Months dynamically by current date in PowerBI

Trying to create a slicer that when clicked only the months YTD, specifically up the the last month of current.
Such as slicer box clicked: (Say current month is August) then Months "Jan","Feb",..."July" would only display to click.
I've tried several things, my last attempt was to create a Dax table with dates and try to do a switch:
Dates =
var CurrentMonthInt = month(TODAY())
var monthis8 = "Jan" + "Feb" + "March" + "April" + "May" + "June" + "July"
VAR BaseTable =
CALENDAR(
DATE( YEAR ( MIN(Reporting[InvoiceDate])),01,01),
DATE( YEAR ( MAX(Reporting[InvoiceDate])),12,31)
)
RETURN
ADDCOLUMNS(
BaseTable,
"Year", YEAR([Date]),
"Month", FORMAT([Date],"mm"),
"Year Month", FORMAT([Date],"YYYY MM"),
"Month YTD", SWITCH(TRUE(),
CurrentMonthInt = 1, "Jan",
CurrentMonthInt = 8, monthis8,
"testing"
)
)
this returns a variant data type and will not work. I am thinking this is trying to add them all up on the same row.
Hello Please test this and let me know if It solves your problem.
Dates =
VAR CurrentMonthInt =
MONTH ( TODAY () )
VAR BaseTable =
CALENDAR (
DATE ( YEAR ( MIN ( Reporting[InvoiceDate] ) ), 01, 01 ),
DATE ( YEAR ( MAX ( Reporting[InvoiceDate] ) ), 12, 31 )
)
RETURN
FILTER (
ADDCOLUMNS (
BaseTable,
"Year", YEAR ( [Date] ),
"Month", MONTH ( [Date] ),
"Year Month", FORMAT ( [Date], "YYYY MM" )
),
[Month] <= CurrentMonthInt - 1
)
Note: I haven't tested it.

How to do if with multiple rows in PowerBi?

I have two tables like this
Table: creation date
ID user create date
1 ABC Jan 1, 2021
2 EFC Feb 1, 2021
3 HIJ Feb 1, 2021
1 ABC Feb 1, 2021
1 ABC Feb 20, 2021
And a log of blocking
ID user blocked from blocked till
1 ABC Jan 20, 2021 Feb 10, 2021
3 HIJ Jan 5, 2021 Jan 10, 2021
1 ABC Feb 25, 2021 Mar 10, 2021
As you can see the relationship between the tables are Many to Many. What I wanted to check is if the create date is during the blocked date in the log.
If it was ONE to MANY relationship, I could have used
Yes or No = IF(AND(creation date < RELATED(log[blocked from]), creation date > RELATED(log[blocked till])), "Yes", "No")
But now as there are multiple rows for the same ID how can I just check if the creation date is between any of the blocked from and Blocked till date?
You can use a measure like this
Measure2 =
VAR _maxID =
MAX ( creationDate[ID] )
VAR _minBlockedFrom =
CALCULATE (
MIN ( blockLog[blocked from] ),
FILTER ( VALUES ( blockLog[ID] ), blockLog[ID] = _maxID )
)
VAR _maxBlockedTill =
CALCULATE (
MAX ( blockLog[blocked till] ),
FILTER ( VALUES ( blockLog[ID] ), blockLog[ID] = _maxID )
)
VAR _maxDate =
MAX ( creationDate[create date] )
RETURN
IF ( _maxDate >= _minBlockedFrom && _maxDate <= _maxBlockedTill, "Yes", "No" )
Edit
please use a measure like this
Measure3 =
MAXX (
ADDCOLUMNS (
FILTER (
CROSSJOIN (
SELECTCOLUMNS (
creationDate,
"_id", creationDate[ID],
"_user", creationDate[user],
"_date", creationDate[create date]
),
blockLog
),
[_user] = [user]
),
"condition",
IF ( [_date] >= [blocked from] && [_date] <= [blocked till], "Yes", "No" )
),
[condition]
)
You can create a Measure as below-
is_from_block_range =
var current_row_id = min('CreationDate'[ID])
var current_row_user = min('CreationDate'[user])
var creation_date = min('CreationDate'[create date])
var chk_macth =
COUNTROWS(
FILTER(
all('BlockLog'),
'BlockLog'[ID] = current_row_id
&& 'BlockLog'[user] = current_row_user
&& 'BlockLog'[blocked from] <= creation_date
&& 'BlockLog'[blocked till] >= creation_date
)
)
return if(chk_macth >=1, "Yes", "No")
Output will be as below-

DAX - Plot Previous working day value against today's date

I have a table like this,
ReportingDate ReportingDateOrder Status Customer
01/06/2021 1 Active Present
01/06/2021 1 Active
01/06/2021 1 Inactive Present
27/05/2021 2 Inactive Present
27/05/2021 2 Active Present
27/05/2021 2 Active Present
26/05/2021 3 Active Present
I want to generate an visual table like this,
ReportingDate PreviousDaySales
01/06/2021 2
27/05/2021 1
26/05/2021 0
The logic for previousdaysales is the count of active lines for the previous available reporting date.
This is what I have tried so far, but it is returning empty.
PreviousDaySales =
var selectedreportingdate = SELECTEDVALUE('Table1'[Reporting Date])
var selectedreportingrank = CALCULATE(MIN('Table1'[ReportingDateOrder]),FILTER('Table1', 'Table1'[Reporting Date] = selectedreportingdate))
var old_rank = selectedreportingrank + 1
var val1 = CALCULATE(COUNT('Table1'[Action Status]), FILTER('Table1', 'Table1'[Status] = "Active" && 'Table1'[ReportingDateOrder] = old_rank))
return val1
Kindly help me with this.
CurrentActiveLines :=
CALCULATE(COUNTROWS(RStatus),RStatus[Status]="Active")
PreviousReportingDateActiveLines :=
VAR CurrentDate = SELECTEDVALUE( RStatus[ReportingDate] )
VAR PreviousDate =
CALCULATE(
LASTDATE( RStatus[ReportingDate] ),
RStatus[ReportingDate] < CurrentDate
)
VAR Result =
CALCULATE(
COUNTROWS( RStatus ),
RStatus[ReportingDate] = PreviousDate,
RStatus[Status] = "Active"
)
RETURN
Result

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-

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.