How to count different elements inside a column in power BI? - powerbi

I have to implement one misure which is to be used to count the number of "1" inside a column. In this column are contained 1, 0 and NULL. Which DAX formula do you suggest me?

Count 1's =
CALCULATE( COUNT( 'Table'[Column1] ),
'Table'[Column1] = 1 )
Use calculate to specify what you want (count) and then give it parameters, which in this case is where the column values = 1.

Related

In Power BI dax, please help me out to count the total occurrences on consecutive number in "n" times

I would like to count in Power BI the total counts when a value comes out in 3 consecutive order. Highly appreciated the support.
If I may denote your columns as "tme" and "val" and the table as "tbl": a two-step approach with a calculated column would look as follows:
First, we evaluate the time-difference as a calculated column in every row to the next-largest zero with time before the time of the respective row (see "EARLIER"). We further take the modulus with respect to 3, so that we expose all entries, which are 3, 6, 9 elements greater than the next-smaller zero.
__DiffToLargestZeroBeforeMod3 = MOD(tbl[tme] - CALCULATE(
MAX(tbl[tme]),
FILTER(tbl, tbl[val] = 0),
tbl[tme] < EARLIER(tbl[tme])
), 3)
Next, we define a measure, which counts all '1' entries that have 3-modulo 0
_CntTriplets = CALCULATE(
COUNT(tbl[val]),
tbl[__DiffToLargestZeroBeforeMod3] = 0,
tbl[val] = 1
)
Hope this does the job for you! Note, that this also works if the column were to start with three 1's, as the result from the MAX operator would be None and, e.g. 3-None = 3 for DAX. Also note, that the calculated column is probably not the most compute-efficient approach ;)

Power BI Calculate sum of column 2 when column 1 = " ? "

I want to create two measures that sum up the values of column 2 when column 2 equals column 1's parameters.
For example I have a column named Current/Reset and another column named Linear Ft. I want to sum up the values of Linear Ft where column 1 = "Current" and then again for "Reset."
I continue to get errors when I try and write the formula. Any help is much appreciated!
First of all you don't need explicit measures for this. It's the default behaviour of Power BI to sum up numbers (aka. implicit measure), and if you filter Linear Ft. by the (unique) values of Current/Reset you get your desired result.
However, if you still want to have separate measures for this, use CALCULATE()
Current Sum =
CALCULATE(
SUM('Table'[Linear Ft.]),
'Table'[Current/Reset] = "Current"
)
Reset Sum =
CALCULATE(
SUM('Table'[Linear Ft.]),
'Table'[Current/Reset] = "Reset"
)

COUNTIF in Power BI (New calculated column Required)

I want to calculate the running count of each value based on column A. In Excel, I am applying the following formula
=COUNTIF($A$2:$A2,A2)
I would like to get the same result in Power BI. Can you please advise.
With just a single column, this is impossible in DAX because duplicated rows cannot be distinguished as there is no inherent order to a column.
However, if you have an index column on the table (you can easily add one in the Query Editor), then it is possible to define such a calculated column so that it works similarly to the Excel formula.
CountIf =
VAR CurrentIndex = DATA[Index]
RETURN
CALCULATE (
COUNTROWS ( DATA ),
ALLEXCEPT ( DATA, DATA[ITEM] ),
DATA[Index] <= CurrentIndex
)

How to replace no data with 0 using measure in Power Bi

I have a matrix that I am using in Power BI visualization.
Percentage Status
High Low Medium
10% 1
20% 1
30% 1
"1" is the count of values in a column. I want to display 0 where there is no data in the above matrix.
I have tried dong below:
Adding +0 at the end of measure.
= IF(CALCULATE(COUNT(Table[col])=ISBLANK()),0,COUNT(Table[col]))
But, nothing seems to work as it is not considering no data as blank.
I don't think you can do this with just a measure. There are no rows of data in the table that correspond to those empty cells, so it doesn't even try to evaluate a measure.
What you can do is create a calculated table and use that in your Columns field.
Status = VALUES(Table[Status])
Create the relationship from that table to your original table on the Status column and replace Table[Status] with Status[Status] in your matrix visual. Now adding +0 should do the trick.
Measure = COUNT ( Table[col] ) + 0
You are checking the True False condition with ISBLANK(), use directly value as Blank() to compare the cell value,
=
IF (
CALCULATE (
COUNT ( Table[col] )
= BLANK ()
),
0,
COUNT ( Table[col] )
)
Hope it helps!!
Please check it here..

How to get the first value from a sorted table into a measure?

I have a DAX table with 2 columns RANK and VALUE. I need a measure that returns the VALUE for the row where RANK is the lowest.
Solution needs to be efficient as table has tens of millions of rows with distinct values in column VALUE.
I have tried using TOPN, but this returns a table, not a single value.
I have tried using MIN, but this would return the lowest VALUE, not the VALUE with the lowest RANK
I cannot use LOOKUPVALUE because in my real world example, the table has many more columns than those 2, and some of those columns would have filters applied to them by the client application, and obviously I don't know those filters at design time.
VALUE RANK MANY_MORE_COLUMNS ...
100 1 ...
200 2 ...
50 3 ...
In this case, I would need the value 100 to be returned, because lowest RANK is 1.
Try this:
Measure =
CALCULATE (
MAX ( Table[VALUE] ),
FILTER ( Table, Table[RANK] = MIN ( Table[RANK] ) )
)
The FILTER function filters 'Table' to the row with the lowest RANK. MAX(Table[VALUE]) then returns the VALUE in that row. Allthough there is only one row, you need to use a function here that returns a scalar value (MAX in this case). The CALCULATE function expects a scalar value as an argument here.