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

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?

Related

Excel IF AND statement with percentage output

I have a dataset of co-ordinates, except the co-ordinate is split into two column X and Y.
I'm trying to find the percentage of co-ordinates in a single quadrant, and thought IF/AND statements would work i.e.
IF "numbers in X Column is between -5 and 0" AND "numbers in Y is between -5 and 0" then display total as Percentage of all data
IF "numbers in X Column is between 0 and 5" AND "numbers in Y is between 0 and 5" then display total as Percentage of all data
Etc..
I think that may be the easiest way but I'm stuck on three things;
How to do it for negative numbers
How to do "is greater than a number BUT less than a number" (rather
than just greater/less than), and
How to show results as a percentage (I only know how to return a
TRUE or FALSE) i.e.:
=IF(AND(A2:A100>5,B2:B100>5),TRUE, FALSE)
If you have the newest version of Excel you can use FILTER.
For example, if you are looking for the percent of coordinates in Q1 (0<=X<=5, 0<=Y<=5), you can use something like:
=ROWS(
FILTER($A$2:$B$100,
(($A$2:$A$100>=0)*($A$2:$A$100<=5)*($B$2:$B$100>=0)*($B$2:$B$100<=5))))/
ROWS($A$2:$A$100)
If not, then a simple way is to do what you were originally trying in a helper column, and divide the results by the total rows.
To answer your questions:
For negative numbers, e.g. Q3, just use negative numbers. Something like:
=IF(AND($A$2:$A$100>=-5, $A$2:$A$100<=0, $B$2:$B$100<=0, $B$2:$B$100>=-5),1, 0)
Logically, it is not really "greater than a number BUT less than a number", but "greater than a number AND less than a number". The example in question 1 shows that in action.
To get as a percent, you add up all the values, and divide by the total. In an IF statement you can return anything (format is =IF(test_is_true, value1, value2)), so in the above example, I elect to return 1 for whenever a row meets our criteria.

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.

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

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:

Excel - averaging an n amount of rows based on condition in prior column

I have this table in excel:
Date value
1/2/1970 100.00
1/5/1970 99.99
1/6/1970 100.37
1/7/1970 100.74
1/8/1970 101.26
1/9/1970 100.74
1/12/1970 100.79
1/13/1970 101.27
1/14/1970 101.95
1/15/1970 101.97
1/16/1970 101.76
1/19/1970 102.21
1/20/1970 102.70
1/21/1970 102.00
1/22/1970 101.46
1/23/1970 101.49
1/26/1970 100.97
1/27/1970 101.45
1/28/1970 101.70
1/29/1970 102.08
1/30/1970 102.19
2/2/1970 102.02
2/3/1970 101.85
These are values that I have daily, and I need to construct a sheet that takes a monthly index of the daily values, example below:
date index
1/31/1970 some_index
2/28/1970 some_index
3/31/1970 some_index
4/30/1970 some_index
I could only get this far when it came to getting the index of 30 days:
=AVERAGE(INDEX(B:B,1+30*(ROW()-ROW($C$1))):INDEX(B:B,30*(ROW()-ROW($C$1)+1)))
I'm just not sure how to structure this in the most efficient, yet correct way possible. Not all months are the same amount of days, so I was hoping to check to get all the next n rows where the date starts with a "1" for example, sometimes certain days are also missing. I can't think of a catch all approach.
With 1/31/1970 in C1 try this,
=averageifs(daily!b:b, daily!a:a, "<="&c1, daily!a:a, ">="&eomonth(c1, -1)+1)
A PivotTable might be more convenient:

Sum up values on distinct rows filter

I need to calculate the total shipping cost for some sales orders. The dataset is as follows:
The issue I have is that although the shipping cost should be taken into account only once per order in the calculation, in the dataset it is repeated for each order item, thus a simple duplicates shipping costs:
=SUM(MyTable[Shipping]) = 90 // wrong value
However what I need is to:
filter the table to only keep 1 line for each order
sum up the shipping
Which should be something like:
=SUMX(FILTER(MyTable,<filter>),MyTable[Shipping]) = 35 // correct value
But I'm struggling to write the <filter>. I found DISTINCT which returns the list of unique order IDs, but not their corresponding row.
Does anybody have any ideas how I could write the filter to calculate shipping properly?
The X functions are non intuitive but very powerful - you are on the right lines.
I would approach this with two measures, the first to sum the shipping cost and divide it by the number of rows for that order. (Key to the second half is the ALL() which opens up the context on the column referenced whilst retaining the other contexts.)
And the second to iterate that measure by order and sum the outcomes.
[Allocated Shipping] =
SUM ( MyTable[Shipping] )
/ CALCULATE ( COUNTROWS ( MyTable ), ALL ( MyTable[Item] ) )
[Iterated Shipping] =
SUMX(VALUES(MyTable[Order]), [Allocated Shipping])
The simplest approach would be to use a Helper column. In E2 enter:
=IF(COUNTIF($A$1:A2,A2)>1,0,1)
and copy down. This will identify the unique values in column A. To sum these unique values, use:
=SUMPRODUCT(--(E2:E9=1)*(D2:D9))
For your data:
The value is 35
Naturally if the data were filtered you would use a variation of the SUBTOTAL() function or an additional helper column.
In a very similar way to Gary's recommendation, you could use (in an additional col - E2):
=IF(COUNTIF($A$2:A2,A2)>1,D2,0)
This will show the cost of the delivery in col E itself. You can then just SUM(E2:E) to see the total cost (35).

Resources