I have dataset (simplified) that looks like the example below
Score | First Language | FLPecentage | FLScore | Second Language | SLPercentage | SLScore | Year | Quarter |
---|---|---|---|---|---|---|---|---|
120 | Java | 90 | 108 | C# | 10 | 12 | 2024 | 4 |
200 | C# | 75 | 150 | C++ | 25 | 50 | 2025 | 1 |
50 | F# | 25 | 12,5 | Java | 75 | 37,5 | 2024 | 3 |
100 | Cobol | 100 | 100 | - | 0 | 0 | 2024 | 4 |
80 | C# | 50 | 40 | VB.NET | 50 | 40 | 2024 | 2 |
I have to create a pivottable for this table but the pivot should contain the unique values from the columns "First Language" and "Seccond Language" as a column and the sumed scores of he column FLScore and SLScore.
In this example the result would look like:
I used:
=UNIQUE(VSTACK(dtScore[First Language];dtScore[Second Language]))
Result is in range M10:M16 (second image) And
=SUMIFS(dtScore[FLScore];dtScore[First Language];M10)+SUMIFS(dtScore[SLScore];dtScore[Second Language];M10)
to get the total sum for each language.
But I have to transform the data in such a way I can use it in a Pivot-table also able to filter on Year and Quarter.
I have been thinking of using GROUPBY, FILTER, HSTACK, VSTACK and lambda functions to transform the data, but I don't know where to start and how to get the result into a Pivot table meeting the criteria mentioned.
I prefer not to use VBA, just Excel functions
Can anyone put in a direction?
Update: Though I used the PQ solution, because it suited my needs best, I didn't stop experimenting. I was able to use PIVOTBY to have a matrix by Language, score and the years as columns.
The formula looks like
=PIVOTBY(VSTACK(dtScore[First Language];dtScore[Second Language]);VSTACK(dtScore[Year];dtScore[Year]);VSTACK(dtScore[FLScore];dtScore[SLScore]);SUM)
One thing to note, I head to use VSTACK also for the columns-parameter. Though the column is the same (dtScore[Year]). If I just putted in dtScore[Year] the formula throws an #Value-error saying there is an inconsistent number of rows.
The result of the PIVOTBY
I would approach by creating a Power Query, but I'm sure other will provide similar solutions using Excel formulas:
let
// 1. Reference your original table.
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
// 2. Create a subset for the First Language
FLTable = Table.SelectColumns(Source, {"First Language", "Fl Score"}),
// Rename columns to a generic [Language] and [Score]
FLRenamed = Table.RenameColumns(
FLTable,
{
{"First Language", "Language"},
{"Fl Score", "Score"}
}
),
// 3. Create a subset for the Second Language
SLTable = Table.SelectColumns(Source, {"Second Language", "SL Score"}),
// Rename columns to the same [Language] and [Score] for easy append
SLRenamed = Table.RenameColumns(
SLTable,
{
{"Second Language", "Language"},
{"SL Score", "Score"}
}
),
// 4. Append the two subtables
Appended = Table.Combine({FLRenamed, SLRenamed}),
// 5. Group by Language, summing up the scores
Grouped = Table.Group(
Appended,
{"Language"},
{
{"Total Score", each List.Sum([Score]), type nullable number}
}
)
in
Grouped
Or less verbose and more compact:
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
Combined = Table.Combine({
Table.RenameColumns(Table.SelectColumns(Source, {"First Language", "Fl Score"}), {{"First Language", "Language"}, {"Fl Score", "Score"}}),
Table.RenameColumns(Table.SelectColumns(Source, {"Second Language", "SL Score"}), {{"Second Language", "Language"}, {"SL Score", "Score"}})
}),
Result = Table.SelectRows(
Table.Group(Combined, {"Language"}, {{"Total Score", each List.Sum([Score]), type nullable number}}),
each [Language] <> null and [Language] <> ""
)
in
Result