Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!
Hi there,
I have a scenario when I want to assign common variables to rows based on the values of a column within that row.
Basically I have a number of Groups of students within a number of schools but the groups are always named differently, my report has a slicer on school but I want pages of my report to always point to seperate groups. So page 1 will always filter to an arbitrary 'Group 1' within the school the slicer is on.
My current data structure in short is:
School | Group | Student |
School A | Rachel's Year 7's | xxx |
School A | Rachel's Year 7s | xxx |
School A | Sam Year 9s | xxx |
School B | Rachel's Year 8's | xxx |
School B | one to one sessions | xxx |
Anything can be put into the group column, and I have hundreds of unique group names. I want a DAX calculated column that simply assigns Group 1 to a specific Group within a school and then Group 2 to the next and then starts again when reaching the next school. So something like this:
School | Group | Student | Arbitrary Group |
School A | Rachel's Year 7's | xxx | Group 1 |
School A | Rachel's Year 7s | xxx | Group 1 |
School A | Sam Year 9s | xxx | Group 2 |
School B | Rachel's Year 8's | xxx | Group 1 |
School B | one to one sessions | xxx | Group 2 |
This would allow me to assign Group 1 as a page level filter in my report and that page will always show the first group in whichever school is selected, without having to manually filter it each time the school is changed.
Any help would be great!
Thanks,
This is a job for Power Query, not DAX. Here's the code:
// Original Schools
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCk7OyM/PUXBU0oEzFXQV3A2VYnXIkzQiWtIJIemEYSy6JF6d5Esaky1pgk/SlIBkLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [School = _t, Group = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"School", type text}, {"Group", type text}}),
#"Merged Queries" = Table.NestedJoin(#"Changed Type", {"School", "Group"}, #"Original Schools Without Dups", {"School", "Group"}, "Original Schools Without Dups", JoinKind.Inner),
#"Expanded Original Schools Without Dups" = Table.ExpandTableColumn(#"Merged Queries", "Original Schools Without Dups", {"General Group"}, {"General Group"})
in
#"Expanded Original Schools Without Dups"
// Original Schools Without Dups
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCk7OyM/PUXBU0oEzFXQV3A2VYnXIkzQiWtIJIemEYSy6JF6d5Esaky1pgk/SlIBkLAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [School = _t, Group = _t]),
#"Changed Type" = Table.TransformColumnTypes(Source,{{"School", type text}, {"Group", type text}}),
#"Sorted Rows" = Table.Sort(#"Changed Type",{{"School", Order.Ascending}, {"Group", Order.Ascending}}),
#"Removed Duplicates" = Table.Distinct(#"Sorted Rows"),
#"Added Index" = Table.AddIndexColumn(#"Removed Duplicates", "Index", 1, 1, Int64.Type),
#"Reordered Columns" = Table.ReorderColumns(#"Added Index",{"Index", "School", "Group"}),
#"Added Custom" = Table.AddColumn(#"Reordered Columns", "General Group",
(outer) =>
let
IndexesForCurrentSchool = Table.SelectRows(
#"Reordered Columns",
(inner) => inner[School] = outer[School]
)[Index],
MinIndex = List.Min(IndexesForCurrentSchool),
RestartedIndex = outer[Index] - MinIndex + 1,
GeneralGroup = "Group " & Text.From(RestartedIndex)
in
GeneralGroup
),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Index"})
in
#"Removed Columns"
The 'Original Schools' table is the start. Then you create a copy (in Power Query!) called 'Original Schools Without Dups'. Then on the latter you perform the operations above and then on the original you perform the code that's marked "// Original Schools". You then Load the original table and disable load for the one without dups (as it's only a help table to get you what you want).
Still struggling with this one, I'm guessing some form of RANKX function would work but I'm sturggling to put it together.
User | Count |
---|---|
42 | |
28 | |
24 | |
20 | |
16 |
User | Count |
---|---|
54 | |
35 | |
18 | |
18 | |
15 |