Using DAX to Count or Calulate Values - powerbi

I'm trying to create a measure in Power BI and I'm having some trouble.
I have a table called 'clients' which as a column called 'status' that has multiplies values in it.
I want to create a formula that counts the number of how many times the value 'Kept' shows up in that column.
Total = COUNT('client'[status])
I'm just not sure where to go from here. I don't know if COUNT or SUM would be the correct function. I know this is a simple solution but I can't figure this out.

The CALCULATE function allows you to add simple filters using additional arguments.
CALCULATE(
COUNT('client'[status]),
'client'[status] = "Kept"
)

Related

Calculate % of two columns Power BI

I want to calculate % of two columns which are already in %.
I want to calculate formula like
Target achieved= ACTUAL/TARGET
But here is ACTUAL is already a measure/calculated metrics so I'm not able to divide these two columns.
Any help would be appreciated..
Make sure both target and actual are actually numbers and not strings. You can do it in transform data (aka Power Query) part, before data is loaded into the report.
After that you should be able to create the measure you want, e.g. something like that:
UPDATE : What happens if Actual is not a column, but a measure?
If Actual measure is based on the columns in the same table as target you shouldn't have a problem. You cannot combine measure and column in the same formula. Measure is an aggregated field and needs to be grouped by another field (if you are familiar with SQL,think of SUM and GROUP BY). Coming back to your problem, you need to create measure out of "Target" column as well (notice I have in the formula SUM('Table'[Plan]) which makes it a measure). Than you can use both of them in the formula, but of course you need to "group" them by something(e.g. date) otherwise it will just show you a total.

Parameters usage in DAX Formula

I have created a parameter which has values 1-20.
Now I want to use this in a calculated column , wherein the calculations is
if(Parameter[Parameter Value]>'GPH Activity'[Idle Time 2],1,0)
Here Idle time is a number field.
This however doesn't seem to work. What I am doing wrong?
You can't use parameters in Calculated Columns, only in Measures.
Calculated Columns are calculated when the data model is refreshed, they're not dynamically recalculated as the report is consumed.
Decide how you wish to aggregate this calculation, and write an appropriate measure instead. You may wish to use an Iterator function, such as COUNTX or SUMX.

Cant measure from date column

I'm trying to count days between a date from the column 'completionDate' and today
The table name is 'Incidents (2)'
I have a simuler table called 'incidents' here it's working.
The code:
DaysClosed = DATEDIFF('Incidents (2)'[completionDate].[Dag];TODAY();DAY)
The error i get:
'A single value for variaton 'Dag' for column 'completionDate' in table 'Incidents (2)' cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result.'
The error you get strongly depends on how you are evaluating your formula, that's why it might work on another table but not on this one. As #JBfreefolks pointed out correctly you are specifying a column where a scalar value is expected. That can work depending on the context you are evaluating your formula over (assuming it is a measure).
For instance, imagine a data-set with 100 rows equally divided into four categories A,B,C,D. When you create a table visual with a row for each category, each row will have 25 underlying records that will be used in any calculation added to this row (either a measure or an aggregate of any value). This means that when using a formula like datediff with a column reference, it will get 25 values for it's second argument where it expects one.
There are several ways to solve the problem depending on your desired result.
Use a measure like MAX like #JBfreefolks suggested to make sure that a single value is selected from multiple values. The measure will still be calculated over a group of records but will summarize it by taking the maximum date.
Make sure the visual you are using has something like an ID in there so it doesn't group, it displays row context. Any measure added to this visual will evaluate in row context as well.
Use a calculated column instead. They are always evaluated in row context first and their values can be aggregated in a visual later on. When using TODAY() , you probably need to refresh your report at least every day to keep the column up to date.
A more complicated way is to use an iterator like SUMX() or AVERAGEX() to force row context evaluation inside of a measure.
Good to see you solved it already, still posting as it might be helpful to others.
'Incidents (2)'[completionDate].[Dag] referencing a colomn. It is in your computation returning a table (multiple date in the evaluation context) instade of a scalar needed in DATEDIFF calculation.
You need to leverage to be sure that 'Incidents (2)'[completionDate].[Dag] is rturning a scalar value. To do that you can leverage rowcontext and then also formula like Max.

Create Multiple Calculated columns using one DAX formula

Is it possible to create 2 different columns using one DAX Expression?
I have 2 column, for example Work Done this month and Invoiced Amount. I want to create 2 columns using these.
Work Done - Invoiced and return only positive values (Deferred)
Invoiced - Work Done and return only positive values (Extra)
Note: I know how to add these columns using 2 DAX formula's here, but I would like to know if its possible with one formula.
Samsple Screenshot below:
I believe it is possible but not within the existing table and it strongly depends on the context on which your are calculating. When your calculation is performed on a row level, ADDCOLUMNS could help you out. It allows you to create a new table and add multiple calculated columns.
https://learn.microsoft.com/en-us/dax/addcolumns-function-dax

Can't find DAX function to correctly calculate Sum

I run a simple query in SQL:
select count(*) FROM Abattoir.RecordScan_BloodPit
JOIN Abattoir.RecordScan RS ON RS.ScanId = RecordScan_BloodPit.ScanId
WHERE rs.HarvestDate = '07/16/2018'
which gives me a correct number of rows (say 2000)
I then go to my measure editor in Power BI, and enter:
CALCULATE(COUNT(BloodPit[ScanId]))
and get an insane number of around 254000000.
I don't understand why counting on a field so simply would give me a completely Different number.
New to DAX so my Google strings might not be the best when conducting searches.
Here is what the report looks like. Please note the nubers inside the rectangle are what I'm trying to calculate. The numbers vary from day to day...