IF condition in DAX is creating Cartesian Join on rows - powerbi

Dax formula without If condition works fine, but as soon as I add an IF condition rows start multiplying. I believe it's doing Cartesian Product.
My requirement is simple. I need to show only those rows in which Sickness[Start_Date]>LASTDATE(Test[Date])
The complete formula is -
Measure =
var val = CALCULATE(MAX(Sickness[Start_Date]),FILTER(Sickness,Sickness[Start_Date]>LASTDATE(Test[Date])),ALL())
return
IF(val = BLANK(),0,1)
I have 3 separate tables Emp_data, Test and Sickness.
To replicate this scenario, Please follow the below steps:
1st Step : Create table Emp_data
Emp_data = DATATABLE("Emp_no",INTEGER,"Name",STRING,{{101,"A"},
{102,"B"},
{103,"C"},
{104,"D"},
{105,"E"},
{106,"F"},
{107,"G"},
{108,"I"},
{109,"J"},
{110,"K"},
{111,"L"},
{112,"N"},
{113,"M"},
{114,"O"},
{115,"P"},
{116,"Q"},
{117,"R"},
{118,"S"},
{119,"T"},
{120,"U"}
})
create table : Test
Test = DATATABLE("Emp_No",INTEGER,"Date",DATETIME,"Result",INTEGER,{{101,"3/10/2020",1},
{101,"3/13/2020",2},
{102,"3/11/2020",1},
{103,"3/12/2020",2},
{104,"3/13/2020",1},
{105,"3/14/2020",1},
{106,"3/15/2020",2},
{107,"3/16/2020",1},
{108,"4/20/2020",1},
{109,"4/21/2020",2},
{110,"4/22/2020",2},
{111,"4/23/2020",1},
{112,"4/24/2020",1},
{113,"4/25/2020",2},
{114,"4/26/2020",1},
{115,"4/27/2020",2},
{116,"5/5/2020",1},
{117,"5/5/2020",1},
{118,"5/5/2020",1},
{119,"5/5/2020",1},
{120,"5/5/2020",2}
})
Create table Sickness
Sickness = DATATABLE("Emp_no",INTEGER,"Start_Date",DATETIME,"End_Date",DATETIME,"Sickness_Code",INTEGER,{{101,"2/12/2020","2/12/2020",30},
{101,"3/10/2020","3/15/2020",50},
{101,"3/20/2020","3/30/2020",50},
{101,"4/5/2020","4/10/2020",40},
{102,"3/11/2020","3/11/2020",50},
{107,"3/1/2020","3/2/2020",30},
{107,"3/15/2020","3/20/2020",50},
{107,"3/21/2020","3/31/2020",40},
{112,"4/20/2020","4/30/2020",50},
{112,"5/1/2020","5/15/2020",50},
{116,"4/1/2020","4/15/2020",30},
{116,"5/3/2020","5/15/2020",50},
{116,"5/16/2020","5/26/2020",50},
{116,"5/27/2020","5/29/2020",40}}
)
Second Step is to create relationship between these 3 tables. Emp_Data and Test table have one to many relationship. I changed the filter direction to BOTH. Thinking may be this will resolve the issue. Create relation many to many between the tables Test and Sickness. The image is attached
The output is as follows :
If I remove the IF condition my out is what I need :
Can someone help me in understanding this behavior of Power BI. This looks very strange. Why row count is increasing after applying IF condition. Thanks in advance

It's because you assigned a 0 value to all blank values in your Sickness data table. Once your remove the if statement, Power BI only shows data rows that do not return a blank value.
To fix your issue try this updated meassure:
Measure =
VAR val = CALCULATE(MAX(Sickness[Start_Date]),FILTER(Sickness,Sickness[Start_Date]>LASTDATE(Test[Date])),ALL())
VAR OneZero = IF(ISBLANK(val),0,1)
RETURN
IF(HASONEVALUE(Sickness[Emp_no],OneZero)

Related

Power BI DAX How to add column to a calculated table that summarizes another

I Have a TestTable that summarizes a table Receipts on the Month column and adds a column that counts the number of times (occurence) that each month appears in the Receipts Table.
TestTable = SUMMARIZE(Receipts, Receipts[Month], "TotalReceiptsIssuedInThisMonth", SUM(Receipts[Receipts Issued]), "OccurenceOfMonth", COUNT(Receipts[Month]))
I want to add two columns to this TestTable which will tell me the following:
Sum the TotalReceiptsIssuedInThisMonth of the TestTable and show the
value in each row
For each Month (row), divide the, TotalReceiptsIssuedInThisMonth by the SumOfTotalReceiptsIssued
I know I can click "New Column" and use these formulas:
AvgPercentageReceiptsIssuedInThisMonth = TestTable[TotalReceiptsIssuedInThisMonth]/TestTable[TotalReceiptsIssued]
TotalReceiptsIssued = SUM(TestTable[TotalReceiptsIssuedInThisMonth])
However, I need to integrate those two columns directly into the original TestTable formula to make it all happen in one step for use as a variable in the original Receipts table (otherwise I end up with circular logic if I try using relationships).
I've tried the following:
TestTable = SUMMARIZE(PPTs, PPTs[Month], "TotalReceiptsIssuedInThisMonth", SUM(PPTs[PPTs Issued]), "OccurenceOfMonth", COUNT(PPTs[Month]), "TotalReceiptsIssued", SUM(TestTable[TotalReceiptsIssuedInThisMonth]), "AvgPercentageReceiptsIssuedInThisMonth", TestTable[TotalReceiptsIssuedInThisMonth]/TestTable[TotalReceiptsIssued])
but this returns an error saying "A single value for column 'TotalReceiptsIssuedInThisMonth' in table 'TestTable" cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result." and I've tried:
TestTable =
VAR first = SUMMARIZE(Receipts, Receipts[Month], "TotalReceiptsIssuedInThisMonth", SUM(Receipts[Receipts Issued]), "OccurenceOfMonth", COUNT(Receipts[Month]))
VAR second = SUM(TestTable[TotalReceiptsIssuedInThisMonth])
VAR third = first[TotalReceiptsIssuedInThisMonth]/second
RETURN
third
But this returns an error saying "The variable'first' cannot be used in current context because a base table is expected."
So my question is, how do I go about combining these three steps into one DAX formula?
I would do something like this. I prefer ADDCOLLUMN(SUMMARIZE()...), because it helps to avoid a miscontexting. As you need a var table, then you need the CALCULATE in ADDCOLUMNS, as it adds the row context.
VAR TestTable =
ADDCOLUMNS(
SUMMARIZE(
Receipts
,Receipts[Month]
)
,"TotalReceiptsIssuedInThisMonth",CALCULATE(SUM(Receipts[Receipts Issued]))
,"OccurenceOfMonth", CALCULATE(COUNT(Receipts[Month]))
,"TotalReceiptsIssued ",SUM(Receipts[Receipts Issued])
)
RETURN
ADDCOLUMNS(
TestTable
,"AvgPercentageReceiptsIssuedInThisMonth"
,DIVIDE([TotalReceiptsIssuedInThisMonth],[TotalReceiptsIssued])
)
Check out More regarding the use of DAX to create a table
https://www.advancelearnlinux.com/how_to_create-table-in-microsoft-power-bi-using-dax-function/

Finding the first date between multiple entries // PowerBI

My co-worker has run into an issue finding the amount of days between 2 different entries on a database.
In this database , there can be multiple entires with the same RegNumber , however sometimes the entries are on separate dates.
We would need to fetch the first date (TxrDate) for each RegNr and add it to the "FirstInvDate" column on each line.
Please see the below sample data:
And the below is what he has tried:
Does anyone know if there is an easier way to do this or a specific formula to follow ?
To create a new table use:
FistInvDate =
var __currRegNumber = 'Table'[RegNumber]
return
calculate( min( table[yourDate]), filter(ALL('table'), __currRegNumber = 'Table'[RegNumber]))

PowerBI: Aggregate Measure correctly by condition on DATEDIFF

I have the following Table:
BaseTable
It represents processes with a certain category.
And there is also a Date Table over column TIMESTAMP.
I would like to show a Measure based on another Measure that calculates the Date-Difference until the selected Date.
So first this is how I calculate the Date-Difference:
AGE =
VAR SELECTED_DATE = CALCULATE(MAX(DATUM[Date]), ALLSELECTED(DATUM))
VAR STARTDATE_PROCESS = Calculate(MAX(Workflow[MIN_TIMESTAMP]),DATUM[Date]<=MAX(DATUM[Date]), ALL(DATUM[Date]))
RETURN
DATEDIFF(STARTDATE_PROCESS,SELECTED_DATE,DAY)
Now I want to use a Measure which depends on the result of AGE, like
NEW = IF([AGE]<=3,CALCULATE(COUNT(Workflow[PROCESS]),DATUM[Date]<=MAX(DATUM[Date]),ALL(DATUM)))
or
OLD = IF([AGE]>3,CALCULATE(COUNT(Workflow[PROCESS]),DATUM[Date]<=MAX(DATUM[Date]),ALL(DATUM)))
The Measures AGE, OLD and NEW look like that with the Base Table:
Measures
As you can see the aggregation is not working correctly:
Result_Wrong
But it should be like that
Result_Correct
Any idea how to fix that?
Thank you!
So the problem is that the subtotal is calculated at a whole different context, and because your Age measure is based on the MAX(Workflow[MIN_TIMESTAMP]) that won't take into account that there can be multiple processes.
To do what you want, you need to change the New and Old measures to perform an aggregation per process and then return the result of that. Something like this:
New_agg =
VAR tbl = ADDCOLUMNS(CALCULATETABLE(VALUES(Workflow[Process]), ALL('Date')), "age", [Age], "count_process", CALCULATE(COUNT(Workflow[Process]), ALL('Date')))
RETURN SUMX(tbl, IF([age]<=3, [count_process]))
Demo File
Let me know if below solution is working
Unfortunately I am unable to generate the dummy data that you have been using, so Created my own data for developing the solution.
Now from this data I have calculated the difference of dates and put it as Age
Now to get the count of process for the condition like yours, I have created two formulas and the result is:
Logic I followed here is, instead of creating measure I have created columns and took the sum of those columns which will give the data you need as sum of those columns.
Column for New:
New = IF((Sheet1[Age]) > 20, 1,0)
Column for Old:
Old = IF((Sheet1[Age]) < 20, 1,0)
Now place both formulas in "Values" and take sum as the aggregation.
Final result is

Treatas DAX function not working as expected, It is returning complete blank column

I trying to understand Treatas DAX function. There are two tables Assets and Ticket. Ticket table has parent and child relationship. For each value of Asset[AssetKey], I want to calculate count of childs in Ticket table. There is two relationships between these tables. One active and one inactive.
The Problem: When I use Treatas function complete measure Number of Child is retured blank. I used the formula -
Number of Child = CALCULATE(COUNT(Tickets[AssetKey]),TREATAS(SUMMARIZE(Asset,Asset[AssetKey]),Tickets[ParentId]))
To replicate the scenario follow the below steps:
Step 1: create table Asset:
Asset = DATATABLE("AssetKey",INTEGER,"Name",STRING,{{1,"Australia"},
{2,"Belgium"},
{3,"Canada"},
{4,"Denmark"},
{5,"England"}})
Create table Ticket
Tickets = DATATABLE("AssetKey",INTEGER,"ParentId",INTEGER,"TicketKey",INTEGER,{{3,1,1},
{1,Blank(),1},
{3,1,3},
{2,Blank(),4},
{4,2,5},
{3,1,6},
{2,Blank(),7},
{4,2,8},
{1,Blank(),9},
{5,2,10}})
Step2 : create relationship between Assets and Ticket table(one to many) on column AssetKey.
Step3: Now create the below Measures -
Number Of Tickets = COUNT(Tickets[TicketKey])
Number of Child = CALCULATE(COUNT(Tickets[AssetKey]),TREATAS(SUMMARIZE(Asset,Asset[AssetKey]),Tickets[ParentId]))
Now the problem: Why the Number of Child column comes out to be blank.
The expected output is :
Your problem is not the TREATAS but the SUMMARIZE. TREATAS expects table input so you summarized.
Try the following:
summarizedAsset = SUMMARIZE(Asset,Asset[AssetKey])
This returns you 1,2,3,4,5. Logic because this is what you asked it to do. Now TREATAS is going to do the same on the table Ticket. So it returns a table with one column values "empty",1,2. Exactly what we can expect.
Next thing you do a COUNT on AssetKey, this table just created does not have this column so it returns empty.
What you are looking for is a column what is calculating the children:
Number of Child =
var Akey = Asset[AssetKey]
return CALCULATE(COUNT(Tickets[ParentId]), filter(Tickets, Akey = Tickets[ParentId]))
This returns exactly what you where looking for.
PS: 10 points, you did an excellent job on the question asking, easy for others to reproduce. You work as a pro!!

Display Matched and Non Matched Values based on a slicer value Power BI

I am working on a Viewership table which tells which customer watches which asset. Based on the asset filter, I need to display the customers who watched the show & customers who didn't watched the show. below is my example table
If the asset_id selected as 1 in the slicer, the desired output will be as below
I have tried creating a cross-join table with asset_id and customer_id , but that approach taking much time with large data. Request the experts here to suggest the best optimal solution to achieve this.
First, create a new table "Asset":
This table contains unique assets, and we will use it to create a slicer that affects DAX measure but does not affect the visual (table). To achieve that, the Asset table must be disconnected from the Viewership table (no relationships).
In your viewership table, I just renamed "asset" to "asset_id", to be consistent:
Next, create a measure:
Status =
VAR Selected_Asset = SELECTEDVALUE(Asset[asset_id])
VAR Customer_Asset = SELECTEDVALUE(Viewership[asset_id])
RETURN
IF(Customer_Asset = Selected_Asset, "Watched", "Not Watched")
Result:
Slicer here is created from the "Asset" table, and table is a table visual with customer_id and asset_id from the Viewership table (set them as "don't summarize" values). I turned off "total", assuming you don't need it.
This design requires to set Asset slicer to "single selection" mode, to make sure that you are only getting one value from it. If you want the model to work with multi-select slicer, change DAX measure as follows:
Multi Status =
VAR Selected_Assets = ALLSELECTED(Asset[asset_id])
VAR Customer_Asset = SELECTEDVALUE(Viewership[asset_id])
RETURN
IF(Customer_Asset IN Selected_Assets, "Watched", "Not Watched")
Result:
Edit:
To make it work at the customer level:
Customer Status =
VAR Selected_Assets = ALLSELECTED(Asset[asset_id])
VAR Customer_Assets = VALUES(Viewership[asset_id])
VAR Assets_Watched = COUNTROWS(INTERSECT(Customer_Assets, Selected_Assets))
RETURN
IF(Assets_Watched > 0, "Watched", "Not Watched")
Result:
Explanation: store selected assets in a table variable. Then, store assets visible per customer in another table variable. Find an intersect of the two tables (what they have in common), and count intersect rows. If none - not watched, otherwise watched. If you want, you can actually display the number of movies watched (just return "Assets_Watched" instead of IF statement).