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]
)
Related
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 )
I having trouble calculating the cumulative sum of a column on PowerBI.
I have a big offer table and I want to run a pareto analysis on it. Following many tutorials, I created a SUMMARIZED table by offer and a sum of their sales. So the table definition is:
summary = SUMMARIZE(big_table; big_table[offer]; "offer sales"; sum(big_table[sales]))
Many of the forums and stackoverflow answers I found have direct me to the following formula for cumulative sum on column:
cum_sales =
CALCULATE(
sum([offer_sales]);
FILTER(
ALLSELECTED(summary);
summary[offer_sales] <= max( summary[offer_sales])
)
)
However the resulting table is not correct:
What I need is simply to have the offers ordered by sales descending and then add the current row's sales amount to the previous row's sales,
So I excepted numbers closer to:
1st row: 1.5M
2nd row: 2.1M
3rd row: 2.6M and so on
But (maybe) because of my data structure and (certainly) lack of knowledge on how PowerBI works, I'm not getting the right results...
Total Amount = SUM ( 'Fact'[Amount] )
Offer Visual Cumulative =
VAR OfferSum =
ADDCOLUMNS (
ALLSELECTED ( 'Offer'[Offer] ),
"amt", [Total Amount]
)
VAR CurrentOfferAmount = [Total Amount]
VAR OffersLessThanCurrent =
FILTER (
OfferSum,
[amt] <= CurrentOfferAmount
)
RETURN
SUMX (
OffersLessThanCurrent,
[amt]
)
There's no need to pre-aggregate to a summary table. We can handle that as in the measure above.
This assumes a single fact table named 'Fact', and a table of distinct offers, 'Offer'.
Depending on what you're doing in terms of other filters on 'Offer', you may need to instead do as below:
Offer Visual Cumulative =
VAR OfferSum =
ADDCOLUMNS (
ALLSELECTED ( 'Offer'[Offer] ),
"amt", CALCULATE ( [Total Amount], ALLEXCEPT ( 'Offer', 'Offer'[Offer] ) )
)
...
The rest of the measure would be the same.
The measure is fairly self-documenting in its VARs. The first VAR, OfferSum is a table with columns ('Offer'[Offer], [amt]). This will include all offers displayed in the current visual. CurrentOfferAmount is the amount for the offer on the current row/axis label of the visual. OffersLessThanCurrent takes OfferSum and filters it. Finally, we iterate OffersLessThanCurrent and add up the amounts.
Here's a sample:
I am trying to write a code for a data table in Power BI that averages values of a table but categorizes them based on ID and Project but at the same time exclude a value from another column. Below is what I am trying to accomplish and column AVG is the goal. Excluding Type = "II" and averaging the values based on category columns [ID] and [Project].
Below is the code I am working on but it is incorrect. What would be the best solution?
AVG =
CALCULATE (
AVERAGEX ( FILTER ( Table, Table[Type] <> "II" ), Table[Values] ),
ALLEXCEPT ( 'Table', 'Table'[ID], 'Table'[Project] )
)
How about this?
AVG =
CALCULATE (
AVERAGE ( Table[Values] ),
ALLEXCEPT ( Table, Table[ID], Table[Project] ),
Table[Type] <> "II"
)
I don't see a reason to use an iterator function (AVERAGEX) and a simple Boolean filter should work how you want (instead of using FILTER).
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.
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]
)