Creating a histogram over a Power BI measure - powerbi

Since Microsoft decided to deprecate the Histogram visualization in Power BI it became a problem to visualize distributions in this application.
I solved this problem in the case where the histogram is over a table column: using a clustered column chart with the value as the X axis and the count as the Y axis (It is also possible to add range bins to group together different values).
However, if I have a measure, this approach will not work. For example, If I have a table of movie ratings (with columns 'movieId', 'userId', and 'userMovieRating') I don't know how to create a histogram of the average ratings for each movie. If I try the approach that was described above, Power BI will not even let me use the measure in the X axis.
sample data:
movieId
userId
rating
m1
u1
5
m1
u2
3
m2
u1
2
m2
u3
4
m2
u4
3
m3
u2
3
m3
u3
3
m3
u5
3
expected output:
Average rating
number of Movies
4
1
3
2
Can you suggest a way to achieve this without creating additional tables?

movies table looks like this as per your sample data.
Create a measure as follows:
Ave Rating = AVERAGE(movies[rating])
Create a new disconnected table as follows:
Average Ratings =
VAR maxRating = MAXX(VALUES(movies[movieId]), [Ave Rating])
RETURN GENERATESERIES(0,maxRating,1)
which produces the following:
You need to use generate series for a proper histogram as some averages (0,1,2) will have a count of zero and you want these to show too.
Create a column chart. On the x axis, add the column from your newly created table ensuring show all values is selected.
On the y axis, add the following measure:
Measure =
VAR cursor = SELECTEDVALUE('Average Ratings'[Value])
VAR segment =
FILTER(
ADDCOLUMNS(
SUMMARIZE(movies, movies[movieId]),
"#aveRating", [Ave Rating]
),
[#aveRating] = cursor
)
RETURN COUNTROWS(segment)
Finished result:

Related

PowerBI DAX Query - undo filter slicer on certain values based on expression

I have below table structure:
enter image description here
here I want to put a date slicer in Power BI to filter on dates and return the count of total rows as total in the card as shown below:
enter image description here
simple, the only twist is that I want to have the total of hybrid car added at all times.
i.e.
Brands before 5/25/2020 = 4 Hybrid + 1 Electric = 5
Brands before 12/5/2020 = 4 Hybrid + 3 Electric = 7
I have found a solution, which is creating a view in my database, which jus holds the number count of hybrid car (select count(*) from table where cartype = 'hybrid') and using it to sum with filter rows in power bi - but I am looking for a solution completely in Power BI DAX query.
any measure I have tried to create in power bi is filtered by date slicer and so doesn't work.
Create these measure:
TOTALROWS = COUNT('cars'[brand])
ELECTRIC_NUM = CALCULATE([TotalRows],('cars'),'cars'[cartype]="ELECTRIC")
HYBRID_NUM = CALCULATE([TOTALROWS],ALL('cars'),'cars'[cartype]="HYBRID")
TOTALBYBUSINESSLOGIC = CALCULATE([ELECTRIC_NUM]+[HYBRID_NUM])
Now use the last measure (i.e. TOTALBYBUSINESSLOGIC) to be used in your Card to display the total, Notice the expression diffrence between ELECTRIC_NUM and HYBRID_NUM
(In HYBRID_NUM I have used ALL, All will have it bypass the Date Slicer filter) whereas ELECTRIC_NUM will only proivde sum of rows falling in the active date sliver range.

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).

PowerBi - Create chart using 2 X axis value and 1 Y axis Value

I want to create chart in powerbi. There are 2 X axis value(Date) and one Y axis value(amount). Below is the source data table.
Final table should look like below image
Output chart:
Can someone help me out to put together the chart.
From ShiftDate and BilledDate, add columns Shift_Month, Billed_Month for name of the months in Data Table
Create calculate Table Billed_Table using DAX: Billed_Table = SUMMARIZE(Data, Data[Billed_month], "Sum_Billed", CALCULATE(SUM(Data[Amount])))
Create calculate Table Shift_Table using DAX:Shift_Table = SUMMARIZE(Data, Data[Shift_Month], "Sum_Shift", CALCULATE(SUM(Data[Amount])))
Then join Billed_Table and Shift_Table
Now go to power view and plot column chart using join_table data

Microsoft Power BI DAX - tricky situation using ALLEXCEPT

I have a situation in which I have a single excel file with one sheet, I am loading this single sheet (Sales) as a Power BI table.
I have two major dimension columns - Model and Product. I have a single numerical column called SalesAmount.
In all there are just 3 source columns in my Sales table.
One Model can have many Products, hence a 1:M relationship, but in the same (Sales) table.
I am using a Power BI matrix, as shown.
I have the following:
1)
Measure called [Sum of SalesAmount]: a simple sum of the SamesAmount column
2)
A Calculated Column called [ModelSalesAmount] in the Sales table:
ModelSalesAmount = CALCULATE(
SUM(Sales[SalesAmount]),
ALLEXCEPT(Sales,Sales[Model])
)
A Calculated Column called [ProductOnModel] in the Sales table:
ProductOnModel = Sales[SalesAmount]/Sales[ModelSalesAmount]
I have the matrix visual as shown.
The aggregated value for the ModelSalesAmount is incorrect.
HL Fountain Frame is a Model, and there are 8 products under this Model.
The Calculated column Sales[ModelSalesAmount] with a SUM aggregation in the matrix, works correctly at the product level (3,365,069.274), but not at the Model level. The Model level aggregation 26,920,554.19 is incorrect. I would want 3,365,069.274 at the Model level, NOT 26,920,554.19.
Interestingly, the Calculated Column Sales[ProductOnModel] with a SUM aggregation in the matrix, while using the Sales[ModelSalesAmount] Calculated Column in the denominator works correctly, both at the individual product level, as well as at the Model level!
What should I do to remove the undesired value 26,920,554.19, and make it 3,365,069.274 ?
I tried this below, but not working:
ModelSalesAmount_CC = IF (
HASONEVALUE(Sales[Product]) = TRUE,
CALCULATE(
SUM(Sales[SalesAmount]),
ALLEXCEPT(Sales,Sales[Model])
),
...........
)
Any suggestion please?
Change the Calculated Column [ModelSalesAmount] to a measure, I get the right output. Thanks Alexis.

Power BI (DAX) Multiple Filter Sum Total

I am a new user of Power BI. I have a two step process which I need help with.
(1) - Calculate filtered (string) sum totals, which update based on slicers in report
(2) - Calculate ratio using outputs from Step 1 by quarter.
Data set approximates this:
The idea is that my report should be able to calculate Price per Liter if filtered by Color and if filtered by Shade (ie. multiple filters, but not restricted), by Quarter, so the trend can be plotted.
So far, I can calculate what I need using only one filter.
Total Price per quarter =
CALCULATE (
SUM ( Data[Price] ),
FILTER ( data, data[Quarter] = EARLIER ( data[quarter] ) ),
FILTER ( data, data[Colour] = EARLIER ( data[Colour] ) )
)
The trouble is that this approach doesn't seem to be so accurate when including more than one filter. From what I can gather it seems to double count Price (if say using two filters)
Total Price (TP) should essentially adjust for each filter (Quarter, Colour & Shade). If Q1 Selected TP = 2800 (1000+500+600+700); If Q1 & Green, TP = 1500 (1000+500); If Q1, Green & Light, TP = 1000. I know it is a simple problem, but I am not quite familiar with the functions and syntax just yet.
Can you please provide me with a DAX expression which might achieve this?
Just use the following measure without any filter (right click on your table and add measure):
Total Price = SUM(data[Price])
Now put that measure into a KPI visual and put the rest of your columns as slicers on the report. You will see if you select for example Q1 the KPI visuals value will change. The same goes for every filter combination you will choose.