Sum of rows that satisfy a calculated measure in Power BI - powerbi

All.
I have a set of data that includes the following:
Store Name
Survey ID
Number of surveys per store
Score
Each survey has a score, and then the overall score for that store is calculate as an average.
The score is a calculated measure based off 3 other calculated measures. These are created as below:
Overall Score 1 = CALCULATE(CALCULATE(COUNT('Table'[Score 1]), 'All Detail'[Score 1] = 5) / (COUNT('Table'[Score 1]))) * 100
Overall Score 2 = CALCULATE(CALCULATE(COUNT('Table'[Score 2]), 'All Detail'[Score 2] = 5) / (COUNT('Table'[Score 2]))) * 100
Overall Score 3 = CALCULATE(CALCULATE(COUNT('Table'[Score 3]), 'All Detail'[Score 3] = 5) / (COUNT('Table'[Score 3]))) * 100
Score = ([Overall Score 1] + [Overall Score 2] + [Overall Score 3])/3
I am wanting to count how many stores have beat a target score.
Currently I have a calculated measure which indicates that the store has beat the target:
Achieved = IF([Score] >= 60, 1, 0)
This works well on a cube at Store level, telling me that the average score for the store is over 60 or not, as below:
However, I am struggling to get the total of stores that have achieved target. In the example above the total would be 3 stores.
I have tried to create a filter measure as below:
Total Achieved = COUNTROWS(FILTER('Table', [Achieved] = 1))
However, this brings back the number of surveys per store that have beat the target. Examples below:
As you can see the Total Achieved calculated measure works well at a survey level but this is not the desired output.
Example of what I am looking for as the output below:
Any advice would be appreciated. I have tried to use calculated columns but with no luck.

Hi this is the expected behavior of table visual first step is to create a summarize table as below. Here I have only group by from ‘Table'[Store] use your own grouping conditions.
s
sumtable =
ADDCOLUMNS(
SUMMARIZE(
‘Table’,
‘Table'[Store],
),
”Surveys”,SUM(Table[Surveys]),
"Score" ,[Score]
)
After that for the above summarize table create the measure as below
Achieved = IF(sumtable[Score] >= 60, 1, 0)
This will ensure your results.

Related

How to apply nested group by in DAX power bi

I have a survey data in a DWH and I want to calculate satisfaction rate,
I was able to do so with SQL server query, but due to lack of knowledge in DAX I couldn't achieve it as a measure in power BI using DAX.
The data warehouse contains 3 tables plus the DIM_DAT, bellow is a sample of the data:
Data model:
The formula to calculate the satisfaction:
(Count of RESPONDENT where ANSWER_VALUE >= 4 for each question (group by QUESTION_DESC).
divided by the number of answers for each question)
grouped by QUESTION_PRIMARY_STAGE and multiply by the weight (the weight is different for each QUESTION_PRIMARY_STAGE)
I was able to achieve the desired result by SQL server query, but because it's only the final result (satisfaction I can't do further analytics on it for example : satisfaction by department / Question / Stage).
.
My SQL server query:
SELECT SURVEY_NAME, SUM(FINALPERC) as SATISFACTION -- sum the satisfaction of the Stages
FROM
(
SELECT SURVEY_NAME, QUESTION_PRIMARY_STAGE,
(AVG("4-5 %") * WEIGHT)/100 AS FINALPERC -- multiply by the weight of each QUESTION_PRIMARY_STAGE and divid it by 100 (the sum of the weight for the stages are 100)
FROM (
select tmp0.SURVEY_NAME, tmp0.QUESTION_DESC, tmp0.QUESTION_PRIMARY_STAGE, tmp0.WEIGHT ,
("4-5"*1.0 / tmp1.COUNTR) as "4-5 %" -- divide by the number of answers for each question
from(
select da.SURVEY_NAME, QUESTION_DESC, da.QUESTION_PRIMARY_STAGE, da.WEIGHT ,
count(fr.RESPONDENT_SRGT) as "4-5" -- Count of RESPONDENT where ANSWER_VALUE >= 4 for each question (group by QUESTION_DESC)
from [dbo].[DWH_QLT_FACT_SURVEY_RESPONSES] fr
join [dbo].[DWH_QLT_DIM_QUESTION_ANSWER] da
on fr.QUESTION_ANSWER_SRGT = da.QUESTION_ANSWER_SRGT
and da.ANSWER_VALUE >= 4
group by da.SURVEY_NAME, QUESTION_DESC, QUESTION_PRIMARY_STAGE, WEIGHT
) tmp0
join
(
--------------------------------Get the number of answers for each question----------------------
select da.SURVEY_NAME, QUESTION_DESC, COUNT(RESPONDENT_SRGT) COUNTR
from [dbo].[DWH_QLT_FACT_SURVEY_RESPONSES] fr
join [dbo].[DWH_QLT_DIM_QUESTION_ANSWER] da
on fr.QUESTION_ANSWER_SRGT = da.QUESTION_ANSWER_SRGT
group by da.SURVEY_NAME, QUESTION_DESC
)tmp1
on tmp0.QUESTION_DESC = tmp1.QUESTION_DESC
-------------------------------------------------------------------------------------------------
)TMP2
GROUP BY SURVEY_NAME, QUESTION_PRIMARY_STAGE,WEIGHT
)TMP2
GROUP BY SURVEY_NAME
Problem:
I want to convert this formula in DAX.
Original masked data are in the shared excel sheet bellow:
https://1drv.ms/x/s!Aq0hOy_0CV0IhD04ClHKc9F-DP2H?e=s5dhjG

DAX - DISTINCTCOUNT based on measure

I have the following dataset:
and the following measures:
Count of Filters = if(ISFILTERED(Table1[Product]), COUNTROWS(FILTERS (Table1[Product])), 0)
Count = if(ISFILTERED(Table1[Product]), COUNT(Table1[Unique_ID]),0)
What I would like to to is, when I filter the table at Product, that a measure disctinct counts the unique_ID that have a count equivalent to the count of filters.
The following should be able to explain better:
When filtering to product 1, A list of all appear, because they all have product 1:
But when also selecting Product 2, A and B have a total count of 2 because they have entries for both product 1 and 2.
Now, what I ultimately want, is a measure that can do a distinctcount on only the Unique_ID that have the Count equal the count of filters. In the above scenario, I would want the total to give me 2 (as only UNIQUE_ID A and B have both products 1 and 2).
I have put together the following:
Total = CALCULATE(DISTINCTCOUNT(Table1[Unique_ID]),FILTER(Table1, Table1[Count]=Table1[Count of Filters]))
but this doesn't seem to work. I understand why it's not working, but I can't seem to figure out how to put together a measure to calculate this for me.
Many thanks in advance!

How can I divide a value by a different row and keep the others values in the same way?

In this table I want to calculate the percentage in a different way than the rest:
For example if the general calculate is always for Men:
2.MenActPer =
CALCULATE(SUM(TablaUnica[Men]),
FILTER(ALL('Date'[Date]), 'Date'[Date] >= DATE(YEAR(MAX('Date'[Date])), MONTH(MAX('Date'[Date])) - 6, DAY(MAX('Date'[Date])))))
/
CALCULATE(SUM(TablaUnica[Total]),
FILTER(ALLSELECTED(TablaUnica), TablaUnica[Item]= SELECTEDVALUE(TablaUnica[Item])),
FILTER(ALL('Date'[Date]), 'Date'[Date] >= DATE(YEAR(MAX('Date'[Date])), MONTH(MAX('Date'[Date])) - 6, DAY(MAX('Date'[Date])))))
But if in one Item I don't want to divide by the total row and I want to divide by the column Men Total, for example in Retirements I don`t do (65/205)*100 but I want to do (65/80.294)*100, how can I keep the other percentages in the same way (divided by every total row)?
Do I have to create a new measure?

PowerBI: Get values from a column based on a measure filter

I have a PowerBI dashboard that contains casino guest information, including their latitude and longitude coordinates. The dashboard allows a user to input a zip code and select a radius around that zip (slicers). When doing so, it filters the table of casino guests to only those with a distance (measure) less than or equal to the radius chosen. I used this distance measure and logic to create another measure "InRadius" that would show 1 or 0 depending on if the given guest is within the radius. then used this "InRadius" measure as a filter for my table, as well as other visuals. everything works fine, except I am wanting to add a "between" slicer that will filter guest's ADW Range and I am unable to filter this slicer by my measure "InRadius", therefore my min and max values for ADW in the slicer, do not accurately reflect the ADW ranges for guests filtered in my table. Is there any way for me to get around this? Is it possible to create a new measure that I could use specifically for this slicer? Is it possible to filter a slicer by only the values present in an already filtered table? In general, I am just wanting to create a slicer that filters a columns values based on a measure's value, but can't figure out how to go about accomplishing it.
some codes I have tried while creating a new measure but haven't given desired result:
InRadiusADWFilter = IF([InRadiusPatron]=1, VALUES(TF_MapRadius[ADWNet]))
InRadiusADWFilter = FILTER(VALUES(TF_MapRadius[ADWNet]), [InRadiusPatron]=1)
InRadiusADWFilter = CALCULATE(VALUES(TF_MapRadius[ADWNet]), [InRadiusPatron]=1)
Here is my code for my measure distance:
// Haversine/great-circle distance calculation adapted from Stack Overflow: https://stackoverflow.com/questions/27928/calculate-distance-between-two-latitude-longitude-points-haversine-formula
VAR __latSelected = SELECTEDVALUE('TF_MapRadius'[maplat])
VAR __lonSelected = SELECTEDVALUE('TF_MapRadius'[maplong])
VAR __radius = 3956
VAR __multiplier = PI()/180
VAR __latDiff = (MIN('Selector'[Latitude])-__latSelected) * __multiplier
VAR __lonDiff = (MIN('Selector'[Longitude])-__lonSelected) * __multiplier
VAR __formula1 =
SIN(__latDiff/2) * SIN(__latDiff/2) +
COS(MIN('Selector'[Latitude]) * __multiplier) * COS(__latSelected * __multiplier) *
SIN(__lonDiff/2) * SIN(__lonDiff/2)
VAR __formula2 = 2 * ATAN(DIVIDE(SQRT(__formula1),SQRT(1-__formula1)))
VAR __distance = __radius * __formula2
RETURN __distance
here is my code for the measure "InRadius":
InRadiusPatron = if('TF_MapRadius'[Distance2] < Radius[Radius Value],1,0)
I've created dummy table that represents my filtered table:
ID
PatronName
ADWNet
Distance
InRadius
1
Smith, John
325,555
5.2
1
2
Jobs, Steve
200
7.0
1
3
Jeter, Derek
90,000
10.1
1
4
Jordan, Michael
-2,567
14.5
1
If you have measure that returning 1/0 (for example InRadius), then you can filter your visualization (select it and go to filter pane).

Visualization Issues using Running Total on an Appended Query

Using PowerBI linked to two separate Access Databases.
I have two datasets containing cost estimates. The cost estimates in Dataset 1 run through 2054; the cost estimates in Dataset 2 run through 2074. I used the Append function to join the two tables together and used the Quick Measure for Running Total to create values for cumulative cost by year. I charted this measure and noticed a significant decrease between 2054 and 2055 and was able to determine that the decrease is the cumulative value for Dataset 1. Does anybody know any ways to fix this?
Roughly explained:
Dataset 1 through 2054 totals to 4.5M.
Dataset 2 through 2054 totals to 3M
Dataset 2 through 2055 totals to 3.25M
Appended Dataset through 2054 totals to 7.5M
Appended Dataset through 2055 totals 3.25M instead of the expected 7.75M
I think the issue might be caused by Dataset 1 not having a value for 2055 or after, but I'm not sure how to resolve this issue.
The measure I'm using is:
Cumulative Cost =
CALCULATE(
SUM('AppendedQuery'[Value]),
FILTER(
ALLSELECTED('AppendedQuery'[Year]),
ISONORAFTER('AppendedQuery'[Year], MAX('AppendedQuery'[Year]), DESC)
)
)
ETA: Picture to explain
Here is your Dataset 1-
Here is your Dataset 2-
Here is your final Dataset after appending Dataset 1 & 2
And finally, here is the output when you are adding column Year and Cumulative Cost to a table visual. As standard PBI behavior, this is just grouping data using column Year and and applying SUM to the column Cumulative Cost.
The calculations are simple-
2051 > 1 + 1 = 2
2052 > 2 + 2 = 4
2053 > 3 + 3 = 6
2054 > 4 + 4 = 8
2055 > 5 = 5
2056 > 6 = 6
=========================
Solution for your case:
I already said in the comments that the solution current data will be not a standard one and will consider fixed $1 per year per department. But if you are happy with this static consideration, you can apply these following steps to achieve your required output-
Step-1 Create a Custom Column as below (Adjust the table name as per yours)-
this_year_spent = IF('Dataset 3'[Cumulative Cost] = BLANK(),0,1)
Step-2 Create the following Measure-
cumulative =
VAR current_year = MIN('Dataset 3'[Year])
RETURN
CALCULATE(
SUM('Dataset 3'[this_year_spent]),
FILTER(
ALL('Dataset 3'),
'Dataset 3'[Year] <= current_year
)
)
Here is the final output-