DAX - Retrieve a value from another unrelated table - powerbi

I have two tables: 'Events' and 'Occurrences'.
In Events, I have the name of the event, start date and end date.
In 'Occurrences' I have a date from occurrence, ID occurrence and description of occurrence.
Events Table
Event
Start Date
End Date
Event 1
01/01/2022
02/14/2022
Event 2
02/15/2022
03/10/2022
Event 3
02/11/2022
03/30/2022
Occurrence Table
ID Ocurrence
Occurrence Create Date
Description
1
01/10/2022
Foo 1
2
02/11/2022
Foo 2
3
02/20/2022
Foo 3
4
03/20/2022
Foo 4
5
03/30/2022
Foo 5
My Question is: How can I retrieve which event is each occurrence?
In this example, the expected result is:
ID Ocurrence
Occurrence Create Date
Description
Event Related
1
01/10/2022
Foo 1
Event 1
2
02/11/2022
Foo 2
Event 1
3
02/20/2022
Foo 3
Event 2
4
03/20/2022
Foo 4
Event 3
5
03/30/2022
Foo 5
Event 3

add this measure to your table...
Event Related =
VAR _occ =
SELECTEDVALUE ( 'Table (2)'[Occurrence Create Date] )
RETURN
CALCULATE (
FIRSTNONBLANK ( 'Table'[Event], 1 ),
FILTER (
ALL ( 'Table' ),
'Table'[Start Date] <= _occ
&& 'Table'[End Date] >= _occ
)
)
or if you want to add only as a column to the table, not the visual, you can use this calculated column (more or less the same)
Event Related 2 =
var _occ = 'Table (2)'[Occurrence Create Date]
return
CALCULATE (
FIRSTNONBLANK ( 'Table'[Event], 1 ),
FILTER (
ALL ( 'Table' ),
'Table'[Start Date] <= _occ
&& 'Table'[End Date] >= _occ
)
)

Related

Count of active employees between two date columns, by department

I'm trying to imitate this report (page 3) where it slices active headcount and all the other metrics (1) by date and (2) by department.
My data looks like this (with relationships, of course):
ID
Name
DEPID
Hired Date
Terminated Date
Terminated (Y/N)
1
John
2
1/1/2019
2020/12/31
Y
2
Jane
2
1/3/2018
2019/07/26
Y
3
Jack
1
1/5/2022
null
N
Using the following measure, I was able to extract total number of employees by date, but I wasn't able to filter by department:
CountOfActive =
var _selectedDate = MAX('Calendar'[Date])
return
CALCULATE(COUNTROWS('Table'); filter(ALL('Table'); Table[HIREDDATE] <= VALUE(_selectedDate) && (Table[TERMINATEDDATE] >= VALUE(_selectedDate) || ISBLANK(Table[TERMINATEDDATE]))))
My ideal output is something like the following (where I'll create a table for each department and list the number of active employees, then join them to my department key table afterwards so I can slice them):
Date
Count of Active Employees
Department
2019/1/1
3
Retail
2019/1/2
3
Retail
2019/1/3
4
Retail
...
...
...
The "Date" column would be a calendar table built with CALENDAR().
What should I do to achieve the last table based on the data I have?
My relationship schema looks like this.
try this : 'Table 2' is your Calendar Table which is also a slicer on the visual.
Make sure that your Calendar Table's Date has a relation with the Hired Date and also the relation between the Department Table
Count of Emp =
VAR _latest =
MAX ( 'Table 2'[Date] )
VAR _from =
MIN ( 'Table 2'[Date] )
VAR _dept =
SELECTEDVALUE ( Department[Department] )
RETURN
CALCULATE (
COUNTX ( 'Table', 'Table'[ID ] ),
FILTER (
ALL ( 'Table' ),
'Table'[Terminated Date ] >= _from
&& 'Table'[Hired Date ] <= _from
&& 'Table'[Terminated (Y/N)] = "Y"
&& RELATED ( Department[Department] ) = _dept
)
)
+ CALCULATE (
COUNTX ( 'Table', 'Table'[ID ] ),
FILTER (
ALL ( 'Table' ),
'Table'[Terminated (Y/N)] = "N"
&& 'Table'[Hired Date ] <= _from
&& RELATED ( Department[Department] ) = _dept
)
)

DAX Measure to count Rows if relation may not exist

I am looking for a DAX measure to solve the following problem:
Count the number of rows in the dimension table where the Fact table either has no rows or the score is 0.
Table A (Dimension Table)
ID
name
1
a
2
b
3
c
Table B (Fact Table)
ID
score
1
0
1
1
1
2
2
5
Expected Result
In this example, I would expect 2, as ID=1 has one row with score=0 and ID=3 as no corresponding row in the Fact Table.
I came up with this measure which gives me the number of rows that have no corresponding row in the fact table, but I am not able to integrate the first condition:
CALCULATE(COUNTROWS('Dimension'), FILTER ('Dimension', ISBLANK ( CALCULATE ( COUNT ('Fact'[id]) ) )))
Probably much more straightforward methods, but try this measure for now:
MyMeasure =
VAR MyTable =
ADDCOLUMNS(
Table_A,
"Not in Table_B", NOT (
Table_A[ID]
IN DISTINCT( Table_B[ID] )
),
"Zero Score in Table_B",
CALCULATE(
COUNTROWS( Table_B ),
Table_B[score] = 0
) > 0
)
RETURN
SUMX(
MyTable,
[Not in Table_B] + [Zero Score in Table_B]
)
You can also try this
CountID =
VAR ScoreZero =
COUNTROWS ( FILTER ( TableB, [score] = 0 ) )
VAR NonExistentIDs =
COUNTROWS ( EXCEPT ( DISTINCT ( TableA[ID] ), DISTINCT ( TableB[ID] ) ) )
RETURN
ScoreZero + NonExistentIDs
This also works, not sure it's a good idea to nest CALCULATE:
CALCULATE(COUNTROWS('Table_A'), FILTER ('Table_A', ISBLANK ( CALCULATE ( COUNT ('Table_B '[id]) ) ) || CALCULATE(COUNTAX(Filter('Table_B ','Table_B '[score]=0),'Table_B '[id])>=1)))

Find which product is often ordered with her one in DAX

I have a powerbi report containing a table that looks like :
Product ID
Transaction ID
Qty
A
X
1
B
X
2
C
X
1
A
Y
2
B
Y
1
A
Z
1
C
Z
1
I would like to create a new table in dax, that would show for each unique occurence of the prodcut ID in the previous table, each product that has been bought at the same time (=in a transaction), and the sum of qty associated.
So for the example above, that would give a table like :
Product ID
Associated Product ID
Qty
Count of Transactions
A
B
3
2
A
C
2
2
B
A
3
2
B
C
1
1
C
A
2
2
C
B
2
1
I hope this is clear enough.
In SQL I could probably pull it off, but I have no idea how to this in DAX (using a DAX generated table)
Can somebody help me out ?
Thanks
this may be a little tricky and I don't know how it performs on a large dataset. I use GENERATE to create Main and AssociatedProduct (the first step create a corssjoin!), then we pack MainProd to variable to generate virtualtable with all transaction id (__Trans). Then we can easly compare [AssociatedProd] with source ProductId and Transaction with our __Trans. In the end, I remove SelfRelation (Product A vs Product A).
AssocAgg =
FILTER (
GENERATE (
SELECTCOLUMNS (
VALUES ( Associated[Product ID] ),
"MainProd", Associated[Product ID]
),
VAR __Main = [MainProd]
VAR __Trans =
CALCULATETABLE (
VALUES ( Associated[Transaction ID] ),
Associated[Product ID] = __Main
)
RETURN
ADDCOLUMNS (
SELECTCOLUMNS (
VALUES ( Associated[Product ID] ),
"AssociatedProd", Associated[Product ID]
),
"Trans",
CALCULATE (
COUNTROWS ( VALUES ( Associated[Transaction ID] ) ),
FILTER (
Associated,
[AssociatedProd] = Associated[Product ID]
&& Associated[Transaction ID] IN __Trans
)
),
"qty",
CALCULATE (
SUM ( Associated[Qty] ),
FILTER (
Associated,
[AssociatedProd] = Associated[Product ID]
&& Associated[Transaction ID] IN __Trans
)
)
)
),
[AssociatedProd] <> [MainProd]
)

ALLEXCEPT not working when filtering blanks

I have a simple problem. My DAX measure does not seem to be working correctly when I filter for non-existing values. Here are some details:
Table:
Column1: A,A,A,A,A,B,B,B,B
Column2: 1,2,3,4,5,1,2,3,5
Measure = calculate(countrows(table), allexcept(column1))
Card Visual returns correct row count when I filter by column1 (any value in filtering pane)
However it returns wrong row count when I filter by column2 = "4" and Column1 = "B" (in filtering pane). It seems that it should ingore filtering by column2 and it does except when I specifically filer for value = "4". It gives "blank" result value in a card visual then.
Any ideas why?
Here's the screen. I would like to populate that blank cell with "4" (in a singe-table data model.enter image description here
In your case you dont need to add allexcept in your measure. Below code would be fine.
TestMeasure = countrows(Test_Data)
PFB screenshot
I am hoping that you have a data model as following
table name _dim1
colA
A
B
C
table name _dim2
colB
1
2
3
4
5
table name _fact
colA
colB
A
1
A
2
A
3
A
4
A
5
B
1
B
2
B
3
B
5
C
2
C
3
If you have this you can reach where you need by using following measures
Measure3 =
CALCULATE ( COUNTROWS ( _fact ), ALL ( _dim2[colB] ), VALUES ( _fact[colA] ) )
Measure9 =
VAR _1 =
MAX ( _dim2[colB] )
VAR _2 =
CALCULATE (
MAXX (
FILTER ( _dim2, _dim2[colB] <= _1 ),
LASTNONBLANKVALUE ( _dim2[colB], [Measure3] )
),
ALL ( _dim2[colB] )
)
RETURN
_2
Measure10 =
VAR _1 =
MAX ( _dim2[colB] )
VAR _2 =
CALCULATE (
MAXX (
FILTER ( _dim2, _dim2[colB] > _1 ),
FIRSTNONBLANKVALUE ( _dim2[colB], [Measure3] )
),
ALL ( _dim2[colB] )
)
RETURN
IF ( ISBLANK ( [Measure9] ) = TRUE (), _2, [Measure9] )
I don't think you can reach here from a single table like following
colA
colB
A
1
A
2
A
3
A
4
A
5
B
1
B
2
B
3
B
5
C
2
C
3

Percentage of change with 2 data slicers in Power BI

I have a scenario with two data slicers. The first data slicer filters data for one period, the second one for another period. By editing visual interactions I got this works at the same page.
Now I want to compare two resulting values (in this case, number of transactions, and find a percentage of change between two selected periods.
I duplicated data column so I have two date columns for each slicer and I calculated the next measures:
# of Transactions 1 = CALCULATE(COUNT(Report[ProductID]),DATESBETWEEN(Report[Date1],[Start Date 1],[Last Date 1]))
# of Transactions 2 = CALCULATE(COUNT(Report[ProductID]),DATESBETWEEN(Report[Date2],[Start Date 2],[Last Date 2]))
% Transaction Change = ([# of Transactions 1]/[# of Transactions 2]) - 1
The first 2 measures are accurate (# of Transactions 1 & 2), but % of change doesn't work.
If you look at the screenshot below, you'll see # od Transactions 1 = 1,990 and # of Transactions 2 = 2,787. I want to compare this 2 values now.
How can I solve this?
Thank you.
First create two measure for your date bounds:
Min Date :=
MIN ( 'Report'[Date] )
Max Date :=
MAX ( 'Report'[Date] )
Then create a date table using the following DAX, this will join to you 'Report' table on the primary date:
Dates :=
VAR MinDate = [Min Date]
VAR MaxDate = [Max Date]
VAR BaseCalendar =
CALENDAR ( MinDate, MaxDate )
RETURN
GENERATE (
BaseCalendar,
VAR BaseDate = [Date]
VAR YearDate =
YEAR ( BaseDate )
VAR MonthNumber =
MONTH ( BaseDate )
VAR YrMonth =
100 * YEAR ( BaseDate )
+ MONTH ( BaseDate )
VAR Qtr =
CONCATENATE ( "Q", CEILING ( MONTH ( BaseDate ) / 3, 1 ) )
VAR YrMonthQtr =
100 * YEAR ( BaseDate )
+ MONTH ( BaseDate )
& CONCATENATE ( "Q", CEILING ( MONTH ( BaseDate ) / 3, 1 ) )
VAR YrMonthQtrDay =
100 * YEAR ( BaseDate )
+ MONTH ( BaseDate )
& CONCATENATE ( "Q", CEILING ( MONTH ( BaseDate ) / 3, 1 ) )
& DAY ( BaseDate )
RETURN
ROW (
"Day", BaseDate,
"Year", YearDate,
"Month Number", MonthNumber,
"Month", FORMAT ( BaseDate, "mmmm" ),
"Year Month", FORMAT ( BaseDate, "mmm yy" ),
"YrMonth", YrMonth,
"Qtr", Qtr,
"YrMonthQtr", YrMonthQtr,
"YrMonthQtrday", YrMonthQtrDay
)
)
Now create another date table from which to compare, and join to your primary date table in 'Report' and ensure the relationship is inactive:
Compare Dates :=
ALLNOBLANKROW ( 'Dates' )
Now create the [# of transaction] measure; one for 'Dates' and another for 'Compare Dates' like so:
[# of Transaction 1] :=
CALCULATE (
COUNT ( Report[ProductID] )
)
[# of Transaction 2] :=
CALCULATE (
[# of transaction 1],
ALL ( 'Dates' ),
USERELATIONSHIP ( 'Compare Dates'[Date], 'Report'[Date] )
)
Now Create the % Delta measure:
Transaction Change := CALCULATE(DIVIDE([# of Transactions 1],[# of Transactions 2]) - 1)
This should work like a charm and will work for any dates selected in your slicers, you will still need to associate your date slicers with your new date tables.
I hope this helps!!