How to eliminate "BLANK" when the criteria does not meet using DAX - powerbi

How to eliminate "BLANK" when the criteria does not meet? I'm trying to get count of rows with value greater than 0 else return nothing or 0 using DAX measure. I have negative values also in my column so just trying to count only above 0. Any suggestions? With below I can achieve count but if there are no rows meeting the criteria it returns "BLANK"
Measure=CALCULATE(COUNT('TableX'[column]),FILTER('TableX','TableX'[column]>0))

Try adding a zero:
Measure =
CALCULATE (
COUNT ( 'TableX'[column] ),
FILTER ( 'TableX', 'TableX'[column] > 0 )
) + 0
You can also wrap the measure with the FORMAT function to output as text, which turns a blank into an empty string "".
Measure =
FORMAT (
CALCULATE (
COUNT ( 'TableX'[column] ),
FILTER ( 'TableX', 'TableX'[column] > 0 )
),
"0"
)
Use whatever custom format you'd prefer to display the result as.

Related

Percentage value of a segment against segment total Power BI(DAX)

Hi guys, I am new to Power BI(DAX formulas) and I am attempting to calculate the percentage contribution of the sum of "count" where "category" = X and "item_no"=1 to the total of "count" across all categories where 'item_no' = 1.
The ideal mathematical statement here will be the (30/50)*100%
I intend to represent the percentage values in a chart showing percentage contribution of each distinct item_no to its total in the format as represented in the example above.
The standard way to approach this is
calculation over partial set / same calculation over larger set
Since you haven't made it clear what context you are trying to calculate this, I will assume it's a visual along these lines:
The measure I use here is
%ItemTotal =
DIVIDE (
SUM ( Table1[count] ),
CALCULATE ( SUM ( Table1[count] ), ALLEXCEPT( Table1, Table1[item_no] ) )
)
In the numerator, you have the sum in the local filter context. For example, in that top-left cell, this means all rows that match item_no = 1 and category = "X".
In the denominator, we do the same thing except we remove all filter context except the context we say to keep (item_no) so it includes all category values.
If you're trying to calculate that 60% outside of the context of a visual, then you can explicitly define what filters you want. For example, this should work in any filter context:
X%_Item1 =
DIVIDE (
CALCULATE (
SUM ( Table1[count] ),
ALL ( Table1 ),
Table1[category] = "X",
Table1[item_no] = 1
),
CALCULATE (
SUM ( Table1[count] ),
ALL ( Table1 ),
Table1[item_no] = 1
)
)
See here and here for other ways to modify the filter context instead of ALLEXCEPT.

How to show rows when column match criteria?

How can I define a measure to set a condition to show values of apples when column = fruits contains the string apple?
I've tried this but I only see query batch complete with errors.
MEASURE Table1[apples] = IF(SEARCH("apple",'Table1'[Fruits]),"Yes","No")
Assuming you want to return a sum of the column 'Table1'[Value] for those rows where the 'Table1'[Fruits]-column contains the string "apple", you should use:
MEASURE Table1[apples] =
CALCULATE (
SUM ( 'Table1'[Value] ),
KEEPFILTERS(
SEARCH ( "apple", 'Table1'[Fruits], 1, 0 ) <> 0
)
)

DAX PowerBI: Replaced blank values with zero and issue with chart

I tried to create report in Power BI with sales month by month for last 20 months, when sales is blank I want to see month with 0 value.
I decided to change Blank() values with zero adding a 0 at the end of calculation.
It works great, however I have an issue with limitaton date hierarchy, because now my chart contains a lot of months without value (first value begins in 2017, date hierarchy first value begins in 2000).
Test:=
CALCULATE (
SUM( quantity ),
flag = 1,
title = "WEEKS"
) + 0
Instead of a plain 0, you could add an IF to specify to only add that after the first value. Something like this:
Test:=
VAR FirstDate = CALCULATE ( MIN ( date ), ALL( Dates ), quantity > 0 )
RETURN
CALCULATE (
SUM( quantity ),
flag = 1,
title = "WEEKS"
) + IF( date > FirstDate, 0 )
If the condition is false, the IF returns a blank and it shouldn't show up.

Calculation Error: A table of multiple values was supplied where a single value was expected

I want to show this year store count based on to date and from date.The following code is throwing this error when i am placing this column in table.
ThisYearStoreCount =
IF (
[DateDiff] > 365,
0,
IF (
DATESBETWEEN ( WUSA_CAL_DIM[End_Date], [From_Date], [To_Date] ),
DISTINCTCOUNTNOBLANK ( WUSA_STORE_DIM[Store Code] )
)
)
#RADO is spot on,
If I understand your logic, something like this should work.
ThisYearStoreCount =
IF (
[DateDiff] > 365,
0,
IF (
[From_Date] >= WUSA_CAL_DIM[End_Date]
&& [To_Date] <= WUSA_CAL_DIM[End_Date]
&& WUSA_STORE_DIM[Store Code] <> BLANK(),
1
)
)
That error usually means that you're using a function that returns a set of data, in a column context ( column contexts only allow a single value to exist per row ).
I'm reading between the lines a little bit, but the new column formula above will check if the end date is between the From_Date and To_Date before allowing the number 1 through.
Then you can just add the ThisYearStoreCount to any visual and it will sum wherever appropriate.
Your existing code should work as a measure ( instead of a column ) I think but it's impossible to tell without more information.

Count if <= row by row calculation

I am trying to count the number of results that are <= 25%.
See bellow for example data
I am trying to create a measure that counts if "Pallet Utilization" is <= 25%.
"Pallet Utilization" is not a column within the data, it would need to be calculated within the measure.
From my understanding, i need to ask the measure to calculate Row by Row?
Bellow is my attempt at doing this however it is returning a count of all rows
AC_Less25 =
CALCULATE (
COUNTAX (
Chilterns_STORAGE,
DIVIDE (
DIVIDE ( Chilterns_STORAGE[NO_CASES], Chilterns_STORAGE[NO_PALLETS] ),
Chilterns_STORAGE[POU_MAX]
) <= 0.25
)
)
Not very experienced with DAX so any help appreciated.
Thanks
So you could use COUNTX, but you can also just use COUNT and add a calculated column.
Add a new Calculated Column:
Pallet Utilization =
DIVIDE (
DIVIDE ( Chilterns_STORAGE[NO_CASES], Cilterns_STORAGE[NO_PALLETS] ),
Chilterns_STORAGE[POU_MAX],
BLANK ()
)
And then add the new measure:
AC_Less25 =
CALCULATE (
COUNT ( Chilterns_STORAGE[Pallet Utilization] ),
FILTER ( Chilterns_STORAGE, Chilterns_STORAGE[Pallet Utilization] <= .25 )
)
EDIT:
If you're dead-set on using COUNTX, something like this would help. In a COUNTX ( or any 'X' measure for that matter ), you define the table you want to iterate over, then provide what it is counting/summing/averaging as the second parameter. So conditions are placed on the table like so:
AC_Less25 = COUNTX(
FILTER(Chilterns_STORAGE,
DIVIDE(
DIVIDE ( Chilterns_STORAGE[NO_CASES], Chilterns_STORAGE[NO_PALLETS] ),
Chilterns_STORAGE[POU_MAX]) <= .25),
Chilterns_STORAGE[NO_PALLETS])
Please note that I'm not sure my Pallet Utilization is correct because I'm not getting the same numbers as you are in your OP... But the screenshot speaks for itself and the CountX above will still do what you want it to do, provided you tweak the conditions in the first parameter of the CountX: DIVIDE(DIVIDE ( Chilterns_STORAGE[NO_CASES], Chilterns_STORAGE[NO_PALLETS] ), Chilterns_STORAGE[POU_MAX]) <= .25))