I have a data that I simply create:
Col1 Col2 Col3 Col4
2014/1/1 A Y 10
2014/4/1 A Y 15
2015/1/1 A Z 15
2015/4/1 A Z 30
2014/1/1 B Y 20
2014/4/1 B Y 30
2015/1/1 B Z 40
2015/4/1 B Z 80
I want to create a measure
in Power BI
so I can create an interactive visualization. The above data is created for example so we need to suppose that col2
, col3
have multiple factors.
The measure
I want is relativity
, the value in Col4
divide the first value under Col1
and Col2
.
Result I supposed but I do not need this in data table since when I create the visualization and add the filter for other columns, Col5
, Col6
, and etc. that I did not show in this example:
Col1 Col2 Col3 Col4 relativity_Col3ALL relativity_Col3EqualsYorZ
2014/1/1 A Y 10 1 1
2014/4/1 A Y 15 1.5 1.5
2015/1/1 A Z 15 1.5 1
2015/4/1 A Z 30 3 2
2014/1/1 B Y 20 1 1
2014/4/1 B Y 30 1.5 1.5
2015/1/1 B Z 40 2 1
2015/4/1 B Z 80 4 2
So I plot it and add filters beside the plot. When I select Y in the filter Col3
, the plot will automatically change.
I provide the code I think in R
:
dt <- data.table::as.data.table(dt)
dt[, relativity := Col4 / Col4[1], by = .(Col1, Col2)]
But above code is incorrect because it did not consider Col3
. I just want to mention Col4 / Col4[1]
or Col4 / first(Col4)
.
I tried measure in Power BI:
relativity = CALCULATE(DIVIDE(dt[Col4], dt[AnotherMeasure]), MIN(dt[Col1]))
I know this is false.
Can anyone help?
UPDATE
I tried @Alexis Olson's code and modified as:
relativity =
VAR YR = MIN(dt[Col1].[Year])
VAR QT = MIN(dt[Col1].[Quarter])
VAR PF = CALCULATE(TOTALQTD(SUM(dt[Col4]), dt[Col1].[Date]), dt[Col1].[Year] = YR, dt[Col1].[Quarter] = QT)
RETURN
DIVIDE(SUM(dt[Col4]), PF)
However, when I visualize in the report, it all shows 1
.
I also tried this:
relativity =
VAR YR = CALCULATE(MIN(dt[Col1].[Year]), ALLEXCEPT(dt, dt[Col2]))
VAR QT = CALCULATE(MIN(dt[Col1].[Quarter]), ALLEXCEPT(dt, dt[Col2]))
VAR PFQTD = TOTALQTD(SUM(dt[Col4]), dt[Col1].[Date])
VAR MPFQTD = CALCULATE(MAX(PFQTD), FILTER(dt, dt[Col1].[Year] = YR), FILTER(dt, dt[Col1].[Quarter] = QT))
RETURN
MPFQTD
Failed either