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

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!!

Related

Power BI LOOKUPVALUE with a column of values for the search items? (VLOOKUP alternative)

In Power BI, I need to create a VLOOKUP alternative. From the research I've done, this is done with the LOOKUPVALUE function, but the problem is that function needs one specific SEARCH ITEM, which isn't super helpful in a VLOOKUP type scenario where you have a full column of values to search for?
Given these two tables, connected through the user_name and first_name columns:
...what's the formula needed in order to create a new column in the Employee_Table called phone_call_group by using the names as the search items in order to return the group they belong to? So how can I end up with this?
(Forget that the entries in each table are already sorted, needs to be dynamic). Will be back tomorrow to review solutions.
In Power BI you have relations between tables instead of Excel's VLOOKUP function.
In your case you just have to create a one-to-one relation between
'Phone_Call_Table'[user_name] and 'Employee_Table'['first_name]'
With that you can add a Calculated Column to your 'Employee_Table' using the following expression:
phone_call_group = RELATED(Phone_Call_Table[group])
and in the data view the table will look like this:
LOOKUPVALUE() is just a workaround if for other reasons you can't establish that relation. What you've been missing so far is that in a Calculated Column there is a Row Context which gives you exactly one value per row for the <search_value> (this is different from Measures):
alt_phone_call_group =
LOOKUPVALUE(
Phone_Call_Table[group],
Phone_Call_Table[user_name],
Employee_Table[first_name]
)

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/

In the Films table create a calculated column called NumberBreaks which shows for each film the number of breaks needed

The Films table looks like this
There is a ComfortBreaks table looking like image
In the Films table I need to create a calculated column called NumberBreaks which shows for each film the number of breaks needed. To do this I have to pick out the value of the Breaks column where:
The value of the lower limit in the ComfortBreaks table is less than or equal to this film's running time in minutes
and
The value of the upper limit in the ComfortBreaks table is greater than this film's running time in minutes.
the result should look like the image below
There cannot be a relationship between the two tables. so this has to be done without creating relationship between them.
I tried lookup function but it showed error:A table of multiple values was supplied where a single value was expected.
You can use this below code for your custom column. Considering
number_of_breaks =
VAR current_row_run_time_minutes = Films[RunTimeMinutes]
RETURN
MINX (
FILTER(
ComfortBreaks,
ComfortBreaks[LowerLimit] <= current_row_run_time_minutes
&& ComfortBreaks[UperLimit] > MonthNumber
),
ComfortBreaks[Breaks]
)
You can perform your further average calculation on the new custom column now.

Power BI - totals per related entity

Basically, I’d like to get one entity totals, but calculated for another (but still related/associated!) entity. Relation type between these entities is many-to-many.
Just to be less abstract, let’s take Trips and Shipments as mentioned entities and Shipments’ weight as a total to be calculated.
Calculating weight totals just per each trip is pretty easy task. Here is a table of Shipments weights:
We place them into some amounts of trucks/trips and get following weight totals per trip:
But when I try to show SUM of Trip weight totals (figures from 2nd table) per each related Shipment (Column from 1st table), it becomes much harder than I expect.
It should look like:
And I can’t get such table within Power BI.
Data model for your reference:
Seems like SUMMARIZE function is almost fit, but it doesn’t allow me to use a column from another table than initialized in the function:
Additional restrictions:
Selections made by user (clicks on cells etc.) should not affect calculation anyhow.
The figures should be able to be used in further calculations, using them as a basis.
Can anyone advise a solution? Or at least proper DAX references to consider? I thought I could find a quick answer in DAX reference guide on my own. However I failed to find a quick answer.
Version 1
Try the following DAX function as a calculated column inside of your shipments table:
TripWeight =
VAR tripID =
RELATED ( Trips[TripID] )
RETURN
CALCULATE (
SUM ( Shipments[ShipmentTaxWeightKG] );
FILTER ( Shipments; RELATED ( InkTable[TripID] ) = tripID )
)
The first expression var tripID is storing the TripID of the current row and the CALCULATE function gets the SUM of all of the weight for all the shipments that belong to the current trip.
Version 2
You can also create a calculated table using the following DAX and create a relationship between the newly created table and your Trips table and simply display the weight in this table:
TripWeight =
GROUPBY (
Shipments;
Trips[TripID];
"Total Weight KG"; SUMX ( CURRENTGROUP (); Shipments[ShipmentTaxWeightKG] )
)
Version 3
Version 1 and 2 are only working if the relationship between lnkTrip and Shipment is a One-to-One relationship. If it is a many-to-one relationship, the following calculated column can be created inside of the Trips table:
ShipmentTaxWeightKG by Trip = SUMX(RELATEDTABLE(Shipments); Shipments[ShipmentTaxWeightKG])
hope this helps.

How to Perform a Full-Outer Join on Two Separate, Filtered Tables using DAX?

I have an original table named Error, and two additional tables (ErrorBefore and ErrorAfter) derived from the original (e.g. ErrorAfter = ALLSELECTED('Error')). I want to compare values from a 'before' version with an 'after' version, with the different version picked by slicer with 'Single select' on. That's working okay. Now I want to perform a full-outer join on the two results, joining on the Message column. The image below shows the result I have so far, with a fabricated table at the bottom of what I'm trying to achieve. I've tried using NATURALLEFTOUTERJOIN and GENERATE but they either don't give the result that I seek. Does anyone know how to perform the join?
PBIX share here.
First, change your data model to this:
I removed all your derived tables and relations, and instead created 2 tables like this:
Version Before = DISTINCT('Error'[Version])
Version After = DISTINCT('Error'[Version])
Both tables should have no relations with the Error table.
Then, create a measure:
Message Count = COUNT('Error'[Message])
You should always create measures yourself, never use Power BI auto-aggregations.
Next, create a measure for "Before" count"
Message Count Before =
VAR Version_Before = SELECTEDVALUE('Version Before'[Version])
RETURN
CALCULATE([Message Count], 'Error'[Version] = Version_Before)
and, similarly:
Message Count After =
VAR Version_After = SELECTEDVALUE('Version After'[Version])
RETURN
CALCULATE([Message Count], 'Error'[Version] = Version_After)
Finally, adjust your visuals:
Slicer "Before" should be based on table "Version Before"
Slicer "After" should be based on table "Version After"
Charts and tables should use "Message Count Before" and "Message Count After" measures in values
Add another table with messages and both measures
Result: