PowerBi/Dax Summarize table and get average rating - powerbi

I'm trying to create a new measure to find the average of the rating from my table, either the whole table or a particular 'id' as selected by a slicer. The issue is, in the original data if a user appears in more than one user group then they have a table row included for each of their groups (eg. harry appears 3 times). I want to show the average of the rating in a visual but can't work out how to calculate it without the duplicated values.
For the below table, I would like to show the average rating for id 1 in the visual as 4.5 ((5+4)/2) instead of 4.75 ((5+5+5+4)/4).
Table
id
user group
user id
rating
1
group 1
harry
5
1
group 2
harry
5
1
group 3
harry
5
2
group 1
betty
2
2
group 2
phil
3
1
group 1
ted
4

Since the specification is to remove duplicate values, but also to consider existing slicers, a possible solution is
Avg Rating =
CALCULATE(
AVERAGEX( SUMMARIZE( T, T[id], T[rating] ), T[rating] ),
ALLSELECTED( T[user id], T[user group], T[rating] ),
VALUES( T[id] )
)

Related

Power BI - Filtering model on latest version of all attributes of all dimensions through DAX

I have a model that's comprised of multiple tables containing, for every ID, multiple rows with a valid_from and valid_to dates.
This model has one table in that is linked to every other table (a table working as both a fact and a dimension).
This fact has bi-directional cross filtering with the other tables.
I also have a date dimension that is not linked to any other table.
I want to be able to calculate the sum of a column in this table in the following way:
If a date range is selected, I want to get the sum of the latest value per ID from the fact able that is before the max selected date from the date dimension.
If no date is selected, I want to get the sum of the current version of the value per ID.
This comes down to selecting the latest value per ID filtered on the dates.
Because of the nature of the model (bi-directional with the fact/dimension table), I want to have the latest version of any attribute from any dimension selected in the visual.
Here's an data example and the desired outcome:
fact/dimension table:
ID
Valid_from
Valid_to
Amount
SK_DIM1
SK_DIM2
1
01-01-2020
05-12-2021
50
1234
6787
1
05-13-2021
07-31-2021
100
1235
6787
1
08-01-2021
12-25-2021
100
1236
6787
1
12-26-2021
12-31-2021
200
1236
6787
1
01-01-2022
12-31-9999
200
1236
6788
Dimension 1:
ID
SK
Valid_from
Valid_to
Name
1
1234
10-20-2019
06-01-2021
Name 1
1
1235
06-02-2021
07-31-2021
Name 2
1
1236
08-01-2021
12-31-9999
Name 3
Dimension 2:
ID
SK
Valid_from
Valid_to
Name
1
6787
10-20-2019
12-31-2021
Name 1
1
6788
01-01-2022
12-31-9999
Name 2
My measure is supposed to do the following:
If no date is selected than the result will be a matrix like the following:
Dim 1 Name
Dim 2 Name
Amount Measure
Name 3
Name 2
200
If July 2021 is selected than the result will be a matrix like the following:
Dim 1 Name
Dim 2 Name
Amount Measure
Name 2
Name 1
100
So the idea here is that the measure would filter the fact table on the latest valid value in the selected date range, and then the bi-directional relationships will filter the dimensions to get the corresponding version to that row with the max validity (last valid row) in the selected range date.
I have tried to do the following two DAX codes but it's not working:
Solution 1: With this solution, filtering on other dimensions work and I get the last version in the selected date range for all attributes of all used dimensions. But the problem here is that the max valid from is not calculated per ID, so I only get the max valid from overall.
Amount Measure=
VAR _maxSelectedDate = MAX(Dates[Dates])
VAR _minSelectedDate = MIN(Dates[Dates])
VAR _maxValidFrom =
CALCULATE(
MAX(fact[valid_from]),
DATESBETWEEN(fact[valid_from], _minSelectedDate, _maxSelectedDate)
|| DATESBETWEEN(fact[valid_to], _minSelectedDate, _maxSelectedDate)
)
RETURN
CALCULATE(
SUM(fact[Amount]),
fact[valid_from] = _maxValidFrom
)
Solution 2: With this solution, I do get the right max valid from per ID and the resulting number is correct, but for some reason, when I use other attributes from the dimensions, it duplicates the amount for every version of that attribute. The bi-directional filtering does not work anymore with Solution 2.
Amount Measure=
VAR _maxSelectedDate = MAX(Dates[Dates])
VAR _minSelectedDate = MIN(Dates[Dates])
VAR _maxValidFromPerID =
SUMMARIZE(
FILTER(
fact,
DATESBETWEEN(fact[valid_from], _minSelectedDate, _maxSelectedDate)
|| DATESBETWEEN(fact[valid_to], _minSelectedDate, _maxSelectedDate)
),
fact[ID],
"maxValidFrom",
MAX(fact[valid_from])
)
RETURN
CALCULATE(
SUM(fact[Amount]),
TREATAS(
_maxValidFromPerID,
fact[ID],
fact[valid_from]
)
)
So if somebody can explain why the bi-directional filtering doesn't work anymore that will be great, and also, more importantly, if you have any solution to have both the latest value per ID and still keep filtering on other attributes, that would be great!
Sorry for the long post, but I thought it's best to give all the details for a complete understanding of my issue, this has been picking my brain since few days now and I'm sure I'm missing something stupid but I turned to this community for help because I cannot seem to be able to find a solution!
Thank you very much in advance for any help!
Seems to be workable with a dummy model. I didn't got the point how filter ID, so if it creates a problem let me know how you handle ID. Then I changed fact to facts as fact is a function. Also, I'm not sure about the workability of the measure at your real model. Hope you will give some feedback.
Amount Measure =
VAR ValidDate=
calculate(
max(facts[Valid_to])
,ALLEXCEPT(facts,facts[ID])
,facts[Valid_to]<=MAX(Dates[Date])
)
Return
CALCULATE(
SUM(facts[Amount])
,TREATAS({ValidDate},facts[Valid_to])
)

Is there a way to filter a table based on criteria from another table in Power BI using DAX?

So, I have two tables, Scores and Accounts.
ID
Score
1
120
2
150
3
100
ID
Account
1
Account 1
2
Account 2
3
Account 3
I also have 4 measures that calculate the quartile percentile for all of the scores in the Scores table. I was wondering if it was possible to have a measure that concatenates the accounts into one line if their score is, for example, greater than the Quartile 1 measure. For example, if quartile 1 is 110, then I want a measure that would give me "Account 1, Account 2". Is this possible?
I managed to get your result by implementing the following measure, assuming you have a relationship between the two tables.
Accounts GT Q1 =
CONCATENATEX(
FILTER(
Scores,
Scores[Scores] > [Quartile 1]
),
RELATED( Accounts[Account] ),
", "
)
Output
There may be a simpler way. Let me know if that worked.

How do I make my Power BI table dynamic to change with the filters the user selects?

I have a table of calls with a column for phone number and call id. I want to create a visual that shows how many callers called how many times. (e.g. 5 callers called once, 4 called twice etc.) The challenge I am facing is that once I calculate the count of the number of callers that called count times, the table is static and is not affected by the filters on the report (e.g. the date filter).
This is what I have done so far. Is there a better way?
CallerNumber
CallID
DateTime
1
a
2022-01-01
1
b
2022-01-01
2
c
2022-01-02
3
d
2022-01-03
4
e
2022-01-01
4
f
2022-01-05
4
g
2022-01-06
From the above original data, I created a table...
Table1 =
SUMMARIZE(
Query,
Query[CallerNumber],
"Call Count", COUNT(Query[CallId])
)
CallerNumber
Call Count
1
2
2
1
3
1
4
3
and then another table from that table which gave me...
Table2 =
SUMMARIZE (
'Table1',
'Table1'[Call Count],
"Number of Callers", COUNTROWS('Table1')
)
Call Count
Number of Callers
1
2
2
1
3
1
How would I instead show the below if someone were interested in calls on Jan1?
Call Count
Number of Callers
1
1
2
1
Thanks!
CalculatedTable is populated once at powerbi Model refresh, this is why it don't reflect any change in your filters.
A better option is to use a measure:
CountOF = CALCULATE( countrows(VALUES('Table'[CallID])))
Add additional "counter" table with number from 1 to 10 .
how manyCaller = var _virtual = ADDCOLUMNS(VALUES(detail[ids]), "CountOfCalls", [CountOF])
return
CALCULATE( countrows(FILTER(_virtual, [CountOfCalls] = SELECTEDVALUE(counter[CallCount]))))

Sum distinct values for first occurance in Power BI with Filter

In Power BI I have some duplicate entries in my data that only have 1 column that is different, this is a "details" column.
Basically, when I sum up my Value column on a Power BI card, I want it to filter IsActive = 1 and sum for each unique name, so in this case:
Total= 10 + 7
Is there any way I can filter this with a DAX formula?
Assuming your table can also have a row with the same value of another row but a different name, and a row where Details column doesn't always include "Feature 1"
Name Values Details IsActive
Item 1 10 Feature 1 1
Item 1 10 Feature 2 1
Item 2 15 Feature 1 0
Item 3 7 Feature 1 1
Item 3 7 Feature 2 1
Item 3 7 Feature 3 1
Item 4 10 Feature 1 1
Item 5 10 Feature A 1
then we should use the Name column an write something like follows
Total =
CALCULATE(
SUMX( SUMMARIZE( T, T[Name], T[Values] ), T[Values] ),
T[IsActive] = 1
)
You can create a calculated column wherein you rank the rows based on the occurrence via M-query as provided in the below link :
https://community.powerbi.com/t5/Desktop/How-to-add-Row-number-over-partition-by-Customer-DAX-or-M-query/td-p/566949
Once the calculated column is done, you can achieve your result based on the below measure :
sum(value) where IsActive=1 and calculatedColumn=1 via on Filter DAX
It doesn't appear that the first occurrence is relevant, so you can just write a measure to sum distinct values.
SUMX (
CALCULATETABLE (
VALUES ( Table1[Value] ),
Table1[IsActive] = 1
),
Table1[Value]
)

Calculate monthly value between 2 tables without an explicit relationship in Power BI model

I am trying to create a measure that calculates (a/qty)*100 for each month,
where qty commes from Delivery table (generated with an R script)
month qty
<date> <dbl>
1 2019-02-01 1
2 2019-03-01 162
3 2019-04-01 2142
4 2019-05-01 719
And a comes from a table TABLE_A that has been created within Power BI that looks like this :
Client Date a
x 2019-03-07 3
x 2019-04-14 7
y 2019-03-12 2
So far, I managed to calculate that value overall with the following measure formula :
MEASURE = CALCULATE( (Sum(TABLE_A[a])/sum(Delivery[qty]))*100)
The issue I have is that I would need this measure monthly (i.e. join the tables on month) without explicitly defining a link between the tables in the PowerBI model.
For each row in TABLE_A you need to look up the corresponding qty in Delivery, so try something along these lines:
MEASURE =
DIVIDE(
SUM( TABLE_A[a] ),
SUMX(
TABLE_A,
LOOKUPVALUE(
Delivery[qty],
Delivery[month], EOMONTH( TABLE_A[Date], -1 ) + 1
)
)
) * 100
The formula EOMONTH( TABLE_A[Date], -1 ) returns the end of the previous month relative to that date and adding 1 day to that to get the start of the current month for that date.