Power BI count the occurrence of a value in a measure - powerbi

I'm trying to count the occurrence of a value in a measure. I wonder if it is possible? This is what I have tried so far. [EUI Yearly (kbtu/ft2)] is a measure I made.
EUI Count =
CALCULATE (
COUNTROWS ( df ),
ALLSELECTED ( df ),
VALUES ( df[EUI Yearly (kbtu/ft2)] )
)
It returns an error: Column 'EUI Yearly (kbtu/ft2)' in table 'df' cannot be found or may not be used in this expression.
Any fix to this? Thanks in advance!

Try countx method, accept if help :)
CountX = COUNTX(
FILTER(Sheet1,EARLIER(Sheet1[item])= Sheet1[item]),
Sheet1[item])

Related

RANKX DAX not updating other graph

I've got the following DAX Measure that provides me with the ability to filter down to the last XX of activity from an individual.
I can only seem to add the measure to the Filter on Visual so when chosing to filter down on say the last 10 this does not update other visuals in the report.
What can I do so that I am able to view the last 10 activities, but for the other visuals to update?
Rank = RANKX
(ALLEXCEPT(Sheet1,Sheet1[Name]),
CALCULATE(MAX(Sheet1[Date])),,
DESC)
It's likely you are applying that filter to only one visual.
It's better to implement the logic in the DAX calculation.
DAX Calculation
Rank =
VAR _Calc =
RANKX (
ALLEXCEPT ( Sheet1, Sheet1[Name] ),
CALCULATE ( MAX ( Sheet1[Date] ) ),
,
DESC
)
RETURN
IF ( _Calc <= 10, _Calc )

Power BI: Sum column distinct per another particular column

I am building a report about costumer complaints.
example
Now I am trying to get the right sum for "Reklamationskosten" (cost).
The correct answer is: 113 EUR.
The formula should do (in words):
"Sum the 'Reklamationskosten' for each 'Rekl. ID' but only once for each 'Reklamationskosten Art' "
Sure there is a way to do this in DAX but I cannot find out how.
Thank you all very much in advance!
For this you can create a measure using SUMX that iterates over a summarized table:
Reklamationskosten Measure =
SUMX (
SUMMARIZE (
'Table',
'Table'[Rekl. ID],
'Table'[Reklamationskosten Art],
'Table'[Reklamationskosten]
),
[Reklamationskosten]
)
This assumes that each "corresponding" row of Reklamationskosten Art has the same value of Reklamationskosten - else the values will be duplicated. You can alter the functionality of this by introducing aggregators handling multiple values using e.g. MAX:
Reklamationskosten Measure (Agg) =
SUMX (
SUMMARIZE (
'Table',
'Table'[Rekl. ID],
'Table'[Reklamationskosten Art],
"Reklamationskosten", MAX ( 'Table'[Reklamationskosten] )
),
[Reklamationskosten]
)

DAX DateDiff with slicer

Need help with DAX Syntax in what I am trying to accomplish. Here is what I have currently tried
Number of Months = ABS(DATEDIFF(myLeas[RENTDATE],TODAY(),Month))
The Problem is, in place of "Today" i need to pass the date coming from the Slicer on the visual. How do I do this?
Replace TODAY() with:
SELECTEDVALUE ( Table[Column] )
where Table[Column] is the column you have put on the slicer.
So your measure will be:
Number of Months =
ABS (
DATEDIFF (
myLeas[RENTDATE],
SELECTEDVALUE ( Table[Column] ),
MONTH
)
)
Search for additional tips:
capture slicer value pbi
For further tips:
https://powerpivotpro.com/2018/02/using-selectedvalues-capture-power-bi-slicer-selections/
Since it is a date slicer, you probably have start and end dates. In this case SELECTEDVALUE might not work. You could try using variables in this case:
Months =
VAR MAX_DATE = MAXX(ALLSELECTED(myLeas),myLeas[RentDate])
Return ABS(DATEDIFF(Table[RentDate],MAX_DATE,MONTH))
This should give the value you are looking for. Hope this helps.

DAX measure to return alternative result for total

How to create DAX measure returning different value for total in table visual? I would like it for conditional formatting for whatever dimension split in table visuals. But since conditional formatting does not work for totals I do not want to display it for that line.
I need something like:
IF(condition_identifying_total_line, "Alternative result", [TrafficLightIcon])
Edit. This does exactly what I want but I hope for more elegant approach or any other suggestions:
IsTotal =
COUNTROWS(FactTable) =
CALCULATE (
COUNTROWS ( FactTable ),
ALLSELECTED ( FactTable)
)
This measure works for whatever dimension split of Sales figures in table visual.
There are a variety of options depending on exactly what you want to do. I suggest taking a look a the following functions for ideas:
ISFILTERED
ISCROSSFILTERED
HASONEFILTER
HASONEVALUE
FILTERS
SELECTEDVALUE
For example, if Sales broken out by a column A, here are a couple possible approaches:
Sales = IF( HASONEVALUE( T[A] ), SUM ( T[Sales] ), <Alternative Result> )
Sales = IF( ISFILTERED ( T[A] ), <Alternative Result>, SUM ( T[Sales] ) )
You can find full documentation for how to handle granularities from the SQLBI website here: https://www.daxpatterns.com/handling-different-granularities/
Hope this helps!
William
I have ended up using INSCOPE function:
IsTotal = NOT(
ISINSCOPE(products[dimension1])
|| ISINSCOPE(products[dimension2])
|| ISINSCOPE(stores[dimension1])
|| ISINSCOPE(stores[dimension2])
)
Unfortunately it requires hard coding all dimensions by which we want to slice or group visuals.

DAX: Average distinct values per day

I've got some categories spread over a number of days. The same category can occur several times on the same date. How can I get the average number of distinct categories per day?
Dataset:
Date,Category
11.10.2018,A
11.10.2018,B
11.10.2018,C
12.10.2018,A
12.10.2018,A
12.10.2018,A
13.10.2018,B
13.10.2018,B
Table from data view:
Table visualization:
My attempt:
I'm able to get distinct values per day as a measure using dist = CALCULATE(DISTINCTCOUNT(Table1[Category]);DISTINCT(Table1[Date]))
So what I'd like to end up with is the average of dist in the table above which is 1.67. But how do you do that? I've tried different combinations with AVERAGE, AVAREGEX, VALUES and CALCULATE, but with no luck. And the more I try, the more I convince myself that DAX is useless (even though I know deep down it can't be). Thank you for any suggestions!
Use SUMMARIZE to calculate the distinct count for each date, then you can use AVERAGEX to iterate over each date value:
dist:=
IF (
HASONEFILTER ( Table1[Date] ),
DISTINCTCOUNT ( Table1[Category] ),
AVERAGEX (
SUMMARIZE (
Table1,
Table1[Date],
"Daily Average", DISTINCTCOUNT ( Table1[Category] )
),
[Daily Average]
)
)
EDIT: You don't really need the IF function - it seems to perform just as well using simply:
dist:=
AVERAGEX (
SUMMARIZE (
Table1,
Table1[Date],
"Daily Average", DISTINCTCOUNT ( Table1[Category] )
),
[Daily Average]
)