Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Earn the coveted Fabric Analytics Engineer certification. 100% off your exam for a limited time only!

Reply
Mark_Clipsham
Frequent Visitor

Increment Variable based off of category

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:

 

SchoolGroupStudent
School ARachel's Year 7'sxxx
School ARachel's Year 7sxxx
School ASam Year 9sxxx
School BRachel's Year 8'sxxx
School Bone to one sessionsxxx

 

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:

 

SchoolGroupStudentArbitrary Group
School ARachel's Year 7'sxxxGroup 1
School ARachel's Year 7sxxxGroup 1
School ASam Year 9sxxxGroup 2
School BRachel's Year 8'sxxxGroup 1
School Bone to one sessionsxxxGroup 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, 

2 REPLIES 2
Anonymous
Not applicable

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).

Mark_Clipsham
Frequent Visitor

Still struggling with this one, I'm guessing some form of RANKX function would work but I'm sturggling to put it together. 

Helpful resources

Announcements
April AMA free

Microsoft Fabric AMA Livestream

Join us Tuesday, April 09, 9:00 – 10:00 AM PST for a live, expert-led Q&A session on all things Microsoft Fabric!

March Fabric Community Update

Fabric Community Update - March 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors