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

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..

Related

calculated column stops working when exdternal filter used

I have some Fact Revenue, I am trying to group by Conca, and display the values only if negative…
For doing it I have this calculated column:
=
VAR name1 = Revenue[Conca]
VAR name2=
CALCULATE (
SUM ( Revenue[CostOfQuality] ),
FILTER ( Revenue, Revenue[Conca] = name1 )
)
RETURN
if (name2>0, 0, Revenue[CostOfQuality])
It works:
(highest value is 0 as expected):
Now...
If I drag fiscal year it stops working:
Why is it that I see numbers higher than 0?? (I want it to still work even if I bring other filters...)
a calculated column is computed computed once on the whole dataset after the data are loaded, and is basically a "static column".
Whatever the expected result, you are looking for a measure, which gets computed in the current context
Calculated columns and calculated tables are different from measures, since they are static: You can filter them, but they don't recalculate on filter changes.

PowerBi Distinctcount not working properly with 3 measures

I am having an issue using the Distinctcount function in DAX. I have a table with a total of 1,154,493 rows. I have a measure created to count the number of distinct values in column 1. I have another measure created to count the number of distinct values in column 1 with filters. I have a 3rd and final measure created to count the number of distinct values of column 1 with different filters. The issue I am running into is the count of measure 2 + measure 3 should equal measure 1 however added together they are GREATER than the value of measure 1 which is just a grand total. How is this possible? Unfortunately I can't share the table but below is the code I am using for the two measures:
Measure1=distinctcount('Table1'[Column1])
Measure2=calculate(distinctcount('Table1'[Column1]),'Table1'[CTest] = 1,'Table1'[CTest2] = "07")
Measure3=calculate(distinctcount('Table1'[Column1]),'Table1'[CTest] = 2,'Table1'[CTest2] = "07")
I am at a loss. Thank you in advance!!
For troubleshooting you should identify values of Column1 with Measure2 and Measure3 > 0, those are being added twice.

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

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.

DAX query for counting values based on another cumulative DAX measure

I have this table:
Id Length(m) Defect Site Date
1 10 1 y 10/1/19
2 60 0 x 09/1/19
3 30 1 y 08/1/19
4 80 1 x 07/1/19
5 20 1 x 06/1/19
I want to count the amount of defects and ids that are in the last 100m of length(sorted by date DESC), whilst maintaining the ability for this to change with additional filters. For example, what are the amount of defects for site x in the last 100m, or what are the amount of defects in the last 100m that have an ID bigger than 1.
For the question 'What are the amount of defects for site x in the last 100m', I would like the result to be 2, as the table should look like this:
Id Length(m) Length Cum. Defect Site Date
4 80 80 1 x 07/1/19
5 20 100 1 x 06/1/19
I believe the issue in creating this query so far has been that I need to create a cumulative DAX query first and then base the counting query off of that DAX query.
Also important to note that the filtering will be undertaken in PowerBI. I don't want to hardcode filters in the DAX query.
Any help is welcome.
Allwright!
I have taken a crack at this. I did assume that the id of the items(?) increments through time, so the oldest item has the lowest id.
You were correct that we need to filter the table based on the cumulative sum of the meters. So I first add a virtual column to the table (CumulativeMeters) which I can then use to filter the table on. I need to break the filter context of the ADDCOLUMNS function to sum up the hours of multiple rows.
Important is to use ALLSELECTED to keep any external filters in place. After this it is pretty straightforward to filter the table on a maximum CumulativeMeters of <= 100 meters and where the row is a defect. Counting the rows in the resulting table gives you the result you are looking for:
# Defects last 100m =
CALCULATE (
COUNTROWS ( Items ),
FILTER (
ADDCOLUMNS (
Items,
"CumulativeMeters", CALCULATE (
SUM ( Items[Length(m)] ),
FILTER (
ALLSELECTED( Items ),
Items[Date] <= EARLIER ( Items[Date] )
&& Items[Id] <= EARLIER ( Items[Id] )
)
)
),
[CumulativeMeters] <= 100
&& Items[Defect] = 1
)
)
Hope that helps,
Jan

How to divide each row of a calculated column by the total of another calculated column?

I can't get a division correct with this sample data:
Calculated column Another calc. column
48 207
257 370
518 138
489 354
837 478
1,005 648
1,021 2,060
1,463 2,164
2,630 1,818
2,993 2,358
3,354 3,633
4,332 5,234
4,885 6,108
4,514 6,008
4,356 6,888
4,824 7,382
7,082 5,988
7,498 6,059
4,865
4,192
3,816
2,851
2,768
2,093
2,207
770
397
149
178
336
167
124
18
What I'm trying to do is to create a new calculated column.
For each row I want to get the value of Calculated column and divide it by the Total of Another calc. column.
The Total of Another calc. column = 82826
This is the desired output in a brand new calculated column, let's call it % Column:
% Column
0,000579528167484
0,003102890396735
0,006254074807428
.
.
.
NOTE - these 3 columns: Calculated column, Another calc. column and % Column are all in the same table and are all calculated columns.
I tried lots of formulas but not a single one returned the desired output. :| I guess it's because of the nature of calculated columns or I'm not getting the gist of it.
Is this even possible or I should follow another path using a Measure?
Can you shed some light?
####### EDIT #######
I put together a sample file to help debugging this. Here it is:
https://drive.google.com/open?id=1r7kiIkwgHnI5GUssJ6KlXBAoeDRISEuC
As you see:
Earned Daily % HARDCODED works just fine because 82826 is hardcoded as the denominator.
Earned Daily % by StelioK and Earned Daily % by Alexis Olson output the same wrong value for the division when using SUM formula.
I'm using the latest Power BI Desktop version if that matters: Version: 2.70.5494.701 64-bit (June 2019)
Basically, there is nothing wrong with the calculated columns, and both Alexis and StelioK formulas are correct.
The root problem here is a confusion between calculated columns and measures. You are looking at the results in a conceptually wrong way - through the matrix visual, with several filters active on slicers. If you remove the filters, you will see that the total amount is 140,920, not 82,826. The latter number is the total for the filtered data set, not the entire table.
To get this right, you need to understand several fundamental concepts behind Power BI:
Calculated columns are always static. Once a calculation is
completed, it can not respond to slicers or other UI controls. It's
just static data, identical to data in non-calculated columns. DAX
formulas used to calculate columns are active only when you create
them, or upon data reload.
If you want your calculations to respond to slicers etc, they must be measures. It's the only way, no exceptions.
Avoid calculated columns, they are utterly useless. Power BI is all about measures; I can't think of a single reason for using calculated columns. When you add a column, you are essentially enhancing your source data, because you feel like you are missing something you need for your report. But that need can be much better addressed at the source (database or file you import), or using Power Query, which is designed exactly for this kind of tasks. The best practice is: build your columns at the source, for everything else design measures.
Another important advice: never drop fields (columns) into visuals directly. Always write a DAX measure, and then use it. Relying on Power BI auto-aggregations is a very bad practice.
You can do this by using the following DAX:
% Column =
VAR TotalSum =
SUM ( 'Table'[Another Calc column] )
RETURN
IF (
NOT ( ISBLANK ( 'Table'[Calc Column] ) ),
CALCULATE ( DIVIDE ( SUM ( 'Table'[Calc Column] ), TotalSum ) ),
0
)
Which yields the following:
I Hope it helps!!
For me the following works:
DIVIDE( Table1[Calculated column], SUM(Table1[Another calc column]) )
If that's not working, I'd need to see a file where you can reproduce the problem.
Edit: After looking at your file, the total of 82,826 is only true with the filters you've selected.
Note that calculated columns are not dynamic and cannot be responsive to filters since they are calculated only when the table is first loaded.
If you need it to be dynamic, then write it as a measure more like this:
Earned Daily =
DIVIDE (
CALCULATE (
SUM ( 'Test data'[Value] ),
'Test data'[Act Rem] = "Actual Units",
'Test data'[Type] = "Current"
),
CALCULATE (
SUM ( 'Test data'[Value] ),
ALLSELECTED ( 'Test data' ),
'Test data'[Act Rem] = "Remaining Units",
'Test data'[Type] = "PMB"
)
)