Forum Discussion
lukeSDM
5 years agoHelper V
Calculating the difference between different rows with a common subject
Hi All, I am looking to calculate the difference between the grades , between the result sets for each aspect. So for 'Jim' I want to calculate the difference between his target and in autumn , ...
BA_Pete
5 years agoSuper User
That makes waaaay more sense!
I've done this in two parts to simplify the measures.
1) Split out dimensions in Power Query.
In Power Query, go to New Source>Blank Query then in Advanced Editor paste my code over the default code. You can then follow the steps I took to complete this.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("tZNNDoIwEIWv0nTNApALsBCjRhfSjSEsGhmERSuZlnh9i5j4QxWBsJ3p99502pckdFMK6tAjcCSeR6KIkQNUF9SQkRXyDExzGwf3xh6uZCnPpuLT1BmM7rgulKktLHBY61pIwgCbKgN+KgBJqBQoJUDqFm5kvrj3CpjBH3jX350dfx3fswjEFZbGYUaB5wUCG14LYc5P8O8T+O2/lhqwFNMX+bdQ50Ehz8f8Zn8U3cbI4uzOy35k8J2etrPhKUhv", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [name = _t, #"Result set" = _t, Aspect = _t, Grade = _t]),
chgSourceTypes = Table.TransformColumnTypes(Source,{{"name", type text}, {"Result set", type text}, {"Aspect", type text}, {"Grade", Int64.Type}}),
dupeResultSet = Table.DuplicateColumn(chgSourceTypes, "Result set", "Result set - Copy"),
splitResultSetDupe = Table.SplitColumn(dupeResultSet, "Result set - Copy", Splitter.SplitTextByCharacterTransition({"0".."9"}, (c) => not List.Contains({"0".."9"}, c)), {"gradeYear", "gradePeriod"}),
trimGradePeriod = Table.TransformColumns(splitResultSetDupe,{{"gradePeriod", Text.Trim, type text}}),
addGradeType = Table.AddColumn(trimGradePeriod, "gradeType", each if Text.Contains([Aspect], "Assessment") then "Target" else "Actual", type text),
addSubject = Table.AddColumn(addGradeType, "Subject", each if Text.Contains([Aspect], "Eng") then "English" else if Text.Contains([Aspect], "Maths") then "Maths" else "No subject", type text)
in
addSubject
2) Create variance measures as before.
_varianceSpring =
VAR grade =
CALCULATE(
MAX(lukeSDMTable2[Grade]),
lukeSDMTable2[gradeType] = "Actual"
)
VAR target =
CALCULATE(
MAX(lukeSDMTable2[Grade]),
lukeSDMTable2[gradePeriod] = "Spring Term"
)
RETURN
grade - target_varianceSummer =
VAR grade =
CALCULATE(
MAX(lukeSDMTable2[Grade]),
lukeSDMTable2[gradeType] = "Actual"
)
VAR target =
CALCULATE(
MAX(lukeSDMTable2[Grade]),
lukeSDMTable2[gradePeriod] = "Summer Term"
)
RETURN
grade - target_varianceAutumn =
VAR grade =
CALCULATE(
MAX(lukeSDMTable2[Grade]),
lukeSDMTable2[gradeType] = "Actual"
)
VAR target =
CALCULATE(
MAX(lukeSDMTable2[Grade]),
lukeSDMTable2[gradePeriod] = "Autumn Term"
)
RETURN
grade - target
You can then apply your dimensions/measures to a matrix like this:
The main thing to note is, that in order to keep the measures as dynamic as possible, I've added an extra dimension to the matrix i.e. [gradeYear]. You could take this out of the matrix and add it as a slicer if you prefer, but it will need to be applied as a filter somewhere as it's not hardcoded into the measures.
Pete