Count unique matching items as a calculated column - powerbi

I have two tables are Data and Report.
Data Table:
In Data table contain two columns are Item and status.
The item column contains duplicated entry and the item column contains text and number or number only or text only.
The status column contains two different text/comments, "Okay" and "Not Okay"
The report table
In the Report table, I updated both comments/text as "Okay" or "Not Okay".
I would like to create a new calculated column in the report table in order to get the unique count according to the comments based on the data table columns item and status.
In Excel, I am applying the following formula
F2=SUM((FREQUENCY(MATCH(A$2:$A$19&"",$A$1:$A$19&"",0)*($B$2:$B$19=$D3),ROW($A$2:$A$19))>0)+0)-1
in order to get my final result.
I don't want measure solutions.
DATA TABLE:
REPORT TABLE:
EXCEL LOGIC:

This is much easier in DAX than in Excel and there are many ways to do it.
Here are some possibilities with different approaches:
Desired Result =
VAR Comment = REPORT[COMMENTS]
RETURN
CALCULATE (
DISTINCTCOUNT ( DATA[ITEM] ),
DATA[STATUS] = Comment
)
Desired Result =
COUNTROWS (
SUMMARIZE (
FILTER ( DATA, DATA[STATUS] = REPORT[COMMENTS] ),
DATA[ITEM]
)
)
Desired Result =
SUMX (
DISTINCT ( DATA[ITEM] ),
IF ( CALCULATE ( SELECTEDVALUE ( DATA[STATUS] ) ) = REPORT[COMMENTS], 1, 0 )
)

Related

Power BI - Comparing data at Row Level and concatenating the respective data in other column for same records

I want to show the consolidated data for similar record in separate column in power bi data.
I have a data with two columns and i want the result like below in third & fourth column
3rd column result nothing but comparing unique id in rows, e.g. 1=2 =false, 2=2=true
4th column result nothing but concatenation of Value column for unique record
could you please help to achieve this in power bi - i want to create custom columns for these two result in data table
We can use CalculatedColumn in DAX:
Result =
CALCULATE(
CONCATENATEX (
CALCULATETABLE ( VALUES('Unique'[Value] ) ),
'Unique'[Value],
", ",
'Unique'[Value],
ASC
), ALL('Unique'[Value])
)
And you Validation may be a measure:
Valid = if(SELECTEDVALUE('Unique'[Value]) = CALCULATE( max('Unique'[Value]), ALL('Unique'[Value])), FALSE(), TRUE())

IFERROR then NA in Power BI

I have two tables are data and report.
The data table contains two columns: item and text. The item and text columns contain duplicates.
I am applying the following calculated column in the report table
RESULT =
CALCULATE (
FIRSTNONBLANK ( DATA[TEXT], TRUE () ),
FILTER ( DATA, DATA[ITEM] = REPORT[ITEM] )
)
In order to get the data from the data table into the report table.
If the item is not available in the data table, then return “NA” according to the item in the report table. My calculated column is showing blanks if the item is not available in the data table but I want "NA" instead of blanks.
My calculated column is picking the min qty according to the item in the report table based on the data table where the same item contains multiple qty but I want max qty in the report table according to the item where the same item contains multiple qty.
Can you please advise?
Herewith attached the power bi for your reference.
https://www.dropbox.com/s/e3fa45f8qz8a2tm/LOOKUP%20MULTIPLE%20VALUE_QTY.pbix?dl=0
Data & Report:
You can get the max with just
RESULT = CALCULATE ( MAX ( DATA[TEXT] ) )
Replacing blanks with NA is possible but it means the RESULT column must be converted to a text data type (since a column cannot have multiple data types).
I don't advise doing that, but you can like this:
RESULT =
VAR MaxText = CALCULATE ( MAX ( DATA[TEXT] ) )
RETURN
IF ( ISBLANK ( MaxText ), "NA", FORMAT ( MaxText, "0" ) )

Filtering on calculated column of Cartesian product in DAX

I need a slicer for ticking only those Products and Regions which have budgeted Targets.
My data model is a bit complicated than I show here. In my real scenario table Budget does not exist and Target values have to be calculated from other tables of varying granularity. Lets assume we cannot use calculated column on Budget table.
Here green tables are one-column-all-values-dimension bridges. The red table is a Cartesian product of Products and Brands with calculated Target.
Here is a DAX code for the red table I cooked to solve the problem.
#Brand x Region =
ADDCOLUMNS (
CROSSJOIN ( '#product', '#region' ),
"Target", CALCULATE ( SUM ( Budget[target] ) ),
"IsTarget", IF ( CALCULATE ( SUM ( Budget[target] ) ) > 0, "Yes", "No" )
)
The table shows like this:
But such cunningly obtained column IsTarget does not affect my visuals through the slicer. How to fix it.
File PBIX here.
Edit after comments.
Alexis, is that what you mean? I added column P#R which is concatenation of Product and Region. It seems to work:-)
This is what I was suggesting, where the bottom relationships are on the Index columns.
In order to do this, my #Brand x Region table was this:
#Brand x Region =
VAR CrossProduct =
ADDCOLUMNS (
CROSSJOIN ( '#product', '#region' ),
"Target",
CALCULATE (
SUM ( Budget[target] ),
FILTER (
Budget,
Budget[product] = EARLIER ( '#product'[product] ) &&
Budget[region] = EARLIER ( '#region'[region] )
)
)
)
RETURN
ADDCOLUMNS(
CrossProduct,
"IsTarget", IF ( [Target] > 0, "Yes", "No" ),
"Index", RANKX(CrossProduct, '#product'[product] & '#region'[region])
)
(Note: The filtering has to be explicit since I'm not using the relationships you originally had.)
From there I pulled over the index to the FactTableSales and Budget with a lookup:
Index =
LOOKUPVALUE (
'#Brand x Region'[Index],
'#Brand x Region'[product], [product],
'#Brand x Region'[region], [region]
)
Note that creating an index column is often easier in the query editor rather than trying to do it in DAX but you can't modify a calculated table in the query editor.

Filter existing table to another table without adding measures or column on existing table

I want to create a table based on input table.
Input table is:
The new table filters the input table to show the last entry of every day.
I have tried working with measure but sometimes cant tell if it is working right until I graph it in pivot tables which is not so bad but sometimes just doesn't show me what I need to see exactly.
I have tried this measure:
History_Daily Efficiency =
VAR LastDailyEfficiency =
GENERATE(
VALUES ('Table_Full'[Cell]),
CALCULATETABLE (
TOPN (
1,
GROUPBY (
'Table_Full',
'Table_Full'[Date],
'Table_Full'[Time],
'Table_Full'[Efficiency]
),
'Table_Full'[Date], DESC,
'Table_Full'[Time], DESC,
'Table_Full'[Efficiency], ASC
)
)
)
RETURN
CALCULATE (
AVERAGE('Table_Full'[Efficiency]),
TREATAS( LastDailyEfficiency, 'Table_Full'[Cell], 'Table_Full'[Date], 'Table_Full'[Time], 'Table_Full'[Efficiency]),
'Table_Full'[Efficiency] < 80
)
But I got this:
I would like to see this as the output:
You can create a new table:
LastDayCount = GROUPBY(Table_Full;Table_Full[lob/Part Number];Table_Full[Date];"LastDate";MAXX(CURRENTGROUP(); Table_Full[DateTime]))
This will create a table with the last DateTime of the day.
Next we add a column giving us the max of that particular last datetime of the day. I noticed that you have more the same entries, the logic below takes the max part count at the end of the day when more than one entry.
Count =
CALCULATE(MAX(Table_Full[Part Count]);
FILTER(Table_Full;LastDayCount[Table_Full_lob/Part Number] = Table_Full[lob/Part Number]
&& LastDayCount[LastDate] = Table_Full[DateTime]))
End result:

How to create stack bar chart from 4 different tables in Power BI

I have 4 different tables that are not joined to each other and all have same column names: 'Status' and 'Count'.
I want to create stuck bar chart that would display count for each Status.
As an example I'm using first two tables: Overall_Results and PredictedBoundResults
So I put column Status on Axis, Count on Value and it works fine. But if I put second Count in value then it gives me total number, instead of breaking out by Status.
So my question: if I bring ID column to all those 4 tables and join them together, will that give me desirable result?
.ipbx can be accessed here:
https://www.dropbox.com/s/ursrwhyz9xqtd3c/PredictionsUnderwrProducer.pbix?dl=0
Yes, this is possible. The easiest way, is to create a conformed dimension, just for Status. In this table, you collect all possible (distinct) values of Status. You can do this with UNION and VALUES/DISTINCT.
After that, connect the status column (from the new table) to all four tables. Then you can report on Status, like you want.
Edit, like this:
Calculated table:
DimStatus =
DISTINCT (
FILTER (
UNION (
UNION (
SELECTCOLUMNS ( PredictedBoundResults; "Status"; PredictedBoundResults[Status] );
SELECTCOLUMNS (
PredictedNotBoundResults;
"Status"; PredictedNotBoundResults[Status]
)
);
UNION (
SELECTCOLUMNS ( Overall_Results; "Status"; Overall_Results[Status] );
SELECTCOLUMNS ( Not_Predicted; "Status"; Not_Predicted[Status] )
)
);
NOT ISBLANK ( [Status] )
)
)
Connect to all four statusses
Result: