PowerBi Measure - divide by value based on another value - powerbi

I have the following dataset.
name duration date begintime time_type
Person 1 15 20190910 0845 AM
Person 1 15 20190910 1000 OV
Person 1 15 20190910 1100 AM
Person 2 15 20190910 0915 AM
Person 2 15 20190910 1015 AM
Person 2 15 20190910 0945 AM
Person 2 15 20190910 1045 AM
I trying to create a measure that will sum the duration by person, but because Person 1 has time type of OV on 20190910, I need to divide the sum by 5. If the person doesn't have a time type of OV for that day, I will need to divide by 8 (as Person 2 does not have time type of OV on that day). It is possible that Person 1 doesn't have a time type of OV on another day, but for that day I will need to divide by 8. How can I accomplish this in a PowerBi Measure?
UPDATE:
The expedected output should looks like this:
Person 1 - 9 (15 + 15 + 15) / 5
Person 2 - 7.5 (15 + 15 + 15 + 15) / 8

This measure will check if the values present in the context contain "OV", and based on that will choose the proper value for the division
Measure =
SUM('Table'[duration]) /
IF(CONTAINSROW(VALUES('Table'[time_type]),"OV")
,5 --true
,8 --false
)

If I understand your scenario, you apply the division on a daily basis, therefore the adjusted divisor can be different in different days. Then you should also interate over names, otherwise totals would be incorrect.
Adjusted Duration =
SUMX(
VALUES( TImes[name] ),
SUMX(
VALUES( Times[date] ),
VAR timeTypeOV =
CALCULATE(
COUNTROWS( Times ),
ALLEXCEPT( Times, Times[name], Times[date] ),
Times[time_type] = "OV"
)
VAR duration =
CALCULATE(
SUM( Times[duration] ),
ALLEXCEPT( Times, Times[name], Times[date] )
)
VAR divisor =
IF( timeTypeOV > 0, 5, 8 )
RETURN
DIVIDE( duration, divisor )
)
)

Related

PowerBI Matrix Average instead of Subtotal and Conditional Formatting According to That Average

Hello I am just new in powerBI and it is still hard to work on for me.
I have a matrix like that
DATE Sales Refund
26 Agu 45 5
p1 10 3
p2 15 2
p3 20 0
27 Agu 60 1
p1 15 1
p2 20 0
p3 25 0
In the date parts I have subtotals as it normally does. However, I want to show the average of that day there and when I get the average I will make conditional formatting according to it. If a cell is below average I will mark it with red point and in refunds I will do it for the values above the average.
Is there a way to do that. I searched for it for awhile but could not find.
The output I want is like that. (star is for red point.)
DATE Sales Refund
26 Agu 15 1.66
p1 10* 3*
p2 15 2*
p3 20 0
27 Agu 20 0.33
p1 15* 1*
p2 20 0
p3 25 0
Thanks.
You can colour the background; For example, create this measure:
AVG =
IF( SELECTEDVALUE(RefundTab[Sale] ) < CALCULATE(AVERAGE(RefundTab[Sale]), ALL(RefundTab[Code])),0,1)
From menu -> Conditional formatting -> Background color:
And here:
OR
you can create measure where we return string instead of number where we put some unicode value:
SumSaleIf =
var _sale = sum(RefundTab[Sale])
var _IfAVG = CALCULATE(AVERAGE(RefundTab[Sale]), ALL(RefundTab[Code]))
var _check = if(_sale < _IfAVG, _sale & UNICHAR(128315), _sale &"")
return _check

PowerBI- How to increment a column for an hourly basis?

So, I have a table with many columns and what I am trying to do is increment the number of sales that have been in that hour and then reset it after after the next hour. I have tried to use summarize key word, but it doesn't seem to be letting me accumulate it. At the moment my data is in 15min bands so the data shows sales in that 15 minute time period. But I would like to accumulate it into next hour.
This is what I have now
15minperiod. Sales
09:00:00. 10
09:15:00. 10
09:30:00. 10
09:45:00. 10
10:00:00. 10
10:15:00. 9
10:20:00. 13
This is what I would like to get:
15minperiod. Sales Sales in hour
09:00:00. 10. 10
09:15:00. 10 20
09:30:00. 10 30
09:45:00. 10 40
10:00:00. 10 10
10:15:00. 9 19
10:20:00. 13 32
Yes, this can be done with a calculated column like this:
Sales in an hour =
var currentTime = [15minperiod]
return
CALCULATE(
SUM('Data'[Sales]);
FILTER(
ALL('Data');
'Data'[15minperiod] <= currentTime && HOUR('Data'[15minperiod]) = HOUR(currentTime)
)
)

Sum the values of different groups at its latest date using Power BI

I am new to PowerBI and now trying to sum the values of each group at its own latest date by using Measure. I have tried to use "New Table" and Lookupvalue and it's success. However, I would like to use slicer for the date to sum the values as at that date.
Date Group Value
1/8 A 3
1/8 B 4
1/8 C 5
1/8 D 6
2/8 A 7
2/8 B 8
2/8 C 9
4/8 A 10
4/8 B 11
7/8 C 12
7/8 D 13
When the slicer is 7/8, value is A 10, B11, C12, D13, total = 46
When the slicer is 5/8, value is A10, B11, C9, D6, total = 36
I am using the measure as follows,
Measure =
var _maxdate =
CALCULATE(MAX(Sheet1[Date]),
VALUES(Sheet1[Group]))
return
CALCULATE(SELECTEDVALUE(Sheet1[Value]),
Sheet1[Date]=_maxdate)
It can show in visuals the last value of each group, but it can't sum them up. Can anyone help about that? Thanks a lot.
You are almost there, your return value needs to be a SUM not the SELECTEDVALUE. You also need to clear the filter on date with the ALL function:
Measure =
var _maxdate =
CALCULATE(MAX(Sheet1[Date]),
VALUES(Sheet1[Group]))
return
CALCULATE(SUM(Sheet1[Value]),
ALL(Sheet1[Date]),
Sheet1[Date] =_maxdate)
I have figured it out myself and let me post it here. I am expecting the value of each group at its own latest date and sum those values.
First, use the measure as the above.
Measure =
var _maxdate =
CALCULATE(MAX(Sheet1[Date]),
VALUES(Sheet1[Group]))
return
CALCULATE(SELECTEDVALUE(Sheet1[Value]),
Sheet1[Date]=_maxdate)
Second, use the first measure and sum the total.
Measure 2 =
var _table =
SUMMARIZE(Sheet1,Sheet1[Group],
"_value", [Measure])
return
SUMX(_table, [_value])

Difference between two matrix columns appears two times (Power BI)

I created a new measure with the difference between two categories of data; but when I put it in a matrix, this information appears two times.
I have a database with this format:
Type Month Item Value
real 1 earnings 10
real 2 earnings 15
real 3 earnings 20
real 4 earnings 10
real 5 earnings 11
ppto 1 earnings 10
ppto 2 earnings 12
ppto 3 earnings 13
ppto 4 earnings 12
ppto 5 earnings 10
then I created a Measure like this:
Difference =
CALCULATE ( SUM ( BASE[Value] ); BASE[Type] = "real" )
- CALCULATE ( SUM ( BASE[Value] ); BASE[Type] = "ppto" )
the problem is when I put the new variable in a Matrix, I obtain this (repeated two columns of differences):
but I would like the result in one column. Like this:
How can I fix this?
This is because you have Tipo in the Columns field and that has multiple values.
One possible solution is to remove Tipo from the columns and use three measures.
real = CALCULATE ( SUM ( BASE[Value] ); BASE[Type] = "real" )
ppto = CALCULATE ( SUM ( BASE[Value] ); BASE[Type] = "ppto" )
Difference = [real] - [ppto]

How to obtain a nested average with Dax in Power BI?

I am trying to calculate the average of a column in a measure in DAX, but I have the problem that the values are duplicated on multiple rows.
The data looks like this:
Line ID Value
1 1 10
2 1 10
3 1 10
4 1 10
5 1 10
6 2 40
7 2 40
8 3 90
9 3 90
10 3 90
When I do
sum(data[Value]) / countrows(data)
the answer will be (50 + 80 + 270) / 10 = 40.
However, I want the answer to be (10 + 40 + 90) / 3 = 46.7
I know I have to divide by 3 and can achieve the 3 by
distinctcount(data[ID])
But I find it difficult to extract the 10, 40 and 90.
Can you help me?
There are many ways to achieve that. For example:
Average Value =
VAR Summarized_Table = SUMMARIZE ( Data, Data[ID], Data[Value] )
VAR Total_Value = SUMX ( Summarized_Table, Data[Value] )
VAR Total_Count = COUNTROWS ( Summarized_Table )
RETURN
DIVIDE ( Total_Value, Total_Count )
Result:
Explanation:
First, we create a summarized version of the data, by grouping it on ID and Value columns. Grouping eliminates duplication, so the summary table contains only 3 records. We save the table in a variable;
Second, we sum up values in the summary table (140);
Third, we count number of records in the summary table (3)
Finally, we return the result by dividing sum and count
Alternatively, you can do this:
Average Value =
AVERAGEX ( VALUES ( Data[ID] ), CALCULATE ( AVERAGE ( Data[Value] ) ) )
Result is the same, but the logic is different:
First, using VALUES we create a list of distinct IDs;
Second, we use AVERAGEX to iterate the list, and for each ID calculate its average value. For example, for ID=1, result will be Average of (50)/5= 10;
Finally, we average the averages as (10 + 40 + 90) / 3