Forum Discussion

VDS9's avatar
VDS9
Frequent Visitor
1 year ago

Help required with index

Hi I am trying to create a lesson index so that I have them in the correct order when looking at a linechart. Unfortunately isnt as straight forward as there are a couple of exceptions.

 

This data comes from a folder with several identical excel files (1 file p/week).

In each file there are 9 sheets. The first 3 sheets have only 12 "Lessons"

MAMRM1M2MBM3M4MLM5M6MEMDTATRT1T2TBT3T4TLT5T6TETDWAWRW1W2WBW3W4WLW5W6WEWDTHATHRTH1TH2THBTH3TH4THLTH5TH6THETHD

 

The other 6 sheets have 13 "lessons"

MAMRM1M2M3MBM4M5MLM6M7MEMDTATRT1T2T3TBT4T5TLT6T7TETDWAWRW1W2W3WBW4W5WLW6W7WEWDTHATHRTH1TH2TH3THBTH4TH5THLTH6TH7THETHD

 

On Friday, all sheets only have 11 "lessons".

FAFRF1F2FBF3F4FFFLFAFO

 

I've used the code below but the index is coming as null.

 

= Table.AddColumn(
#"Changed Type",
"Lesson Index",
each let
Digits = {"0".."9"},
// Split the Lesson column by digits and non-digits
LessonSplit = Splitter.SplitTextByCharacterTransition(each not List.Contains(Digits,_), each List.Contains(Digits,_))([Lesson]),
DayOfWeek = try LessonSplit{0} otherwise null,
NumberPart = try Number.From(LessonSplit{1}) otherwise null,
DayOfWeekIndex = if DayOfWeek = "M" then 0
else if DayOfWeek = "T" then 1
else if DayOfWeek = "W" then 2
else if DayOfWeek = "TH" then 4
else if DayOfWeek = "F" then 5
else null,
// Determine if we are on the first three sheets or the others
ColumnCount = Table.ColumnCount(#"Changed Type"),
SlotIndex = if ColumnCount = 12 then // Sheets with 1 less column (first 3 sheets)
if NumberPart = null then
if [Lesson] = "MA" then 10
else if [Lesson] = "MR" then 20
else if [Lesson] = "M1" then 30
else if [Lesson] = "M2" then 40
else if [Lesson] = "MB" then 50
else if [Lesson] = "M3" then 60
else if [Lesson] = "M4" then 70
else if [Lesson] = "ML" then 80
else if [Lesson] = "M5" then 90
else if [Lesson] = "M6" then 100
else if [Lesson] = "ME" then 110
else if [Lesson] = "MD" then 120
else null
else
DayOfWeekIndex * 100 + NumberPart
else if ColumnCount = 13 then // Sheets with all columns (remaining sheets)
if NumberPart = null then
if [Lesson] = "MA" then 10
else if [Lesson] = "MR" then 20
else if [Lesson] = "M1" then 30
else if [Lesson] = "M2" then 40
else if [Lesson] = "M3" then 50
else if [Lesson] = "MB" then 60
else if [Lesson] = "M4" then 70
else if [Lesson] = "M5" then 80
else if [Lesson] = "ML" then 90
else if [Lesson] = "M6" then 100
else if [Lesson] = "ME" then 110
else if [Lesson] = "MD" then 120
else null
else
DayOfWeekIndex * 100 + NumberPart
else if ColumnCount = 10 then // Fridays with special slots
if NumberPart = null then
if [Lesson] = "FA" then 10
else if [Lesson] = "FR" then 20
else if [Lesson] = "F1" then 30
else if [Lesson] = "F2" then 40
else if [Lesson] = "FB" then 50
else if [Lesson] = "F3" then 60
else if [Lesson] = "F4" then 70
else if [Lesson] = "FF" then 80
else if [Lesson] = "FL" then 90
else if [Lesson] = "FA" then 100
else if [Lesson] = "FO" then 110
else null
else
DayOfWeekIndex * 100 + NumberPart
else null // Fallback for unexpected column counts
in
SlotIndex
)

 

Anyone able to help?

 

Thank you

3 Replies