DAX measure of two layers of intersection and a validation - powerbi

I need to find a measure with the following logic:
Firstly, a link is dictated by Table 2 (LinkedID column) back to Table 1 (UniqueID column) = LinkedID -> UniqueID
For example: ID 1/UniqueID AA is a S_Req and has links with ID 9/UniqueID II because ID 1 is found in Table 2 with LinkedID = II which back in Table 1 will be ID 9/Unique II and this is an E_Req, next link is to ID 3/UniqueID CC(M_Req) and from these links(S_Req linked with E_Req or M_Req) also with the help of Table2 will determine the links with an Tst, so from ID 9/UniqueID II will have link to ID 6/UniqueID FF(Tst - Passed) and ID 2/UniqueID BB (Tst - Failed) and ID 3/UniqueID II will have a links to ID 10/UniqueID JJ (Tst - Failed) and ID 8/Unique HH(Tst - Not Done)
I need a measure that calculates the total number of S_Req having at-least one TST with the TestResult "passed" and none "failed" via M_req or E_Req links = 1
= 0 (explanation: as explian above, there's an TST with "Failed" result, so the other doesn't matter) +
0 (explication: ID 5/UniqueID EE -> ID 3/UniqueID CC(M_Req) -> ID 10/UniqueID JJ (Tst - Failed) the other link doesn't matter) +
1 (ID 7/UniqueID GG(S_Req) -> ID 9/Unique II(E_Req) -> ID6/UniqueID FF (Tst - Passed) -> ID 11/Unique KK(M_Req) -> ID8/UniqueID HH (Tst - Not Done) )
Table1: ID UniqueID TestResult S_Req M_Req E_Req Tst
Table1 :
ID
UniqueID
TestResult
S_Req
M_Req
E_Req
TST
1
AA
1
0
0
0
2
BB
Failed
0
0
0
1
3
CC
0
1
0
0
4
DD
Passed
0
0
0
1
5
EE
1
0
0
0
6
FF
Passed
0
0
0
1
7
GG
1
0
0
0
8
HH
Not Done
0
0
0
1
9
II
0
0
1
0
10
JJ
Failed
0
0
0
1
11
KK
0
1
0
0
Table 2:
ID
LinkedID
1
II
1
CC
3
JJ
3
HH
5
CC
7
II
7
KK
9
FF
9
BB
11
HH
That means will be a two layer intersection as i explain earlier: For now all i got is a total number of S_Req that have at least a link with M_Req or E_Req and that's only one layer of intersection:
Measure = VAR _tab1 = CALCULATETABLE ( VALUES ( 'Table 1'[ID] ), FILTER ( 'Table 1', 'Table 1'[S_Req] = 1 ) ) VAR _tabuid = CALCULATETABLE ( VALUES ( 'Table 1'[UniqueID] ), FILTER ( 'Table 1', 'Table 1'[S_Req] = 1 || 'Table 1'[E_Req] = 1 ) ) VAR _tab2 = CALCULATETABLE ( VALUES ( 'Table 2'[ID] ), FILTER ( 'Table 2', 'Table 2'[LinkedID] IN _tabuid ) ) VAR _tab3 = INTERSECT ( _tab2, _tab1 ) RETURN COUNTX ( _tab3, [ID] )

Related

Sum of Value by Category and Status Power BI

I am Attempting to get a sum of ppl that is based on the status of the offer
here is an example of the data
Acc
PPL
Offer comp
ABCDE
1
1
ABCDE
1
0
ABCDE
1
0
CDEFG
2
0
DEFGH
1
1
DEFGH
1
0
DEFGH
1
0
DEFGH
1
1
EFGHI
2
1
EFGHI
2
1
EFGHI
2
0
And this is what I am wanting to achieve as a column either in Power query or Dax
Account
PPL
Offer Comp
total
ABCDE
1
1
1
ABCDE
1
0
1
ABCDE
1
0
1
ABCDE
1
0
1
BCDEF
1
0
1
BCDEF
1
0
1
CDEFG
2
0
2
DEFGH
1
1
2
DEFGH
1
0
2
DEFGH
1
0
2
DEFGH
1
1
2
EFGHI
2
1
4
EFGHI
2
1
4
EFGHI
2
0
4
Basically, we are looking for a count of ppl where if the offer is not completed, we count the ppl, if the offer is accepted, we count the ppl for each acceptance.
Hope this makes sense I would then use this column to create measures as they want ppl count to be unique unless they accepted multiple offers on same account.
I tried to create with selected values and all except but I just couldn't get it to work.
I am probably completely overthinking this.
Very cryptic, and I must admit the sample data and your description is not great, but I think you are after something like this:
Total =
VAR _offer =
SUMX (
ALLEXCEPT (
'Table' ,
'Table'[Account]
) ,
'Table'[Offer Comp] * 'Table'[PPL]
)
RETURN
IF (
_offer > 0 ,
_offer ,
CALCULATE (
MAX ( 'Table'[PPL] ) ,
ALLEXCEPT (
'Table' ,
'Table'[Account]
)
)
)
I have figured it out with help from Marcus
thank you. I realised also need to add a date grouping.
here is the code
Total =
VAR _offer =
CALCULATE(SUM(Table[Offer Comp]),
ALLEXCEPT(Table, Table [Account], Table[Date]))
VAR Pax =
CALCULATE(MAX(Table[PPL]),
ALLEXCEPT(Table, Table[Account]))
RETURN
IF(_offer = 0,
Pax *1,
pax*_offer
)

DAX: Cumulative Completion Rate with Month Slicer

I'm trying to calculate cumulative completion rate by all users over moths, the issue is that in the below table for ex when I filter on october it divides users who finished till october / all users except those who finished in November.
I have a dim_date table which is connect to the data table, the retaltion is between Date from dim_date and Completion Date from Data table
Also in dim date table im numbering the months 1,2,3,4 etc
ID
Completion_status
Completion Date
1
0
2
0
3
0
4
0
5
0
6
1
11/1/2022
7
1
11/1/2022
8
1
11/1/2022
9
1
11/2/2022
10
1
11/1/2022
11
1
11/6/2022
12
1
11/4/2022
13
1
11/2/2022
14
1
10/13/2022
15
1
10/14/2022
16
1
10/14/2022
17
1
10/13/2022
18
1
10/15/2022
19
1
10/13/2022
20
1
10/13/2022
21
1
10/13/2022
22
1
10/13/2022
23
1
10/18/2022
24
1
10/13/2022
25
1
10/13/2022
26
1
10/13/2022
27
1
10/13/2022
28
1
9/10/2022
29
1
9/8/2022
the formula I use
Completion% =
VAR comp rate = SUM(Table[completion_status]) / count(Table[ID])
Return
CALCULATE(Table[Completion%],filter(ALL(Dim_Date),Dim_Date[Month Number] <= MAX(Dim_Date[Month Number])))
the expected result when I filter
on september is 2/29 = 7%
on october is 16/29 = 55%
on November is 24/29 = 83%
Something like:
=
VAR SelectedMonth =
MIN( Dim_Date[Month Number] )
VAR CumulativeTotal =
CALCULATE(
COUNTROWS( 'Table' ),
FILTER(
ALL( Dim_Date ),
Dim_Date[Month Number] <= SelectedMonth
&& NOT ( ISBLANK( Dim_Date[Month Number] ) )
)
)
VAR CountAllRows =
CALCULATE( COUNTROWS( 'Table' ), ALL( Dim_Date ) )
RETURN
DIVIDE( CumulativeTotal, CountAllRows )
I'm presuming that Dim_Date[Month Number] is blank when Table[Completion Date] is blank.
You may want to replace ALL with, for example, ALLSELECTED, depending on your required set-up.

How to create a column based on grouped condition?

My test tabe in powerbi:
IdRecord
Date
Value
1
2022-04-25 23:45:00.000
100
1
2022-04-24 18:07:00.000
344
2
2022-05-01 23:45:00.000
5
2
2022-05-02 18:07:00.000
66
2
2022-05-03 18:07:00.000
31
I require to create a calculated column to mark the earliest of the records grouped by id.
Desired output
IdRecord
Date
Value
IsFirst
1
2022-04-25 23:45:00.000
100
0
1
2022-04-24 18:07:00.000
344
1
2
2022-05-01 23:45:00.000
5
1
2
2022-05-02 18:07:00.000
66
0
2
2022-05-03 18:07:00.000
31
0
Answering to myself
FirstRes= VAR MYMIN = CALCULATE(
MIN(Table[Date]),
FILTER ( Table, Table[IdRecord] = EARLIER(Table[IdRecord]))
)
RETURN
IF(CALCULATE(
MIN(MIN(Table[Date]),MYMIN),
FILTER ( Table, Table[IdRecord] = EARLIER ( Table[IdRecord] ) )
) = Table[Date],1,0)

PowerBI DAX Measure to return 1 for all the items in category if any item in category has result 1

I need measure (or maybe calculated column) which will return 1 in the column called Final when any of the item Table2[name] per category (Table1[Category]) is 1. So even if there is just one item in the category that has result 1 it returns 1 for all of them in the same category and 0 when all the items in the category have result 0. Hope the example below is clear.
Table1[Category]
Table2[name]
Result
Final
A
A:1
0
1
A
A:2
1
1
A
A:3
0
1
B
B:1
0
0
B
B:2
0
0
C
C:1
1
1
C
C:2
0
1
Using variables you can access the Category to filter on the table and obtain the MAX result.
Calculation: Calculated Column
Final =
VAR CurrentCat = [Table1[Category]]]
VAR MaxResult =
MAXX ( FILTER ( 'Table', [Table1[Category]]] = CurrentCat ), [Result] )
RETURN
IF ( MaxResult = 1, 1, 0 )
Output
Table1[Category]
Table2[name]
Result
Final
A
A:1
0
1
A
A:2
1
1
A
A:3
0
1
B
B:1
0
0
B
B:2
0
0
C
C:1
1
1
C
C:2
0
1

Power BI : DAX: Running Sum with fixed start date - even when filtering

I have two tables, with:
Entrydate, several categories
ChurnDate, several categories
The categories are connected via different tables, and the dates are connected with a Calendar.
Now I want to calculate how many customers I have. So I have following DAX formulas
1. SumChurn =
CALCULATE(
SUM('kuendigungen'[KUENDIGUNG]);
FILTER(
ALLSELECTED('Calendar'[Date]);
ISONORAFTER('Calendar'[Date]; MAX('Calendar'[Date]); DESC)
)
)
2. SumEntry =
CALCULATE(
SUM('eintritt'[NEUMITGLIED]);
FILTER(
ALLSELECTED('Calendar'[Date]);
ISONORAFTER('Calendar'[Date]; MAX('Calendar'[Date]); DESC)
)
)
3. TotalCustomers = SumEntry - SumChurn
This works, but in my diagram I want to filter the dates, so that it only visualizes 2020 or the last 3 years.When I do this the calculation is wrong because it only counts in this interval.
Is there a solution that I can filter the date in my visuals but in my calculation the start date of the cummulative sum is always fixed?
I dont't want a new column because I still want to filter my categories of customers...
Thanks,
Michaela
Edit: Try to explain it clearer
Example Table 1: contains new customers
Date unique_id1 unique_id2 unique_id3 cat1 cat2 cat3 cat4 cat5 cat6
1886-02-01 2070030124 550261 207000152145 207 0 0 1 0 0
1887-01-01 4350002756 4081878 435000010707 435 0 0 1 0 0
1888-01-01 7030000597 3206858 703000001279 703 0 0 1 0 0
1888-06-01 7030016696 3208056 703000005002 703 0 0 1 0 0
1888-09-01 8210024182 204124 821000008664 821 1 0 1 0 1
1889-01-01 7050055324 1988250 705000018309 705 1 0 1 0 0
1889-01-01 8250000278 439485 825000600296 825 0 0 1 0 0
1889-05-01 7030023754 3208355 703000000884 703 0 0 1 0 0
1889-10-01 2110071206 2849359 211000330019 211 0 1 1 0 0
1889-10-01 2110071236 2851371 211000120014 211 0 0 1 0 0
1889-11-14 5190529889 4260192 519000123846 519 1 0 1 0 0
1890-07-01 7330349030 4819467 733000013102 733 0 0 1 0 0
1890-07-01 7330152914 4817492 733000075604 733 1 0 1 0 1
1890-07-01 8190000889 486170 819000215708 819 0 0 1 0 0
1890-07-01 8190444976 486199 819000215740 819 0 0 1 0 0
1890-12-01 8190001388 476049 819000100005 819 0 0 1 0 0
1891-01-01 7030001248 3206975 703000000043 703 0 0 1 0 1
Example Table 2: contains leaving customers
similiar to table 1
Example Calendar Table:
01.01.1990
02.01.1990
03.01.1990 ... (till today)
Output shut be a measure
for each day in calendar: number of customer at this date = cumulative_sum(newcustomer) - cumulative_sum(churncustomer)
I get exactly this output, when I run the calculations I wrote, but I want the measure in a way, ehen I filter the date, the sum is still the cummulative sum from the very first date, otherwise the numbers are wrong.
Edit3:
I did exactly the same thing, as mkrabbani posted, but it doesnt't work for me, following calculations:
TotalKuendigungen =
CALCULATE(
SUM('kuendigungen'[KUENDIGUNG]);
FILTER (
ALL ( 'Calendar'[Date] );
( 'Calendar'[Date] <= MAX ( ( 'Calendar'[Date] ))
)))
TotalNeukunden = CALCULATE(
SUM('eintritt'[NEUMITGLIED]);
FILTER (
ALL ( 'Calendar'[Date] );
( 'Calendar'[Date] <= MAX ( ( 'Calendar'[Date] ))
)))
AnzahlMitglieder = [SummeNeumitglied] - [SummeKuendigung]
This is how it looks for me: (Neukunden: new customers, kündigungen: leaving, aktuellemitglieder: number of customers)
Picture 1 correct calculation
Picture 2: also correct calculation, but filter doesnt work
thanks for adding some sample data with more explanation. If I get your requirement correct, this below steps with explanation will help you solving your issue I hope.
Assumption: If my understanding is correct, you have 3 tables with Date, new_customer and leaving_customer and they are related as below diagram shown.
Now, I have created some sample data for 10 days, to visualize your requirement/issue. Hope, cumulative counts in the below table is correctly calculated (using basics of cumulative calculation).
At this stage, you need a measure that will calculate current number of customer for each row based on calculation > "cumulative_new_customer - cumulative_leaving_customer" which is not a tough job for you.
But, you are having issue when you are slicing your data using Date slicer. If you are selecting date number 5, which is "January 05 2020" in my sample data. You wants the final counts based on date January 01 to 05, but you are getting only counts from one single date "January 05 2020".
If the above explanation is correct, I would suggest to write 3 separate Measure as explained below in this answer. You can have a look on the output in the below picture I have added with comparison with before and after slicing the data. You can see the number of current user for "January 05 2020" is 41 for both case (Before and After Slicing)
Now, if everything above is meeting your expectation, you can use this below 3 measures as written.
1.
cumulative_new_customer =
CALCULATE (
COUNT(new_customer[unique_id]),
FILTER (
ALL ( 'Dates'[Date] ),
'Dates'[Date] <= MAX ( 'Dates'[Date] )
)
)
2.
cumulative_leaving_customer =
CALCULATE (
COUNT(leaving_customer[unique_id]),
FILTER (
ALL ( 'Dates'[Date] ),
'Dates'[Date] <= MAX ( 'Dates'[Date] )
)
)
3.
number_of_cutomer_today = [cumulative_new_customer] - [cumulative_leaving_customer]
Hope the above details will help you.