Power BI - I need a "countif" formula to look at unique entries in row/column - excel-formula

I have only been working in Power BI for a little over one week now, and now I have run into my first problem..
I need a way to do the same as in below Excel formula..
In Power Bi I have ONE table.
I want to count unique "Split Orders" in column "B"
But I only want to count them once..
So the first time it is found, it is counted as 1
Next I see it it is counted as 2 or 3 or 4 or more..
So formula says "if counted MORE than once, then return "X", else return "1"
And the IF statement then says if higher than 1, return 0 else return 1
I have many thousands of rows and I need to identify each as either unique or not..
But I can't seem to find the right combo of DAX formulas to do so.
I have looked at COUNTDISTINCT, CALCULATE, FILTERS, COUNTROWS, and many more.
They might be the right ones to use but I haven’t managed to find the right syntax.
Many of the DAX formulas I have tried, either return ONLY "1" or ONLY "0" or they return the total amount of unique orders..
Excel formula:
Row 2 = IF(B2="";0;IF(COUNTIF(B$1:$B2;B2)>1;0;1))
Row22 = IF(B22="";0;IF(COUNTIF(B$1:$B22;B22)>1;0;1))
Row32 = IF(B32="";0;IF(COUNTIF(B$1:$B32;B32)>1;0;1))

What you are looking for is a Calculated Column and not a Measure. Also you need to determine the order in which the [Split Order] column should be read.
You can do this by adding an index column in Power Query (Add Column tab). After doing that, add this Calculated Column:
IsFirstOccurence =
IF (
COUNTROWS (
FILTER (
'table',
'table'[Split Order] = EARLIER ( 'table'[Split Order] )
&& 'table'[Index] <= EARLIER ( 'table'[Index] )
)
) = 1,
1,
0
)
The result looks like this:

Related

Power BI first IF-Statement then the DAX-Formula

I am new to Power BI and have the following issue:
I tried to build a formula for a frequency counter. I got some examples from the web and I was able to build this working formula. The basic idea behind is to categorize an item with the values: daily, weekly or first time.
I tried to add an IF-Statement to the formula, that is checking a calculated column "Time frame", which shows the duration of an item in minutes.
Basically it should run this formula only if the Column "Time frame" is equal or bigger 1.
Now the formula gives to items with a Time frame of 0, the value first time. But they should be ignored or blanked.
Calculated column =
Var freqcount =
COUNTAX(FILTER(ALL('Count'),
AND([Date]>=DATEADD('Count'[Date],-6,DAY)&&[Date]<=EARLIER([Date]),[ID]=EARLIER('Count'[ID]))),ID])
return
if(freqcount>=4,"Daily",if(freqcount>=2,"Weekly",if(freqcount>=1,"First time","Inactive")))
I would be thankful, if someone could support me with this issue.
Edit: an ID can occur multiple times in my table but with different dates. But only once with the same date. For example:
ID 1, Date 01.01.2020
ID 1, Date 02.01.2020
ID 1, Date 03.01.2020
it is easier to use calculate:
Calculated column =
var rDate = yourTable[Date]
var rID = yourTable[ID]
var freqCount = CALCULATE(yourTable('Count'), FILTER(yourTable, rDate >= DATEADD(yourTable[Date], -6 , DAY) && rID = yourTable[ID] && yourTable['Time frame'] > 0))
return if(freqcount>=4,"Daily",if(freqcount>=2,"Weekly",if(freqcount>=1,"First time","Inactive")))
you see how I simply added the Time frame to the expression. Also I removed the use of earlier by using var's so it is better readable.

DAX measure that returns the distinct number of values that have duplicates within the table

Fairly new and self-taught with DAX. I run an accuracy log that tracks incoming applications (Application[Application_ID]) and errors committed in processing that application (Error_Log[Application_ID]).
I want to find the number of applications that contain more than one error. For example, if 10 applications have errors, 6 of those applications have 1 error and the rest have 2 or more errors, I want to return the value of 4.
I'm trying to avoid a calculated column (like a "Multiple_Errors" TRUE/FALSE column) as it's refresh times are already longer than I'd like, but if it's unavoidable, it could be accommodated.
We were able to build an Excel formula with SUMPRODUCT for a very high level summary of the information, but I want more granularity than that formula can give me.
The online search has only led to finding articles on how to count the number of duplicates, flag the duplicates, remove duplicates or some other task, where I need to count a distinct number of values that have been duplicated within a table.
I have tried a few different DAX measures, but all of them have yielded incorrect results. For example...
=
CALCULATE (
DISTINCTCOUNT ( Error_Log[Appplication_ID] ),
FILTER ( Error_Log, COUNTA ( Error_Log[Appplication_ID] ) > 1 )
)
Drilling down into this result shows that all of the applications with errors are being pulled over, rather than only those with greater than one error.
After playing with a few options, I haven't been able to find the solution. Any help/pointers/direction would be greatly appreciated!
I think you are looking for something like this:
Measure =
COUNTROWS (
FILTER (
SUMMARIZE (
Error_Log,
Error_Log[Application_ID],
"count", COUNTROWS ( Error_Log )
),
[count] > 1
)
)
The SUMMARIZE function returns a virtual summarized table, with the count of each Application_ID in a column called "count". The outer COUNTROWS function then returns the number of rows in the virtual table where [count] is greater then 1.
Your measure is fine and works as defined. Please see the attached screen.
App ID 100 has 4 Type 1 errors, 101 has 2 Type 2 and 1 Type 3 errors but because of the distinct count, they have 1 each.
102 has single Type 3 but we are using Error Type to group the log, Type 3 show two counts (1 each for 102 and 101)
Note that COUNTA ( Error_Log[Appplication_ID] ) > 1 condition has been satisfied for 102 also because of grouping column.
We do not see Type 6 in the pivot table at the right because of COUNTA ( Error_Log[Appplication_ID] ) > 1.
So, although the measure works, we might miss interpreting the result or we might miss to use correct DAX for the requirement.

Excel - MAX and MIN value by month for a certain item

Can someone help me out with a formula?
I have a large database and I`m trying to figure out, how to get the MAX amount used in January 2017 for a product X.
I have found the averages using - AVERAGEIFS(Avg.de.time!E3:E80231;Avg.de.time!A3:A80231;C2;Avg.de.time!C3:C80231;">="&H7;Avg.de.time!C3:C80231;"<="&EOMONTH(H7;0))
Column A - Item no.
Column B - Supplier name
Column C - Order date
Column D - Receive date
Column E - Delivery time (D-C)
I`ve spent too many hours on trying to figure this out.
So I m asking for Help :)
Using array formulae you can rewrite your AVERAGEIFS statement using a conditional array expression as follows:
=AVERAGE(
IF(
(
(Avg.de.time!A3:A80231 = C2) *
(Avg.de.time!C3:C80231 >= H7) *
(Avg.de.time!C3:C80231 <= EOMONTH(H7,0))
) > 0,
Avg.de.time!E3:E80231
)
)
I've just formatted the code to make it easier to see where each of the criteria, criteria_ranges and value_range appear however this will obviously be one long line in your cell.
It's now very simple to swap out AVERAGE at the start with MAX, MIN or another aggregate function with the rest of the formula remaining identical.
=MAX(
IF(
(
(Avg.de.time!A3:A80231 = C2) *
(Avg.de.time!C3:C80231 >= H7) *
(Avg.de.time!C3:C80231 <= EOMONTH(H7,0))
) > 0,
Avg.de.time!E3:E80231
)
)
As this is an array formula, you will need to type it into your Excel cell and hit Ctrl-Enter to make it an array formula. You can check this worked as curly braces {} will appear around the formula.

(Excel 2013/Non-VBA) Format Data column based on value of another cell?

We have a column that is query driven, and the query partially formats the values in the column using math based off the value of a "user entry cell" on another sheet.
For the really curious, our query looks like this:
DECLARE #rotationsNum INT
SET #rotationsNum = ?
SELECT t.Piece_ID, t.Linear_Location, ((ROW_NUMBER() OVER(ORDER BY
Linear_Location) -1 )%#rotationsNum )*(360/#rotationsNum) AS Rotation FROM
(SELECT Position.Feature_Key, Piece_ID, ((Place-1)%(Places/#rotationsNum))+1 AS Linear_Location, Place, Measured_Value, Places FROM Fake.dbo.Position LEFT JOIN Fake.dbo.Features
ON Position.Feature_Key = Features.Feature_Key WHERE Position.Inspection_Key_FK = (SELECT Inspection_Key FROM Fake.dbo.Inspection WHERE Op_Key = ?)) AS t
ORDER BY Piece_ID, Linear_Location
The first parameter "#rotationsNum" is a cell that will always have a value between 1-4. IF the value is 1, the entire column will show "0"s, which we want to show as "N/A". However, it isn't as simple as "How to hide zero data.." Because if the "#rotationsNum" == 2, 3, or 4, there will still be 0 values in the column that need to be shown.
A "#rotationsNum" value of 2 will have the query write the column as such: example
So I am trying to come up with a way to format the column =IF(cell>1, do nothing, overwrite entire column to say "NA"). But I don't think it is that straight forward since the column is query driven.
My resolution was to format the column so that if the cell that drives the "#rotationsNum" parameter is below 2, then the whole column just gets "grayed out". It kind of makes it look like a redaction, and isn't as desirable as "NA", but it works for our purposes. Hopefully this solution helps someone else who stumbles upon this problem.

Is there a function like AVERAGEX but counts instead of averaging the sums?

I have a function in PowerPivot Excel that gives me the average of the summed rows, and only counts if the sums are 100%.
The reason I do this: For each order coming in on one trailer (load), some of the items were not completely fulfilled, therefore "Is On Time" is set to 0 instead of 1 for each. I then average the whole load to see the total "on time" percentage.
Formula:
On Time:=AVERAGEX(VALUES('table1'[Loads]), IF(AVERAGE[Is On Time]=1, 1, 0))
Here's an example of what the table in PowerPivot looks like, and what I'd like the output to eventually look like:
Is there a way to do the same but only count the total instead of averaging it all. I was hoping COUNTX but it doesn't work the same.
Something like :
{ = MIN( IF( $A$3:$A$11=D3, $B3:B$8 ) ) }
Or
=IF(COUNTIFS($A$3:$A$11;"="&D3;$B$3:$B$11;"=0")>0;1;0)
This KPI seems to work. (Is On Time is a KPI to give me the average of the column, which is usually 1s or 0s)
Loads OT:=COUNTX(VALUES('table1'[Load]), IF([Is On Time]=1, 1, BLANK()))
The only issue I have is it returns BLANK if no rows were counted. Is there a way to make it return a 0?

Resources